Source Code Externally Modifying Functions

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
I found out how to read/write the bytes of assembly instructions today while trying to create an external program that makes a hook without injecting a DLL (I got close, still working on it). Anyways, I'll get straight to the codenz:

C++:
BYTE * writeBytes(HANDLE handle, DWORD address, BYTE * bytes, int len)
{
    BYTE * restoreBytes = new BYTE[len];
    for (int i = 0; i < len; i++)
    {
        //Save our bytes for when we want to restore them
        ReadProcessMemory(handle, (LPVOID)(address + i), &restoreBytes[i], sizeof(BYTE), NULL);
        //Write the bytes we sent in to the address
        WriteProcessMemory(handle, (LPVOID)(address + i), &bytes[i], sizeof(BYTE), NULL);
    }

    return restoreBytes;
}

void restBytes(HANDLE handle, DWORD address, BYTE * restoreBytes, int len)
{
    for (int i = 0; i < len; i++)
    {
        //Restore the original bytes
        WriteProcessMemory(handle, (LPVOID)(address + i), &restoreBytes[i], sizeof(BYTE), NULL);
    }
    
    //So we don't have a memory leak
    delete[] restoreBytes;
}
Using it on AssaultCube for example:
C++:
//Our variables
BYTE jump[] = { 0xEB };
BYTE * allAutoRestore = NULL;
const DWORD all_auto = 0x463716;

//Setting the opcode to a jmp
all_auto_restore = obj.writeBytes(all_auto, jump, 1);

//Returning it to the original opcode
obj.restBytes(all_auto, all_auto_restore, 1);
This could probably be done much better. It also is good to wrap it into a class that holds the handle, and has a method that toggles the changes.
 

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
Why the for loops? Can't you just read/write an array of bytes?
Funny you bring that up, I found out that you can do that last night heh. The first time I had tried it I kept running into errors and didn't expand my brain enough to actually try and use len itself for the size :p
C++:
    BYTE * writeBytes(DWORD address, BYTE * bytes, int len)
    {
        BYTE * restoreBytes = new BYTE[len];
        ReadProcessMemory(m_prcHndl, (LPVOID)address, &*restoreBytes, len, NULL);
        WriteProcessMemory(m_prcHndl, (LPVOID)address, &*bytes, len, NULL);
        return restoreBytes;
    }

    void restBytes(DWORD address, BYTE * restoreBytes, int len)
    {
        WriteProcessMemory(m_prcHndl, (LPVOID)address, &*restoreBytes, len, NULL);
        delete[] restoreBytes;
    }
 

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
Exactly xD
Another question:
Why this complicated cast (in WPM/RPM):
&*restoreBytes
Just do WriteProcessMemory(m_prcHndl, (LPVOID)address, restoreBytes, len, NULL);
That would probably work as well :p. No idea why I put them there, I think I was getting an error with just the &, and threw a * in there for good measure (1AM coding FTW).

EDIT:
Now that I think about it, wouldn't & put in the address, then * give the first element? I did two extra uneeded steps if so :p
 

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
C++:
BYTE* swag=new BYTE[100];
std::cout<<std::hex<<(DWORD)&swag<<std::endl;
std::cout<<std::hex<<(DWORD)swag;
WPM/RPM need pointers ot the array. In my example swag is the pointer to the array. &swag is the address of the pointer - like the pointer to the pointer.
If you want to call WPM/RPM just pass the pointer to the call:
WriteProcessMemory(hProc,pAddy,swag,size,0);
or this overcomplicated version xD:
WriteProcessMemory(hProc,pAddy,&*swag,size,0);

This won't work:
WriteProcessMemory(hProc,pAddy,&swag,size,0);
Cool beans, that's definitely a good one to know now :)
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods