Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在运行时解密函数并使用它QT c++;_Qt_Antivirus_Drm - Fatal编程技术网

在运行时解密函数并使用它QT c++;

在运行时解密函数并使用它QT c++;,qt,antivirus,drm,Qt,Antivirus,Drm,我是QT新手,我正在尝试创建一个加密函数。 总体而言,您在C/C++中所做的是: 使用指向函数的指针 将函数页设为rwx 加密它(例如,我在同一个程序中加密和解密) 解密并运行它 C中的简单代码大致如下: void TestFunction() { printf("\nmsgbox test encrypted func\n"); } // use this as a end label void FunctionStub() { return; } void XorBlo

我是QT新手,我正在尝试创建一个加密函数。 总体而言,您在C/C++中所做的是:

  • 使用指向函数的指针
  • 将函数页设为rwx
  • 加密它(例如,我在同一个程序中加密和解密)
  • 解密并运行它
C中的简单代码大致如下:

    void TestFunction()
{
    printf("\nmsgbox test encrypted func\n");
}
// use this as a end label
void FunctionStub() { return; }

void XorBlock(DWORD dwStartAddress, DWORD dwSize)
{
    char * addr = (char *)dwStartAddress;
    for (int i = 0; i< dwSize; i++)
    {
        addr[i] ^= 0xff;
    }
}

DWORD GetFuncSize(DWORD* Function, DWORD* StubFunction)
{
    DWORD dwFunctionSize = 0, dwOldProtect;
    DWORD *fnA = NULL, *fnB = NULL;

    fnA = (DWORD *)Function;
    fnB = (DWORD *)StubFunction;
    dwFunctionSize = (fnB - fnA);
    VirtualProtect(fnA, dwFunctionSize, PAGE_EXECUTE_READWRITE, &dwOldProtect); // make function page read write execute permission
    return dwFunctionSize;
}



int main()
{

    DWORD dwFuncSize = GetFuncSize((DWORD*)&TestFunction, (DWORD*)&FunctionStub);
    printf("use func");
    TestFunction();
    XorBlock((DWORD)&TestFunction, dwFuncSize); // XOR encrypt the function
    printf("after enc");
    //TestFunction(); // If you try to run the encrypted function you will get Access Violation Exception.

    XorBlock((DWORD)&TestFunction, dwFuncSize); // XOR decrypt the function
    printf("after\n");
    TestFunction(); // Fine here

    getchar();
}
void XorBlock(DWORD dwStartAddress, DWORD dwSize)
{
    DWORD dwEndAddress = dwStartAddress + dwSize;
    for(DWORD i = dwStartAddress; i < dwEndAddress; i++) {
        // ...
    }
}
void TestFunction()
{
printf(“\nmsgbox test-encrypted func\n”);
}
//将其用作结束标签
void FunctionStub(){return;}
无效XorBlock(DWORD DWSTARTADRENT,DWORD dwSize)
{
char*addr=(char*)dwStartAddress;
对于(int i=0;i
当我试图在QT中运行这样一个示例时,我得到了一个运行时错误

以下是QT中的代码:

    void TestFunction()
{
    QMessageBox::information(0, "Test", "msgbox test encrypted func");
}
void FunctionStub() { return; }

void XorBlock(DWORD dwStartAddress, DWORD dwSize)
{
    char * addr = (char *)dwStartAddress;
    for (int i = 0; i< dwSize; i++)
    {
        addr[i] ^= 0xff;                // here i get seg. fault
    }
}

DWORD GetFuncSize(DWORD* Function, DWORD* StubFunction)
{
    DWORD dwFunctionSize = 0, dwOldProtect;
    DWORD *fnA = NULL, *fnB = NULL;

    fnA = (DWORD *)Function;
    fnB = (DWORD *)StubFunction;
    dwFunctionSize = (fnB - fnA);
    VirtualProtect(fnA, dwFunctionSize, PAGE_EXECUTE_READWRITE, &dwOldProtect); // Need to modify our privileges to the memory

    QMessageBox::information(0, "Test", "change func to read write execute ");
    return dwFunctionSize;
}




void check_enc_function()
{

    DWORD dwFuncSize = GetFuncSize((DWORD*)&TestFunction, (DWORD*)&FunctionStub);
    QMessageBox::information(0, "Test", "use func");
    TestFunction();
    XorBlock((DWORD)&TestFunction, dwFuncSize); // XOR encrypt the function -> @@@ i get seg fault in here @@@
    QMessageBox::information(0, "Test", "after enc");


    TestFunction(); // If you try to run the encrypted function you will get Access Violation Exception.

    XorBlock((DWORD)&TestFunction, dwFuncSize); // XOR decrypt the function
    QMessageBox::information(0, "Test", "after dec");
    TestFunction(); // Fine here

    getchar();
}
void TestFunction()
{
QMessageBox::信息(0,“测试”,“msgbox测试加密函数”);
}
void FunctionStub(){return;}
无效XorBlock(DWORD DWSTARTADRENT,DWORD dwSize)
{
char*addr=(char*)dwStartAddress;
对于(int i=0;i@@@我在这里得到seg错误@@@
QMessageBox::信息(0,“测试”,“在enc之后”);
TestFunction();//如果尝试运行加密函数,将出现访问冲突异常。
XorBlock((DWORD)&TestFunction,dwFuncSize);//对函数进行异或解密
QMessageBox::信息(0,“测试”,“12月后”);
TestFunction();//这里很好
getchar();
}
为什么会这样? QT的行为应该像标准C++一样精确

后脚本

有趣的是,在同样的问题上,对重要函数进行加密的最合法的方式是什么(加密的原因是DRM)

我的意思是,反病毒不会因为我保护自己而错误地将我标记为病毒

PS2

如果我通过网络传递一个加密的函数(比如,我将构建一个服务器-客户端模式,客户端要求从服务器运行它需要的函数,如果得到批准,服务器将其发送给它),我如何安排符号以使函数不会崩溃

PS3

如何在QT中关闭DEP和ASLR防御?(我认为这样我才能执行PS 2。我必须取消它们)

谢谢
yoko

示例是我的系统上未定义的行为

代码中的第一个也是主要问题是:

void TestFunction() { /* ... */ }
void FunctionStub() { return; }
您假设编译器将把
FunctionStub
放在
TestFunction
之后,而不进行任何填充。我编译了您的示例,在我的示例中,
FunctionStub
位于
TestFunction
之上,这导致了负的dwFunctionSize

dwFunctionSize = (fnB - fnA);

TestFunction located at @ 0xa11d90
FunctionStub located at @ 0xa11b50
dwFunctionSize = -0x240
也在XorBlock中

addr[i] ^= 0xff;
他什么也没做

我假设您希望将XorBlock写入内存位置,以对整个
TestFunction
执行XOR操作

你可以这样做:

    void TestFunction()
{
    printf("\nmsgbox test encrypted func\n");
}
// use this as a end label
void FunctionStub() { return; }

void XorBlock(DWORD dwStartAddress, DWORD dwSize)
{
    char * addr = (char *)dwStartAddress;
    for (int i = 0; i< dwSize; i++)
    {
        addr[i] ^= 0xff;
    }
}

DWORD GetFuncSize(DWORD* Function, DWORD* StubFunction)
{
    DWORD dwFunctionSize = 0, dwOldProtect;
    DWORD *fnA = NULL, *fnB = NULL;

    fnA = (DWORD *)Function;
    fnB = (DWORD *)StubFunction;
    dwFunctionSize = (fnB - fnA);
    VirtualProtect(fnA, dwFunctionSize, PAGE_EXECUTE_READWRITE, &dwOldProtect); // make function page read write execute permission
    return dwFunctionSize;
}



int main()
{

    DWORD dwFuncSize = GetFuncSize((DWORD*)&TestFunction, (DWORD*)&FunctionStub);
    printf("use func");
    TestFunction();
    XorBlock((DWORD)&TestFunction, dwFuncSize); // XOR encrypt the function
    printf("after enc");
    //TestFunction(); // If you try to run the encrypted function you will get Access Violation Exception.

    XorBlock((DWORD)&TestFunction, dwFuncSize); // XOR decrypt the function
    printf("after\n");
    TestFunction(); // Fine here

    getchar();
}
void XorBlock(DWORD dwStartAddress, DWORD dwSize)
{
    DWORD dwEndAddress = dwStartAddress + dwSize;
    for(DWORD i = dwStartAddress; i < dwEndAddress; i++) {
        // ...
    }
}
void XorBlock(DWORD dwStartAddress,DWORD dwSize)
{
DWORD DWENDADRESS=dwStartAddress+dwSize;
对于(DWORD i=dwStartAddress;i
在您的示例中,我看不到任何特定于Qt的内容。即使是Qt函数调用,也只是一个调用。所以我猜在这两个例子中都有未定义的行为,但只有第二个例子崩溃了

我看不出编译器和链接器保持函数顺序的任何理由。例如,GCC允许您为每个函数指定代码部分。因此,您可以在可执行文件中对其重新排序,而无需在cpp中重新排序

我认为您需要一些特定于编译器的东西来让它工作