| | |
Running Machine Code from Memory
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Recently, I have wonder how to execute machine code instruction from memory in C/C++. I am aware of data execution protection. Anyway, I have this piece of code:
Using: Microsoft Visual Studio 2008
C Syntax (Toggle Plain Text)
int main() { // allocate 2 bytes for storing machine code char* mc_add = (char*)malloc(sizeof(char)*2); int reg_eax; // for storing register EAX _asm mov eax, 0x10; // assign EAX = 16 _asm mov ecx, 0x01; // assign ECX = 1 // ADD EAX, ECX == 0x01C1 *mc_add = 0x01; *(mc_add+1) = 0xC1; // * I want to execute machine code instructuion from mc_add here * _asm mov reg_eax, eax; // get register EAX printf("Register EAX is%d", reg_eax); // print EAX free(mc_add); // free machine code return 0; }
Last edited by invisal; Aug 29th, 2009 at 7:17 am.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
I have finally found the solution to my problem, so I am going to share it to anyone who interest the same thing as what I am
C Syntax (Toggle Plain Text)
int main() { // allocate 8 bytes for storing machine code char *mc_mov = (char*)VirtualAlloc(0, 8, 0x1000, 0x40); int reg_eax; // for storing register EAX *mc_mov = 0xC7; // MOV (Immediate 32 bit to 32-bit register) *(mc_mov+1) = 0xC0; // MOV to register EAX *(mc_mov+2) = 0x00; // Immediate 32 bit data = 0x00000000 *(mc_mov+3) = 0x00; *(mc_mov+4) = 0x00; *(mc_mov+5) = 0x00; *(mc_mov+6) = 0xC2; // Return 16 *(mc_mov+7) = 0x10; CallWindowProc((WNDPROC)mc_mov, 0, 0, 0, 0); _asm mov reg_eax, eax; // get register EAX printf("Register EAX is %d", reg_eax); // print EAX VirtualFree((LPVOID)mc_mov, 8, 0x4000); // free machine code return 0; }
Last edited by invisal; Aug 30th, 2009 at 12:39 am.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
Actually I have a simple library I wrote for executing machine code off an executable page.
The basis of it all for single page without a pointer(to arguments pushed on the stack) is simply:
Argument support is mind-numbingly easy.
I was working on an experimental metamophic engine, and assembler for it around the begining of summer, but sort of gave up when a bug with literals had me grinding my teeth.
The basis of it all for single page without a pointer(to arguments pushed on the stack) is simply:
-
typedef int (*fp)(); -
fp *code = new fp [original.size()]; -
memmove((void*)code, original.data(), original.size()); - In a wrapper function,
return ((fp)code)(); -
delete [] code;
Argument support is mind-numbingly easy.
I was working on an experimental metamophic engine, and assembler for it around the begining of summer, but sort of gave up when a bug with literals had me grinding my teeth.
Last edited by MosaicFuneral; Aug 30th, 2009 at 3:55 pm.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
![]() |
Similar Threads
- writing a a program in machine code (Assembly)
- Windows "Machine Code" (IT Professionals' Lounge)
- Assembly, machine code and compilers (Assembly)
- What Version am I running ?? (*nix Software)
Other Threads in the C Forum
- Previous Thread: Modularization problem (basic)
- Next Thread: General C Questions and Specific Variable-Type Questions
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework inches include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi





