吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 9333|回复: 15
收起左侧

[C&C++ 转载] C语言调用detours劫持WindowsAPI

  [复制链接]
Git_man 发表于 2017-2-14 18:11
本帖最后由 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()右键选择查看定义
   

查看定义

查看定义


    复制MessageBoxW()的函数声明
    2.png

    修改函数声明,去掉多余的调用约定,修改为一个指向原来函数的指针
    并在前面加上static防止影响其他源文件
    3.jpg

    创建一个同类型的函数以便拦截是调用
    4.jpg

    添加静态库文件
    #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
  
    msg.c:劫持MessageBoxW()完整代码
[C++] 纯文本查看 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#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

    修改为一个指向system函数的指针
    aa.jpg

    程序完整测试代码
[C++] 纯文本查看 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
#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

    工具箱中拖入button并添加代码,并生成exe文件
    asdf.jpg

    detours.h、detours.lib、detver.h文件复制到dll.c同目录下
    修改属性为dll,配置设为Release
    qqqq.jpg

    编写dll.c生成jiechi.dll文件禁止button调用calc
[C++] 纯文本查看 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<string.h>
#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
    这时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<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>

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

}






劫持代码.zip

175.33 KB, 下载次数: 142, 下载积分: 吾爱币 -1 CB

劫持代码

免费评分

参与人数 4吾爱币 +5 热心值 +3 收起 理由
qaz003 + 1 谢谢@Thanks!
述说过去 + 2 + 1 热心回复!
Takitooru + 1 + 1 用心讨论,共获提升!
chinacore_zhao + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| Git_man 发表于 2017-2-14 20:37
511598889 发表于 2017-2-14 18:36
谢谢分享,很详细,不过有个疑问,这个是不是对所有系统都有效

detours是微软花一年时间开发的安全产品,不加修改的话只适用于Windows平台
杯具加杯具 发表于 2018-7-18 11:10
这个和SetWindowsHookEX有什么区别吗,好像都是一样的,不过SetWindowsHookEx只能拦截有消息队列的进程,不知道这个是不是可以拦截所有进程
chinacore_zhao 发表于 2017-2-14 18:33
Takitooru 发表于 2017-2-14 18:36
谢谢分享,很详细,不过有个疑问,这个是不是对所有系统都有效
yysniper 发表于 2017-2-14 19:06
感觉很方便呀
夏雨微凉 发表于 2017-2-14 19:54
楼主咱们用的是一个C语言吗?,为何我学完了就会点建个数组、改个字符串啥的
 楼主| Git_man 发表于 2017-2-14 20:07
没啥子区别,就是调用了几个Windows API,在#include<Windows.h>中
大份水饺 发表于 2017-2-14 21:12
楼主,一定要用注射才行吗,进程通信可不可以实现劫持
xie0080 发表于 2017-2-14 21:43
谢谢分享谢谢分享{:1_914:}
tianhett 发表于 2017-2-15 00:05
膜拜大神中。。。。。。。。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-20 06:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表