Dev/Delphi

자기자신을 삭제하기 예제

슬픈윈드 2011. 3. 29. 13:28
program selfdelete; 
uses 
  windows, 
  sysutils; 
procedure DeleteExeAndDir; 
var hModule:THandle; 
   szModuleName,szDirName:array[0..MAX_PATH] of char; 
   hKrnl32 : THandle; 
   pExitProcess, pDeleteFile, pUnmapViewOfFile,pRemoveDir : pointer; 
   ExitCode:UINT; 
   var r:integer; 
begin 
hModule:= GetModuleHandle(nil); 
GetModuleFileName(hModule, szModuleName, sizeof(szModuleName)); 
StrPCopy(szDirName,ExtractFileDir(szModuleName)); 
     hKrnl32 := GetModuleHandle ( 'kernel32' ); 
     pExitProcess := GetProcAddress ( hKrnl32, 'ExitProcess' ); 
     pDeleteFile := GetProcAddress  ( hKrnl32, 'DeleteFileA' ); 
     pUnmapViewOfFile := GetProcAddress ( hKrnl32, 'UnmapViewOfFile' ); 
     pRemoveDir := GetProcAddress ( hKrnl32, 'RemoveDirectoryA' ); 
ExitCode := system.ExitCode; 
SetCurrentDirectory(pchar(ExtractFileDir(szDirName))); 
if($80000000 and GetVersion())=0 then 
  begin 
    for r:=1 to 100 do 
    begin 
      CloseHandle(r shl 2); 
    end; 
end; 
  asm 
   lea     eax, szModuleName 
   lea     ecx, szDirName 
   push    ExitCode 
   push    0 
   push    ecx 
   push    pExitProcess 
   push    eax 
   push    pRemoveDir 
   push    hModule 
   push    pDeleteFile 
   push    pUnmapViewOfFile 
   ret 
   end 
  end; 
begin 
     DeleteExeAndDir 
end.