几种程序自杀的实现

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;

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 ./file.exe goto loop');
writeln(f,'del ./delme.bat');
closefile(f);
winexec('./delme.bat', SW_HIDE);
close;
end;

winexec(pchar('command.com /c del '+ParamStr(0)),SW_MINIMIZE);//最小化执行删除操作,否则将看到DOS窗口的瞬间闪烁

第三种:

Delphi 版
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellAPI, ShlObj;

type
  TForm1 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$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.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Suicide;
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;

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作为一个 AI,贪吃蛇需要具备以下能力: 1. 寻找食物:贪吃蛇需要知道当前场景中食物的位置,并能够计算出到达食物的最短路径。 2. 避免碰壁:贪吃蛇需要知道当前场景的边界,并且在移动时需要避免碰到边界。 3. 避免自杀:贪吃蛇需要知道自己的身体位置,并在移动时需要避免撞到自己的身体。 下面是一个简单的贪吃蛇 AI 实现: ```python import random class SnakeAI: def __init__(self, board, snake): self.board = board self.snake = snake def find_food(self): # 从当前位置出发,寻找最近的食物 food_pos = None min_distance = float('inf') for i in range(self.board.width): for j in range(self.board.height): if self.board.grid[i][j] == 'food': distance = abs(i - self.snake.head.x) + abs(j - self.snake.head.y) if distance < min_distance: min_distance = distance food_pos = (i, j) return food_pos def avoid_wall(self): # 判断当前位置是否靠近边界 if self.snake.head.x < 2 or self.snake.head.x > self.board.width - 3 \ or self.snake.head.y < 2 or self.snake.head.y > self.board.height - 3: return True else: return False def avoid_self(self): # 判断当前位置是否与自己的身体相撞 for part in self.snake.body[1:]: if self.snake.head.x == part.x and self.snake.head.y == part.y: return True return False def get_move(self): food_pos = self.find_food() if food_pos is None: # 如果没有食物,则随机移动 return random.choice(['up', 'down', 'left', 'right']) if self.snake.head.x < food_pos[0]: if not self.avoid_wall() and not self.avoid_self(): return 'right' elif self.snake.head.x > food_pos[0]: if not self.avoid_wall() and not self.avoid_self(): return 'left' elif self.snake.head.y < food_pos[1]: if not self.avoid_wall() and not self.avoid_self(): return 'down' elif self.snake.head.y > food_pos[1]: if not self.avoid_wall() and not self.avoid_self(): return 'up' # 如果无法直接到达食物,则随机移动 return random.choice(['up', 'down', 'left', 'right']) ``` 在上面的代码中,SnakeAI 类接受两个参数:board 和 snake,分别表示当前场景和贪吃蛇的状态。它实现了三个方法:find_food、avoid_wall 和 avoid_self。 find_food 方法用于寻找最近的食物,它遍历整个场景,找到距离当前位置最近的食物,返回其坐标。 avoid_wall 方法用于判断当前位置是否靠近边界,如果靠近边界则返回 True,否则返回 False。 avoid_self 方法用于判断当前位置是否与自己的身体相撞,如果相撞则返回 True,否则返回 False。 最后,get_move 方法根据当前位置和食物位置,决定贪吃蛇下一步的移动方向。如果能直接到达食物,则选择向食物移动;否则随机移动。在移动时,还需要检查是否靠近边界或与自己的身体相撞,如果是,则选择随机移动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值