您的位置:首页 > 编程语言 > C语言/C++

巩固C语言(九)----静态库文件的生成和使用、劫持技术

2016-04-18 10:15 513 查看

1 静态文件的生成

新建工程MyLib

添加头文件mylib.h

void msg();		//在头文件里边只需要声明函数即可


添加源文件mylib.c

#include<windows.h>

void msg()			//只需对头文件中的声明进行定义即可
{
MessageBoxA(0, "我的静态库文件", "My Lib", 0);
}


打开项目属性----常规----配置类型----改为静态库.lib----生成解决方案,即可生成静态库文件MyLib.lib

注意:静态库不需要导入导出借口,也就是说不需要定义main函数

2 静态库文件的使用

新建工程Hello

添加源文件HelloWord.c
#include<stdio.h>
#include<stdlib.h>
//void msg();		//对静态库的文件做声明
void main()
{
msg();
printf("Hello Word!\n");

system("pause");
}
编译生成之后就会先跳出一个窗口,再在mini行打印输出Hello Word!

代开项目属性----配置属性----链接器----输入-----附加依赖库----新增MyLib.lib----并将MyLib.lib文件拷贝到该工程目录下

3 DetoursExpress文件

下载安装DetoursExpress文件,在安装目录下找到src目录,该src目录包含DetoursExpress文件的所有源代码,通过Makefile文件进行编译(MakeFile文件是跨平台的),windows系统下通过nmake进行编译,成功之后在lib.X86文件夹下生成detours.lib,在include文件夹下生成头文件detours.h和detver.h

4 劫持函数

新建工程劫持1
新建源文件劫持.c,并将Detours生成的detours.lib和detours.h/detver.h拷贝当前工程的目录下
注意:
在源程序中要包含头文件还有库文件即:
#include<detours.h>
#pragma comment(lib, "detours.lib")
定义就函数指针指向原来的函数static int(WINAPI * OLDMessageBoxW)(参数) = MessageBoxA
定义新的函数int WINAPI NEWMessageBoxW(参数){定义}
注意:解决方案配置一定要选择Release模式

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include"detours.h"
#pragma comment(lib, "detours.lib")		//表明要使用静态库

static int (WINAPI *OldMessageBoxW)(	//OldMessageBoxW是一个函数指针,保存MessageBoxW的地址
HWND hWnd,
LPCWSTR lpText,
LPCWSTR lpCaption,
UINT uType) = MessageBoxW;

int WINAPI NewMessageBoxW(				//将被劫持的函数声明为自己的函数,注意该函数应该和MessageBoxW的定义类型完全一致,单函数名可以修改
HWND hWnd,
LPCWSTR lpText,
LPCWSTR lpCaption,
UINT uType)
{
printf("您的函数已经被劫持了!\n");
return 0;
}

//开始拦截
void Hook()
{
DetourRestoreAfterWith();								//恢复原来的状态
DetourTransactionBegin();								//拦截开始
DetourUpdateThread(GetCurrentThread());					//刷新当前线程
//这里可以连续多次调用DetourAttach,表明Hook多个函数
DetourAttach((void**)&OldMessageBoxW, NewMessageBoxW);	//实现函数拦截
DetourTransactionCommit();								//拦截生效
}

//取消拦截
void unHook()
{
DetourTransactionBegin();								//拦截开始
DetourUpdateThread(GetCurrentThread());					//刷新当前线程
//这里可以连续多次调用DetourDetach,表明撤销多个函数Hook
DetourDetach((void **)&OldMessageBoxW, NewMessageBoxW);	//撤销拦截函数
DetourTransactionCommit();								//拦截生效
}

void main()
{
//	printf("%p\n", main);				//函数名即为一个地址,其保留了一个入口点的地址

MessageBoxW(0, L"深度学习C语言", L"C语言", 0);
Hook();								//开始拦截
MessageBoxW(0, L"深度学习C++语言", L"C++语言", 0);

system("pause");
}
运行结果:只输出一个窗口,并在命令行输出文字。表明函数劫持成功!!!





5 劫持函数+锁

劫持system指令,也就是锁的概念:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include"detours.h"
#pragma comment(lib, "detours.h")

static int(*OldSystem)(const char * _Command) = system;

int NewSystem(const char * _Command)
{
char *p = strstr(_Command, "tasklist");
if (p)										//字符串匹配,则阻止该命令行
{
printf("您输入的指令被拦截!\n");
return 0;
}
else {                                       //否则,调用原来的函数,不拦截该指令
OldSystem(_Command);
return 1;
}
}

