下面的代码由Gary Nebbett写就.Gary Nebbett乃是WINDOWS NT/2000 NATIVE API REFERENCE的作者.乃NT系统一等一的高手.下面就分析一些他的这段代码.
这段代码在PROCESS没有结束前就将启动PROCESS的EXE文件删除了.
int main(int argc, char *argv[])
{
HMODULE module = GetModuleHandle(0);
CHAR buf[MAX_PATH];
GetModuleFileName(module, buf, sizeof buf);
CloseHandle(HANDLE(4));
__asm {
lea eax, buf
push 0
push 0
push eax
push ExitProcess
push module
push DeleteFile
push UnmapViewOfFile
ret
}
return 0;
}
现在,我们先看一下堆栈中的东西
偏移 内容
24 0
20 0
16 offset buf
12 address of ExitProcess
8 module
4 address of DeleteFile
0 address of UnmapViewOfFile
调用RET返回到了UnmapViewOfFile,也就是栈里的偏移0所指的地方.当进入UnmapViewOfFile的流程时,栈里见到的是返回地址DeleteFile和HMODUL module.也就是说调用完毕后返回到了DeleteFile的入口地址.当返回到DeleteFile时,看到了ExitProcess的地址,也就是返回地址.和参数EAX,而EAX则是buffer.buffer存的是EXE的文件名.由GetModuleFileName(module, buf, sizeof buf)返回得到.执行了DeleteFile后,就返回到了ExitProcess的函数入口.并且参数为0而返回地址也是0.0是个非法地址.如果返回到地址0则会出错.而调用ExitProcess则应该不会返回.
这段代码的精妙之处在于:
1.如果有文件的HANDLE打开,文件删除就会失败,所以,CloseHandle(HANDLE(4));是十分巧妙的一手.HANDLE4是OS的硬编码,对应于EXE的IMAGE.在缺省情况下,OS假定没有任何调用会关闭IMAGE SECTION的HANDLE,而现在,该HANDLE被关闭了.删除文件就解除了文件对应的一个句柄.
2.由于UnmapViewOfFile解除了另外一个对应IMAGE的HANDLE,而且解除了IMAGE在内存的映射.所以,后面的任何代码都不可以引用IMAGE映射地址内的任何代码.否则就OS会报错.而现在的代码在UnmapViewOfFile后则刚好没有引用到任何IMAGE内的代码.
3.在ExitProcess之前,EXE文件就被删除了.也就是说,进程尚在,而主线程所在的EXE文件已经没了.(WINNT/9X都保护这些被映射到内存的WIN32 IMAGE不被删除.)
Gary Nebbett果然是WIN系列平台的顶尖高手之一.能写出如此代码.独辟蹊径啊:)
/
//98,2000下通过
//nt/2000下面的删除代码方法来自陆麟(lu0)的文章,再此表示感谢
#pragma optimize( "", off )
/*NOTE fun_AfterDelSelf MUST BE memory allocate by HeapAlloc or VirtualAlloc,and you should free it your self.
,can't use normal callback function(which data on diskdrive,and can't access it after we delete ourself
*/
int DeleteSelf(void * fun_AfterDelSelf)//
{
typedef int (WINAPI *PFClose)(LPVOID);
OSVERSIONINFO os_info;
os_info.dwOSVersionInfoSize=sizeof(os_info);
LPVOID pBuffer=NULL;
PFClose pClose;
PFClose pDelete;
char fn[4096];
HINSTANCE hins=GetModuleHandle(NULL);
GetModuleFileName(NULL,fn,4096);
if(!GetVersionEx(&os_info))
return false;
switch(os_info.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
__try{
while(CloseHandle((HANDLE)4));
}__except(1){
}
CloseHandle((HANDLE)4);
pClose=PFClose(UnmapViewOfFile);
break;
case VER_PLATFORM_WIN32_WINDOWS:
pClose=PFClose(FreeLibrary);
break;
default:
return false;
}
pDelete=PFClose(DeleteFile);
pBuffer=VirtualAlloc(NULL,4096,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
__asm{
call _delete_end
}
__asm{
_test_close:
push hins
call [pClose]
or eax,eax
jz _test_close
lea eax,fn
push eax
call [pDelete]
mov eax,fun_AfterDelSelf
or eax,eax
jz _Exit_Process
call eax
_Exit_Process:
push 0
push MEM_RELEASE
push 0
push pBuffer
push ExitProcess
push VirtualFree
ret
}
_delete_end:
__asm{
pop ebx
push 128
push ebx
push [pBuffer]
call memcpy
jmp pBuffer
}
return 0;
}
#pragma optimize( "", on )
/
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等等)。