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.

README.md 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. Introduction
  2. ============
  3. This project aims to give a simple overview on how good various x64 hooking
  4. engines (on windows) are. I'll try to write various functions, that are hard to
  5. patch and then see how each hooking engine does.
  6. I'll test:
  7. * [EasyHook](https://easyhook.github.io/)
  8. * [PolyHook](https://github.com/stevemk14ebr/PolyHook)
  9. * [MinHook](https://www.codeproject.com/Articles/44326/MinHook-The-Minimalistic-x-x-API-Hooking-Libra)
  10. * [Mhook](http://codefromthe70s.org/mhook24.aspx)
  11. (I'd like to test detours, but I'm not willing to pay for it. So that isn't
  12. tested :( )
  13. There are multiple things that make hooking difficult. Maybe you want to patch
  14. while the application is running -- in that case you might get race conditions,
  15. as the application is executing your half finished hook. Maybe the software has
  16. some self protection features (or other software on the system provides that,
  17. e.g. Trustee Rapport)
  18. Evaluating how the hooking engines stack up against that is not the goal here.
  19. Neither are non-functional criteria, like how fast it is or how much memory it
  20. needs for each hook. This is just about the challenges the function to be
  21. hooked itself poses.
  22. Namely:
  23. * Are jumps relocated?
  24. * What about RIP adressing?
  25. * If there's a loop at the beginning / if it's a tail recurisve function, does
  26. the hooking engine handle it?
  27. * How good is the dissassembler, how many instructions does it know?
  28. * Can it hook already hooked functions?
  29. At first I will give a short walk through of the architecture, then quickly go
  30. over the test cases. After that come the results and an evaluation for each
  31. engine.
  32. I think I found a flaw in all of them; I'll publish a small POC which should at
  33. least detect the existence of problematic code.
  34. **A word of caution**: my results are worse than expected, so do assume I have
  35. made a mistake in using the libraries. I went into this expecting that some
  36. engines at least would try to detect e.g. the loops back into the first few
  37. bytes. But none did? That's gotta be wrong.
  38. **Another word of caution**: parts of this are rushed and/or ugly. Please
  39. double check parts that seem suspicious. And I'd love to get patches, even for
  40. the most trivial things -- spelling mistakes? Yes please.
  41. Architecture
  42. ============
  43. This project is made up of two parts. A .DLL with the test cases and an .exe
  44. that hooks those, tests whether they still work and prints the results.
  45. (I could have done it all in the .exe but this makes it trivial to (at some
  46. point) force the function to be hooked and the target function to be further
  47. apart than 2GB. Just set fixed image bases in the project settings and you're
  48. done)
  49. My main concern was automatically identifying whether the hook worked. I
  50. consider a hook to work if: a) the original function can still execute
  51. successfully *and* b) the hook was called.
  52. The criteria a) is really similar to a unit test. Verify that a function
  53. returns what is expected. So for a) the .exe just runs unit tests after all the
  54. hooks have been applied. Each failing function is reported (or the program
  55. crashes and I can look at the callstack) so I can correlate that with which
  56. hooking engine I'm currently testing and see where those fail. I've used
  57. Catch2 for the unit tests, because I wanted to try it anyway.
  58. From the get-to it was clear that I wanted to test multiple hooking engines.
  59. And they all needed to do the same steps in the same order -- so I implemented
  60. a basic AbstractHookingEngine with a boolean for every test case and make a
  61. child class for each engine. The children classes have to overwrite `hook_all`
  62. and `unhook_all`. Inbetween the calls to that, the unit tests run.
  63. Test case: Small
  64. ================
  65. This is just a very small function; it is smaller than the hook code will be -
  66. so how does the library react?
  67. ```ASM
  68. _small:
  69. xor eax, eax
  70. ret
  71. ```
  72. Test case: Branch
  73. =================
  74. Instead of the FASM code I'll show the disassembled version, so you can see the
  75. instruction lengths & offsets.
  76. ```ASM
  77. 0026 | 48 83 E0 01 | and rax,1
  78. 002A | 74 17 | je test_cases.0043 ----+
  79. 002C | 48 31 C0 | xor rax,rax |
  80. 002F | 90 | nop |
  81. 0030 | 90 | nop |
  82. 0031 | 90 | nop |
  83. 0032 | 90 | nop |
  84. 0033 | 90 | nop |
  85. 0034 | 90 | nop |
  86. 0035 | 90 | nop |
  87. 0036 | 90 | nop |
  88. 0037 | 90 | nop |
  89. 0038 | 90 | nop |
  90. 0039 | 90 | nop |
  91. 003A | 90 | nop |
  92. 003B | 90 | nop |
  93. 003C | 90 | nop |
  94. 003D | 90 | nop |
  95. 003E | 90 | nop |
  96. 003F | 90 | nop |
  97. 0040 | 90 | nop |
  98. 0041 | 90 | nop |
  99. 0042 | 90 | nop |
  100. 0043 | C3 | ret <-----------------+
  101. ```
  102. This function has a branch in the first 5 bytes. Hooking it detour-style isn't
  103. possible without fixing that branch in the trampoline. The NOP sled is just so
  104. the hooking engine can't cheat and just put the whole function into the
  105. trampoline. Instead the jump in the trampoline needs to be modified so it jumps
  106. back to the original destinations
  107. (Preliminary) Results
  108. =====================
  109. +----------+-----+------+------------+---+------+----+-------+
  110. | Name|Small|Branch|RIP Relative|AVX|RDRAND|Loop|TailRec|
  111. +----------+-----+------+------------+---+------+----+-------+
  112. | PolyHook| X | X | X | X | | | |
  113. | MinHook| X | X | X | | | | X |
  114. | MHook| | | X | | | | |
  115. +----------+-----+------+------------+---+------+----+-------+