本文來源于網(wǎng)頁設(shè)計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。一種巧妙的刪除程序自己的方法
關(guān)鍵字 一種巧妙的刪除程序自己的方法
大家都知道,一般的程序運行的時候,可執(zhí)行文件本身是被操作系統(tǒng)保護的,不能用改寫的方式訪問,更別提在本身還在運行的時侯刪除自己了。在lu0的主頁上看到一種undocument的方法,通過改變系統(tǒng)底層的文件訪問模式實現(xiàn)刪除自己,那是實在功夫。我看 了很是佩服。但是有沒有一種用在msdn上就能查到的函數(shù)實現(xiàn)呢?有!jeffrey richter給我們做了一個范例:
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);
//***注意了***:
// open the clone exe using file_flag_delete_on_close
handle hfile = createfile(szpathclone, 0, file_share_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);
}
看懂了嗎?
這一段程序思路很簡單:不是不能在運行時直接刪除本身嗎?好,那么程序先復(fù)制(clone)一個自己,用復(fù)制品起動另一個進程,然后自己結(jié)束運行,則原來的exe文件不被系統(tǒng)保護.這時由新進程作為殺手刪除原來的exe文件,并且繼續(xù)完成程序其他的功能。
新進程在運行結(jié)束后,復(fù)制品被自動刪除。這又是值得介紹的一個把戲了,注意:
// open the clone exe using file_flag_delete_on_close
handle hfile = createfile(szpathclone, 0, file_share_read, null,open_existin
g, file_flag_delete_on_close, null);
這里面的file_flag_delete_on_close標(biāo)志,這個標(biāo)志是告訴操作系統(tǒng),當(dāng)和這個文件相關(guān)的所有句柄都被關(guān)閉之后(包括上面這個createfile創(chuàng)建的句炳),就把這個文件刪除。幾乎所有的臨時文件在創(chuàng)建時,都指明了這個標(biāo)志。另外要注意的是:在復(fù)制品進程對原始程序操刀之前,應(yīng)該等待原進程退出.在這里用的是進程同步技術(shù).用handle hprocessorig = openprocess(synchronize, true,getcurrentprocessid());得到原進程句柄.synchronice標(biāo)志在nt下有效,作用是使openprocess得到的句柄可以做為同步對象.復(fù)制品進程用waitforsingleobject函數(shù)進行同步,然后一個deletefile,以及進行其它銷毀證據(jù)(jeffrey說:比如刪目錄)的工作,打完收工!
程序是基于console的,通過傳入的參數(shù)確定是原始的進程還是復(fù)制品新進程,并且得到需要操作的目標(biāo)文件的信息(主要是路徑),復(fù)制品放在系統(tǒng)的temp目錄(gettemppath得到),你也可以隨便找個你認(rèn)為安全的地方(比如:windows/system32等等)。這里面沒有甚么深的技術(shù).再看其他的一些實現(xiàn)刪除自己的例子,比如說在進程退出前,用fwrite等方法輸出一個.bat文件,在里面寫幾句del,然后winexec一下這個bat文件即可.玩兒過dos的蟲蟲大多都會。今天又學(xué)一招,爽。