 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Jugyou Newbie cheater
Reputation: 0
Joined: 18 May 2014 Posts: 11
|
Posted: Fri Aug 01, 2014 11:56 am Post subject: LoadBinary C++ Equivalent |
|
|
I'm currently converting my codes to C++, the Inline Assembler is a little awkward, but it's working quite well so far.
However I'm having trouble converting this code:
| Code: | globalalloc(newmem,2048)
loadbinary(newmem,file.bin) |
I've looked here (viewtopic.php?p=4687001), through a dozen stackoverflow questions and other sites, but to no avail.
Basically I want a function that allocates memory, loads the binary file and returns the address where the file is stored.
How can I achieve this in C++? |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Fri Aug 01, 2014 12:01 pm Post subject: |
|
|
There are a few different ways to go about this. The choice of how you want to do it is up to you.
To start, you will want to read in the file you are wanting to write to memory and obtain its size so you do not hard-code a file size that might chance based on the file being injected.
You can do this with:
- CreateFile
- ReadFile
- GetFileSize
Or you can use the file functions like:
- fopen
- fread
- fseek
- ftell
Once you have the file size, you can allocate a buffer locally to read into.
Afterward, you can then work with the memory remotely via:
- VirtualAllocEx (to allocate the memory block in the remote process.)
- WriteProcessMemory (to write your data from the file to the allocated memory.) _________________
- Retired. |
|
| Back to top |
|
 |
Jugyou Newbie cheater
Reputation: 0
Joined: 18 May 2014 Posts: 11
|
Posted: Fri Aug 01, 2014 2:52 pm Post subject: |
|
|
Thank you so much for the fast and extremely useful answer!
I came up with the following
| Code: | #include <stdio.h>
#include <iostream>
LPVOID LoadBinary(char* filePath)
{
FILE *file = fopen(filePath, "rb");
long fileStart = ftell(file);
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, fileStart, 0); //Back to the start
BYTE *fileBuffer = new BYTE[fileSize];
fread(fileBuffer, fileSize, 1, file);
LPVOID newmem = VirtualAlloc(NULL, fileSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
unsigned long OldProtection;
VirtualProtect(newmem, fileSize, PAGE_EXECUTE_READWRITE, &OldProtection);
memcpy(newmem, fileBuffer, fileSize);
VirtualProtect(newmem, fileSize, OldProtection, NULL);
delete[]fileBuffer;
fclose(file);
return newmem;
} |
And you can use it like this
| Code: | | LPVOID newmem = LoadBinary("C:\\file.bin"); |
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|