Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run some code in memory?

I have a compiler which compiles assembly language to machine language (in memory). My project is in C# .net. Is there any way to run the memory on a thread? How can DEP prevent it?

byte[] a:  
01010101 10111010 00111010 10101011 ...
like image 450
Behrooz Avatar asked May 01 '26 03:05

Behrooz


1 Answers

The key is to put the executable code into a block of memory allocated with VirtualAlloc such that the buffer is marked as executable.

IntPtr pExecutableBuffer = VirtualAlloc(
  IntPtr.Zero,
  new IntPtr(byteCount),
  AllocationType.MEM_COMMIT | AllocationType.MEM_RESERVE,
  MemoryProtection.PAGE_EXECUTE_READWRITE);

(then use VirtualFree to clean up after yourself).

This tells Windows that the memory should be marked as executable code so that it won't trigger a DEP check.

like image 127
homeInAStar Avatar answered May 03 '26 16:05

homeInAStar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!