Advertisement
Altiumbe

messing around with the stack

Jan 31st, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3.  
  4.  
  5. struct intfarm {
  6.     int a;
  7.     int b;
  8. };
  9.  
  10. static void             (*hackptr) ();
  11. static struct intfarm   farm;
  12.  
  13. static void hack    (int, ...);
  14. static void hackdbg (char *, size_t);
  15. static void hackfarm(struct intfarm);
  16.  
  17. void
  18. hack(int foo, ...)
  19. {
  20.     int bar, baz;
  21.     va_list v;
  22.    
  23.     va_start (v, foo);
  24.     bar = va_arg (v, int);
  25.     baz = va_arg (v, int);
  26.     printf ("foo: %d, bar: %d, baz: %d\n", foo, bar, baz);
  27.     printf ("&foo: %p, &bar: %p, &baz: %p\n", &foo, &bar, &baz);
  28.     hackdbg ((char *)(&foo), sizeof foo);
  29.     hackdbg ((char *)(&bar), sizeof bar);
  30.     hackdbg ((char *)(&baz), sizeof baz);
  31.    
  32. }
  33.  
  34. void
  35. hackfarm (struct intfarm f)
  36. {
  37.     printf ("&f.a: %p, &f.b: %p\n", &(f.a), &(f.b));
  38. }
  39.  
  40. void
  41. hackdbg (char *data, size_t len)
  42. {
  43.     size_t i;
  44.    
  45.     for (i= 0; i < len; i ++)
  46.       {
  47.         printf ("%02x ", data[i] & 0xff);
  48.       }
  49.     printf ("\n");
  50. }
  51.  
  52. int
  53. main()
  54. {
  55.     farm.a = 7;
  56.     farm.b = 16;
  57.     hackptr = hackdbg;
  58.    
  59.     hackptr (&farm, sizeof farm);
  60.    
  61.     hackptr = hack;
  62.    
  63.     hackptr (3, farm);
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement