请问如何在运行时删除自己?

shyworm 2001-08-20 06:20:12
加精
例如发布一个demo.exe,只能演示一次,演示完毕即把自己删除。
请问如何才能做到呢?

如果除demo.exe还附带有demo.dll之类的文件,又该如何一并删除呢?
望各路高人不吝指点为谢!

再:在windows环境或者unix环境都可以实现吗?
...全文
385 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
colarcui 2002-01-29
  • 打赏
  • 举报
回复
me
rockhard 2002-01-19
  • 打赏
  • 举报
回复
作个记号先
Star_Desert 2002-01-06
  • 打赏
  • 举报
回复
study
caowk 2001-12-31
  • 打赏
  • 举报
回复
very good
dxhdxh2k 2001-12-26
  • 打赏
  • 举报
回复
2001年的电脑爱好者中有类似的题目,Very easy!仔细瞧瞧吧! 给分吧!
_xiaolifeidao 2001-12-19
  • 打赏
  • 举报
回复
ME TOO
sdice 2001-12-16
  • 打赏
  • 举报
回复
第一种方法WINME 下行不通的。
我用TR跟踪了CloseHandle函数,在遇到参数是某些特定数值时会强制返回,4就是其中之一!
seeking 2001-11-29
  • 打赏
  • 举报
回复
我把两个例程改成delphi,
复制品删除原文件:原文件是删除了,但复制品还在。
堆栈返回的方法可以运行,但没有删除。
TigerHu 2001-11-22
  • 打赏
  • 举报
回复
学习!
temporary 2001-10-10
  • 打赏
  • 举报
回复
用复制品删除原文件的我在98/vc6下通过
danfer 2001-09-10
  • 打赏
  • 举报
回复
gso shi zai shi gao!
ray2_ls 2001-09-10
  • 打赏
  • 举报
回复
好东西
fencer_2000 2001-08-28
  • 打赏
  • 举报
回复
一个都通不过!!!
MountLion 2001-08-21
  • 打赏
  • 举报
回复
我见过一个人写的程序,末尾生成一个批处理,然后运行批处理,退出。
:begin
delete demo.exe
if exist demo.exe goto begin

没有试过,感觉在Windows环境下应该可以。
shyworm 2001-08-21
  • 打赏
  • 举报
回复
太好了,谢谢各位大虾指点!
dcz 2001-08-20
  • 打赏
  • 举报
回复
高!
rjren 2001-08-20
  • 打赏
  • 举报
回复
unix下直接remove就可以了
example:

user@linux:/tmp> cat te.c
#include <stdio.h>

int main( int argc, char** argv )
{
printf( "%s is being deleted!\n", argv[0] );
remove( argv[0] );
return 0;
}
user@linux:/tmp> gcc -o te te.c
user@linux:/tmp> ls -al te*
-rwxr-xr-x 1 wqc sysgrp 11786 Aug 24 06:10 te*
-rw-r--r-- 1 wqc sysgrp 141 Aug 24 06:08 te.c
user@linux:/tmp> ./te
./te is being deleted!
user@linux:/tmp> ls -al te*
-rw-r--r-- 1 user sysgrp 141 Aug 24 06:08 te.c
乱七八糟 2001-08-20
  • 打赏
  • 举报
回复
---------------转--------------------------------

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等等)。

这里面没有甚么深的技术.再看其他的一些实现删除自己的例子,比如说在进程退出前,用fwrite等方法输出一个.bat文件,在里面写几句del,然后winexec一下这个bat文件即可.玩儿过dos的虫虫大多都会。今天又学一招,爽。

prog_st 2001-08-20
  • 打赏
  • 举报
回复
--------摘抄-------

俺也整理了一些东东,但是没有试过:
下面的代码由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 )
/////////////////////////////////////////////////////////////////////////
saturday 2001-08-20
  • 打赏
  • 举报
回复
:)
加载更多回复(2)
项目名称微信小程序教学管理系统+后台管理系统视频效果系统说明根据对系统的需求分析,本系统将分为4个模块:学生管理:管理学生的基本信息,包括个人信息的添加、修改、删除,以及选课信息的添加。科目管理:科目的基本信息,包括科目信息的添加、修改和删除。成绩管理:管理学生的选课的成绩信息,包括成绩的登记与修改。班级管理:对班级信息的管理,包括班级的增加、修改、删除、查询等。 环境需要1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.数据库:MySql 5.7版本;6.是否Maven项目:否;技术栈1. 后端:Spring+SpringMVC+Mybatis2. 前端:JSP+CSS+JavaScript+jQuery使用说明1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;3. 将项目中springmvc-servlet.xml配置文件中的数据库配置改为自己的配置;4. 运行项目,在浏览器中输入http://localhost:8080/ 登录运行截图​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑 

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