您的位置:首页 >> 编程开发 >> Visual C++ >> 正文
RSS
 

介绍一种巧妙的删除程序自己的方法

http://www.rdxx.com 01年06月19日 12:12 Blog 我要投稿

关键词: 删除程序 , 方法 , 程序 , 删除

近日看到网友询问如何实现程序运行之后把自己删除的方法,不知大家对木马甚么的兴趣实在太浓,还是想要这样的效果:用户只要一运行程序,可执行文件就没有了,可是程序还是在跑,胆小的只怕要喊"鬼呀!","老婆,快出来看上帝"甚么的。其实最典型的用法是写反安装程序. 闲来无事,Bear掰到一种还算巧妙的“删除自己”的方法。

大家都知道,一般的程序运行的时候,可执行文件本身是被操作系统保护的,不能用改写的方式访问,更别提在本身还在运行的时侯删除自己了。在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文件,并且继续完成程序其他的功能。

共2页  第1页 第2页

 
 
标签: 删除程序 , 方法 , 程序 , 删除 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站