本帖最后由 Git_man 于 2017-2-14 18:32 编辑
00 detours
detours是微软亚洲研究院出品的信息安全产品,用于劫持
可用Detours Express 3.0编译生成detours.lib
01 劫持MessageBoxW()
vs2015创建空项目,源文件中添加msg.c
把detours.h、detours.lib、detver.h文件复制到msg.c同目录下
输入MessageBoxW()右键选择查看定义
pic1.png (21.98 KB, 下载次数: 1)
查看定义
2017-2-14 17:23 上传
复制MessageBoxW()的函数声明
2.png (20.09 KB, 下载次数: 1)
2017-2-14 17:26 上传
修改函数声明,去掉多余的调用约定,修改为一个指向原来函数的指针
并在前面加上static防止影响其他源文件
3.jpg (8.49 KB, 下载次数: 1)
2017-2-14 17:29 上传
创建一个同类型的函数以便拦截是调用
4.jpg (7.72 KB, 下载次数: 1)
2017-2-14 17:30 上传
添加静态库文件
#pragma comment(lib,"detours.lib")
调用劫持代码
[C++] 纯文本查看 复制代码//开始拦截void Hook()
{
DetourRestoreAfterWith(); //恢复原来状态
DetourTransactionBegin(); //拦截开始
DetourUpdateThread(GetCurrentThread()); //刷新当前线程
//这里可以连续多次调用DetourAttach,表明HOOK多个函数
DetourAttach((void **)&OldMessageBoxW, NewMessageBoxW);//实现函数拦截
DetourTransactionCommit(); //拦截生效
}
//取消拦截
void UnHook()
{
DetourTransactionBegin(); //拦截开始
DetourUpdateThread(GetCurrentThread()); //刷新当前线程
//这里可以连续多次调用DetourAttach,表明撤销HOOK多个函数
DetourDetach((void **)&OldMessageBoxW, NewMessageBoxW); //撤销拦截
DetourTransactionCommit(); //拦截生效
}
vs2015解决方案配置修改为Release
11.jpg (8.85 KB, 下载次数: 1)
2017-2-14 17:44 上传
msg.c:劫持MessageBoxW()完整代码
[C++] 纯文本查看 复制代码#include
#include
#include
#include "detours.h"
#pragma comment(lib,"detours.lib")
static int (WINAPI *OldMessageBoxW)(
HWND hWnd,
LPCWSTR lpText,
LPCWSTR lpCaption,
UINT uType) = MessageBoxW;
int WINAPI NewMessageBoxW(
HWND hWnd,
LPCWSTR lpText,
LPCWSTR lpCaption,
UINT uType)
{
return 0;
}
void Hook()
{
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((void **)&OldMessageBoxW, NewMessageBoxW);
DetourTransactionCommit();
}
void UnHook()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((void **)&OldMessageBoxW, NewMessageBoxW);
DetourTransactionCommit();
}
void main()
{
MessageBoxW(0, "abcd", "test", 0);
Hook();
MessageBoxW(0, "1234", "test", 0);
system("pause");
}
运行结果显示第一个MessageBoxW()运行成功第二个被劫持
02 拦截system函数
查看system()定义
1.jpg (5.69 KB, 下载次数: 1)
2017-2-14 17:46 上传
修改为一个指向system函数的指针
aa.jpg (2.42 KB, 下载次数: 1)
2017-2-14 17:47 上传
程序完整测试代码
[C++] 纯文本查看 复制代码#include
#include
#include
#include
#include "detours.h"
#pragma comment(lib,"detours.lib")
static int(*oldsystem)(const char * _Command) = system;
int newsystem(const char * _Command)
{
printf("%s", _Command);
return 0;
}
int newsystemA(const char * _Command)
{
//实现过滤
const char *p = strstr(_Command, "tasklist");
if (!p == NULL) {
oldsystem(_Command);
}
else {
printf("%s禁止执行", _Command);
return 0;
}
}
void Hook()
{
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((void **)&oldsystem, newsystem);
DetourTransactionCommit();
}
void UnHook()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((void **)&oldsystem, newsystem);
DetourTransactionCommit();
}
void main()
{
system("calc");
Hook();
system("calc");
system("tasklist");
getchar();
}
03 dll注入注入正在运行的进程劫持system函数
vs创建一个MFC程序
*解决方案配置选择Release
a7.jpg (62.33 KB, 下载次数: 1)
2017-2-14 17:55 上传
工具箱中拖入button并添加代码,并生成exe文件
asdf.jpg (10.78 KB, 下载次数: 1)
2017-2-14 17:57 上传
detours.h、detours.lib、detver.h文件复制到dll.c同目录下
修改属性为dll,配置设为Release
qqqq.jpg (68.44 KB, 下载次数: 1)
2017-2-14 18:02 上传
编写dll.c生成jiechi.dll文件禁止button调用calc
[C++] 纯文本查看 复制代码#include
#include
#include
#include
#include "detours.h"
#pragma comment(lib,"detours.lib")
static int(*oldsystem)(const char * _Command) = system;
int newsystem(const char * _Command)
{
return 0;
}
void Hook()
{
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((void **)&oldsystem, newsystem);
DetourTransactionCommit();
}
_declspec(dllexport) void go()
{
MessageBoxA(0, "yes", "success", 0);
Hook();
}
运行生成的mfc应用程序,点击按钮可以调出计算器
用dll注入工具把生成的jiechi.dll注入到mfc应用程序进程中
aaaa.jpg (18.31 KB, 下载次数: 1)
2017-2-14 18:06 上传
这时system函数就被劫持了,程序无法调出计算器
04 劫持CreateProcessW()
CreateProcessW()可以创建进程,此函数被劫持后将无法打开应用程序
CreateProcessW()函数声明
[C++] 纯文本查看 复制代码WINBASEAPI
BOOL
WINAPI
CreateProcessW(
_In_opt_ LPCWSTR lpApplicationName, //可执行模块名
_Inout_opt_ LPWSTR lpCommandLine, //命令行字符串
_In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, //进程的安全属性
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, //线程的安全属性
_In_ BOOL bInheritHandles, //句柄继承标志
_In_ DWORD dwCreationFlags, //创建标志
_In_opt_ LPVOID lpEnvironment, //指向新的环境块的指针
_In_opt_ LPCWSTR lpCurrentDirectory, //指向当前目录名的指针
_In_ LPSTARTUPINFOW lpStartupInfo, //指向启动信息结构的指针
_Out_ LPPROCESS_INFORMATION lpProcessInformation //指向进程信息结构的指针
);
编译生成dll文件并注入到explorer.exe进程中
[C++] 纯文本查看 复制代码#include
#include
#include
#include
#include "detours.h"
#pragma comment(lib,"detours.lib")
static BOOL(WINAPI *Old_CreateProcessW)(
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 New_CreateProcessW(
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, "success", "success", 0);
return 0;
}
void Hook()
{
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((void **)&Old_CreateProcessW, New_CreateProcessW);
DetourTransactionCommit();
}
void UnHook()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((void **)&Old_CreateProcessW, New_CreateProcessW);
DetourTransactionCommit();
}
_declspec(dllexport) void go()
{
Hook();
int i = 0;
while (1) {
if (i == 120) {
UnHook();
break;
}
i++;
Sleep(1000);
}
system("tasklist & pause");
}