首页
社区
课程
招聘
[求助]关于detours的使用,怎么也hook不了
2019-3-11 21:13 6460

[求助]关于detours的使用,怎么也hook不了

2019-3-11 21:13
6460
各位师傅好,lz今天学习detours的使用,看了github文档的同时主要根据这个来弄


但是,我仿照弄了如下代码
#include <stdio.h>
#include <windows.h>
#include "detours.h"

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

int evilhelloworld() {
	printf("This is evilhelloworld\n");
	return 0;
}

int myhelloworld() {
	printf("This is my helloworld\n");
	return 0;
}

int (* real_helloworld)() = myhelloworld;

void hook() {
	DetourRestoreAfterWith();
	DetourTransactionBegin();
	DetourUpdateThread(GetCurrentThread());

	DetourAttach(&(PVOID&)real_helloworld,evilhelloworld);

	if (DetourTransactionCommit() == NO_ERROR) {
		printf("Detour success\n");
	}
}

void unhook() {
	DetourTransactionBegin();
	DetourUpdateThread(GetCurrentThread());

	DetourDetach(&(PVOID&)real_helloworld, evilhelloworld);

	DetourTransactionCommit();
}

int main() {
	printf("before hook\n");
	myhelloworld();
	printf("starthook\n");
	hook();
	myhelloworld();
	unhook();
	printf("Unhook\n");
	myhelloworld();
	return 1;
}
编译成功后怎么都hook不了,都是输出This is my helloworld。非常奇怪。然后我试了下,采用教程里面的代码Hook messagebox那个api是成功的。问题就是我这个代码就是失败了。。。有没有师傅愿意指点一下?感激不尽。。

[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
免费 0
打赏
分享
最新回复 (10)
雪    币: 9
活跃值: (155)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
yber 2019-3-11 21:37
2
0
可以看下示例
雪    币: 86
活跃值: (933)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
luzhmu 2019-3-11 22:23
3
0
你HOOK了哪个函数?
雪    币: 26
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
邓dg 2019-3-12 07:39
4
0
DetourAttach(&(PVOID&)real_helloworld,evilhelloworld);改成DetourAttach(&(PVOID&)myhelloworld,evilhelloworld);
雪    币: 85
活跃值: (101)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
samohyes 2019-3-12 09:14
5
0
luzhmu 你HOOK了哪个函数?
我hook了myhelloworld呀~
雪    币: 85
活跃值: (101)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
samohyes 2019-3-12 09:25
6
0
邓dg DetourAttach(&(PVOID&)real_helloworld,evilhelloworld);改成DetourAttach(&(PVOID&)myhell ...
这个似乎不行。。。DetourAttach第一个参数必须是指向函数的指针的指针...那个教程里面就是这么弄的。。
雪    币: 218
活跃值: (2824)
能力值: ( LV7,RANK:140 )
在线值:
发帖
回帖
粉丝
yeyeshun 2 2019-3-12 10:12
7
1
是不是函数被内联了?
调试的时候看一下反汇编啊,看看函数是否独立,如果被内联了或者直接被优化掉了,那肯定会失败。
雪    币: 85
活跃值: (101)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
samohyes 2019-3-12 10:35
8
0
yeyeshun 是不是函数被内联了? 调试的时候看一下反汇编啊,看看函数是否独立,如果被内联了或者直接被优化掉了,那肯定会失败。
大佬,跪谢。。。我关闭优化选项后就Hook成功了。。。哭。。
雪    币: 3089
活跃值: (1548)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
MsScotch 2019-3-12 10:41
9
0
带上 WINAPI
雪    币: 53
活跃值: (228)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
gestic 2019-3-12 14:31
10
1
以前遇到过这个问题,也是Hook一直失败,然后就去跟踪DetourAttach,发现里面VirtualProtect调用失败,把它改成 VirtualProtectEx调用就可以了。
这个库官方没有维护了,可能有bug,不知道你遇到的问题是不是这个,把DetourAttachEx函数实现源代码修改下再编译使用试试。。
    DWORD pid = GetCurrentProcessId();
    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
    if (!VirtualProtectEx(hProcess, pbTarget, cbTarget, PAGE_EXECUTE_READWRITE, &dwOld)) {
        error = GetLastError();
        DETOUR_BREAK();
        goto fail;
    }
    // if (!VirtualProtect(pbTarget, cbTarget, PAGE_EXECUTE_READWRITE, &dwOld)) {
    //     error = GetLastError();
    //     DETOUR_BREAK();
    //     goto fail;
    // }


雪    币: 85
活跃值: (101)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
samohyes 2019-3-14 16:32
11
0
gestic 以前遇到过这个问题,也是Hook一直失败,然后就去跟踪DetourAttach,发现里面VirtualProtect调用失败,把它改成 VirtualProtectEx调用就可以了。这个库官方没有维 ...
谢谢啦~我已经在楼上回复了一位回答者,是他说的内联汇编优化的问题~
游客
登录 | 注册 方可回帖
返回