Created
March 28, 2012 20:48
-
-
Save Jasper-Bekkers/2230394 to your computer and use it in GitHub Desktop.
Simple test w/ data execution
This file contains hidden or 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
| // 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