Skip to content

Instantly share code, notes, and snippets.

@Jasper-Bekkers
Created March 28, 2012 20:48
Show Gist options
  • Select an option

  • Save Jasper-Bekkers/2230394 to your computer and use it in GitHub Desktop.

Select an option

Save Jasper-Bekkers/2230394 to your computer and use it in GitHub Desktop.
Simple test w/ data execution
// ExecuteSomeCode.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
typedef int (__stdcall MyFunctionType)(int a, int b, int c);
static int test(int a)
{
return 12 * a;
}
static int __stdcall MyFunction(int a, int b, int c)
{
return test(a) * b * c;
}
static void __stdcall MyFunction_END() {}
void ExecFuncInMem()
{
printf("%p %p\n", MyFunction, MyFunction_END);
UINT size = (UINT)MyFunction_END - (UINT)MyFunction + 1;
BYTE* mem = (BYTE*)VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
memcpy(mem, MyFunction, size);
MyFunctionType* mf = (MyFunctionType*)mem;
int result = mf(2, 3, 4);
printf("%d\n", result);
}
int _tmain(int argc, _TCHAR* argv[])
{
ExecFuncInMem();
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment