The running program deletes its own method.

Source: Internet
Author: User
As we all know, when running a program, the executable file itself is protected by the operating system and cannot be accessed in a rewrite mode, not to mention deleting itself when it is still running. You can see an undocument method on the Internet. You can delete yourself by changing the file access mode at the bottom of the system. But is there a function implementation that can be found on msdn? The answer is yes. The following is an example:

 
Deleteme. cpp
 
Module name: deleteme. cpp
Written by: Jeffrey Richter
Description: 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 using file_flag_delete_on_close
Handle hfile = createfile (szpathclone, 0, file_assist_read, null, open_existi
Ng, file_flag_delete_on_close, null );
 
// Spawn the clone 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 was opened with file_flag_delete_on_close
}
Return (0 );
}

 
 
The idea of this program is simple: Isn't it possible to directly delete itself at runtime? Well, the program will first clone itself, start another process with the replica, and then run it on its own, then the original EXE file will not be protected by the system. at this time, the new process is used as the killer to delete the original EXE file and continue to complete other functions of the program.

After the new process is completed, the replica is automatically deleted. This is another trick worth introducing. Note:

// Open the clone EXE using file_flag_delete_on_close
Handle hfile = createfile (szpathclone, 0, file_assist_read, null, open_existin
G, file_flag_delete_on_close, null );

The file_flag_delete_on_close flag tells the operating system that when all the handles related to the file are closed (including the statement Bing created in the createfile above ), delete the file. This flag is specified when almost all temporary files are created. In addition, you should wait for the original process to exit before the replica process performs operations on the original program. the process synchronization technology is used here. use handle hprocessorig = OpenProcess (synchronize, true, getcurrentprocessid (); to obtain the original process handle. the synchronice flag is valid in NT so that the handles obtained by OpenProcess can be used as synchronization objects. the replica process uses the waitforsingleobject function for synchronization, and then a deletefile, and other work to destroy evidence (such as deleting Directories). Everything is done.
 
The program is based on the console. The input parameters are used to determine whether the process is the original process or the new copying process, and the information of the target file to be operated (mainly the path) is obtained ), put the copies in the temp directory of the system (obtained by gettemppath). You can also find a safe place (such as Windows/system32 ). There is no deep technology here. let's look at other examples of how to delete itself. For example, before the process exits, use fwrite or other methods to output one. BAT file, write a few del statements in it, and then winexec the BAT file. most of the worms that have played with DOS will.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.