zl程序教程

您现在的位置是:首页 >  系统

当前栏目

windows、linux劫持技术

2023-09-14 08:57:16 时间

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")


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”


/* 文件名:verifypasswd.c */

/* 这是一段判断用户口令的程序,其中使用到了标准C函数strcmp*/

#include stdio.h 

#include string.h 

int main(int argc, char **argv)

 char passwd[] = "password";

 if (argc 2) {

 printf("usage: %s password \n", argv[0]);

 return;

 if (!strcmp(passwd, argv[1])) {

 printf("Correct Password!\n");

 return;

 printf("Invalid Password!\n");

}

编译程序:

$ gcc -o verifypasswd verifypasswd.c

测试一下程序:(得到正确结果)

$ ./verifypasswd asdf

Invalid Password!

在上面这段程序中,我们使用了strcmp函数来判断两个字符串是否相等。下面,我们使用一个动态函数库来重载strcmp函数:

#include stdio.h 

int strcmp(const char *s1, const char *s2)

 printf("hack function invoked. s1= %s s2= %s \n", s1, s2);

 /* 永远返回0,表示两个字符串相等 */

 return 0;

}

编译程序:

$ gcc -shared -o hack.so hack.c


设置LD_PRELOAD变量:(使我们重写过的strcmp函数的hack.so成为优先载入链接库)

$ export LD_PRELOAD="./hack.so"

再次运行程序:

$ ./verifypasswd  asdf

hack function invoked. s1= password s2= asdf

Correct Password!