delphi 程序删除自身方法(4种方法)

多种方法,请自已测试吧

procedure DeleteMe;
var
  BatchFile: TextFile;
  BatchFileName: string;
  ProcessInfo: TProcessInformation;
  StartUpInfo: TStartupInfo;
begin
  BatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat';
  AssignFile(BatchFile, BatchFileName);
  Rewrite(BatchFile);

  Writeln(BatchFile, ':try');
  Writeln(BatchFile, ' del "' + ParamStr(0) + '"');
  Writeln(BatchFile, 'if exist "' + ParamStr(0) + '" ' + ' goto try');
  Writeln(BatchFile, 'del %0');
  CloseFile(BatchFile);

  FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
  StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartUpInfo.wShowWindow := SW_HIDE;
  if CreateProcess(nil, PChar(BatchFileName), nil, nil, False, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo, ProcessInfo) then
  begin
    CloseHandle(ProcessInfo.hThread);
    CloseHandle(ProcessInfo.hProcess);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteMe;
  close;
end;

第二种:(系统控制批处理方式)

我们经常遇到这样的软件,运行之后就消失的无影无踪,特别是一些黑客的木马工具。

如果我们能掌握这个技术,即使不做黑客工具,也可以在程序加密、软件卸载等方面发挥作用。

那么他们是怎样实现的呢? ---- 以delphi为例,在form关闭的时候执行以下函数closeme即可。

procedure TForm1.closeme;
var
  f: textfile;
begin
  assignfile(f, '.\delme.bat');
  rewrite(f);
  writeln(f, '@echo off');
  writeln(f, ':loop');
  writeln(f, 'del "' + application.ExeName + '"');
  writeln(f, 'if exist "' + application.ExeName + '" goto loop'); //exist 是判断文件是否存在,.表示本目录,\号,就是本目录
  writeln(f, 'del .\delme.bat');
  closefile(f);
  winexec('.\delme.bat', SW_HIDE);
  close;
end;

procedure TForm1._deleteme;
var
  f: textfile;
begin
  winexec(pchar('cmd /c ping 127.0.0.1 -n 3&del /q "' + ParamStr(0) + '"'), SW_HIDE);
  close();
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
  _deleteme; //方式一
  closeme; //方式二
end;

第三种:

uses
  ShlObj, ShellAPI;
{$R *.dfm}

function Suicide: Boolean;
var
  sei: TSHELLEXECUTEINFO;
  szModule: PChar;
  szComspec: PChar;
  szParams: PChar;
begin
  szModule := AllocMem(MAX_PATH);
  szComspec := AllocMem(MAX_PATH);
  szParams := AllocMem(MAX_PATH);
    // get file path names:
  if ((GetModuleFileName(0, szModule, MAX_PATH) <> 0) and //
    (GetShortPathName(szModule, szModule, MAX_PATH) <> 0) and //
    (GetEnvironmentVariable('COMSPEC', szComspec, MAX_PATH) <> 0)) then
  begin
    // set command shell parameters
    lstrcpy(szParams, ' /c del ');
    lstrcat(szParams, szModule);
    // set struct members
    sei.cbSize := sizeof(sei);
    sei.Wnd := 0;
    sei.lpVerb := 'Open';
    sei.lpFile := szComspec;
    sei.lpParameters := szParams;
    sei.lpDirectory := 0;
    sei.nShow := SW_HIDE;
    sei.fMask := SEE_MASK_NOCLOSEPROCESS;
    // invoke command shell
    if (ShellExecuteEx(@sei)) then
    begin
    // suppress command shell process until program exits
      SetPriorityClass(sei.hProcess, HIGH_PRIORITY_CLASS); //IDLE_PRIORITY_CLASS);
      SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
      SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
      // notify explorer shell of deletion
      SHChangeNotify(SHCNE_Delete, SHCNF_PATH, szModule, nil);
      Result := True;
    end
    else
      Result := False;
  end
  else
    Result := False;
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
  Suicide;
  close;
end;


第四种:

procedure deleteSelf;
var
  hModule: THandle;
  szModuleName: array[0..MAX_PATH] of char;
  hKrnl32: THandle;
  pExitProcess, pdeleteFile, pFreeLibrary, pUnmapViewOfFile: pointer;
  ExitCode: UINT;
begin
  hModule := GetModuleHandle(nil);
  GetModuleFileName(hModule, szModuleName, sizeof(szModuleName));
  hKrnl32 := GetModuleHandle('kernel32');
  pExitProcess := GetProcAddress(hKrnl32, 'ExitProcess');
  pdeleteFile := GetProcAddress(hKrnl32, 'deleteFileA');
  pFreeLibrary := GetProcAddress(hKrnl32, 'FreeLibrary');
  pUnmapViewOfFile := GetProcAddress(hKrnl32, 'UnmapViewOfFile');
  ExitCode := system.ExitCode;
  if ($80000000 and GetVersion()) <> 0 then
    // Win95, 98, Me
    asm
        lea     eax, szModuleName
        push    ExitCode
        push    0
        push    eax
        push    pExitProcess
        push    hModule
        push    pdeleteFile
        push    pFreeLibrary
        ret
    end
  else
  begin
    CloseHandle(THANDLE(4));
    asm
        lea     eax, szModuleName
        push    ExitCode
        push    0
        push    eax
        push    pExitProcess
        push    hModule
        push    pdeleteFile
        push    pUnmapViewOfFile
        ret
    end
  end
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
  deleteSelf;
  close;
end;


打赏
关键词: delphi 删除自身

0 评论

发表评论