You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++如何实现可执行文件运行时自动随机修改自身文件名?

How to Modify a Running Executable's Filename

Alright, let's tackle this—modifying a running executable's filename is totally feasible, but there are platform-specific nuances you need to keep in mind, especially since you're working with a Windows .exe like Injector.exe. Here's a step-by-step breakdown of how to pull this off:

Windows Platform (Your Injector.exe Use Case)

On Windows, running executables can be renamed while active because the OS locks the file's content, but not its directory entry (the "name" part). You can use native Windows APIs to generate a random name and rename the file directly.

Example C++ Implementation

#include <windows.h>
#include <string>
#include <random>
#include <algorithm>

// Generate a random alphanumeric filename with .exe extension
std::string generateRandomName(int length = 8) {
    const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dist(0, sizeof(charset) - 2);

    std::string name;
    name.reserve(length);
    for (int i = 0; i < length; ++i) {
        name += charset[dist(gen)];
    }
    return name + ".exe";
}

int main() {
    // Get the full path of the current executable
    char currentPath[MAX_PATH];
    GetModuleFileNameA(NULL, currentPath, MAX_PATH);

    // Extract the directory path and create the new full filename
    std::string currentDir = std::string(currentPath).substr(0, std::string(currentPath).find_last_of("\\/"));
    std::string newFullPath = currentDir + "\\" + generateRandomName();

    // Rename the executable
    if (MoveFileA(currentPath, newFullPath.c_str())) {
        MessageBoxA(NULL, "Filename updated successfully!", "Success", MB_OK);
    } else {
        // Get error code for debugging
        DWORD error = GetLastError();
        char errorMsg[256];
        sprintf(errorMsg, "Failed to rename file. Error code: %lu", error);
        MessageBoxA(NULL, errorMsg, "Error", MB_OK);
    }

    // The program continues running unaffected—only the disk filename changes
    // Add your core injector logic here...

    return 0;
}

Key Windows Notes

  • Permissions: Ensure the executable has write access to its directory (e.g., avoid protected folders like Program Files unless running as admin).
  • Post-Rename Access: If your program needs to read its own file later (e.g., for updates), use GetModuleFileName again to get the new path—don't rely on the original Injector.exe name, as it no longer exists.
  • Antivirus Alerts: This behavior can trigger AV detections (malware often renames itself to avoid detection), so add your executable to AV exceptions during testing.

Linux Platform (For Completeness)

On Linux, filenames are just links to file inodes. A running executable's inode remains open, so you can rename the file without disrupting execution using standard POSIX functions.

Example C Implementation

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

// Generate a random filename with .exe extension
char* generateRandomName() {
    char* name = malloc(10); // 8 random chars + ".exe" + null terminator
    const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    srand(time(NULL));
    
    for (int i = 0; i < 8; ++i) {
        name[i] = charset[rand() % (sizeof(charset) - 1)];
    }
    strcpy(name + 8, ".exe");
    return name;
}

int main() {
    char currentPath[PATH_MAX];
    // Get the executable's path via /proc/self/exe (Linux-specific)
    ssize_t pathLen = readlink("/proc/self/exe", currentPath, PATH_MAX);
    if (pathLen == -1) {
        perror("Failed to read executable path");
        exit(EXIT_FAILURE);
    }
    currentPath[pathLen] = '\0';

    // Create the new full path in the same directory
    char* dirSeparator = strrchr(currentPath, '/');
    char newFullPath[PATH_MAX];
    if (dirSeparator) {
        strncpy(newFullPath, currentPath, dirSeparator - currentPath + 1);
        newFullPath[dirSeparator - currentPath + 1] = '\0';
    } else {
        strcpy(newFullPath, "./");
    }
    
    char* newName = generateRandomName();
    strcat(newFullPath, newName);

    // Rename the executable
    if (rename(currentPath, newFullPath) == 0) {
        printf("Filename updated successfully!\n");
    } else {
        perror("Failed to rename file");
    }

    free(newName);
    // Program continues running normally
    return 0;
}

General Important Notes

  • Process Identity: The running process's internal state (PID, memory, etc.) won't change—only the disk filename is modified.
  • Command Line: The original filename may still appear in process lists (like Task Manager on Windows), but this is just cached metadata and doesn't affect functionality.
  • Edge Cases: If another process has an exclusive lock on your executable (e.g., a file scanner), the rename will fail—add error handling to catch these scenarios.

内容的提问来源于stack exchange,提问作者Gabriel Zăvoianu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:09:19