程序的自我修改
April 30th, 2008 | lonkil | Visual C++ | 發表評論 | trackback
本文目的在於向讀者說明程序進行自我修改的基本方法,
如果讀者有更好的方法或見解,歡迎來信交流E-mail: default_and_default_AT_yahoo.
C++代碼
/*////////////////////////////
This program will modify itself at the running time,
These methods will be very useful in some situations,
Gook Luck!
//////////////////////////////
#include
#include
void main()
{
TCHAR Info001[MAX_PATH]=「Welcome to Big Apple!」;
TCHAR Info002[MAX_PATH]=「Welcome to Washington!」;
char temp=(char)0×90;
WORD temp001=0×9090;
DWORD temp002=0×90909090;
PVOID BaseAddressOne=NULL;
PVOID BaseAddressTwo=NULL;
_asm
{
mov BaseAddressOne,offset LabelOne
mov BaseAddressTwo,offset LabelTwo
}
MessageBox(NULL,Info001,「
//a kind of method to modify itself
WriteProcessMemory(
WriteProcessMemory(
/*
//Another method to modify itself,this method needs to modify the code section’s
//characteristics in PE file.
_asm
{
mov ebx,BaseAddressOne
mov ecx,BaseAddressTwo
mov dx,temp001
mov [ebx],dx
mov [ecx],dx
}
*/
LabelTwo:
_asm
{
jmp LabelOne
}
_asm
{
nop
nop
nop
}
MessageBox(NULL,Info002,「
LabelOne:
_asm
{
jmp Over
}
MessageBox(NULL,Info002,「
Over:
return;
}
編譯這個程序,我們發現WriteProcessMemory(
然後我們屏蔽程序中的WriteProcessMemory()
運行後會發現系統拋出異常 Access Violation.這是因為PE 中 代碼節的屬性默認為 0×60000020,
20 表示代碼 20000000表示可執行,40000000表示可讀,
操作系統的loader在裝載可執行文件時,
這樣就不會有異常了。
讀者可使用下面的程序來修改節屬性:
C++代碼
/*****************************
//The following code is used to modify characteristics of sections
#include
#include
BOOL ModifyCharacteristicsOfSection
{
DWORD i=0;
HANDLE hDestinationFile=NULL;
TCHAR DestinationPEFile[MAX_PATH];
DWORD NumberOfBytesRead=0; //Number of bytes read
DWORD NumberOfBytesWritten=0; //Number of bytes written
DWORD ImageNtSignature=0; //PE signature
DWORD OffsetOfNewHeader=0;
DWORD NumberOfSections=0;
DWORD SizeOfSectionTable=0; //size of section table
HANDLE hGlobalAllocatedMemory=NULL; //use GlobalAlloc();
PIMAGE_SECTION_HEADER pImageSectionHeader=NULL; //a pointer to IMAGE_SECTION_TABLE
IMAGE_DOS_HEADER ImageDosHeader;
IMAGE_NT_HEADERS ImageNTHeaders;
IMAGE_FILE_HEADER ImageFileHeader;
IMAGE_OPTIONAL_HEADER ImageOptionalHeader;
IMAGE_SECTION_HEADER ImageSectionHeader;
DWORD dwFileSize=0;
RtlZeroMemory(&ImageDosHeader,
RtlZeroMemory(&ImageNTHeaders,
RtlZeroMemory(&
RtlZeroMemory(&
RtlZeroMemory(&
strcpy(DestinationPEFile,
hDestinationFile=CreateFile(
FILE_WRITE_DATA|FILE_READ_
FILE_SHARE_WRITE,NULL,OPEN_
if(hDestinationFile==INVALID_
{
// printf(」\nCreateFile() fails!Can’t open file. Please try again!\n」);
// CloseHandle
"上一篇: Windows XP SP3 簡體中文官方http下載地址下一篇: 算法的時間複雜度"
- 程序的自我修改 « Vc爱好者 v3.0 (在「Google 網頁註解」中檢視)
沒有留言:
張貼留言