//开始拦截
void Hook()
{
DetourRestoreAfterWith();								//恢复原来的状态
DetourTransactionBegin();								//拦截开始
DetourUpdateThread(GetCurrentThread());					//刷新当前线程
//这里可以连续多次调用DetourAttach,表明Hook多个函数
DetourAttach((void**)&OldSystem, NewSystem);			//实现函数拦截, 如果想拦截多个指令,只需添加多个拦截函数即可
DetourTransactionCommit();								//拦截生效
}

//取消拦截
void unHook()
{
DetourTransactionBegin();								//拦截开始
DetourUpdateThread(GetCurrentThread());					//刷新当前线程
//这里可以连续多次调用DetourDetach,表明撤销多个函数Hook
DetourDetach((void **)&OldSystem, NewSystem);	//撤销拦截函数
DetourTransactionCommit();								//拦截生效
}

void main()
{
Hook();
system("notepad");
system("tasklist");
system("ipconfig");

system("pause");
}


运行结果:当遇到指令为tasklist时会发生自动拦截的情况!

6 动态库的注入截止程序执行

首先写一个界面,该节点有两个按钮,如下图所示,点击计算器将会打开电脑上的计算器程序,同理,记事本。



写一个动态库文件,将该dll文件注射到该程序本身,使得打开计算器的指令失效!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include"detours.h"
#pragma comment(lib, "detours.h")

static int(*OldSystem)(const char * _Command) = system;

int NewSystem(const char * _Command)
{
char *p = strstr(_Command, "calc");
if (p)										//字符串匹配,则阻止该命令行
{
printf("您输入的指令被拦截!\n");
return 0;
}
else {                                       //否则,调用原来的函数,不拦截该指令
OldSystem(_Command);
return 1;
}
}

//开始拦截
void Hook()
{
DetourRestoreAfterWith();								//恢复原来的状态
DetourTransactionBegin();								//拦截开始
DetourUpdateThread(GetCurrentThread());					//刷新当前线程
//这里可以连续多次调用DetourAttach,表明Hook多个函数
DetourAttach((void**)&OldSystem, NewSystem);			//实现函数拦截, 如果想拦截多个指令,只需添加多个拦截函数即可
DetourTransactionCommit();								//拦截生效
}

_declspec(dllexport) void go()
{
MessageBoxA(0, "拦截成功", "计算器", 0);
Hook();
}


思考:必须要知道被劫持的程序的源代码才能劫持该程序,那么,这么做又有什么意义呢,毕竟不是所有的程序都能告诉你源代码。

7 系统劫持

#include<Windows.h>
#include<string.h>
#include<stdlib.h>

#include"detours.h"
#pragma comment(lib, "detours.lib")

//保存函数指针地址
static  BOOL ( WINAPI  *POLDCreateProcessW)(
LPCWSTR lpApplicationName,
LPWSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCWSTR lpCurrentDirectory,
LPSTARTUPINFOW lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
) = CreateProcessW;

BOOL  NEWCreateProcessW
(
LPCWSTR lpApplicationName,
LPWSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCWSTR lpCurrentDirectory,
LPSTARTUPINFOW lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
)
{
MessageBoxA(0, "你的保护费没交", "请缴费", 0);
return 0;

}

//开始拦截
void Hook()
{

DetourRestoreAfterWith();//恢复原来状态
DetourTransactionBegin();//拦截开始
DetourUpdateThread(GetCurrentThread());//刷新当前线程
//这里可以连续多次调用DetourAttach,表明HOOK多个函数
DetourAttach((void **)&POLDCreateProcessW, NEWCreateProcessW);//实现函数拦截
DetourTransactionCommit();//拦截生效

}
//取消拦截
void UnHook()
{

DetourTransactionBegin();//拦截开始
DetourUpdateThread(GetCurrentThread());//刷新当前线程
//这里可以连续多次调用DetourDetach,表明撤销多个函数HOOK
DetourDetach((void **)&POLDCreateProcessW, NEWCreateProcessW);//撤销拦截函数
DetourTransactionCommit();//拦截生效
}

//导出函数,可以加载的时候调用
_declspec(dllexport)  void go()
{
Hook();
int i = 0;
while (1)
{
if (i == 120)
{
UnHook();
break;
}
i++;
Sleep(1000);//1秒
}

}


注射到Explorer,在两分钟之内不能创建进程!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: