首页
社区
课程
招聘
[原创]为无源码的DLL增加接口功能 (2)
2023-1-16 23:21 6661

[原创]为无源码的DLL增加接口功能 (2)

2023-1-16 23:21
6661

承接前篇[原创]为无源码的DLL增加接口功能的内容,本篇作为补充。因为前者是禁用了优化选项。导致接口中循环的index变量,是保存在调用栈的栈帧中,可以使用栈指针进行内存寻址,从而达到控制index的目的。那么,本篇是开启了编译最大优化,接口函数在编译之后,循环变量index是直接保存在寄存器 esi 中的。这样就造成了之前的方法就不能正常使用了。

 

本篇应该算是使用了轻量级的hook吧,使用Windows API的VirtualProtect修改了代码段的属性,使其为可读写。所以,这个相对于之前的方法有些暴力。但是,对于这种修改寄存器的需求,我目前没有更好的办法。这也难怪前篇下面的评论中,有位老兄说,都是靠hook走天下了。

 


执行效果和之前的一致,同样能满足需求。
图片描述

 

Hook还是极为重要的,后面还是研究一下流行的Hook库吧。


 

完整源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// SleepDemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
 
#include <iostream>
#include <thread>
#include <Windows.h>
#pragma warning(disable:4996)
 
using namespace std;
 
 
void __declspec(dllexport) CalculateFunc(int count)
{
    int index = 0;
    for (int i = 0; i < count; i++)
    {
        printf("i=%d, %d...\n", i, ++index);
        Sleep(1000);
    }
 
    printf("Threader Over...\n");
}
 
void CalculateFuncAsync()
{
    std::thread thread(CalculateFunc, 100);
    thread.join();//等待线程结束
}
 
int main()
{
    std::thread thread(CalculateFuncAsync);
    Sleep(3000);
 
 
#define INSTRUCT_LEN 2
    char instructBak[INSTRUCT_LEN]{ 0 };
    DWORD hookLength = INSTRUCT_LEN;
    DWORD hookAddress = (DWORD)CalculateFunc + 0x2F;
 
    SuspendThread(thread.native_handle());
 
    DWORD curProtection;
    VirtualProtect((void*)hookAddress, hookLength, PAGE_EXECUTE_READWRITE, &curProtection);
 
    memcpy(instructBak, (void*)hookAddress, INSTRUCT_LEN);
    memset((void*)hookAddress, 0x90, hookLength); // 相当于break
 
    ResumeThread(thread.native_handle());
 
    thread.join();
    printf("Main Over...\n");
 
    // 还原
    memcpy((void*)hookAddress, (void*)instructBak, INSTRUCT_LEN);
    DWORD temp;
    VirtualProtect((void*)hookAddress, hookLength, curProtection, &temp);
 
 
    return 0;
}

[CTF入门培训]顶尖高校博士及硕士团队亲授《30小时教你玩转CTF》,视频+靶场+题目!助力进入CTF世界

最后于 2023-1-16 23:28 被_THINCT编辑 ,原因:
收藏
点赞4
打赏
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回