943,737 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1495
  • C RSS
Aug 29th, 2009
0

Running Machine Code from Memory

Expand Post »
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:

  1. int main()
  2. {
  3. // allocate 2 bytes for storing machine code
  4. char* mc_add = (char*)malloc(sizeof(char)*2);
  5. int reg_eax; // for storing register EAX
  6.  
  7. _asm mov eax, 0x10; // assign EAX = 16
  8. _asm mov ecx, 0x01; // assign ECX = 1
  9.  
  10. // ADD EAX, ECX == 0x01C1
  11. *mc_add = 0x01;
  12. *(mc_add+1) = 0xC1;
  13.  
  14. // * I want to execute machine code instructuion from mc_add here *
  15.  
  16. _asm mov reg_eax, eax; // get register EAX
  17.  
  18. printf("Register EAX is%d", reg_eax); // print EAX
  19.  
  20. free(mc_add); // free machine code
  21.  
  22. return 0;
  23. }
Using: Microsoft Visual Studio 2008
Last edited by invisal; Aug 29th, 2009 at 7:17 am.
Similar Threads
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Aug 30th, 2009
0

Re: Running Machine Code from Memory

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

  1.  
  2. int main()
  3. {
  4. // allocate 8 bytes for storing machine code
  5. char *mc_mov = (char*)VirtualAlloc(0, 8, 0x1000, 0x40);
  6. int reg_eax; // for storing register EAX
  7.  
  8. *mc_mov = 0xC7; // MOV (Immediate 32 bit to 32-bit register)
  9. *(mc_mov+1) = 0xC0; // MOV to register EAX
  10. *(mc_mov+2) = 0x00; // Immediate 32 bit data = 0x00000000
  11. *(mc_mov+3) = 0x00;
  12. *(mc_mov+4) = 0x00;
  13. *(mc_mov+5) = 0x00;
  14. *(mc_mov+6) = 0xC2; // Return 16
  15. *(mc_mov+7) = 0x10;
  16.  
  17. CallWindowProc((WNDPROC)mc_mov, 0, 0, 0, 0);
  18.  
  19. _asm mov reg_eax, eax; // get register EAX
  20.  
  21. printf("Register EAX is %d", reg_eax); // print EAX
  22.  
  23. VirtualFree((LPVOID)mc_mov, 8, 0x4000); // free machine code
  24.  
  25. return 0;
  26. }
Last edited by invisal; Aug 30th, 2009 at 12:39 am.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Aug 30th, 2009
0

Re: Running Machine Code from Memory

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:
  • 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.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Modularization problem (basic)
Next Thread in C Forum Timeline: General C Questions and Specific Variable-Type Questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC