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 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. _small:
  68. xor eax, eax
  69. ret
  70. Test case: Branch
  71. =================
  72. Instead of the FASM code I'll show the disassembled version, so you can see the
  73. instruction lengths & offsets.
  74. 0026 | 48 83 E0 01 | and rax,1
  75. 002A | 74 17 | je test_cases.0043 --+
  76. 002C | 48 31 C0 | xor rax,rax |
  77. 002F | 90 | nop |
  78. 0030 | 90 | nop |
  79. ... |
  80. 0041 | 90 | nop |
  81. 0042 | 90 | nop |
  82. 0043 | C3 | ret <----------------+
  83. This function has a branch in the first 5 bytes. Hooking it detour-style isn't
  84. possible without fixing that branch in the trampoline. The NOP sled is just so
  85. the hooking engine can't cheat and just put the whole function into the
  86. trampoline. Instead the jump in the trampoline needs to be modified so it jumps
  87. back to the original destinations
  88. Test case: RIP relative
  89. =======================
  90. One of the new things in AMD64 is RIP relative addressing. I guess the reason
  91. to include it was to make it easier to generate PIC -- all references to data
  92. can now be made relative, instead of absolute. So it doesn't matter anymore
  93. where the program is loaded into memory and there's less need for the
  94. relocation table.
  95. A quick and dirty[1] test for this is re-implementing the well known C rand
  96. function.
  97. public _rip_relative
  98. _rip_relative:
  99. mov rax, qword[seed]
  100. mov ecx, 214013
  101. mul ecx
  102. add eax, 2531011
  103. mov [seed], eax
  104. shr eax, 16
  105. and eax, 0x7FFF
  106. ret
  107. seed dd 1
  108. The very first instruction uses rip relative addressing, thus it needs to be
  109. fixed in the trampoline.
  110. Test case: AVX & RDRAND
  111. =======================
  112. The AMD64 instruction set is extended with every CPU generation. Becayse the
  113. hooking engines need to know the instruction lengths and their side effects to
  114. properly apply their hooks, they need to keep up.
  115. The actual code in the test case is boring and doesn't matter. I'm sure there
  116. are disagreements on whether I've picked good candidates of "exotic" or new
  117. instructions, but those were the first that came to mind.
  118. Test case: loop and TailRec
  119. ===========================
  120. My hypothesis before starting this evaluation was that those two cases would
  121. make most hooking engines fail. Back in the good ol' days of x86 detour hooking
  122. didn't require any special thought because the prologue was exactly as big as
  123. the hook itself -- 5 bytes for `PUSH ESP; MOV EBP, ESP` and 5 bytes for `JMP +-
  124. 2GB`[2]. That isn't so easy for AMD64: a) the hook sometimes needs to be *way*
  125. bigger b) due to changes in the calling convention and the general architecture
  126. of AMD64 there just isn't a common prologue, used for almost all functions,
  127. anymore.
  128. Those by itself arn't a problem, since the hooking engines can fix all the
  129. instructions they would overwrite. However I hypothesized that only a few would
  130. check whether the function contained a loop that jumps back into the
  131. instructions that have been overwritten. Consider this:
  132. public _loop
  133. _loop:
  134. mov rax, rcx
  135. @loop_loop:
  136. mul rcx
  137. nop
  138. nop
  139. nop
  140. loop @loop_loop ; lol
  141. ret
  142. There's only 3 bytes that can be safely overwritten. Right after that is the
  143. destination of the jump backwards. This is a very simple (and kinda pointless)
  144. function so detecting that the loop might lead to problems shouldn't be a
  145. problem. But consider what happens with MHook (and all the others):
  146. _loop original:
  147. 008C | 48 89 C8 | mov rax,rcx
  148. 008F | 48 F7 E1 | mul rcx
  149. 0092 | 90 | nop
  150. 0093 | 90 | nop
  151. 0094 | 90 | nop
  152. 0095 | E2 F8 | loop test_cases.008F
  153. 0097 | C3 | ret
  154. _loop hooked:
  155. 008C | E9 0F 69 23 00 | jmp <MHook_Hooks::hookLoop>
  156. 0091 | E1 90 | loope test_cases.0023
  157. 0093 | 90 | nop
  158. 0094 | 90 | nop
  159. 0095 | E2 F8 | loop test_cases.008F
  160. 0097 | C3 | ret
  161. trampoline:
  162. 00007FFF7CD200C0 | 48 89 C8 | mov rax,rcx
  163. 00007FFF7CD200C3 | 48 F7 E1 | mul rcx
  164. 00007FFF7CD200C6 | E9 C7 96 DC FF | jmp test_cases.0092
  165. then executes:
  166. 0092 | 90 | nop
  167. 0093 | 90 | nop
  168. 0094 | 90 | nop
  169. 0095 | E2 F8 | loop test_cases.008F
  170. But that jumps back into the middle of the jump and thus executes:
  171. 008F | 23 00 | and eax,dword ptr ds:[rax]
  172. 0091 | E1 90 | loope test_cases.0023
  173. Which isn't right and will crash horribly.
  174. (Preliminary) Results
  175. =====================
  176. +----------+-----+------+------------+---+------+----+-------+
  177. | Name|Small|Branch|RIP Relative|AVX|RDRAND|Loop|TailRec|
  178. +----------+-----+------+------------+---+------+----+-------+
  179. | PolyHook| X | X | X | X | | | |
  180. | MinHook| X | X | X | | | | X |
  181. | MHook| | | X | | | | |
  182. +----------+-----+------+------------+---+------+----+-------+
  183. [1] This is one of the things that could easily be improved, but haven't been
  184. because I just couldn't motivate myself. Putting the data right after the func
  185. meant that a section containing code needed to be writable. Which is bad. Also
  186. I load the seed DWORD as a QWORD -- which only works because the upper half is
  187. then thrown away by the multiplication. It's shitty code is what I'm saying.
  188. In retrospect I should have used a jump table like a switch-case could be
  189. compiled into. That would be read only data. Oh well.
  190. [2] And Microsoft decided at some point to make it even easier for their code
  191. with the advent of hotpatching.