發(fā)信人: Litoad (Rick), 信區(qū): Delphi
標(biāo) 題: Self-Delete程序之Delphi版本,終于搞定了。 (轉(zhuǎn)載)
發(fā)信站: BBS 水木清華站 (Mon Jun 4 20:51:55 2001)
【 以下文字轉(zhuǎn)載自 PRogramming 討論區(qū) 】
發(fā)信人: Litoad (Rick), 信區(qū): Programming
標(biāo) 題: Self-Delete程序之Delphi版本,終于搞定了。
發(fā)信站: BBS 水木清華站 (Mon Jun 4 20:50:42 2001)
到Borland的論壇去問了問,確實(shí)跟laoduan說得一樣,要
自己GetProcAddress。代碼如下:
program Project1;
uses
Windows;
procedure DeleteSelf;
var
hModule: THandle;
buff: array[0..255] of Char;
hKernel32: THandle;
pExitProcess, pDeleteFileA, pUnmapViewOfFile: Pointer;
begin
hModule := GetModuleHandle(nil);
GetModuleFileName(hModule, buff, sizeof(buff));
CloseHandle(THandle(4));
hKernel32 := GetModuleHandle('KERNEL32');
pExitProcess := GetProcAddress(hKernel32, 'ExitProcess');
pDeleteFileA := GetProcAddress(hKernel32, 'DeleteFileA');
pUnmapViewOfFile := GetProcAddress(hKernel32, 'UnmapViewOfFile');
asm
LEA EAX, buff
PUSH 0
PUSH 0
PUSH EAX
PUSH pExitProcess
PUSH hModule
PUSH pDeleteFileA
PUSH pUnmapViewOfFile
RET
end;
end;
begin
DeleteSelf;
end.
現(xiàn)在有一點(diǎn)比較古怪,那就是必須把代碼放在一個Procedure里,
直接放在begin ... end.中間是不行的。也許是全局變量不能使用
的緣故,但為什么不能使用,還是不是很清楚。
還有,不GetProcAddress,直接如下寫:
PUSH OFFSET UnmapViewOfFile
trace的結(jié)果是執(zhí)行進(jìn)入了KERNEL32.UnmapViewOfFile的,只是在
函數(shù)內(nèi)RET $4出就出錯了,跳到了一個莫名其妙的地方。為什么會
這樣?難道是Delphi的編譯器的問題嗎?
另外,Borland論壇上RE的代碼不是上面的,不過效果跟我寫的一樣
。但是FreeLibrary(p)跟UnmapViewOfFile(hModule)效果一樣嗎?
代碼如下:
program Project1;
uses
windows;
procedure DeleteSelf;
var
module : HMODULE;
buf : array [ 0 .. MAX_PATH - 1 ] of char;
p : ULONG;
hKrnl32 : HMODULE;
pExitProcess, pDeleteFile, pFreeLibrary : pointer;
begin
module := GetModuleHandle ( nil );
GetModuleFileName ( module, buf, sizeof ( buf ) );
CloseHandle ( THandle ( 4 ) );
p := ULONG ( module ) + 1;
//上面這一句什么意思?
hKrnl32 := GetModuleHandle ( 'kernel32' );
pExitProcess := GetProcAddress ( hKrnl32, 'ExitProcess' );
pDeleteFile := GetProcAddress ( hKrnl32, 'DeleteFileA' );
pFreeLibrary := GetProcAddress ( hKrnl32, 'FreeLibrary' );
asm
lea eax, buf
push 0
push 0
push eax
push pExitProcess
push p
push pDeleteFile
push pFreeLibrary
ret
end;
end;
新聞熱點(diǎn)
疑難解答
圖片精選