c++数组在运行时修改另一个数组

问题描述 投票:0回答:0

嗨,我有一个关于 2 个不同字节数组的问题,基本上我试图在内存中 nop 字节,但在 0x90 之前,我复制了我想要 nop 的字节,将它们存储在字节数组中。 问题是,如果我同时 nop 2 个地址,第一个数组会以某种方式被修改,导致原始数组的恢复不佳,有人知道我做错了什么吗?

Also this is a pic of the array somehow being modified with random bytes.

#include <Windows.h>
#include <iostream>
#include <array>



void Console() {

    AllocConsole();

    freopen("CONOUT$", "w", stdout);

}

class Addresses {

public:

    uintptr_t BaseAdd = reinterpret_cast<uintptr_t>(GetModuleHandle(NULL));

    uintptr_t GameAssembly = reinterpret_cast<uintptr_t>(GetModuleHandle("GameAssembly.dll"));

    uintptr_t LibBurst = reinterpret_cast<uintptr_t>(GetModuleHandle("lib_burst_generated.dll"));

    uintptr_t HealthFunction = GameAssembly + 0x285FBFC;

    uintptr_t HungerFunction = LibBurst + 0x14BE819;

};

class Bytes {

protected:

    void NopBytes(PVOID Address, int bytes) {

        DWORD d, ds;
        VirtualProtect(Address, bytes, PAGE_EXECUTE_READWRITE, &d);
        memset(Address, 0x90, bytes);
        VirtualProtect(Address, bytes, d, &ds);
    }

    void CopyBytes(PVOID Address, int bytes, byte Buffer[]) {

        DWORD d, ds;
        VirtualProtect(Address, bytes, PAGE_EXECUTE_READWRITE, &d);
        memcpy(Buffer, Address, sizeof(Buffer));
        VirtualProtect(Address, bytes, d, &ds);

    }
    
    void RestoreBytes(PVOID Address, int bytes, byte Buffer[]) {

        DWORD d, ds;
        VirtualProtect(Address, bytes, PAGE_EXECUTE_READWRITE, &d);
        memcpy(Address, Buffer, sizeof(Buffer));
        VirtualProtect(Address, bytes, d, &ds);
    }

    void ClearBuffer(byte Buffer[]) {

        memset(&Buffer, 0, sizeof(Buffer));

    }
};

class GameFunctions : public Bytes {

private:

    Addresses a;

    byte HungerBuffer[2];

    byte HealthBuffer[6];

public:

    void Invincible() {

        CopyBytes(reinterpret_cast<PVOID>(a.HealthFunction), 6, HealthBuffer);

        NopBytes(reinterpret_cast<PVOID>(a.HealthFunction), 6);

    }

    void NotInvincible() {

        RestoreBytes(reinterpret_cast<PVOID>(a.HealthFunction),6, HealthBuffer);

        ClearBuffer(HealthBuffer);

    }

    void NoHunger() { 

        CopyBytes(reinterpret_cast<PVOID>(a.HungerFunction), 2, HungerBuffer);

        for (byte b : HungerBuffer) {

            std::cout << "Copied byte: " << b << std::endl;
        }

        NopBytes(reinterpret_cast<PVOID>(a.HungerFunction), 2);

    }

    void Hunger() {

        for (byte b : HungerBuffer) {

            std::cout << "Restored byte: " << b << std::endl;
        }

        RestoreBytes(reinterpret_cast<PVOID>(a.HungerFunction),2, HungerBuffer);

        ClearBuffer(HungerBuffer);

    }
};

void Mian(HMODULE Module) {

    Console();

    GameFunctions g;

    bool Health = false, Hunger = false;

    while (true) {

        if (GetAsyncKeyState(VK_END) & 1) {

            FreeLibraryAndExitThread(Module, 0);
        }
        else if (GetAsyncKeyState(VK_F1) & 1) {

            Health = !Health;

            if (Health) {

                g.Invincible();

            }

            else {

                g.NotInvincible();

            }

        }
        else if (GetAsyncKeyState(VK_F2) & 1) {

            Hunger = !Hunger;

            if (Hunger) {

                g.NoHunger();

            }

            else {

                g.Hunger();

            }

            Sleep(1);
        }
    }

    
}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        CreateThread(NULL, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(Mian), nullptr, NULL, nullptr);
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}


我真的不知道为什么...

c++ arrays winapi memory
© www.soinside.com 2019 - 2024. All rights reserved.