您的位置:首页 > 其它

删除进程自身可执行文件的另一种方法

2004-07-16 19:49 351 查看
此文章系收藏之作,并非本人所写
大家都知道,一般的程序运行的时候,可执行文件本身是被操作系统保护的,不能用改写的方式访问,更别提在本身还在运行的时侯删除自己了。在lu0的主页上看到一种undocument的方法,通过改变系统底层的文件访问模式实现删除自己,那是实在功夫。我看了很是佩服。但是有没有一种用在msdn上就能查到的函数实现呢?有!jeffrey richter给我们做了一个范例:

deleteme.cpp

module name: deleteme.cpp

written by: jeffrey richter

description: allows an executable file to delete itself

********************************************************************/

#include <windows.h>

#include <stdlib.h>

#include <tchar.h>

/////////////////////////////////////////////////////////////////////

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);

//***注意了***:

// open the clone exe using file_flag_delete_on_close

handle hfile = createfile(szpathclone, 0, file_share_read, null, open_existing, 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, szpathorig);

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);

}

 

看懂了吗?

这一段程序思路很简单:不是不能在运行时直接删除本身吗?好,那么程序先复制(clone)一个自己,用复制品起动另一个进程,然后自己结束运行,则原来的exe文件不被系统保护.这时由新进程作为杀手删除原来的exe文件,并且继续完成程序其他的功能。

新进程在运行结束后,复制品被自动删除。这又是值得介绍的一个把戏了,注意:

// open the clone exe using file_flag_delete_on_close

handle hfile = createfile(szpathclone, 0, file_share_read, null,open_existing, file_flag_delete_on_close, null);

这里面的file_flag_delete_on_close标志,这个标志是告诉操作系统,当和这个文件相关的所有句柄都被关闭之后(包括上面这个createfile创建的句炳),就把这个文件删除。几乎所有的临时文件在创建时,都指明了这个标志。

另外要注意的是:在复制品进程对原始程序操刀之前,应该等待原进程退出.在这里用的是进程同步技术.用

handle hprocessorig = openprocess(synchronize, true,getcurrentprocessid());

得到原进程句柄.synchronice标志在nt下有效,作用是使openprocess得到的句柄可以做为同步对象.复制品进程用waitforsingleobject函数进行同步,然后一个deletefile,以及进行其它销毁证据(jeffrey说:比如删目录)的工作,打完收工!

程序是基于console的,通过传入的参数确定是原始的进程还是复制品新进程,并且得到需要操作的目标文件的信息(主要是路径),复制品放在系统的temp目录(gettemppath得到),你也可以随便找个你认为安全的地方(比如:windows/system32等等)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  exe delete null file path winapi