Advertisement
filashkov

Untitled

Nov 2nd, 2020
2,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dlfcn.h>
  5. #include <limits.h>
  6.  
  7. static volatile void (*void_function)();
  8. static volatile int (*int_function)();
  9. static volatile double (*double_function)();
  10. static volatile char *(*string_function)();
  11.  
  12. static void *handle;
  13. static volatile int index_int;
  14. static volatile int signature_size;
  15. static volatile int int_to_stack;
  16. static volatile double temp;
  17. static volatile int *first_int_to_stack_p;
  18. static volatile int *second_int_to_stack_p;
  19. static char file_name[PATH_MAX + 24] = "./";
  20. static volatile int result_int = 0;
  21. static volatile double result_double = 0;
  22. static volatile char *result_string;
  23.  
  24. static const char fmt_print_int[25] = "%d\n";
  25. static const char fmt_print_string[25] = "%s\n";
  26. static const char fmt_print_double[25] = "%.10g\n";
  27.  
  28. static const char percent_d[25] = "%d";
  29. static const char percent_lf[25] = "%lf";
  30. static const char percent_s[25] = "%s";
  31.  
  32. static const char symbol_i = 'i';
  33. static const char symbol_d = 'd';
  34. static const char symbol_s = 's';
  35. static const char symbol_v = 'v';
  36.  
  37. static volatile int current_head = 0;
  38.  
  39. static volatile int current_stack_before_call = 0;
  40. static volatile int current_stack_after_call = 0;
  41.  
  42. int
  43. main(int argc, char **argv)
  44. {
  45.     handle = dlopen(argv[1], RTLD_LAZY);
  46.     if (handle == NULL) {
  47.         strcat(file_name, argv[1]);
  48.         handle = dlopen(file_name, RTLD_LAZY);
  49.         if (handle == NULL) {
  50.             fprintf(stderr, "Error! %s\n", dlerror());
  51.             exit(1);
  52.         }
  53.     }
  54.     signature_size = strlen(argv[3]);
  55.     volatile unsigned old_stack_head = 0;
  56.     asm
  57.     (
  58.         "movl %%esp, %0\n\t"
  59.         :"=r"(old_stack_head)
  60.         :
  61.         :
  62.     );
  63.     printf("Верхушка стека: %d\n", old_stack_head);
  64.     index_int = signature_size - 1;
  65.     asm volatile
  66.     (
  67.         "pushl %1\n\t"
  68.         "movl %%esp, %0\n\t"
  69.         : "=r"(current_head)
  70.         : "r"(argv[3 + index_int])
  71.         :
  72.     );
  73.     printf("Добавили 4 байт %d\n", current_head);
  74.     *(void **)(&void_function) = dlsym(handle, argv[2]);
  75.     asm volatile
  76.     (
  77.         "movl %%esp, %0\n\t"
  78.         "call *%2\n\t"
  79.         "movl %%esp, %1\n\t"
  80.         "mov %3, %%esp\n\t"
  81.         : "=r"(current_stack_before_call), "=r"(current_stack_after_call)
  82.         : "r"(void_function), "r"(old_stack_head)
  83.         : "eax", "ecx", "edx"
  84.     );
  85.     printf("Стек до вызова = %d, стек после вызова = %d\n", current_stack_before_call, current_stack_after_call);
  86.     dlclose(handle);
  87.     return 0;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement