【问题标题】:Auto deleting exe on windows 7windows 7自动删除exe
【发布时间】:2016-03-06 11:10:13
【问题描述】:

我在 Internet 上找到了这段代码,但它说要在 Windows XP 上运行。 我尝试在 Windows 7 上运行它并且它工作,但我想知道它是否安全,不仅运行此代码,而且还在 Windows 7 上运行。

//
//  Self-deleting exe under Windows XP
//
#include <windows.h>
#include <tchar.h>

// get this right!
#define EXPLORER_PID 1444

typedef UINT  (WINAPI * WAIT_PROC)(HANDLE, DWORD);  // WaitForSingleObject
typedef BOOL  (WINAPI * CLOSE_PROC)(HANDLE);        // CloseHandle
typedef BOOL  (WINAPI * DELETE_PROC)(LPCTSTR);      // DeleteFile
typedef VOID  (WINAPI * EXIT_PROC)(DWORD);          // ExitProcess

typedef struct
{
    WAIT_PROC   fnWaitForSingleObject;
    CLOSE_PROC  fnCloseHandle;
    DELETE_PROC fnDeleteFile;
    EXIT_PROC   fnExitProcess;

    HANDLE      hProcess;
    TCHAR       szFileName[MAX_PATH];

} INJECT;

#pragma optimize("gsy", off)
#pragma check_stack(off)        // doesn't work :-(

DWORD WINAPI RemoteThread(INJECT *remote)
{
    remote->fnWaitForSingleObject(remote->hProcess, INFINITE);
    remote->fnCloseHandle(remote->hProcess);
    remote->fnDeleteFile(remote->szFileName);
    remote->fnExitProcess(0);

    return 0;
}

#pragma check_stack

HANDLE GetRemoteProcess()
{
    STARTUPINFO         si = { sizeof(si) };
    PROCESS_INFORMATION pi;

    //return OpenProcess(PROCESS_ALL_ACCESS, FALSE, EXPLORER_PID);

    if(CreateProcess(0, "explorer.exe", 0, 0, FALSE, CREATE_SUSPENDED|CREATE_NO_WINDOW|IDLE_PRIORITY_CLASS, 0, 0, &si, &pi))
    {
        CloseHandle(pi.hThread);
        return pi.hProcess;
    }
    else
    {
        return 0;
    }
}

PVOID GetFunctionAddr(PVOID func)
{
#ifdef _DEBUG

    // get address of function from the JMP <relative> instruction
    DWORD *offset = (BYTE *)func + 1;
    return (PVOID)(*offset + (BYTE *)func + 5);

#else

    return func;

#endif
}

BOOL SelfDelete()
{
    INJECT local, *remote;
    BYTE   *code;
    HMODULE hKernel32;
    HANDLE  hRemoteProcess;
    HANDLE  hCurProc;

    DWORD   dwThreadId;
    HANDLE  hThread = 0;

    char ach[80];

    hRemoteProcess = GetRemoteProcess();

    if(hRemoteProcess == 0)
        return FALSE;

    // Allocate memory in remote process
    code = VirtualAllocEx(hRemoteProcess, 0, sizeof(INJECT) + 128, MEM_RESERVE|MEM_COMMIT, PAGE_EXECUTE_READWRITE);

    if(code == 0)
    {
        CloseHandle(hRemoteProcess);
        return FALSE;
    }

    hKernel32 = GetModuleHandle(_T("kernel32.dll"));

    // setup remote structure
    remote = (INJECT *)(code + 128);

    local.fnWaitForSingleObject  = (WAIT_PROC)GetProcAddress(hKernel32,  "WaitForSingleObject");
    local.fnCloseHandle          = (CLOSE_PROC)GetProcAddress(hKernel32, "CloseHandle");
    local.fnExitProcess          = (EXIT_PROC)GetProcAddress(hKernel32, "ExitProcess");

#ifdef UNICODE
    local.fnDeleteFile            = (DELETE_PROC)GetProcAddress(hKernel32, "DeleteFileW");
#else
    local.fnDeleteFile            = (DELETE_PROC)GetProcAddress(hKernel32, "DeleteFileA");
#endif

    // duplicate our own process handle for remote process to wait on
    hCurProc = GetCurrentProcess();
    DuplicateHandle(hCurProc, hCurProc, hRemoteProcess, &local.hProcess, 0, FALSE, DUPLICATE_SAME_ACCESS);

    // find name of current executable
    GetModuleFileName(NULL, local.szFileName, MAX_PATH);

    // write in code to execute, and the remote structure
    WriteProcessMemory(hRemoteProcess, code,    GetFunctionAddr(RemoteThread), 128, 0);
    WriteProcessMemory(hRemoteProcess, remote, &local, sizeof(local), 0);

    wsprintf(ach, "%x %x\n", code, remote);
    OutputDebugString(ach);

    // execute the code in remote process
    hThread = CreateRemoteThread(hRemoteProcess, 0, 0, code, remote, 0, &dwThreadId);

    if(hThread != 0)
    {
        CloseHandle(hThread);
    }

    return TRUE;
}

int main(void)
{
    SelfDelete();

    return 0;
}

顺便问一下,这怎么能用作 C/C++ 中的库? 我的目标是只使用,例如,

#include "selfdel.h" 所以我可以在 C++ 程序中只使用函数SelfDelete()

【问题讨论】:

    标签: c++ windows-7 windows-xp


    【解决方案1】:

    你应该知道这段代码是什么。它是将代码注入另一个进程,该进程将作为该进程执行,然后该进程将退出。它应该可以正常工作(尽管请看下面的 cmets)。我想这段代码的作者 sn-p 是在 Win Vista 发布之前写的,所以你有这个顾虑。

    您可以在“selfdel.h”中声明SelfDelete()。调用此函数并立即退出应该可以解决问题。

    该实现不需要库用户的任何输入,因为它可以获得所需的一切。

    // duplicate our own process handle for remote process to wait on
    hCurProc = GetCurrentProcess();
    ...
    // find name of current executable
    GetModuleFileName(NULL, local.szFileName, MAX_PATH);
    

    一些cmets:

    • 您的进程应该有足够的权限来创建另一个进程
    • 此类活动可能会被防病毒软件视为可疑
    • 不要忘记,“僵尸”进程在调用SelfDelete() 后,只要您的进程存在,它就会一直等待
    • 考虑其他方法:How can a program delete its own executable

    【讨论】:

    • 我试过了,但它不起作用:#ifndef SELFDELETE_H_ #define SELFDELETE_H_ BOOL SelfDelete(); #endif // SELFDELETE_H_
    • 您是说代码按原样工作,但是当您将其包含到另一个项目时它停止工作?我已经尝试过了,它在 Win10 + MSVC 2015 上对我来说效果很好。唯一的问题是,如果你使用 /RTCs 编译它,'SelfDelete()' 会导致“explorer.exe”(或任何其他)崩溃(如果你启用了将其编译为调试)。也许这就是你放#pragma check_stack(off) // doesn't work :-(的原因?
    猜你喜欢
    • 2015-06-04
    • 2011-12-02
    • 2011-10-28
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 2023-02-02
    相关资源
    最近更新 更多