编程c语言被windows拦截,C语言调用detours劫持WindowsAPI
本帖最后由 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");
}
總結
以上是生活随笔為你收集整理的编程c语言被windows拦截,C语言调用detours劫持WindowsAPI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bootstrap表单样式
- 下一篇: DSP技术:基于TMS320F28027