AntiDebugLIB 编程指南 |
引言:
对于计算机软件项目,在它的设计目标开发完成之后,通常还要对其最终发布的软件程序进行加密保护,以保护该项目的知识产权,防止软件的非法复制与盗版。软件加密的目标是通过技术手段使得被保护的程序只有在许可的范围内才能使用它的功能。
为了防止软件的非法复制、盗版,保护软件开发者的利益,就必须对软件进行加密保护。
通过对Windows下PE可执行文件的结构及载入机制进行深刻的剖析, 巧妙的使用各种密码学算法及多种反破解方案,实现对PE文件加密保护。研究了大量的相关资料,特别是对解密者的思路、过程、步骤进行了仔细的研究,静态分析和动态分析是解密者必用的手段,而动态分析是其终极手段。所谓动态分析就是利用各种程序调试工具对程序进行分析。
因此:如果一个被加密的程序在运行时,任何调试工具都被禁止,那么要破解它是不可能的。 要做到这一点是不容易的,目前有很多流行的软件加密工具,但总是被Cracker破解,主要原因是在反跟踪反调试方面存在漏洞。
方法:如果一个被加密的程序在运行时,每时每刻都要使用调试工具所必需使用的资源,那么任何调试工具都将被禁止或者不能与被加密程序同时运行。
在使用VC++完成正常的设计之后,通常需要对软件进行保护、加密的工作,除非这个软件是作为免费的软件来开发的。为了达到不被破解的目的,必须研究调试器是如何工作的,如何阻止他们正常的工作,如何阻止代码被动态和静态地分析,如何...,这些繁重的工作在AntiDebugLIB地协助下可轻松地完成。
本文通过发表一个演示程序来展示AntiDebugLIB的使用方法。
它是如何工作的呢?
1. AntiDebugLIB V2.3 提供了一些函数,这些函数的定义格式和C++ 运行时库的一样,不同的是它们需要许可证才能正常运行。
2. AntiDebugLIB V2.3 提供了函数定制和加密宏机制。他们能帮助软件开发者快速加密他们的代码。
3. AntiDebugLIB V2.3 提供了强有力的PE文件保护工具--Eagle Protector V2.3 。
在你用VC++完成正常的开发之后,只须4个步骤即可完成程序加密的工作:
第 1 步
在非共享或关键代码中,将C++的函数用AntiDebugLIB的函数进行简单的替换。
显示实例
#ifndef _ANTIDEBUG
strcpy(buffer,mess);
#else
GJ_strcpy(buffer,mess);
#endif第 2 步
使用AntiDebugLIB的 函数定制功能产生具有自生代码功能的函数。
第 3 步
插入一些AntiDebugLIB的加密宏标注出想要加密的代码段范围。
第 4 步
重建工程之后再用Eagle Protector V2.3将PE文件加密即可。
1.AntiDebugLIB 文件
文件名称
说明
antidebug.h 函数库的头文件。 antidebug.lib 函数库文件。 (包含在ADL_Register.EXE 文件中) gjglly.sys AntiDebugLIB 驱动文件。 (包含在ADL_Register.EXE 文件中) ADL_Register.EXE AntiDebugLIB 许可证生成工具和PE文件保护工具。 AntiDebugLib.CHM AntiDebugLIB 帮助文件。
2.程序开发环境操作系统: Windows XP/2003/Vista/2008/7 (X86)
开发环境: Microsoft Visual Studio 2008 / C++Builder / Delphi / VB6.0.3.举例说明
下列步骤将从头创建一个演示工程以演示AntiDebugLIB的使用方法。你可以略过这些步骤,首先在这里下载“antidebug_demo” , 直接编译并得到第一印象。在编译的过程中如果遇到问题请参考文章"How to build antidebug_demo project?" 。
3.1 程序设计
(1) 打开“Microsoft Visual Studio 2008”,选择 “File | New | Project” 菜单命令 ,弹出“New Project” 窗口,选择 Project types:“Visual C++ | MFC”,选择 Templates:“Visual Studio installed templates |MFC Application”,确认工程的名称是“antidebug_demo”,并选择路径。
(2) 在弹出的 “MFC Application AppWinzard - Application Type” 对话框中,选择 应用类型为 “Single document”.
(3) 接下来的步骤,保持默认设置,最后点击 [Finish] 按钮。
(4) 增加两个菜单命令:"Display Hello World !" 和"Display Hello AntiDebug !"
Show illustration
(5) 通过Event Handler Wizard 映射这两个菜单命令函数:
void CAntidebug_demoDoc::OnDisplayHelloWorld().
void CAntidebug_demoDoc::OnDisplayHelloAntidebug().插入如下代码:
显示代码
//File: antidebug_demoDoc.cpp
//Antidebug LIB Demo Code
void CAntidebug_demoDoc::OnDisplayHelloWorld() //share function
{
// TODO: Add your command handler code here
char mess[]="Hello World !";
char buffer[100];
strcpy(buffer,mess);
AfxMessageBox(buffer);
}
void CAntidebug_demoDoc::OnDisplayHelloAntidebug() //no share function
{
// TODO: Add your command handler code here
char mess[]="Hello Antidebug !";
char buffer[100];
strcpy(buffer,mess);
AfxMessageBox(buffer);
}
//Antidebug LIB Demo Code
(6) 完成之后,antidebug_demo能够显示两个消息框:"Hello World ! " 和 "Hello AntiDebug ! ",都是免费的,而且也不需要许可证。
3.2 加密程序
(1) 拷贝 antidebug.h 和 antidebug.lib 到 antidebug_demo 工程目录(\antidebug_demo\)。
(2) 选择 “Project | Protertys” 菜单命令,只对 "Win32 Release"进行设置。
添加 "antidebug.lib setupapi.lib NETAPI32.LIB" 到 Linker 的 input Additional Dependencies 编辑框。显示图解
设置 "Buffer Security Check" 为"NO(/GS)"
显示图解
设置 "Data Execution Prevention(DEP)" 为 "Default" 或者 "Image is not compatible with DEP(/NXCOMPAT:NO)"。显示图解
设置"Randomized Base Address"为"Disable Image Randomization ".
显示图解
设置 "UAC Execution Level " 为 "requireAdministrator"。
显示图解
(3) 在antidebug_demoDoc.h 文件中添加代码:
显示代码
//#####################################################################
//Antidebug LIB Demo Code#ifndef _DEBUG //If define _DEBUG,we don't use the AntiDebugLIB in order to debug the program.
#include "antidebug.h"
#define _ANTIDEBUG //If define _ANTIDEBUG,the no shared functions can't be debugged.#define _TRIAL_VERSION //If define _TRIAL_VERSION,we use the AntiDebugLIB's demo function,
// and use Eagle Protector Trial Version to encrypt the antidebug_demo.exe.
//If no define _TRIAL_VERSION,we use the registered version function of AntiDebugLIB,
// and use Eagle Protector Registered Version to encrypt the antidebug_demo.exe.
//This is very important ! You mustn't cross them.//If define _TRIAL_VERSION,and use Eagle Protector Trial Version to encrypt the antidebug_demo.exe,
//all encryption macro will be ignored.
// That means only Registered Version supports the encryption macro.
#endif//Antidebug LIB Demo Code
//#####################################################################
(4) 在antidebug_demoDoc.cpp 文件中添加代码:
显示代码
/////////////////////////////////////////////////////////////////////////////
// CAntidebug_demoDoc construction/destructionCAntidebug_demoDoc::CAntidebug_demoDoc()
{
// TODO: add one-time construction code here
//#####################################################################
//Antidebug LIB Demo Code#ifdef _ANTIDEBUG
GJ_ENCRYPT_CODE_START //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO START
if(GJ_Install_AntiDebugLIB())
{
AfxMessageBox("Install AntiDebugLIB Successfully !"); //Here it is only a demo code,you should use "//" to clear it.
}
else
{
AfxMessageBox("Install AntiDebugLIB Unsuccessfully !"); //Here it is only a demo code,you should use "//" to clear it.
}
#ifdef _TRIAL_VERSION
char antidebug_userid[]=
"088A8EA376ECBE1F141F83C99946A068BE7681469A4157CDE99E2EECEF6133054E50DC281402D4F0A361D9486804ADFE";
//antidebug_userid is generated by AntideBug LIB REGISTER programme.
//Here is the antidebug demo userid.
char antidebug_licence_filename[]="antidebuglib_demo.lic";
//antidebuglib_demo.lic is generated by AntideBug LIB REGISTER programme(ADL_Register.EXE).
//Here is the antidebug demo license.
//antidebuglib_demo.lic is in the current directory.#else
char antidebug_userid[]="**********************************your userid goes here******************************************";
//antidebug_userid is generated by AntideBug LIB REGISTER programme.
char antidebug_licence_filename[]="antidebuglib.lic";
//antidebuglib.lic is generated by AntideBug LIB REGISTER programme(ADL_Register.EXE).
#endif
BYTE append_data[]={'1','2','3','4','5','6'}; //AntiDebug LIB provides two kinds of version: Ultimate and Professional Version.
//Only Ultimate Version can use appending datas to generate license file.
//
//If no appending datas(e.g. hardware serial number or programme code fingerprint
//data generated by MD5.) are supplied or AntiDebug LIB's Version is Professional,
//set antidebug_licence_filename=NULL,antidebug_append_data_len=0.
//
//The format of appending data file can be found in the help document.It is very simple:
//Just save all datas to a file.//When generate the demo license by AntideBug LIB REGISTER programme,
// please use antidebug_append.dat in the current directory(\antidebug_demo).UINT antidebug_append_data_len=6; //MAX_LENGTH<300
if(!GJ_IsAntidebugWorking())
{
MessageBox(NULL,
"AntiDebug LIB DRIVER isn't installed or started,
Antidebug_demo programme will EXIT !",
"Antidebug Demo",MB_ICONINFORMATION
);
//Here it is only a demo code,you should use "//" to clear it.
ExitProcess(0);
};
#ifdef _TRIAL_VERSION
GJ_OpenAntidebug_demo(antidebug_userid,
antidebug_licence_filename,
antidebug_append_data_len,
append_data
);
#else
GJ_OpenAntidebug(antidebug_userid,
antidebug_licence_filename,
antidebug_append_data_len,
append_data
);
#endif
//TEST IF REGISTER OK
//Not only the GJ_add function but also the others can do the same thing.
int a=1;
int b=2;
int c=a+b;
#ifdef _TRIAL_VERSION
a=GJ_add_demo(a,b);
#else
a=GJ_add(a,b);
#endif
if(a==c)
{
AntiDebug_Reg_OK=TRUE;
AfxMessageBox("Register Successfully !"); //Here it is only a demo code,you should use "//" to clear it.
}
else
{
AntiDebug_Reg_OK=FALSE;
AfxMessageBox("Register Unsuccessfully !"); //Here it is only a demo code,you should use "//" to clear it.
}
//TEST IF REGISTER OK
GJ_ENCRYPT_CODE_END //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO END
#endif
//Antidebug LIB Demo Code
//#####################################################################}
CAntidebug_demoDoc::~CAntidebug_demoDoc()
{
//#####################################################################
//Antidebug LIB Demo Code#ifdef _ANTIDEBUG
#ifdef _TRIAL_VERSION
GJ_CloseAntidebug_demo();
#else
GJ_CloseAntidebug();
#endif
GJ_Remove_AntiDebugLIB();
GJ_MACRO_END //This MACRO should be executed only once
#endif
//Antidebug LIB Demo Code
//#####################################################################
}
(5) 修改非共享函数代码:
显示代码
//File:antidebug_demoDoc.cpp
void CAntidebug_demoDoc::OnDisplayHelloAntidebug() //non-sharing function
{
// TODO: Add your command handler code here#ifdef _ANTIDEBUG
if(AntiDebug_Reg_OK)
{
#endifchar mess[]="Hello Antidebug !";
char buffer[100];#ifndef _ANTIDEBUG
strcpy(buffer,mess);
#else
#ifdef _TRIAL_VERSION
GJ_strcpy_demo(buffer,mess);
#else
GJ_strcpy(buffer,mess);
#endif
#endif
AfxMessageBox(buffer);
#ifdef _ANTIDEBUG
}
else
{
AfxMessageBox("Only registered user can use this function !");
}#endif
}
(6) 选择 “Build | Rdbuild Solution” 菜单命令加密antidebug_demo。现在,如果你想要显示"Hello AntiDebug !" 消息框,必须有由ADL_register.exe产生的许可证才行。
(7) 定制函数
对于那些非常重要的代码,强烈建议你使用AntiDebug LIB提供的定制函数机制,快速产生具有代码自生功能的函数,那么这些代码就成为了只有你一个人知道的秘密了。
AntiDebug LIB 提供三个函数来完成这个工作:
GJ_encrypt_custom_function.
GJ_load_custom_founction.
GJ_free_custom_founction.GJ_encrypt_custom_function (or GJ_encrypt_custom_function_demo) 函数只使用在另外一个不公开发布的工程中使用,定制的函数在其中调试并加密,当GJ_encrypt_custom_function (or GJ_encrypt_custom_function_demo) 返回TRUE,表示函数执行成功,将产生一个"founction_name.h" 头文件,该头文件包含经过加密的定制函数代码,将它包含在要发布工程文件中,使用时调用GJ_load_custom_founction (or GJ_load_custom_founction_demo) 解密,然后执行,使用完毕,调用GJ_free_custom_founction 清除。
现在,我们创建另外一个工程,命名为custom_function_demo,创建的方法和 antidebug_demo工程类似。 函数 void __stdcall custom_function(...) 是想要加密的函数,我们调用void encrypt_custom_function()将它加密,得到了包含custom_function(...)函数密文代码的头文件 custom_function.h 。
显示 void __stdcall custom_function() 和 void encrypt_custom_function() 代码
得到了custom_function.h之后(当然要运行custom_function_demo才能产生custom_function.h),我们将语句 #include "..\\antidebug_demo\\custom_function_demo\\custom_function.h"插入到antidebug_demoDoc.cpp文件当中,这个文件属于antidebug_demo 工程。
//File: custom_function_demoDoc.cpp
//#####################################################################
//Antidebug LIB Demo Code//The custom_function is the name of custom function,the function must be relocation code.
//
//1:Only use local variable,don't use global variable,static variable,and constant string variable.
//2:If custom_function wants to invoke a function,the pointer of the function should be passed to it.
//
//in a word, the custom function must be relocation code.void __stdcall custom_function(int x,
int y,
char* str_a,
char* str_b,
void* (__cdecl *_memcpy )( void *dest, const void *src, size_t count ),
int (__cdecl *_sprintf )( char *buffer, const char *format, ... ),
void* (__cdecl *_malloc )( size_t size ),
void (__cdecl *_free )( void *memblock ),
size_t (__cdecl *_strlen )( const char *string ),
int (__stdcall* _MessageBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
)
{
char* pTemp;
int str_a_len=_strlen(str_a);
int str_b_len=_strlen(str_b);
pTemp=(char*)_malloc(str_a_len+str_b_len+20);if(x>y)
{
//_sprintf(pTemp,"%s%s",str_a,str_b); error:constant string variable
_memcpy(pTemp,str_a,str_a_len);
_memcpy(pTemp+str_a_len,str_b,str_b_len);
pTemp[str_a_len+str_b_len]=0;//_MessageBox(NULL,pTemp,"",MB_OK); error:constant string variable
_MessageBox(NULL,pTemp,str_a,MB_OK);
}
else
{
//_sprintf(pTemp,"%s%s",str_b,str_a); error:constant string variable
_memcpy(pTemp,str_b,str_b_len);
_memcpy(pTemp+str_b_len,str_a,str_a_len);
pTemp[str_a_len+str_b_len]=0;//_MessageBox(NULL,pTemp,"",MB_OK); error:constant string variable
_MessageBox(NULL,pTemp,str_b,MB_OK);
}for(int i=0;i<10;i++)
{
int j=1;
j^=i;
}_free(pTemp);
}void __stdcall custom_function_END() //In order to calculate the length of custom function,
// custom_function_END function must follow on the heels of custom_function.
{
}#ifdef _ANTIDEBUG
void encrypt_custom_function()
{
void ( __stdcall *_custom_function)( int x,
int y,
char* str_a,
char* str_b,
void* (__cdecl *_memcpy )( void *dest, const void *src, size_t count ),
int (__cdecl *_sprintf )( char *buffer, const char *format, ... ),
void* (__cdecl *_malloc )( size_t size ),
void (__cdecl *_free )( void *memblock ),
size_t (__cdecl *_strlen )( const char *string ),
int (__stdcall* _MessageBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
);
void ( __stdcall *_custom_function_END)();
unsigned int n_custom_function_size;
char* function_name="custom_function";
_custom_function=custom_function;
_custom_function_END=custom_function_END;
n_custom_function_size=abs((INT)_custom_function_END-(INT)_custom_function)+1; //calculate the length of custom function
#ifdef _TRIAL_VERSION
BOOL nstatus=GJ_encrypt_custom_function_demo((BYTE*)_custom_function,n_custom_function_size,function_name);
#else
BOOL nstatus=GJ_encrypt_custom_function((BYTE*)_custom_function,n_custom_function_size,function_name);
#endif
if(nstatus)
{
AfxMessageBox("The custom function is encrypted successfully ! The encrypted code is included in custom_function.h file.");
}
else
{
AfxMessageBox("The custom function is encrypted unsuccessfully !");
}}
#endif
//Antidebug LIB Demo Code
//#####################################################################显示 custom_function.h 代码
//custom_function.h//Created by AntiDebug LIB
//Http://www.antidebuglib.comunsigned char custom_function_00001_code[]="\
\xa5\x3f\x84\x0b\x63\x23\x34\x9f\xc1\x51\xc5\xb7\x21\xbb\x13\xe8\x4a\x77\xaa\x2a\
\x1e\xfc\x89\xfc\x54\x0f\x48\xfc\x50\x45\xfb\xbe\x28\xe6\x65\x53\x2f\xc5\x16\x4c\
\x84\xd6\x67\x9d\xbf\x49\x1b\x48\x64\x5d\xe7\xe9\x18\x14\x8b\xed\xed\x51\xd7\x75\
\x5b\x2e\xc2\x55\x96\x25\x07\xa8\x40\x6c\xb7\x3e\x52\xdd\x3f\x0c\x81\x44\x8b\xd0\
\xce\x7d\xa3\x49\x02\x0f\x8d\xbb\xeb\x9f\x48\xed\x09\xa7\xae\x30\x41\x7b\xc1\xaa\
\xb7\xbd\x91\xdd\xe8\xe8\xe5\xd4\x65\xae\xa7\xb3\x8a\x03\x21\x11\x08\xd7\x32\xcb\
\xf9\x60\x94\x49\x8e\xc4\xbe\x1a\xcb\x25\xbc\xa9\x49\x69\x8c\x56\x8e\x12\xc1\x83\
\x71\x84\xa9\xb4\xd3\x76\x8f\x5d\x03\xfc\x47\xfe\xc8\x8a\x2e\x59\x88\x98\x72\x90\
\xa5\x2f\xd5\x13\x0c\x00\x2b\x66\x66\x2b\x66\x66\x2b\x66\x66\x2b\x66\x66\x2b\x66\
\xe1";
#define custom_function_00001_code_LEN 181#define custom_function_ARRAY_NUM 1
#define custom_function_CODE_LEN 181
显示 CUSTOM FUNCTION TEST 代码
//File: antidebug_demoDoc.cpp
//Antidebug LIB Demo Code 2: CUSTOM FUNCTION TEST
#ifdef _ANTIDEBUG
//Explanation:custom_function is the name of custom function.
//The custom_function.h head file is generated by the custom_function_demo Project.#include "..\\antidebug_demo\\custom_function_demo\\custom_function.h"
#define _founc(x) custom_function_##x##_code
#define _founc_len(x) custom_function_##x##_code_LENunsigned char* p_custom_function;
//1: Load custom function.
// First assemble cryptograph of custom function into one buffer,
// then invoke GJ_load_custom_function_demo(BYTE* custom_function_code,int custom_function_length).void Load_custom_function()
{
int code_len=custom_function_CODE_LEN;
unsigned char* pcode=(unsigned char*)malloc(code_len*sizeof(unsigned char));if(pcode==NULL)
{
#ifdef _DEBUG
AfxMessageBox("Memory used up!");
#endifreturn;
}GJ_ENCRYPT_CODE_WITH_LIC_START //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO START
int p;
int hp=0;for(int k=1;k<=custom_function_ARRAY_NUM;k++){
switch (k){
//The number of case equal to custom_function_ARRAY_NUM defined in custom_function.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;
}
}
#ifdef _TRIAL_VERSION
p_custom_function=GJ_load_custom_function_demo(pcode,code_len);
#else
p_custom_function=GJ_load_custom_function(pcode,code_len);
#endif
free(pcode);
GJ_ENCRYPT_CODE_WITH_LIC_END //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO END
}
//2:Execute the custom function.
void Run_custom_function()
{GJ_ENCRYPT_CODE_WITH_LIC_START //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO START
int x=1;
int y=2;
char str_a[]=" HELLO CUSTOM_FOUNCTION ! ";
char str_b[]=" Hello Custom_function ! ";void* (__cdecl *_memcpy )( void *dest, const void *src, size_t count );
int (__cdecl *_sprintf )( char *buffer, const char *format, ... );
void* (__cdecl *_malloc )( size_t size );
void (__cdecl *_free )( void *memblock );
size_t (__cdecl *_strlen )( const char *string );
int (__stdcall* _MessageBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);_memcpy=memcpy;
_sprintf=sprintf;
_malloc=malloc;
_free=free;
_strlen=strlen;
_MessageBox=MessageBox;void ( __stdcall *_custom_function)( int x,
int y,
char* str_a,
char* str_b,
void* (__cdecl *_memcpy )( void *dest, const void *src, size_t count ),
int (__cdecl *_sprintf )( char *buffer, const char *format, ... ),
void* (__cdecl *_malloc )( size_t size ),
void (__cdecl *_free )( void *memblock ),
size_t (__cdecl *_strlen )( const char *string ),
int (__stdcall* _MessageBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
);
_custom_function=(void ( __stdcall *)(int x,
int y,
char* str_a,
char* str_b,
void* (__cdecl *_memcpy )( void *dest, const void *src, size_t count ),
int (__cdecl *_sprintf )( char *buffer, const char *format, ... ),
void* (__cdecl *_malloc )( size_t size ),
void (__cdecl *_free )( void *memblock ),
size_t (__cdecl *_strlen )( const char *string ),
int (__stdcall* _MessageBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
)) p_custom_function;_custom_function( x,
y,
str_a,
str_b,
_memcpy,
_sprintf,
_malloc,
_free,
_strlen,
_MessageBox
);GJ_ENCRYPT_CODE_WITH_LIC_END //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO END
}
//3:Unload the custom function.
void Free_custom_function()
{
#ifdef _TRIAL_VERSIONGJ_free_custom_function_demo(p_custom_function,custom_function_CODE_LEN);
#else
GJ_free_custom_function(p_custom_function,custom_function_CODE_LEN);
#endif
}
#endif
void CAntidebug_demoDoc::OnCustomFunctionTest() //no shared function
{
// TODO: Add your command handler code here#ifdef _ANTIDEBUG
if(AntiDebug_Reg_OK)
{//GJ_ENCRYPT_CODE_WITH_LIC_START //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO START
//NOTE: Don't use ENCRYPTION MACRO here,because MACRO NESTING will occur !
// If you do use it,the effect will be similar to the MACRO GJ_ONCE_CODE.This code will be executed only once.Load_custom_function(); //Load
Run_custom_function(); //Execute
Free_custom_function(); //Unload//GJ_ENCRYPT_CODE_WITH_LIC_END //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO END
}
else
{
AfxMessageBox("Only registered user can use this function !");
}#endif
}
更多的信息请参考: Atidebug_demo 演示程序和 Code Project 网站中的文章:Programming Self-generating Code for Windows Applications. .(8) 加密宏
AntiDebugLIB 提供四个加密宏来帮助软件开发者可以更快速地加密他们的代码:
只用注册版本才提供此项功。
GJ_MACRO_INIT GJ_MACRO_END GJ_ONCE_CODE_START GJ_ONCE_CODE_END GJ_ENCRYPT_CODE_START GJ_ENCRYPT_CODE_END GJ_ENCRYPT_CODE_WITH_LIC_START GJ_ENCRYPT_CODE_WITH_LIC_END
显示 GJ_MACRO_INIT, GJ_MACRO_END 例子代码
//File: antidebug_demo.cpp
CAntidebug_demoApp::CAntidebug_demoApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance//#####################################################################
//Antidebug LIB Demo Code#ifdef _ANTIDEBUG
GJ_MACRO_INIT //This MACRO should be executed only once.
#endif
//Antidebug LIB Demo Code
//#####################################################################}
// antidebug_demoDoc.cpp
CAntidebug_demoDoc::~CAntidebug_demoDoc()
{
//#####################################################################
//Antidebug LIB Demo Code#ifdef _ANTIDEBUG
#ifdef _TRIAL_VERSION
GJ_CloseAntidebug_demo();
#else
GJ_CloseAntidebug();
#endif
GJ_Remove_AntiDebugLIB();
GJ_MACRO_END //This MACRO should be executed only once
#endif
//Antidebug LIB Demo Code
//#####################################################################
}
显示 GJ_ONCE_CODE_START,GJ_ONCE_CODE_END 例子代码
//File: antidebug_demo.cpp
BOOL CAntidebug_demoApp::InitInstance()
{//#####################################################################
//Antidebug LIB Demo Code#ifdef _ANTIDEBUG
GJ_ONCE_CODE_START
#endif
//Antidebug LIB Demo Code
//#####################################################################AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CAntidebug_demoDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CAntidebug_demoView));
AddDocTemplate(pDocTemplate);// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);//#####################################################################
//Antidebug LIB Demo Code#ifdef _ANTIDEBUG
GJ_ONCE_CODE_END
#endif
//Antidebug LIB Demo Code
//#####################################################################// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();//#####################################################################
//Antidebug LIB Demo CodeGetCurrentDirectory(MAX_PATH,szCurrentDir);
//Antidebug LIB Demo Code
//#####################################################################return TRUE;
}
显示 GJ_ENCRYPT_CODE_START,GJ_ENCRYPT_CODE_END 例子代码
//File: antidebug_demoDoc.cpp
/////////////////////////////////////////////////////////////////////////////
// CAntidebug_demoDoc construction/destructionCAntidebug_demoDoc::CAntidebug_demoDoc()
{
// TODO: add one-time construction code here
//#####################################################################
//Antidebug LIB Demo Code#ifdef _ANTIDEBUG
GJ_ENCRYPT_CODE_START //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO START
if(GJ_Install_AntiDebugLIB())
{
AfxMessageBox("Install AntiDebugLIB Successfully !"); //Here it is only a demo code,you should use "//" to clear it.
}
else
{
AfxMessageBox("Install AntiDebugLIB Unsuccessfully !"); //Here it is only a demo code,you should use "//" to clear it.
}
#ifdef _TRIAL_VERSION
char antidebug_userid[]="088A8EA376ECBE1F141F83C99946A068BE7681469A4157CDE99E2EECEF6133054E50DC281402D4F0A361D9486804ADFE";
//antidebug_userid is generated by AntideBug LIB REGISTER programme.
//Here is the antidebug demo userid.
char antidebug_licence_filename[]="antidebuglib_demo.lic";
//antidebuglib_demo.lic is generated by AntideBug LIB REGISTER programme(ADL_Register.EXE).
//Here is the antidebug demo license.
//antidebuglib_demo.lic is in the current directory.#else
char antidebug_userid[]="**********************************your userid goes here******************************************";
//antidebug_userid is generated by AntideBug LIB REGISTER programme.
char antidebug_licence_filename[]="antidebuglib.lic";
//antidebuglib.lic is generated by AntideBug LIB REGISTER programme(ADL_Register.EXE).
#endif
BYTE append_data[]={'1','2','3','4','5','6'}; //AntiDebug LIB provides two kinds of version: Ultimate and Professional Version.
//Only Ultimate Version can use appending datas to generate license file.
//
//If no appending datas(e.g. hardware serial number or programme code fingerprint
//data generated by MD5.) are supplied or AntiDebug LIB's Version is Professional,
//set antidebug_licence_filename=NULL,antidebug_append_data_len=0.
//
//The format of appending data file can be found in the help document.It is very simple:
//Just save all datas to a file.//When generate the demo license by AntideBug LIB REGISTER programme,
// please use antidebug_append.dat in the current directory(\antidebug_demo).UINT antidebug_append_data_len=6; //MAX_LENGTH<300
if(!GJ_IsAntidebugWorking())
{
MessageBox(NULL,
"AntiDebug LIB DRIVER isn't installed or started,
Antidebug_demo programme will EXIT !",
"Antidebug Demo",MB_ICONINFORMATION
);
//Here it is only a demo code,you should use "//" to clear it.
ExitProcess(0);
};
#ifdef _TRIAL_VERSION
GJ_OpenAntidebug_demo(antidebug_userid,
antidebug_licence_filename,
antidebug_append_data_len,
append_data
);
#else
GJ_OpenAntidebug(antidebug_userid,
antidebug_licence_filename,
antidebug_append_data_len,
append_data
);
#endif
//TEST IF REGISTER OK
//Not only the GJ_add function but also the others can do the same thing.
int a=1;
int b=2;
int c=a+b;
#ifdef _TRIAL_VERSION
a=GJ_add_demo(a,b);
#else
a=GJ_add(a,b);
#endif
if(a==c)
{
AntiDebug_Reg_OK=TRUE;
AfxMessageBox("Register Successfully !"); //Here it is only a demo code,you should use "//" to clear it.
}
else
{
AntiDebug_Reg_OK=FALSE;
AfxMessageBox("Register Unsuccessfully !"); //Here it is only a demo code,you should use "//" to clear it.
}
//TEST IF REGISTER OK
GJ_ENCRYPT_CODE_END //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO END
#endif
//Antidebug LIB Demo Code
//#####################################################################}
显示 GJ_ENCRYPT_CODE_WITH_LIC_START,GJ_ENCRYPT_CODE_WITH_LIC_END 例子代码
// antidebug_demoDoc.cpp
//Antidebug LIB Demo Code 2: CUSTOM FUNCTION TEST
#ifdef _ANTIDEBUG
//Explanation:custom_function is the name of custom function.
//The custom_function.h head file is generated by the custom_function_demo Project.#include "..\\antidebug_demo\\custom_function_demo\\custom_function.h"
#define _founc(x) custom_function_##x##_code
#define _founc_len(x) custom_function_##x##_code_LENunsigned char* p_custom_function;
//1: Load custom function.
// First assemble cryptograph of custom function into one buffer,
// then invoke GJ_load_custom_function_demo(BYTE* custom_function_code,int custom_function_length).void Load_custom_function()
{
int code_len=custom_function_CODE_LEN;
unsigned char* pcode=(unsigned char*)malloc(code_len*sizeof(unsigned char));if(pcode==NULL)
{
#ifdef _DEBUG
AfxMessageBox("Memory used up!");
#endifreturn;
}GJ_ENCRYPT_CODE_WITH_LIC_START //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO START
int p;
int hp=0;for(int k=1;k<=custom_function_ARRAY_NUM;k++){
switch (k){
//The number of case equal to custom_function_ARRAY_NUM defined in custom_function.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;
}
}
#ifdef _TRIAL_VERSION
p_custom_function=GJ_load_custom_function_demo(pcode,code_len);
#else
p_custom_function=GJ_load_custom_function(pcode,code_len);
#endif
free(pcode);
GJ_ENCRYPT_CODE_WITH_LIC_END //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO END
}
//2:Execute the custom function.
void Run_custom_function()
{GJ_ENCRYPT_CODE_WITH_LIC_START //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO START
int x=1;
int y=2;
char str_a[]=" HELLO CUSTOM_FOUNCTION ! ";
char str_b[]=" Hello Custom_function ! ";void* (__cdecl *_memcpy )( void *dest, const void *src, size_t count );
int (__cdecl *_sprintf )( char *buffer, const char *format, ... );
void* (__cdecl *_malloc )( size_t size );
void (__cdecl *_free )( void *memblock );
size_t (__cdecl *_strlen )( const char *string );
int (__stdcall* _MessageBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);_memcpy=memcpy;
_sprintf=sprintf;
_malloc=malloc;
_free=free;
_strlen=strlen;
_MessageBox=MessageBox;void ( __stdcall *_custom_function)( int x,
int y,
char* str_a,
char* str_b,
void* (__cdecl *_memcpy )( void *dest, const void *src, size_t count ),
int (__cdecl *_sprintf )( char *buffer, const char *format, ... ),
void* (__cdecl *_malloc )( size_t size ),
void (__cdecl *_free )( void *memblock ),
size_t (__cdecl *_strlen )( const char *string ),
int (__stdcall* _MessageBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
);
_custom_function=(void ( __stdcall *)(int x,
int y,
char* str_a,
char* str_b,
void* (__cdecl *_memcpy )( void *dest, const void *src, size_t count ),
int (__cdecl *_sprintf )( char *buffer, const char *format, ... ),
void* (__cdecl *_malloc )( size_t size ),
void (__cdecl *_free )( void *memblock ),
size_t (__cdecl *_strlen )( const char *string ),
int (__stdcall* _MessageBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
)) p_custom_function;_custom_function( x,
y,
str_a,
str_b,
_memcpy,
_sprintf,
_malloc,
_free,
_strlen,
_MessageBox
);GJ_ENCRYPT_CODE_WITH_LIC_END //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO END
}
//3:Unload the custom function.
void Free_custom_function()
{
#ifdef _TRIAL_VERSIONGJ_free_custom_function_demo(p_custom_function,custom_function_CODE_LEN);
#else
GJ_free_custom_function(p_custom_function,custom_function_CODE_LEN);
#endif
}
#endif
void CAntidebug_demoDoc::OnCustomFunctionTest() //no shared function
{
// TODO: Add your command handler code here#ifdef _ANTIDEBUG
if(AntiDebug_Reg_OK)
{//GJ_ENCRYPT_CODE_WITH_LIC_START //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO START
//NOTE: Don't use ENCRYPTION MACRO here,because MACRO NESTING will occur !
// If you do use it,the effect will be similar to the MACRO GJ_ONCE_CODE.This code will be executed only once.Load_custom_function(); //Load
Run_custom_function(); //Execute
Free_custom_function(); //Unload//GJ_ENCRYPT_CODE_WITH_LIC_END //$$$$$$$$$$$$$$$$$$$$$$$$$$$ ENCRYPTION MACRO END
}
else
{
AfxMessageBox("Only registered user can use this function !");
}#endif
}
显示加密宏的使用方法
更多的信息请参考 Atidebug_demo 演示程序。
(9) 为了使用户能方便地产生Serial Number,在Antidebug_demo的about 对话框中插入一些必要的代码。
显示代码
//File: antidebug_demo.cpp
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
{
public:
CAboutDlg();//#####################################################################
//Antidebug LIB Demo Codechar adl_sn[1024];
char* szCurrentDir;
BOOL OnInitDialog();//Antidebug LIB Demo Code
//#####################################################################// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
afx_msg void OnSnCopy();
afx_msg void OnLicBrowse();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};// App command to run the dialog
void CAntidebug_demoApp::OnAppAbout()
{
CAboutDlg aboutDlg;//#####################################################################
//Antidebug LIB Demo CodeaboutDlg.szCurrentDir=szCurrentDir;
//Antidebug LIB Demo Code
//#####################################################################aboutDlg.DoModal();
}/////////////////////////////////////////////////////////////////////////////
// CAntidebug_demoApp message handlersBOOL CAboutDlg::OnInitDialog()
{//#####################################################################
//Antidebug LIB Demo Code#ifdef _ANTIDEBUG
CDialog::OnInitDialog();
if(GJ_IsAntidebugWorking())
{
char version[]="2.3.0.0"; //Don't use any other string ! This is the AntiDebugLIB interior Version.
char username[]="AntiDebug_Demo";
GJ_Create_ADL_Serial_Number(version,username,NULL,adl_sn);
CEdit* pedit=(CEdit*) GetDlgItem(IDC_EDIT_SN);
pedit->SetWindowText(adl_sn);
}
return true;
#elsereturn true;
#endif
//Antidebug LIB Demo Code
//#####################################################################}
void CAboutDlg::OnSnCopy()
{
// TODO: Add your control notification handler code here//#####################################################################
//Antidebug LIB Demo CodeHANDLE hData;
LPSTR lpData;//Allocate memory and copy the string to it
if (!(hData=GlobalAlloc(GMEM_MOVEABLE, (DWORD)strlen(adl_sn)+1)))
{
return;
}
if (!(lpData = (char *)GlobalLock(hData)))
{
return;
}
strcpy(lpData, adl_sn);GlobalUnlock(hData);
// Clear the current contents of the clipboard, and set the data handle to the new string.
if (OpenClipboard())
{
EmptyClipboard();
SetClipboardData(CF_TEXT, hData);
CloseClipboard();
}
GlobalFree (hData);//Antidebug LIB Demo Code
//#####################################################################}
void CAboutDlg::OnLicBrowse()
{
// TODO: Add your control notification handler code here//#####################################################################
//Antidebug LIB Demo CodeCFileDialog dlg(TRUE,"lic","",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"(*.lic) |*.lic| All Files (*.*) |*.*|",NULL);
if(dlg.DoModal()==IDOK)
{
CString adl_license_file_name=dlg.GetPathName();char newfilename[MAX_PATH];
char filename[MAX_PATH];char* pszSlash = strrchr(adl_license_file_name.GetBuffer(5), '\\');
strcpy(filename,pszSlash);int eee=sprintf(newfilename,"%s",szCurrentDir);
sprintf(newfilename+eee,"%s",filename);if(CopyFile(adl_license_file_name.GetBuffer(5),newfilename,FALSE))
{
char mes[1024];
eee=sprintf(mes,"<%s> is copyed to <%s> successfully ! AntiDebug_demo should be restarted ! ",filename+1,szCurrentDir);
MessageBox(mes,"ADL_LICENSE",MB_OK|MB_ICONINFORMATION);
CEdit* pedit=(CEdit*)GetDlgItem(IDC_EDIT_ADL_LIC_MES);
pedit->SetWindowText(mes);
}
}
SetCurrentDirectory(szCurrentDir);//Antidebug LIB Demo Code
//#####################################################################
}
(10) 最后,使用Eagle Protector加密antidebug_demo.exe 。
终于,Antidebug_demo 能够显示三个消息框:一个免费的 "Hello World ! "消息框, 两个收费的 "Hello AntiDebug ! " 和 "Hello Cuntom function ! "消息框。
现在,你可以非常安全地发布你的试用版软件了,当然要带上gjglly.sys。4.许可证生成
(1) 粘贴 拷贝自用户的antidebug_demo about 对话窗口的Serial Number到 ADL Register 的 Serial Number Edit box。
(2) 选择 antidebug_append.dat 文件作为 Appending Data.
(3) 单击 <Generate ADL DEMO LICENSE> 按钮。
(4) 保存许可证数据到 antidebuglib_demo.lic 文件并邮寄给用户。5.结束语
上述 antidebug_demo 程序演示了AntiDebug LIB 反跟踪静态函数库加密应用程序的基本步骤和框架。如果在使用AntiDebug LIB过程中遇到任何技术问题或者希望在下一个版本中添加一些特殊的功能,请随时通过Email:support@antidebuglib.com,MSN:
或者电话:132 0383 3305,QQ: 86029535联系我们
。您也可以登录我们的论坛,留下您最宝贵的意见和建议。我们的最新版的软件总是可以在 http://www.antidebuglib.com下载。















