使用绕道DLL钩子从函数内读取数据

Reading data from within a function with detours DLL hook

本文关键字:函数 读取 数据 DLL      更新时间:2023-10-16

我想知道是否有可能从函数中读取数据。
我知道我可以使用绕道来挂钩函数和自由地改变参数。
但这就是我对绕路的理解。

例如:

//cryptkeys
typedef int (WINAPI *pCryptKey)(int crypt1, int crypt2, int crypt3);
int WINAPI MyCryptKey(int crypt1, int crypt2, int crypt3);
pCryptKey MyCrypt2Key = (pCryptKey)(0x60FF50);
int WINAPI MyCryptKey(int crypt1, int crypt2, int crypt3)
{
    cout << crypt1 << crypt2 << crypt3 << endl;
    return MyCrypt2Key(999,135,2);
}

这段代码绕过了游戏中的crypt key函数,并在调用它之前更改了参数。所以当它被调用时,参数被改变了。

我在想,如果函数内部有数据而不是参数上的数据怎么办?
如何更改或显示它?

我应该重写整个函数吗?

我想要得到的是使用游戏本身来加密和解密数据包。
我已经钩住了执行此操作的函数,但我所能做的就是更改参数。
游戏只是继续它的事情。

在它被加密之前我已经改变了数据包,所以另一个数据包被发送。但这只会在我试图发送数据包并修改它时发生。我想直接调用函数,而不是等待它被游戏调用,只是修改值。
比如我将使用游戏输入我自己未加密的数据包,然后按加密键来查看加密的值,反之亦然。

一个解释或教程的链接将是伟大的。


如果我这样做呢:

int WINAPI MyCryptKey(int crypt1, int crypt2, int crypt3)
{
    //dont know whats supposed to be in here. But it should be alot of codes.
}

并像

那样调用返回值
int cryptValue = MyCrypt2Key(999,135,2);
cout << cryptValue << endl;    //to get the return?

您的示例已经在正确的轨道上,coutendl都存在于绕道函数之外。对于在绕道函数中放置什么代码实际上没有任何明确的限制。在函数外访问数据与其他程序一样。

int globalVar = 1;
int WINAPI MyCryptKey(int crypt1, int crypt2, int crypt3)
{
    cout << crypt1 << crypt2 << crypt3 << globalVar << endl;
    globalVar++;
    return MyCrypt2Key(999,135,2);
}

要将数据保存在函数内部,可以像通常那样声明变量,必要时使用静态存储时间。

int WINAPI MyCryptKey(int crypt1, int crypt2, int crypt3)
{
    static int staticVar = 1;
    int localVar = staticVar + 1;
    staticVar++;
    cout << localVar << crypt1 << crypt2 << crypt3 << endl;
    return MyCrypt2Key(999,135,2);
}

如果您想完全替换函数,只需删除对原始函数的调用并提供完整的实现。您应该记住,函数在调用时应该表现出与原始函数相同的行为,否则使用它的代码可能会失败。

int WINAPI MyCryptKey(int crypt1, int crypt2, int crypt3)
{
    cout << crypt1 << crypt2 << crypt3 << endl;
    return 5;
}

弄清楚一个函数是如何工作的,以取代它的实现是你必须付出努力的地方。通过阅读该函数的文档,通常可以很好地了解该函数应该如何工作。如果没有文档或者文档不多,你就得自己去找了。

你可以像平常一样调用原始函数。下面的示例将调用真正的MyCryptKey函数,而不需要被您所挂接的应用程序调用。

int  FunctionForAnotherThread()
{
    int cryptValue = MyCrypt2Key(999,135,2);
    cryptValue += rand() % 10;
    return cryptValue;
}