Advertisement
NovaYoshi

first class

Nov 24th, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5.  
  6. int a = 0;
  7.  
  8. void test() {
  9.   a=0xabcd;
  10. }
  11.  
  12. int main() {
  13.   unsigned char Template[100];
  14.   int Index = 0;
  15.  
  16. // make a copy of the test() function in Template and find where the constant is
  17.   int i, Length;
  18.   unsigned char *OldTemplate = (char*)test;
  19.   for(i=0;i<50;i++) {
  20.     Template[i] = OldTemplate[i];
  21.     if((OldTemplate[i] == 0xcd) && (OldTemplate[i+1] == 0xab))
  22.       Index = i;
  23.     if(OldTemplate[i] == 0xc3) // x86 RET opcode
  24.       break;
  25.   }
  26.   Length = i+1;
  27.  
  28. // display the function
  29.   for(i=0;i<Length;i++) {
  30.     if(i == Index || i == Index + 1)
  31.       printf("xx ");
  32.     else
  33.       printf("%.2x ", Template[i]&255);
  34.   }
  35.   putchar('\n');
  36.  
  37. // create 20 functions that set a global variable 0 to 19
  38.   char *Created[20];
  39.   for(i=0;i<20;i++) {
  40.     Created[i] = (void*)malloc(Length);
  41.     memcpy(Created[i], Template, Length);
  42.     Created[i][Index+0] = i;
  43.     Created[i][Index+1] = 0;
  44.   }
  45.  
  46. // call all the functions we just made
  47.   for(i=0;i<20;i++) {
  48.     void (*foo)(void) = (void*)Created[i];
  49.     foo();
  50.     printf("%i ", a);
  51.   }
  52.   putchar('\n');
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement