在产生了包括对my_funcion进行加密的myfunction.h文件之后,我创建了一个名为self_engendered_code的工程。在这个工程中,My_function进行了加密并在堆上或者其它一些内存缓冲区诸如通过不被允许的malloc栈缓冲区来进行执行。
步骤2: 解密My_function代码并执行
首先,包括在self_engendered_code项目中的myfunction.h文件。其次,定义一些macro 以便加载多个unsigned char变量到一个内存缓冲区中。
//
#include "..\\\self_engendered_code\\My_function\\myfunction.h"
#define _founc(x) myfunction_##x##_code
#define _founc_len(x) myfunction_##x##_code_LEN
unsigned char p_my_function[1024];
//
#include "..\\\self_engendered_code\\My_function\\myfunction.h"
#define _founc(x) myfunction_##x##_code
#define _founc_len(x) myfunction_##x##_code_LEN
unsigned char p_my_function[1024];
//
void Load_my_function()能够解密My_function代码到一个内存缓冲区中:这里p_my_function是一个全局变量。它能够取代一个定义在函数体中定义的局部变量,而该变量位于函数堆上。
//
void Load_my_function()
{
int code_len=myfunction_CODE_LEN;
unsigned char* pcode=
(unsigned char*)malloc(code_len*sizeof(unsigned char));
if(pcode==NULL)
{
#ifdef _DEBUG
AfxMessageBox("Memory used up!");
#endif
return;
}
int p;
int hp=0;
for(int k=1;k<=myfunction_ARRAY_NUM;k++)
{
switch (k)
{
//The number of case equal to myfunction_ARRAY_NUM
//defined in myfunction.h.
case 1:
for(p=0;p<_founc_len(00001);p++) pcode[hp+p]=_founc(00001)[p];
hp=hp+p;
break;
/*
case 2:
for(p=0;p<_founc_len(00002);p++) pcode[hp+p]=_founc(00002)[p];
hp=hp+p;
break;
case 3:
for(p=0;p<_founc_len(00003);p++) pcode[hp+p]=_founc(00003)[p];
hp=hp+p;
break;
case 4:
for(p=0;p<_founc_len(00004);p++) pcode[hp+p]=_founc(00004)[p];
hp=hp+p;
break;
.
.
.
*/
default:
break;
}
}
//Note: Here is just a simple encryption algorithm, you should
//replace it with your own.
//There are a lot of encryption algorithms which you can get
//from the Internet.
for(int i=0;i<code_len;i++) p_my_function[i]=pcode[i]^99;
}
//
void Load_my_function()
{
int code_len=myfunction_CODE_LEN;
unsigned char* pcode=
(unsigned char*)malloc(code_len*sizeof(unsigned char));
if(pcode==NULL)
{
#ifdef _DEBUG
AfxMessageBox("Memory used up!");
#endif
return;
}
int p;
int hp=0;
for(int k=1;k<=myfunction_ARRAY_NUM;k++)
{
switch (k)
{
//The number of case equal to myfunction_ARRAY_NUM
//defined in myfunction.h.
case 1:
for(p=0;p<_founc_len(00001);p++) pcode[hp+p]=_founc(00001)[p];
hp=hp+p;
break;
/*
case 2:
for(p=0;p<_founc_len(00002);p++) pcode[hp+p]=_founc(00002)[p];
hp=hp+p;
break;
case 3:
for(p=0;p<_founc_len(00003);p++) pcode[hp+p]=_founc(00003)[p];
hp=hp+p;
break;
case 4:
for(p=0;p<_founc_len(00004);p++) pcode[hp+p]=_founc(00004)[p];
hp=hp+p;
break;
.
.
.
*/
default:
break;
}
}
//Note: Here is just a simple encryption algorithm, you should
//replace it with your own.
//There are a lot of encryption algorithms which you can get
//from the Internet.
for(int i=0;i<code_len;i++) p_my_function[i]=pcode[i]^99;
}
//