You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #ifdef WINDOWS
  6. #include <windows.h>
  7. #else
  8. #include <sys/mman.h>
  9. #endif
  10. #include "udis86.h"
  11. #include "misc.h"
  12. void* alloc_rwx(size_t size) {
  13. #ifdef WINDOWS
  14. return VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWIRTE);
  15. #else
  16. return mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  17. #endif
  18. }
  19. bool set_rwx(void* addr, size_t size) {
  20. #ifdef WINDOWS
  21. DWORD tmp;
  22. return VirtualProtect(addr, size, PAGE_EXECUTE_READWIRTE, &tmp) == TRUE;
  23. #else
  24. return mprotect(addr, size, PROT_READ | PROT_WRITE | PROT_EXEC) == 0;
  25. #endif
  26. }
  27. void disassemble_func(void* function, size_t numberOfInstr)
  28. {
  29. unsigned char* p = function;
  30. ud_t ud;
  31. ud_init(&ud);
  32. ud_set_input_buffer(&ud, function,
  33. (size_t)function | (0x1000 - 1) // Can't read further than page boundary - there be dragons
  34. );
  35. ud_set_pc(&ud, (uint64_t)function);
  36. ud_set_mode(&ud, 64);
  37. ud_set_syntax(&ud, UD_SYN_INTEL);
  38. for(size_t i = 0; i < numberOfInstr; i++)
  39. {
  40. size_t instrLen = 0;
  41. if(!(instrLen = ud_disassemble(&ud)) || ud.error)
  42. return;
  43. p += instrLen;
  44. printf("%p %s\n", ud_insn_off(&ud), ud_insn_asm(&ud));
  45. /* jmp qword [rip] is used by the hooking engine
  46. * x64 relative jumps don't exists so this way is used:
  47. * jmp qword [rip]
  48. * address where to tjump to
  49. * other code
  50. * Disassembling the address fucks up the dissassembling
  51. * todo: Follow all jump targets?
  52. */
  53. if(strcmp(ud_insn_asm(&ud), "jmp qword [rip]") == 0)
  54. p += 8;
  55. }
  56. }