正文

自删除程序小结2008-06-07 18:48:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/vfdff/35974.html

分享到:

基本思路:

使用C程序,在程序结束运行之前创建一个批处理文件$.bat

 然后在程序最后退出之前调用这个批处理文件 WinExec("$.bat",SW_HIDE);

注意:不能使用 system("$.bat");因为system需要等待system调用结束才执行下面的代码

只要在这个批处理文件中包含删除这个程序 aa.exe  和删除自身批处理文件$.bat功能就可

代码为:del /q/s aa.exe 和 del /q/s %0

具体实现示例使用C代码

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main()
{
    FILE *output = NULL;
 if((output=fopen("$.bat","w"))==NULL)
  return -1 ;
 
 fprintf(output,"@echo off\n"\
  "set tt = %%cd%%\n"\
  "rd c:\\zhong /s/q \n"\
  "md c:\\zhong\n"\
     "reg add HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run /v * /d * /f\n"\
  "cd %%tt%%\n"\
  "del /q/s aa.exe \n"\
  "del /q/s %%0\n"\
  "exit\n");
 fclose(output);
 WinExec("$.bat",SW_HIDE);
 //system("pause");
 
    return 0;
}

运行后看看什么效果:)

晕,怎么这个aa.exe仍来在呀!!不要慌,把这个system("pause")注释去掉看看有什么

对了 aa.exe拒绝访问,为什么会这样呢??原来在批处理执行删除操作del /q/s aa.exe的时候,这个aa.exe仍来还在运行。这样系统就对这个文件进行了保护(运行的东西是不能删除的,这点常识大家都有吧)那怎么办呢?

很简单,不就是程序还在运行没有结束嘛/!那你等等他不就完了。加个延时命令

嗨,dos中还真没有直接的延时程序,怎么办呢?自己动手,丰衣足食呀!

使用如下的测试程序

@echo off
setlocal enableextensions
echo %time%
call :ProcDelay 200
md  zhong
echo %time%
goto :EOF

:ProcDelay delayMSec_
setlocal enableextensions
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set start_=%%h%%i%%j%%k
:_procwaitloop
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set now_=%%h%%i%%j%%k
set /a diff_=%now_%-%start_%
if %diff_% LSS %1 goto _procwaitloop
endlocal & goto :EOF

大家看到了吧,程序会等待一段时间,然后创建一个zhong文件夹

有思路了吧!对的,就是这样,把这个延时程序加到刚才的&.bat文件中:

@echo off

setlocal enableextensions
echo %time%
call :ProcDelay 200
echo %time%
rem goto :EOF

rem ------------------------
set tt = %cd%
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v * /d * /f
cd %tt%
del /q/s aa.*
del /q/s %0
rem ---------------------------

:ProcDelay delayMSec_
setlocal enableextensions
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set start_=%%h%%i%%j%%k
:_procwaitloop
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set now_=%%h%%i%%j%%k
set /a diff_=%now_%-%start_%
if %diff_% LSS %1 goto _procwaitloop
endlocal & goto :EOF

exit

这样不久行了!

创建上面文件的代码完整示例:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main()
{
    FILE *output = NULL;
 if((output=fopen("$.bat","w"))==NULL)
  return -1 ;
 
 fprintf(output,"@echo off\n"\
  "setlocal enableextensions\n"\
  "echo %time%\n"\
  "call :ProcDelay 200\n"\
  "echo %time%\n"\
  "set tt = %%cd%%\n"\
     "reg add HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run /v * /d * /f\n"\
  "cd %%tt%%\n"\
  "del /q/s aa.exe \n"\
  "del /q/s %%0\n"\
  ":ProcDelay delayMSec_\n"\
  "setlocal enableextensions\n"\
  "for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set start_=%%h%%i%%j%%k\n"\
  ":_procwaitloop\n"\
  "for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set now_=%%h%%i%%j%%k\n"\
  "set /a diff_=%now_%-%start_%\n"\
  "if %diff_% LSS %1 goto _procwaitloop\n"\
  "endlocal & goto :EOF\n");
 fclose(output);
 WinExec("$.bat",SW_HIDE);
 //system("pause");
 
    return 0;
}

 

 

 

 

 

 

阅读(538) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册