~clacke/stackoverflowq-2623437/patchfun-rollstack

« back to all changes in this revision

Viewing changes to patchfun-cc.cc

  • Committer: Claes Wallin
  • Date: 2010-05-09 13:20:48 UTC
  • Revision ID: clacke@sefagl485.raby.clacke.se-20100509132048-rb45tye633bi8dgm
init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cstdio>
 
2
#include <windows.h>
 
3
 
 
4
using namespace std;
 
5
 
 
6
void real (int a,int b,int c,int d) __attribute__((noinline));
 
7
 
 
8
// A function that is not mine but to which I have access and want to patch so that it calls a function of mine with its original arguments
 
9
void real (int a,int b,int c,int d)
 
10
{
 
11
 __asm__("");
 
12
}
 
13
 
 
14
// A function that I want to be called, receiving the original arguments
 
15
void receiver(int a,int b,int c,int d);
 
16
 
 
17
void patch(int a, int b, int c, int d)
 
18
{
 
19
 receiver(a, b, c, d);
 
20
 __asm__("jmp dummy\n\t"
 
21
         ".ascii \"dummy\\0\"\n"
 
22
         "dummy:\n");
 
23
}
 
24
 
 
25
int main()
 
26
{
 
27
 FlushInstructionCache(GetCurrentProcess(),(const void *) &real,5);
 
28
 
 
29
 DWORD oldProtection;
 
30
 VirtualProtect((void *) &real,5,PAGE_EXECUTE_READWRITE,&oldProtection);
 
31
 
 
32
 // Patching the real function to go to my patch
 
33
 ((unsigned char*)real)[0] = 0xE9;
 
34
 *((long*)((long)(real) + sizeof(unsigned char))) = (char*)patch - (char*)real - 5;
 
35
 
 
36
 puts("real");
 
37
 __asm__(
 
38
  "push 666          \n\t"
 
39
  "push 1337         \n\t"
 
40
  "push 69           \n\t"
 
41
  "push 100          \n\t"
 
42
  "call __Z4realiiii \n\t"
 
43
  "add  esp, 16      \n\t"
 
44
 );
 
45
 
 
46
 puts("real-real");
 
47
 real(1,2,3,4);
 
48
 
 
49
 puts("receiver");
 
50
 receiver(1,2,3,4);
 
51
 return 0;
 
52
}