Created
November 11, 2018 16:37
-
-
Save Barakat/564ffab103e6a4f8fc2b0a930c912fb8 to your computer and use it in GitHub Desktop.
Allocate executable memory by creating a memory section with CreateFileMapping and MapViewOfFile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <cinttypes> | |
int main() | |
{ | |
static uint8_t code[] = { | |
0x90, // nop | |
0x90, // nop | |
0xc3 // ret | |
}; | |
static const size_t code_size = sizeof(code); | |
// للتأكد فقط for debugging | |
if (IsDebuggerPresent()) | |
{ | |
code[0] = 0xcc; // int 3 | |
} | |
HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, 0, code_size, nullptr); | |
void *p = MapViewOfFile(section, FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE, 0, 0, 0); | |
memcpy(p, code, code_size); | |
reinterpret_cast<void (*)()>(p)(); | |
UnmapViewOfFile(p); | |
CloseHandle(section); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment