×
嵌入式开发 > 详情

程序运行之后把自己删除的方法

发布时间:2020-06-13 发布时间:
|

近日看到网友询问如何实现程序运行之后自己删除方法,不知大家对木马甚么的兴趣实在太浓,还是想要这样的效果:用户只要一运行程序,可执行文件就没有了,可是程序还是在跑,胆小的只怕要喊鬼呀!,老婆,快出来看上帝甚么的

。其实最典型的用法是写反安装程序. 闲来无事,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

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



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
五大标准轻松搞定4K超高清电视选购