当前位置:首页 > Windows程序 > 正文

windows、linux劫持技术

2021-05-26 Windows程序

realse  模式劫持,调试的程序不可以

函数劫持可以实现的效果。

函数的劫持原理

我们如何实现-detours

detours是微软亚洲研究院出品的信息安全产品,主要用于劫持。

detours根据函数指针改变函数的行为,

拦截任何函数,即使操作系统函数。

1.安装detours

2.构建库文件-nmake编译

技术分享


3.包含头文件还有库文件

#include <detours.h>

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

4.

定义旧函数指针指向原来的函数

static int (WINAPI *OLD_MessageBoxW)(HWND hWnd, LPCSTR lpText, LPCSTR lpaptioin, UINT uType) = MessageBoxW;

定义新的函数

int WINAPI  NEW_MessgeBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaptioin, UINT uType)

{

//重新定义函数的行为

//为空可以禁止函数使用

//加上if else 可以限制函数的调用

//加上对话框可以限制同一或者不同意

if (IDYES == MessageBoxW(NULL, lpCommandLine, L"拦截成功!", MB_YESNO) )

return 1;

else

return FALSE;

return ret;

}

5.

开始拦截

void Hook()

{

DetourRestoreAfterWith();//恢复原来状态

DetourTransactionBegin();//拦截开始

DetourUpdateThread(GetCurrentThread());//刷新当前线程

//这里可以连续多次调用DetourAttach,表明HOOK多个函数

DetourAttach( (void **)&OLD_MessageBox, NEW_MessageBox );//实现函数拦截

DetourTransactionCommit();//拦截生效

}

取消拦截

void UnHook()

{

DetourTransactionBegin();//拦截开始

DetourUpdateThread(GetCurrentThread());//刷新当前线程

//这里可以连续多次调用DetourDetach,,表明撤销多个函数HOOK

DetourDetach( (void **)&OLD_MessageBox, NEW_MessageBox );//实现函数拦截

DetourTransactionCommit();//拦截生效

}

6.修改自己,直接挂接函数即可

修改外部程序

需要作为模块注射,,需要导出声明

__declspec(dllexport)


劫持system函数

#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<string.h> #include"detours.h" #pragma comment(lib, "detours.lib") //劫持自己 static int (*poldsystem)( const char * _Command)=system;//存储函数指针地址 int newsystem(const char * _Command) { //tasklist printf("%s", _Command); //禁止你干活 return 0; } int newsystemA(const char * _Command) { //tasklist 过滤 char *p = strstr(_Command, "tasklist"); if (p == NULL) { poldsystem(_Command); } else { printf("%s禁止执行", _Command);//找到 return 0; } return 0; } //开始拦截 void Hook() { DetourRestoreAfterWith();//恢复原来状态 DetourTransactionBegin();//拦截开始 DetourUpdateThread(GetCurrentThread());//刷新当前线程 //这里可以连续多次调用DetourAttach,表明HOOK多个函数 DetourAttach((void **)&poldsystem, newsystemA);//实现函数拦截 DetourTransactionCommit();//拦截生效 } void main() { system("calc"); Hook(); system("calc"); system("tasklist"); getchar(); }

编写成dll文件,注入到其他的程序中,从而能够实现劫持其他应用程序,达到过滤的效果。如果交了保护费,就可以不去劫持你的程序。实现猥琐的技术。

#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<string.h> #include"detours.h" #pragma comment(lib, "detours.lib") static int(*poldsystem)(const char * _Command) = system;//存储函数指针地址 int newsystem(const char * _Command) { //tasklist printf("%s", _Command); //禁止你干活 return 0; } //开始拦截 void Hook() { DetourRestoreAfterWith();//恢复原来状态 DetourTransactionBegin();//拦截开始 DetourUpdateThread(GetCurrentThread());//刷新当前线程 //这里可以连续多次调用DetourAttach,表明HOOK多个函数 DetourAttach((void **)&poldsystem, newsystem);//实现函数拦截 DetourTransactionCommit();//拦截生效 } //导出函数,可以加载的时候调用 _declspec(dllexport) void go() { MessageBoxA(0, "1", "2", 0); Hook(); }

CreateProcessW函数是用来创建进程的。


#include<stdio.h> #include<stdlib.h> #include<Windows.h> void main1() { //system("calc"); //ShellExecuteA(0, "open", "calc", 0, 0, 1); STARTUPINFO si = { sizeof(si) }; //启动信息 PROCESS_INFORMATION pi;//保存了进程的信息 si.dwFlags = STARTF_USESHOWWINDOW; //表示显示窗口 si.wShowWindow = 1; //1表示显示创建的进程的窗口 wchar_t cmdline[] = L"c://program files//internet explorer//iexplore.exe"; CreateProcessW(NULL, cmdline, NULL, NULL, 0, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);//创建进程 }
在Windows平台下可以使用挂钩(Hook)技术,将系统中的鼠标、键盘等事件拦截下来,以添加实现自己的功能。同样的,在Linux系统中也有类似的技术,都可以起到挂钩(Hook)拦截的作用。可以实现拦截的功能。拦截技术的实现是通过环境变量LD_PRELOAD设置优先被加载器加载的动态库(以下简称拦截动态库),这里应该设置LD_PRELOAD=“xxx.so”


温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/71364.html