Created
June 10, 2016 02:38
-
-
Save fsmv/ffb290c99a35795dbcfa7eba05616c20 to your computer and use it in GitHub Desktop.
Loading and executing code directly from an obj file on Windows!
This file contains 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
@echo off | |
rem Set up the Visual Studio 2013 compiler environment variables | |
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x64 | |
cl -c lib.cpp | |
cl runner.cpp /link user32.lib |
This file contains 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
int add(int a, int b) { | |
return a + b; | |
} |
This file contains 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> | |
typedef int (*Add)(int, int); | |
#define Read16(Ptr) ((*((Ptr) + 1) << 8) | (*((Ptr) + 0) << 0)) | |
#define Read32(Ptr) ((*((Ptr) + 3) << 24) | (*((Ptr) + 2) << 16) | \ | |
(*((Ptr) + 1) << 8 ) | (*((Ptr) + 0) << 0 )) | |
void *FindTextSection(unsigned char *FileText) { | |
int NumberOfSections = Read16(FileText + 2); | |
unsigned char *SectionTable = FileText + 0x14; | |
for (int SecTableIdx = 0; SecTableIdx < NumberOfSections; ++SecTableIdx) { | |
if (SectionTable[0] == '.' && | |
SectionTable[1] == 't' && | |
SectionTable[2] == 'e' && | |
SectionTable[3] == 'x' && | |
SectionTable[4] == 't') | |
{ | |
break; | |
} | |
SectionTable += 0x28; | |
} | |
unsigned char *PointerToRawData = SectionTable + 0x14; | |
int TextOffset = Read32(PointerToRawData); | |
return (void *) (FileText + TextOffset); | |
} | |
int main(int argc, char *argv[]) { | |
HANDLE FileHandle = CreateFile("lib.obj", | |
GENERIC_READ | GENERIC_EXECUTE, | |
FILE_SHARE_READ, 0, | |
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
if (FileHandle == INVALID_HANDLE_VALUE) { | |
MessageBoxA(0, "lib.obj not found", "Error", MB_OK); | |
return 1; | |
} | |
HANDLE MappingHandle = CreateFileMapping(FileHandle, 0, PAGE_EXECUTE_READ, 0, 0, 0); | |
if (MappingHandle == 0) { | |
CloseHandle(FileHandle); | |
char str[64]; | |
wsprintf(str, "Could not map file (%d)", GetLastError()); | |
MessageBoxA(0, str, "Error", MB_OK); | |
return 1; | |
} | |
void *Address = MapViewOfFile(MappingHandle, FILE_MAP_EXECUTE | FILE_MAP_READ, | |
0, 0, 0); | |
if (Address == 0) { | |
CloseHandle(FileHandle); | |
CloseHandle(MappingHandle); | |
char str[64]; | |
wsprintf(str, "Could not map view of file (%d)", GetLastError()); | |
MessageBoxA(0, str, "Error", MB_OK); | |
return 1; | |
} | |
CloseHandle(FileHandle); | |
CloseHandle(MappingHandle); | |
void *TextSection = FindTextSection((unsigned char *) Address); | |
Add add = (Add) (TextSection); | |
char str[2] = "."; | |
wsprintf(str, "%d", add(1, 1)); | |
MessageBoxA(0, str, "Result", MB_OK); | |
UnmapViewOfFile(Address); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment