如何编写一个自毁程序?

如何编写一个自毁程序?,第1张

#include <windows.h> 

#include <tchar.h>

#define WIN_X 200

#define WIN_Y 200

TCHAR WIN_NAME[]=_T( "自毁性程序" )

TCHAR WIN_CLASS[]=_T( "__SUICIDE__" )

LRESULT CALLBACK WindowProc( HWND,UINT,WPARAM,LPARAM )

BOOL Suicide()

int WINAPI WinMain( HINSTANCE hInstance,

 HINSTANCE hPrevinstance,

 LPSTR lpCmdline,

 int nCmdshow )

{

HWND hWnd

MSG mSg

WNDCLASSEX winClass

winClass.cbSize=sizeof( WNDCLASSEX )

winClass.style=0

winClass.lpfnWndProc =WindowProc

winClass.cbClsExtra=0

winClass.cbWndExtra=0

winClass.hInstance=hInstance

winClass.hIcon=LoadIcon( NULL,IDI_APPLICATION )

winClass.hCursor =LoadCursor( NULL, IDC_ARROW )

winClass.hbrBackground=( HBRUSH )GetStockObject( BLACK_BRUSH )

winClass.lpszMenuName =NULL

winClass.lpszClassName=WIN_CLASS

winClass.hIconSm =NULL

RegisterClassEx( &winClass )

hWnd=CreateWindow( WIN_CLASS,

  WIN_NAME,  

  WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,

  CW_USEDEFAULT,CW_USEDEFAULT,

  WIN_X,WIN_Y, 

  NULL,

  NULL,

  hInstance,

  NULL )

ShowWindow( hWnd,nCmdshow )

UpdateWindow( hWnd )

while( GetMessage( &mSg,NULL,0,0 ) )

{

TranslateMessage( &mSg )

DispatchMessage ( &mSg )

}

return ( mSg.wParam )

LRESULT CALLBACK WindowProc( HWND hWnd, 

UINT mSg, 

WPARAM wParam, 

LPARAM lParam ) 

{

PAINTSTRUCT ps

HDC hdc

switch( mSg ) 

{

case WM_PAINT: 

hdc=BeginPaint( hWnd,&ps)

EndPaint( hWnd,&ps )

break

case WM_DESTROY: 

Suicide()

PostQuitMessage( 0 )

break

default: return( DefWindowProc( hWnd,mSg,wParam,lParam ) )

}

return 0

BOOL Suicide()

{

TCHAR sCmd[ 1024 ]

TCHAR sName[ MAX_PATH ]

TCHAR sBuffer[ MAX_PATH ]

TCHAR sBatch[]=_T( "suicide.bat" )

TCHAR sFirst[]=_T( ":Repeat\r\n" )

TCHAR sSec []=_T( "del " )

TCHAR sThird[]=_T( "\r\nif exist " )

TCHAR sForth[]=_T( " goto Repeat" )

TCHAR sFifth[]=_T( "\r\ndel " )

TCHAR* p

DWORD dwWritten

HANDLE hFile

lstrcpy( sBuffer,_T( "\"" ) )

GetModuleFileName( NULL,sName,MAX_PATH )

for( p=sName*p!=0x0p++ )

for( *p!=_T( '\\' )p-- )

lstrcat( sBuffer,++p )

lstrcat( sBuffer,_T( "\"" ) )

lstrcpy( sCmd,sFirst )

lstrcat( sCmd,sSec )

lstrcat( sCmd,sBuffer )

lstrcat( sCmd,sThird )

lstrcat( sCmd,sBuffer )

lstrcat( sCmd,sForth )

lstrcat( sCmd,sFifth )

lstrcat( sCmd,sBatch )

hFile=CreateFile( sBatch,GENERIC_WRITE,0,NULL,

CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL )

if( hFile==INVALID_HANDLE_VALUE ) return FALSE

WriteFile( hFile,sCmd,lstrlen( sCmd ),&dwWritten,NULL )

CloseHandle( hFile )

ShellExecute( NULL,NULL,sBatch,NULL,NULL,SW_HIDE )

return TRUE

}

BOOL SelfDel()

{

SHELLEXECUTEINFO sei

TCHAR szModule [MAX_PATH],szComspec[MAX_PATH],szParams [MAX_PATH]

// 获得自身文件名. 获取cmd的全路径文件名

if((GetModuleFileName(0,szModule,MAX_PATH)!=0) &&

(GetShortPathName(szModule,szModule,MAX_PATH)!=0) &&

(GetEnvironmentVariable("COMSPEC",szComspec,MAX_PATH)!=0))

{

    // 设置命令参数.

    lstrcpy(szParams,"/c del ")

    lstrcat(szParams, szModule)

    lstrcat(szParams, " > nul")

    // 设置结构成员.

    sei.cbSize = sizeof(sei)

    sei.hwnd = 0

    sei.lpVerb = "Open"

    sei.lpFile = szComspec

    sei.lpParameters = szParams

    sei.lpDirectory = 0 sei.nShow = SW_HIDE

    sei.fMask = SEE_MASK_NOCLOSEPROCESS

    // 创建cmd进程.

    if(ShellExecuteEx(&sei))

    {

        // 设置cmd进程的执行级别为空闲执行,使本程序有足够的时间从内存中退出.

        SetPriorityClass(sei.hProcess,IDLE_PRIORITY_CLASS)

        // 将自身进程的优先级置高

        SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS)

        SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL)

        // 通知Windows资源浏览器,本程序文件已经被删除.

        SHChangeNotify(SHCNE_DELETE,SHCNF_PATH,szModule,0)

        return TRUE

    }

}

return FALSE

}

其他相关程序直接调用remove函数删除就好. 只有程序本身 需要新建进程删除 也就是上面这个代码. 

1,用BAT文件\x0d\x0aPrivate Sub Form_Load()\x0d\x0a\x0d\x0aOpen App.Path &"\a.bat" For Output As #1\x0d\x0a'"@echo off" 不显示执行过程\x0d\x0aPrint #1, "@echo off"\x0d\x0aPrint #1, "sleep 100"\x0d\x0a'a.bat 删除指定文件\x0d\x0aPrint #1, "del " &App.EXEName + ".exe"\x0d\x0a'a.bat 删除自身\x0d\x0aPrint #1, "del a.bat"\x0d\x0aPrint #1, "cls"\x0d\x0aPrint #1, "exit"\x0d\x0aClose #1\x0d\x0a\x0d\x0aShell App.Path &"\a.bat", vbHide\x0d\x0aEnd\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a2、用API\x0d\x0a一个模块,非原创(我修改部分代码),可以让程序退出后自毁(删除自身)。\x0d\x0a原理还特别不清楚,不过应该和CMD的DEL命令有关,但删除时没看到CMD.EXE进程,速度也很快\x0d\x0a成功率挺高的(我还没失败过)\x0d\x0a\x0d\x0a创建一个模块,写入代码:\x0d\x0aConst NORMAL_PRIORITY_CLASS = &H20\x0d\x0aConst REALTIME_PRIORITY_CLASS = &H100\x0d\x0aConst THREAD_PRIORITY_NORMAL = 0\x0d\x0aConst THREAD_PRIORITY_IDLE = -15\x0d\x0aConst IDLE_PRIORITY_CLASS = &H40\x0d\x0aConst DETACHED_PROCESS = &H8\x0d\x0aConst CREATE_SUSPENDED = &H4\x0d\x0aConst THREAD_PRIORITY_TIME_CRITICAL = 15\x0d\x0aConst SW_HIDE = 0\x0d\x0aConst STARTF_USESHOWWINDOW = &H1\x0d\x0a\x0d\x0aType STARTUPINFO\x0d\x0acb As Long\x0d\x0alpReserved As String\x0d\x0alpDesktop As String\x0d\x0alpTitle As String\x0d\x0adwX As Long\x0d\x0adwY As Long\x0d\x0adwXSize As Long\x0d\x0adwYSize As Long\x0d\x0adwXCountChars As Long\x0d\x0adwYCountChars As Long\x0d\x0adwFillAttribute As Long\x0d\x0adwFlags As Long\x0d\x0awShowWindow As Integer\x0d\x0acbReserved2 As Integer\x0d\x0alpReserved2 As Long\x0d\x0ahStdInput As Long\x0d\x0ahStdOutput As Long\x0d\x0ahStdError As Long\x0d\x0aEnd Type\x0d\x0a\x0d\x0aType PROCESS_INFORMATION\x0d\x0ahProcess As Long\x0d\x0ahThread As Long\x0d\x0adwProcessId As Long\x0d\x0adwThreadId As Long\x0d\x0aEnd Type\x0d\x0a\x0d\x0aType SECURITY_ATTRIBUTES\x0d\x0anLength As Long\x0d\x0alpSecurityDescriptor As Long\x0d\x0abInheritHandle As Long\x0d\x0aEnd Type\x0d\x0a\x0d\x0aDeclare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long\x0d\x0aDeclare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long\x0d\x0aDeclare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long\x0d\x0aDeclare Function GetCurrentProcess Lib "kernel32" () As Long\x0d\x0aDeclare Function GetCurrentThreadId Lib "kernel32" () As Long\x0d\x0aDeclare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long\x0d\x0aDeclare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long\x0d\x0aDeclare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long\x0d\x0aDeclare Function GetCurrentThread Lib "kernel32" () As Long\x0d\x0aDeclare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long\x0d\x0a\x0d\x0aFunction DeleteMe() As Boolean\x0d\x0aDim szModuleAs String\x0d\x0aDim szComspec As String\x0d\x0aDim szParamsAs String\x0d\x0a\x0d\x0aDim si As STARTUPINFO\x0d\x0aDim pi As PROCESS_INFORMATION\x0d\x0aDim sa1 As SECURITY_ATTRIBUTES\x0d\x0aDim sa2 As SECURITY_ATTRIBUTES\x0d\x0a\x0d\x0aszModule = String(512, 0)\x0d\x0aszComspec = String(512, 0)\x0d\x0aszParams = String(512, 0)\x0d\x0a\x0d\x0a'// get file path names:\x0d\x0aIf ((GetModuleFileName(0, szModule, 512) 0) And (GetShortPathName(szModule, szModule, 512) 0) And (GetEnvironmentVariable("COMSPEC", szComspec, 512) 0)) Then\x0d\x0a'// set command shell parameters\x0d\x0aszComspec = Left(szComspec, InStr(szComspec, Chr(0)) - 1)\x0d\x0aszModule = Left(szModule, InStr(szModule, Chr(0)) - 1)\x0d\x0a\x0d\x0aszComspec = szComspec &" /c del " &"""" &szModule &"""" '这里是我(yfdyh000)修改的,支持带空格路径\x0d\x0a'szComspec = szComspec &" /c del " &szModule'原代码\x0d\x0a\x0d\x0a'// set struct members\x0d\x0aWith si\x0d\x0a.cb = Len(si)\x0d\x0a.dwFlags = STARTF_USESHOWWINDOW\x0d\x0a.wShowWindow = SW_HIDE\x0d\x0aEnd With\x0d\x0a'// increase resource allocation to program\x0d\x0aCall SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)\x0d\x0aCall SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL)\x0d\x0a\x0d\x0a'// invoke command shell\x0d\x0a'Debug.Print CreateProcess(vbNullString, szComspec, sa1, sa2, 0&, CREATE_SUSPENDED Or DETACHED_PROCESS, 0&, vbNullString, si, pi)\x0d\x0aIf CreateProcess(vbNullString, szComspec, sa1, sa2, 0, CREATE_SUSPENDED Or DETACHED_PROCESS, 0, vbNullString, si, pi) Then\x0d\x0a'// suppress command shell process until program exits\x0d\x0aCall SetPriorityClass(pi.hProcess, IDLE_PRIORITY_CLASS)\x0d\x0aCall SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE)\x0d\x0a\x0d\x0a'// resume shell process with new low priority\x0d\x0aCall ResumeThread(pi.hThread)\x0d\x0a\x0d\x0a'// everything seemed to work\x0d\x0aDeleteMe = True\x0d\x0aExit Function\x0d\x0aElse '// if error, normalize allocation\x0d\x0aCall SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS)\x0d\x0aCall SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL)\x0d\x0aEnd If\x0d\x0aEnd If\x0d\x0aDeleteMe = False\x0d\x0aEnd Function\x0d\x0a\x0d\x0a在程序中调用命令:DeleteMe(比如在:Form_Unload)\x0d\x0a如果返回True证明成功,退出后会自动删除自身。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12041633.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存