A clever delete program own method

zhaozj2021-02-16  75

Everyone knows that when the general program is running, the executable itself is protected by the operating system. It cannot be accessed by rewritten, let alone delete themselves when it is still running. Seeing a Undocument method on the homepage of LU0, deleting yourself by changing the file access mode of the system underlying. I saw it very admired. But is there a function that can be found on the MSDN? Yeffrey richter gives us a sample: deleteme.cpp, name: deleteme.cppwritten by: Jeffrey RichterDescription: allows an executable file to delete itself ******************** **************************************** / #include

#include

#include

/

Int WinApi WinMain (Hinstance H, Hinstance B, LPSTR PSZ, INT N) {

// Is this the Original EXE or THE CLONE EXE?

// if the command-line 1 argument, this is the Original Exe

// if the command-line> 1 argument, this is the clone exe

IF (__ARGC == 1) {

// Original Exe: Spawn Clone EXE to DELETE THIS EXE

// Copy this Executable Image INTO The User's Temp DIRECTORY

Tchar szpathorig [_max_path], szpathclone [_MAX_PATH];

GetModuleFileName (NULL, SZPATHORIG, _MAX_PATH);

GetTemppath (_MAX_PATH, SZPATHCLONE);

GetTempFileName (szpathclone, __text ("del"), 0, szpathclone);

CopyFile (Szpathorig, Szpathclone, False);

// *** Note ***:

// Open the clone exe usding file_flag_delete_on_close

Handle Hfile = Createfile (Szpathclone, 0, File_Share_read, Null, Open_EXISTI

Ng, file_flag_delete_on_close, null;

// spawn theclone exe passing it Our EXE's Process Handle

// and the full path name to the Original Exe file.

Tchar Szcmdline [512];

Handle HProcessorig = OpenProcess (Synchronize, True, getCurrentProcessId ());

WSPrintf (szcmdline, __text ("% s% d /"% s / "), szpathclone, hprocessorig, szpat

Horig);

Startupinfo Si;

ZeromeMory (& Si, SIZEOF (Si));

Si.cb = sizeof (si);

Process_information pi;

CreateProcess (NULL, SZCMDLINE, NULL, NULL, TRUE, 0, NULL, NULL, & SI, & PI); CloseHandle (HPROCESSORIG);

CloseHandle (HFILE);

// this Original Process Can Now Terminate.

} else {

// Clone Exe: when Original Exe Terminates, Delete IT

Handle HProcessorig = (Handle) _ttoi (__ targv [1]);

WaitforsingleObject (HProcessorig, Infinite);

CloseHandle (HPROCESSORIG);

Deletefile (__ targv [2]);

// INSERT CODE Here To Remove The Subdirectory TOO (if Desired).

// The System Will Delete the Clone Exe Automatically

// Because it is openmed with file_flag_delete_on_close

}

Return (0);

}

Did you understand?

This program is very simple: isn't it possible to delete itself directly at runtime? Ok, then the program is copied (clone) one yourself, start another process with the replica, then end the run, then the original EXE file is not protected by the system. At this time, the original EXE file is removed by the new process as the killer, and continue Complete other functions of the program.

After the new process is over, the replica is automatically deleted. This is another trick worth mentioning, paying attention:

// Open the clone exe usding file_flag_delete_on_close

Handle Hfile = Createfile (Szpathclone, 0, File_Share_read, Null, Open_EXISTIN

G, file_flag_delete_on_close, null;

The file_flag_delete_on_close flag in this is telling the operating system. After all the handles related to this file is turned off (including the Seni-Bing created above), the file is deleted. Almost all temporary documents are created, this flag is indicated. Also note: Before the replica process, you should wait for the original process to exit. Use the process synchronization technology. Use handle hprocessorig = openprocess (synchronize, true, getcurrentprocessid ()); get the original process Handle. Thesynchronice flag is valid in NT. The role is to make the handle you get the OpenProcess as synchronous objects. The replica process is synchronized with the waitforsingleObject function, and then a deletefile, and other destruction evidence (Jeffrey said: such as deleting directory) After finishing the work!

转载请注明原文地址:https://www.9cbs.com/read-14754.html

New Post(0)