contrast-agent 3.8.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (500) hide show
  1. checksums.yaml +7 -0
  2. data/.clang-format +5 -0
  3. data/.dockerignore +10 -0
  4. data/.gitignore +58 -0
  5. data/.gitmodules +6 -0
  6. data/.rspec +6 -0
  7. data/.simplecov +4 -0
  8. data/Gemfile +7 -0
  9. data/LICENSE.txt +12 -0
  10. data/Rakefile +15 -0
  11. data/exe/contrast_service +29 -0
  12. data/ext/build_funchook.rb +48 -0
  13. data/ext/cs__assess_active_record_named/cs__active_record_named.c +47 -0
  14. data/ext/cs__assess_active_record_named/cs__active_record_named.h +10 -0
  15. data/ext/cs__assess_active_record_named/extconf.rb +2 -0
  16. data/ext/cs__assess_array/cs__assess_array.c +38 -0
  17. data/ext/cs__assess_array/cs__assess_array.h +9 -0
  18. data/ext/cs__assess_array/extconf.rb +2 -0
  19. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +50 -0
  20. data/ext/cs__assess_basic_object/cs__assess_basic_object.h +17 -0
  21. data/ext/cs__assess_basic_object/extconf.rb +2 -0
  22. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +86 -0
  23. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.h +34 -0
  24. data/ext/cs__assess_fiber_track/extconf.rb +2 -0
  25. data/ext/cs__assess_hash/cs__assess_hash.c +64 -0
  26. data/ext/cs__assess_hash/cs__assess_hash.h +24 -0
  27. data/ext/cs__assess_hash/extconf.rb +2 -0
  28. data/ext/cs__assess_kernel/cs__assess_kernel.c +36 -0
  29. data/ext/cs__assess_kernel/cs__assess_kernel.h +10 -0
  30. data/ext/cs__assess_kernel/extconf.rb +2 -0
  31. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +47 -0
  32. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +18 -0
  33. data/ext/cs__assess_marshal_module/extconf.rb +2 -0
  34. data/ext/cs__assess_module/cs__assess_module.c +78 -0
  35. data/ext/cs__assess_module/cs__assess_module.h +25 -0
  36. data/ext/cs__assess_module/extconf.rb +2 -0
  37. data/ext/cs__assess_regexp/cs__assess_regexp.c +48 -0
  38. data/ext/cs__assess_regexp/cs__assess_regexp.h +22 -0
  39. data/ext/cs__assess_regexp/extconf.rb +2 -0
  40. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.c +63 -0
  41. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.h +29 -0
  42. data/ext/cs__assess_regexp_track/extconf.rb +2 -0
  43. data/ext/cs__assess_string/cs__assess_string.c +38 -0
  44. data/ext/cs__assess_string/cs__assess_string.h +19 -0
  45. data/ext/cs__assess_string/extconf.rb +2 -0
  46. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +31 -0
  47. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.h +13 -0
  48. data/ext/cs__assess_string_interpolation26/extconf.rb +2 -0
  49. data/ext/cs__common/cs__common.c +60 -0
  50. data/ext/cs__common/cs__common.h +28 -0
  51. data/ext/cs__common/extconf.rb +20 -0
  52. data/ext/cs__contrast_patch/cs__contrast_patch.c +445 -0
  53. data/ext/cs__contrast_patch/cs__contrast_patch.h +196 -0
  54. data/ext/cs__contrast_patch/extconf.rb +2 -0
  55. data/ext/cs__protect_kernel/cs__protect_kernel.c +37 -0
  56. data/ext/cs__protect_kernel/cs__protect_kernel.h +11 -0
  57. data/ext/cs__protect_kernel/extconf.rb +2 -0
  58. data/ext/cs__scope/cs__scope.c +96 -0
  59. data/ext/cs__scope/cs__scope.h +33 -0
  60. data/ext/cs__scope/extconf.rb +2 -0
  61. data/ext/extconf_common.rb +49 -0
  62. data/funchook/LICENSE +360 -0
  63. data/funchook/Makefile +29 -0
  64. data/funchook/Makefile.in +29 -0
  65. data/funchook/README.md +121 -0
  66. data/funchook/appveyor.yml +42 -0
  67. data/funchook/autogen.sh +3 -0
  68. data/funchook/autom4te.cache/output.0 +4976 -0
  69. data/funchook/autom4te.cache/requests +78 -0
  70. data/funchook/autom4te.cache/traces.0 +364 -0
  71. data/funchook/config.guess +1530 -0
  72. data/funchook/config.log +490 -0
  73. data/funchook/config.status +1016 -0
  74. data/funchook/config.sub +1773 -0
  75. data/funchook/configure +4976 -0
  76. data/funchook/configure.ac +59 -0
  77. data/funchook/distorm/COPYING +26 -0
  78. data/funchook/distorm/MANIFEST +25 -0
  79. data/funchook/distorm/MANIFEST.in +4 -0
  80. data/funchook/distorm/README.md +12 -0
  81. data/funchook/distorm/disOps/disOps.py +795 -0
  82. data/funchook/distorm/disOps/x86db.py +404 -0
  83. data/funchook/distorm/disOps/x86header.py +247 -0
  84. data/funchook/distorm/disOps/x86sets.py +1664 -0
  85. data/funchook/distorm/examples/cs/TestdiStorm/Program.cs +79 -0
  86. data/funchook/distorm/examples/cs/TestdiStorm/Properties/AssemblyInfo.cs +36 -0
  87. data/funchook/distorm/examples/cs/TestdiStorm/TestdiStorm.csproj +69 -0
  88. data/funchook/distorm/examples/cs/distorm-net.sln +26 -0
  89. data/funchook/distorm/examples/cs/distorm-net/CodeInfo.cs +23 -0
  90. data/funchook/distorm/examples/cs/distorm-net/DecodedInst.cs +15 -0
  91. data/funchook/distorm/examples/cs/distorm-net/DecodedResult.cs +14 -0
  92. data/funchook/distorm/examples/cs/distorm-net/DecomposedInst.cs +36 -0
  93. data/funchook/distorm/examples/cs/distorm-net/DecomposedResult.cs +14 -0
  94. data/funchook/distorm/examples/cs/distorm-net/Opcodes.cs +1268 -0
  95. data/funchook/distorm/examples/cs/distorm-net/Opcodes.tt +37 -0
  96. data/funchook/distorm/examples/cs/distorm-net/Operand.cs +25 -0
  97. data/funchook/distorm/examples/cs/distorm-net/Properties/AssemblyInfo.cs +36 -0
  98. data/funchook/distorm/examples/cs/distorm-net/diStorm3.cs +411 -0
  99. data/funchook/distorm/examples/cs/distorm-net/distorm-net.csproj +80 -0
  100. data/funchook/distorm/examples/cs/readme +3 -0
  101. data/funchook/distorm/examples/ddk/README +48 -0
  102. data/funchook/distorm/examples/ddk/distorm.ini +11 -0
  103. data/funchook/distorm/examples/ddk/dummy.c +15 -0
  104. data/funchook/distorm/examples/ddk/main.c +91 -0
  105. data/funchook/distorm/examples/ddk/makefile +1 -0
  106. data/funchook/distorm/examples/ddk/sources +10 -0
  107. data/funchook/distorm/examples/java/Makefile +23 -0
  108. data/funchook/distorm/examples/java/distorm/src/Main.java +43 -0
  109. data/funchook/distorm/examples/java/distorm/src/diStorm3/CodeInfo.java +27 -0
  110. data/funchook/distorm/examples/java/distorm/src/diStorm3/DecodedInst.java +32 -0
  111. data/funchook/distorm/examples/java/distorm/src/diStorm3/DecodedResult.java +11 -0
  112. data/funchook/distorm/examples/java/distorm/src/diStorm3/DecomposedInst.java +89 -0
  113. data/funchook/distorm/examples/java/distorm/src/diStorm3/DecomposedResult.java +11 -0
  114. data/funchook/distorm/examples/java/distorm/src/diStorm3/OpcodeEnum.java +131 -0
  115. data/funchook/distorm/examples/java/distorm/src/diStorm3/Opcodes.java +1123 -0
  116. data/funchook/distorm/examples/java/distorm/src/diStorm3/Operand.java +24 -0
  117. data/funchook/distorm/examples/java/distorm/src/diStorm3/distorm3.java +41 -0
  118. data/funchook/distorm/examples/java/jdistorm.c +405 -0
  119. data/funchook/distorm/examples/java/jdistorm.h +40 -0
  120. data/funchook/distorm/examples/java/jdistorm.sln +20 -0
  121. data/funchook/distorm/examples/java/jdistorm.vcproj +208 -0
  122. data/funchook/distorm/examples/linux/Makefile +15 -0
  123. data/funchook/distorm/examples/linux/main.c +181 -0
  124. data/funchook/distorm/examples/tests/Makefile +15 -0
  125. data/funchook/distorm/examples/tests/main.cpp +42 -0
  126. data/funchook/distorm/examples/tests/main.py +66 -0
  127. data/funchook/distorm/examples/tests/test_distorm3.py +1672 -0
  128. data/funchook/distorm/examples/tests/tests.sln +20 -0
  129. data/funchook/distorm/examples/tests/tests.vcxproj +82 -0
  130. data/funchook/distorm/examples/tests/tests.vcxproj.filters +22 -0
  131. data/funchook/distorm/examples/win32/disasm.sln +25 -0
  132. data/funchook/distorm/examples/win32/disasm.vcxproj +201 -0
  133. data/funchook/distorm/examples/win32/disasm.vcxproj.filters +14 -0
  134. data/funchook/distorm/examples/win32/main.cpp +163 -0
  135. data/funchook/distorm/include/distorm.h +482 -0
  136. data/funchook/distorm/include/mnemonics.h +301 -0
  137. data/funchook/distorm/make/linux/Makefile +28 -0
  138. data/funchook/distorm/make/mac/Makefile +24 -0
  139. data/funchook/distorm/make/win32/cdistorm.vcxproj +239 -0
  140. data/funchook/distorm/make/win32/cdistorm.vcxproj.filters +80 -0
  141. data/funchook/distorm/make/win32/distorm.sln +25 -0
  142. data/funchook/distorm/make/win32/resource.h +14 -0
  143. data/funchook/distorm/make/win32/resource.rc +99 -0
  144. data/funchook/distorm/python/distorm3/__init__.py +957 -0
  145. data/funchook/distorm/python/distorm3/sample.py +51 -0
  146. data/funchook/distorm/setup.cfg +10 -0
  147. data/funchook/distorm/setup.py +266 -0
  148. data/funchook/distorm/src/config.h +169 -0
  149. data/funchook/distorm/src/decoder.c +641 -0
  150. data/funchook/distorm/src/decoder.h +33 -0
  151. data/funchook/distorm/src/distorm.c +413 -0
  152. data/funchook/distorm/src/instructions.c +597 -0
  153. data/funchook/distorm/src/instructions.h +463 -0
  154. data/funchook/distorm/src/insts.c +7939 -0
  155. data/funchook/distorm/src/insts.h +64 -0
  156. data/funchook/distorm/src/mnemonics.c +284 -0
  157. data/funchook/distorm/src/operands.c +1290 -0
  158. data/funchook/distorm/src/operands.h +28 -0
  159. data/funchook/distorm/src/prefix.c +368 -0
  160. data/funchook/distorm/src/prefix.h +64 -0
  161. data/funchook/distorm/src/textdefs.c +172 -0
  162. data/funchook/distorm/src/textdefs.h +57 -0
  163. data/funchook/distorm/src/wstring.c +47 -0
  164. data/funchook/distorm/src/wstring.h +35 -0
  165. data/funchook/distorm/src/x86defs.h +82 -0
  166. data/funchook/include/funchook.h +123 -0
  167. data/funchook/install-sh +527 -0
  168. data/funchook/src/Makefile +70 -0
  169. data/funchook/src/Makefile.in +70 -0
  170. data/funchook/src/__strerror.h +109 -0
  171. data/funchook/src/config.h +101 -0
  172. data/funchook/src/config.h.in +100 -0
  173. data/funchook/src/decoder.o +0 -0
  174. data/funchook/src/distorm.o +0 -0
  175. data/funchook/src/funchook.c +440 -0
  176. data/funchook/src/funchook.o +0 -0
  177. data/funchook/src/funchook_internal.h +155 -0
  178. data/funchook/src/funchook_io.c +182 -0
  179. data/funchook/src/funchook_io.h +64 -0
  180. data/funchook/src/funchook_io.o +0 -0
  181. data/funchook/src/funchook_syscall.S +134 -0
  182. data/funchook/src/funchook_syscall.o +0 -0
  183. data/funchook/src/funchook_unix.c +480 -0
  184. data/funchook/src/funchook_unix.o +0 -0
  185. data/funchook/src/funchook_windows.c +397 -0
  186. data/funchook/src/funchook_x86.c +622 -0
  187. data/funchook/src/funchook_x86.o +0 -0
  188. data/funchook/src/instructions.o +0 -0
  189. data/funchook/src/insts.o +0 -0
  190. data/funchook/src/libfunchook.so +0 -0
  191. data/funchook/src/mnemonics.o +0 -0
  192. data/funchook/src/operands.o +0 -0
  193. data/funchook/src/os_func.c +115 -0
  194. data/funchook/src/os_func.h +75 -0
  195. data/funchook/src/os_func.o +0 -0
  196. data/funchook/src/os_func_unix.c +94 -0
  197. data/funchook/src/os_func_unix.o +0 -0
  198. data/funchook/src/os_func_windows.c +32 -0
  199. data/funchook/src/prefix.o +0 -0
  200. data/funchook/src/printf_base.c +1688 -0
  201. data/funchook/src/printf_base.h +46 -0
  202. data/funchook/src/printf_base.o +0 -0
  203. data/funchook/src/textdefs.o +0 -0
  204. data/funchook/src/wstring.o +0 -0
  205. data/funchook/test/Makefile +43 -0
  206. data/funchook/test/Makefile.in +43 -0
  207. data/funchook/test/funchook_test +0 -0
  208. data/funchook/test/libfunchook_test.c +25 -0
  209. data/funchook/test/libfunchook_test.so +0 -0
  210. data/funchook/test/libfunchook_test2.c +18 -0
  211. data/funchook/test/suffix.list +600 -0
  212. data/funchook/test/test_main.c +430 -0
  213. data/funchook/test/test_main.o +0 -0
  214. data/funchook/test/x86_64_test.S +10 -0
  215. data/funchook/test/x86_64_test.o +0 -0
  216. data/funchook/test/x86_test.S +339 -0
  217. data/funchook/win32/config.h +1 -0
  218. data/funchook/win32/funchook.sln +52 -0
  219. data/funchook/win32/funchook.vcxproj +188 -0
  220. data/funchook/win32/funchook.vcxproj.filters +84 -0
  221. data/funchook/win32/funchook_test.vcxproj +170 -0
  222. data/funchook/win32/funchook_test.vcxproj.filters +22 -0
  223. data/funchook/win32/funchook_test_dll.vcxproj +184 -0
  224. data/funchook/win32/funchook_test_dll.vcxproj.filters +30 -0
  225. data/funchook/win32/funchook_test_exe.def +3 -0
  226. data/lib/contrast-agent.rb +8 -0
  227. data/lib/contrast.rb +57 -0
  228. data/lib/contrast/agent.rb +80 -0
  229. data/lib/contrast/agent/assess.rb +45 -0
  230. data/lib/contrast/agent/assess/adjusted_span.rb +25 -0
  231. data/lib/contrast/agent/assess/class_reverter.rb +82 -0
  232. data/lib/contrast/agent/assess/contrast_event.rb +398 -0
  233. data/lib/contrast/agent/assess/frozen_properties.rb +41 -0
  234. data/lib/contrast/agent/assess/insulator.rb +53 -0
  235. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +78 -0
  236. data/lib/contrast/agent/assess/policy/patcher.rb +85 -0
  237. data/lib/contrast/agent/assess/policy/policy.rb +116 -0
  238. data/lib/contrast/agent/assess/policy/policy_node.rb +289 -0
  239. data/lib/contrast/agent/assess/policy/policy_scanner.rb +44 -0
  240. data/lib/contrast/agent/assess/policy/preshift.rb +94 -0
  241. data/lib/contrast/agent/assess/policy/propagation_method.rb +260 -0
  242. data/lib/contrast/agent/assess/policy/propagation_node.rb +127 -0
  243. data/lib/contrast/agent/assess/policy/propagator.rb +35 -0
  244. data/lib/contrast/agent/assess/policy/propagator/append.rb +54 -0
  245. data/lib/contrast/agent/assess/policy/propagator/base.rb +37 -0
  246. data/lib/contrast/agent/assess/policy/propagator/center.rb +73 -0
  247. data/lib/contrast/agent/assess/policy/propagator/custom.rb +36 -0
  248. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +62 -0
  249. data/lib/contrast/agent/assess/policy/propagator/insert.rb +55 -0
  250. data/lib/contrast/agent/assess/policy/propagator/keep.rb +26 -0
  251. data/lib/contrast/agent/assess/policy/propagator/next.rb +42 -0
  252. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +50 -0
  253. data/lib/contrast/agent/assess/policy/propagator/remove.rb +76 -0
  254. data/lib/contrast/agent/assess/policy/propagator/replace.rb +27 -0
  255. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +38 -0
  256. data/lib/contrast/agent/assess/policy/propagator/select.rb +86 -0
  257. data/lib/contrast/agent/assess/policy/propagator/splat.rb +60 -0
  258. data/lib/contrast/agent/assess/policy/propagator/split.rb +49 -0
  259. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +169 -0
  260. data/lib/contrast/agent/assess/policy/propagator/trim.rb +81 -0
  261. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +79 -0
  262. data/lib/contrast/agent/assess/policy/source_method.rb +209 -0
  263. data/lib/contrast/agent/assess/policy/source_node.rb +62 -0
  264. data/lib/contrast/agent/assess/policy/trigger_method.rb +209 -0
  265. data/lib/contrast/agent/assess/policy/trigger_node.rb +198 -0
  266. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +77 -0
  267. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +31 -0
  268. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +40 -0
  269. data/lib/contrast/agent/assess/properties.rb +392 -0
  270. data/lib/contrast/agent/assess/rule.rb +18 -0
  271. data/lib/contrast/agent/assess/rule/base.rb +72 -0
  272. data/lib/contrast/agent/assess/rule/csrf.rb +66 -0
  273. data/lib/contrast/agent/assess/rule/csrf/csrf_action.rb +28 -0
  274. data/lib/contrast/agent/assess/rule/csrf/csrf_applicator.rb +69 -0
  275. data/lib/contrast/agent/assess/rule/csrf/csrf_watcher.rb +132 -0
  276. data/lib/contrast/agent/assess/rule/provider.rb +21 -0
  277. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +62 -0
  278. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +73 -0
  279. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +121 -0
  280. data/lib/contrast/agent/assess/rule/redos.rb +68 -0
  281. data/lib/contrast/agent/assess/rule/response_scanning_rule.rb +47 -0
  282. data/lib/contrast/agent/assess/rule/response_watcher.rb +36 -0
  283. data/lib/contrast/agent/assess/rule/watcher.rb +36 -0
  284. data/lib/contrast/agent/assess/tag.rb +151 -0
  285. data/lib/contrast/agent/at_exit_hook.rb +33 -0
  286. data/lib/contrast/agent/class_reopener.rb +195 -0
  287. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +26 -0
  288. data/lib/contrast/agent/deadzone/policy/policy.rb +57 -0
  289. data/lib/contrast/agent/disable_reaction.rb +24 -0
  290. data/lib/contrast/agent/exclusion_matcher.rb +190 -0
  291. data/lib/contrast/agent/feature_state.rb +379 -0
  292. data/lib/contrast/agent/inventory/policy/policy.rb +32 -0
  293. data/lib/contrast/agent/inventory/policy/trigger_node.rb +22 -0
  294. data/lib/contrast/agent/logger_manager.rb +116 -0
  295. data/lib/contrast/agent/middleware.rb +352 -0
  296. data/lib/contrast/agent/module_data.rb +16 -0
  297. data/lib/contrast/agent/patching/policy/after_load_patch.rb +37 -0
  298. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +58 -0
  299. data/lib/contrast/agent/patching/policy/method_policy.rb +94 -0
  300. data/lib/contrast/agent/patching/policy/module_policy.rb +116 -0
  301. data/lib/contrast/agent/patching/policy/patch.rb +312 -0
  302. data/lib/contrast/agent/patching/policy/patch_status.rb +192 -0
  303. data/lib/contrast/agent/patching/policy/patcher.rb +310 -0
  304. data/lib/contrast/agent/patching/policy/policy.rb +138 -0
  305. data/lib/contrast/agent/patching/policy/policy_node.rb +80 -0
  306. data/lib/contrast/agent/patching/policy/policy_unpatcher.rb +28 -0
  307. data/lib/contrast/agent/patching/policy/trigger_node.rb +81 -0
  308. data/lib/contrast/agent/protect/policy/policy.rb +37 -0
  309. data/lib/contrast/agent/protect/policy/trigger_node.rb +23 -0
  310. data/lib/contrast/agent/protect/rule.rb +58 -0
  311. data/lib/contrast/agent/protect/rule/base.rb +300 -0
  312. data/lib/contrast/agent/protect/rule/base_service.rb +88 -0
  313. data/lib/contrast/agent/protect/rule/cmd_injection.rb +156 -0
  314. data/lib/contrast/agent/protect/rule/csrf.rb +118 -0
  315. data/lib/contrast/agent/protect/rule/csrf/csrf_evaluator.rb +103 -0
  316. data/lib/contrast/agent/protect/rule/csrf/csrf_token_injector.rb +85 -0
  317. data/lib/contrast/agent/protect/rule/default_scanner.rb +300 -0
  318. data/lib/contrast/agent/protect/rule/deserialization.rb +193 -0
  319. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +80 -0
  320. data/lib/contrast/agent/protect/rule/no_sqli.rb +101 -0
  321. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +40 -0
  322. data/lib/contrast/agent/protect/rule/path_traversal.rb +143 -0
  323. data/lib/contrast/agent/protect/rule/sqli.rb +101 -0
  324. data/lib/contrast/agent/protect/rule/sqli/default_sql_scanner.rb +16 -0
  325. data/lib/contrast/agent/protect/rule/sqli/mysql_sql_scanner.rb +38 -0
  326. data/lib/contrast/agent/protect/rule/sqli/postgres_sql_scanner.rb +22 -0
  327. data/lib/contrast/agent/protect/rule/sqli/sqlite_sql_scanner.rb +19 -0
  328. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +20 -0
  329. data/lib/contrast/agent/protect/rule/xss.rb +24 -0
  330. data/lib/contrast/agent/protect/rule/xxe.rb +120 -0
  331. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +82 -0
  332. data/lib/contrast/agent/railtie.rb +30 -0
  333. data/lib/contrast/agent/reaction_processor.rb +47 -0
  334. data/lib/contrast/agent/request.rb +493 -0
  335. data/lib/contrast/agent/request_context.rb +225 -0
  336. data/lib/contrast/agent/require_state.rb +61 -0
  337. data/lib/contrast/agent/response.rb +215 -0
  338. data/lib/contrast/agent/rewriter.rb +244 -0
  339. data/lib/contrast/agent/scope.rb +28 -0
  340. data/lib/contrast/agent/service_heartbeat.rb +37 -0
  341. data/lib/contrast/agent/settings_state.rb +148 -0
  342. data/lib/contrast/agent/socket_client.rb +125 -0
  343. data/lib/contrast/agent/thread.rb +26 -0
  344. data/lib/contrast/agent/tracepoint_hook.rb +51 -0
  345. data/lib/contrast/agent/version.rb +8 -0
  346. data/lib/contrast/api.rb +17 -0
  347. data/lib/contrast/api/.gitkeep +0 -0
  348. data/lib/contrast/api/connection_status.rb +49 -0
  349. data/lib/contrast/api/socket.rb +43 -0
  350. data/lib/contrast/api/speedracer.rb +206 -0
  351. data/lib/contrast/api/tcp_socket.rb +31 -0
  352. data/lib/contrast/api/unix_socket.rb +25 -0
  353. data/lib/contrast/common_agent_configuration.rb +86 -0
  354. data/lib/contrast/components/agent.rb +85 -0
  355. data/lib/contrast/components/app_context.rb +188 -0
  356. data/lib/contrast/components/assess.rb +67 -0
  357. data/lib/contrast/components/config.rb +135 -0
  358. data/lib/contrast/components/contrast_service.rb +113 -0
  359. data/lib/contrast/components/heap_dump.rb +34 -0
  360. data/lib/contrast/components/interface.rb +178 -0
  361. data/lib/contrast/components/inventory.rb +23 -0
  362. data/lib/contrast/components/logger.rb +92 -0
  363. data/lib/contrast/components/protect.rb +38 -0
  364. data/lib/contrast/components/sampling.rb +41 -0
  365. data/lib/contrast/components/scope.rb +106 -0
  366. data/lib/contrast/components/settings.rb +140 -0
  367. data/lib/contrast/config.rb +33 -0
  368. data/lib/contrast/config/agent_configuration.rb +24 -0
  369. data/lib/contrast/config/application_configuration.rb +27 -0
  370. data/lib/contrast/config/assess_configuration.rb +22 -0
  371. data/lib/contrast/config/assess_rules_configuration.rb +18 -0
  372. data/lib/contrast/config/base_configuration.rb +105 -0
  373. data/lib/contrast/config/default_value.rb +16 -0
  374. data/lib/contrast/config/exception_configuration.rb +21 -0
  375. data/lib/contrast/config/heap_dump_configuration.rb +23 -0
  376. data/lib/contrast/config/inventory_configuration.rb +20 -0
  377. data/lib/contrast/config/logger_configuration.rb +20 -0
  378. data/lib/contrast/config/protect_configuration.rb +20 -0
  379. data/lib/contrast/config/protect_rule_configuration.rb +37 -0
  380. data/lib/contrast/config/protect_rules_configuration.rb +30 -0
  381. data/lib/contrast/config/root_configuration.rb +26 -0
  382. data/lib/contrast/config/ruby_configuration.rb +39 -0
  383. data/lib/contrast/config/sampling_configuration.rb +22 -0
  384. data/lib/contrast/config/server_configuration.rb +23 -0
  385. data/lib/contrast/config/service_configuration.rb +22 -0
  386. data/lib/contrast/configuration.rb +214 -0
  387. data/lib/contrast/core_extensions/assess.rb +51 -0
  388. data/lib/contrast/core_extensions/assess/array.rb +58 -0
  389. data/lib/contrast/core_extensions/assess/assess_extension.rb +145 -0
  390. data/lib/contrast/core_extensions/assess/basic_object.rb +15 -0
  391. data/lib/contrast/core_extensions/assess/erb.rb +42 -0
  392. data/lib/contrast/core_extensions/assess/exec_trigger.rb +48 -0
  393. data/lib/contrast/core_extensions/assess/fiber.rb +125 -0
  394. data/lib/contrast/core_extensions/assess/hash.rb +22 -0
  395. data/lib/contrast/core_extensions/assess/kernel.rb +95 -0
  396. data/lib/contrast/core_extensions/assess/module.rb +14 -0
  397. data/lib/contrast/core_extensions/assess/regexp.rb +206 -0
  398. data/lib/contrast/core_extensions/assess/string.rb +75 -0
  399. data/lib/contrast/core_extensions/assess/tilt_template_trigger.rb +73 -0
  400. data/lib/contrast/core_extensions/delegator.rb +14 -0
  401. data/lib/contrast/core_extensions/eval_trigger.rb +52 -0
  402. data/lib/contrast/core_extensions/inventory.rb +22 -0
  403. data/lib/contrast/core_extensions/inventory/datastores.rb +37 -0
  404. data/lib/contrast/core_extensions/module.rb +42 -0
  405. data/lib/contrast/core_extensions/object.rb +27 -0
  406. data/lib/contrast/core_extensions/protect.rb +20 -0
  407. data/lib/contrast/core_extensions/protect/applies_command_injection_rule.rb +70 -0
  408. data/lib/contrast/core_extensions/protect/applies_deserialization_rule.rb +58 -0
  409. data/lib/contrast/core_extensions/protect/applies_no_sqli_rule.rb +81 -0
  410. data/lib/contrast/core_extensions/protect/applies_path_traversal_rule.rb +119 -0
  411. data/lib/contrast/core_extensions/protect/applies_sqli_rule.rb +63 -0
  412. data/lib/contrast/core_extensions/protect/applies_xxe_rule.rb +141 -0
  413. data/lib/contrast/core_extensions/protect/kernel.rb +30 -0
  414. data/lib/contrast/core_extensions/protect/psych.rb +7 -0
  415. data/lib/contrast/core_extensions/thread.rb +31 -0
  416. data/lib/contrast/internal_exception.rb +8 -0
  417. data/lib/contrast/rails_extensions/assess/action_controller_inheritance.rb +48 -0
  418. data/lib/contrast/rails_extensions/assess/active_record.rb +32 -0
  419. data/lib/contrast/rails_extensions/assess/active_record_named.rb +61 -0
  420. data/lib/contrast/rails_extensions/assess/configuration.rb +26 -0
  421. data/lib/contrast/rails_extensions/buffer.rb +30 -0
  422. data/lib/contrast/rails_extensions/rack.rb +45 -0
  423. data/lib/contrast/security_exception.rb +14 -0
  424. data/lib/contrast/sinatra_extensions/assess/cookie.rb +26 -0
  425. data/lib/contrast/sinatra_extensions/inventory/sinatra_base.rb +59 -0
  426. data/lib/contrast/tasks/service.rb +95 -0
  427. data/lib/contrast/utils/assess/sampling_util.rb +96 -0
  428. data/lib/contrast/utils/assess/tracking_util.rb +39 -0
  429. data/lib/contrast/utils/boolean_util.rb +33 -0
  430. data/lib/contrast/utils/cache.rb +69 -0
  431. data/lib/contrast/utils/class_util.rb +58 -0
  432. data/lib/contrast/utils/comment_range.rb +19 -0
  433. data/lib/contrast/utils/data_store_util.rb +23 -0
  434. data/lib/contrast/utils/duck_utils.rb +58 -0
  435. data/lib/contrast/utils/env_configuration_item.rb +52 -0
  436. data/lib/contrast/utils/environment_util.rb +152 -0
  437. data/lib/contrast/utils/freeze_util.rb +36 -0
  438. data/lib/contrast/utils/gemfile_reader.rb +191 -0
  439. data/lib/contrast/utils/hash_digest.rb +148 -0
  440. data/lib/contrast/utils/heap_dump_util.rb +113 -0
  441. data/lib/contrast/utils/invalid_configuration_util.rb +88 -0
  442. data/lib/contrast/utils/inventory_util.rb +126 -0
  443. data/lib/contrast/utils/io_util.rb +61 -0
  444. data/lib/contrast/utils/object_share.rb +117 -0
  445. data/lib/contrast/utils/operating_environment.rb +38 -0
  446. data/lib/contrast/utils/os.rb +49 -0
  447. data/lib/contrast/utils/path_util.rb +151 -0
  448. data/lib/contrast/utils/performs_logging.rb +152 -0
  449. data/lib/contrast/utils/preflight_util.rb +13 -0
  450. data/lib/contrast/utils/prevent_serialization.rb +52 -0
  451. data/lib/contrast/utils/rack_assess_session_cookie.rb +104 -0
  452. data/lib/contrast/utils/rails_assess_configuration.rb +95 -0
  453. data/lib/contrast/utils/random_util.rb +22 -0
  454. data/lib/contrast/utils/resource_loader.rb +23 -0
  455. data/lib/contrast/utils/ruby_ast_rewriter.rb +74 -0
  456. data/lib/contrast/utils/scope_util.rb +99 -0
  457. data/lib/contrast/utils/service_response_util.rb +116 -0
  458. data/lib/contrast/utils/service_sender_util.rb +98 -0
  459. data/lib/contrast/utils/sha256_builder.rb +69 -0
  460. data/lib/contrast/utils/sinatra_helper.rb +49 -0
  461. data/lib/contrast/utils/stack_trace_utils.rb +209 -0
  462. data/lib/contrast/utils/string_utils.rb +72 -0
  463. data/lib/contrast/utils/tag_util.rb +139 -0
  464. data/lib/contrast/utils/thread_tracker.rb +54 -0
  465. data/lib/contrast/utils/timer.rb +78 -0
  466. data/resources/assess/policy.json +1673 -0
  467. data/resources/csrf/inject.js +44 -0
  468. data/resources/deadzone/policy.json +55 -0
  469. data/resources/factory-bot-spec/spec_helper.rb +30 -0
  470. data/resources/inventory/policy.json +110 -0
  471. data/resources/protect/policy.json +417 -0
  472. data/resources/rubocops/kernel/catch_cop.rb +37 -0
  473. data/resources/rubocops/kernel/require_cop.rb +37 -0
  474. data/resources/rubocops/kernel/require_relative_cop.rb +33 -0
  475. data/resources/rubocops/module/autoload_cop.rb +37 -0
  476. data/resources/rubocops/module/const_defined_cop.rb +37 -0
  477. data/resources/rubocops/module/const_get_cop.rb +37 -0
  478. data/resources/rubocops/module/const_set_cop.rb +37 -0
  479. data/resources/rubocops/module/constants_cop.rb +37 -0
  480. data/resources/rubocops/module/name_cop.rb +37 -0
  481. data/resources/rubocops/object/class_cop.rb +37 -0
  482. data/resources/rubocops/object/freeze_cop.rb +37 -0
  483. data/resources/rubocops/object/frozen_cop.rb +37 -0
  484. data/resources/rubocops/object/is_a_cop.rb +37 -0
  485. data/resources/rubocops/object/method_cop.rb +37 -0
  486. data/resources/rubocops/object/respond_to_cop.rb +37 -0
  487. data/resources/rubocops/object/singleton_class_cop.rb +37 -0
  488. data/resources/rubocops/regexp/spelling_cop.rb +44 -0
  489. data/resources/rubocops/thread/new_cop.rb +39 -0
  490. data/resources/ruby-spec/ancestors_spec.rb +70 -0
  491. data/resources/ruby-spec/modulo_spec.rb +831 -0
  492. data/resources/ruby-spec/parameters_spec.rb +261 -0
  493. data/resources/ruby-spec/ruby_spec_spec_helper.rb +35 -0
  494. data/resources/test_marker.txt +1 -0
  495. data/ruby-agent.gemspec +129 -0
  496. data/service_executables/.gitkeep +0 -0
  497. data/service_executables/VERSION +1 -0
  498. data/service_executables/linux/contrast-service +0 -0
  499. data/service_executables/mac/contrast-service +0 -0
  500. metadata +945 -0
@@ -0,0 +1,404 @@
1
+ #
2
+ # x86db.py
3
+ #
4
+ # Copyright (C) 2012 Gil Dabah, http://ragestorm.net/disops/
5
+ #
6
+
7
+ from x86header import *
8
+
9
+ # The mandatory prefix bytes list:
10
+ _MandatoryPrefixesList = [0x9b, 0x66, 0xf3, 0xf2]
11
+ # Map the mandatory prefix to its corresponding entry number in the PREFIXED table.
12
+ # Note that no-prefix is first entry.
13
+ _MandatoryPrefixToEntry = {0x9b: 1, 0x66: 1, 0xf3: 2, 0xf2: 3}
14
+
15
+ #
16
+ # Prefixed Table (12 entries):
17
+ #
18
+ # Normal optional prefix:
19
+ # 0 - NONE, 1 - 66/9b, 2 - f3, 3 - f2
20
+ #
21
+ # With VEX prefix (if based on MOD, then MOD=11):
22
+ # 4 - NONE, 5 - 66/9b, 6 - f3, 7 - f2
23
+ #
24
+ # With VEX prefix, (MOD!=11):
25
+ # 8 - NONE, 9 - 66/9b, 10 - f3, 11 - f2
26
+ #
27
+ # Note: VEX.L and VEX.W affections can be described in a single InstInfo structure and thus we don't need to split them too.
28
+ # The problem with instructions that are encodable with or without VEX.VVVV
29
+ # is that a single InstInfo structure doesn't have a few sets of operands.
30
+
31
+ class DBException(Exception):
32
+ """ Used in order to throw an exception when an error occurrs in the DB. """
33
+ pass
34
+
35
+
36
+ class InstructionInfo:
37
+ """ Instruction Info holds all information relevant for an instruction.
38
+ another string member, self.tag, will be initialized in runtime to have the bytes of the opcode (I.E: 0f_0f_a7). """
39
+ def __init__(self, classType, OL, pos, isModRMIncluded, mnemonics, operands, flags):
40
+ self.tag = ""
41
+ self.classType = classType
42
+ # Check for special mandatory-prefixed instruction.
43
+ if pos[0] in _MandatoryPrefixesList:
44
+ self.pos = pos[1:]
45
+ self.prefix = pos[0]
46
+ self.OL = OpcodeLength.NextOL[OL]
47
+ self.prefixed = True
48
+ self.entryNo = _MandatoryPrefixToEntry[pos[0]]
49
+ else:
50
+ self.pos = pos
51
+ self.prefix = 0
52
+ self.OL = OL
53
+ self.prefixed = False
54
+ self.entryNo = 0
55
+ self.mnemonics = mnemonics
56
+ self.operands = operands
57
+ self.flags = flags
58
+ # CPU affected flags by instruction:
59
+ self.modifiedFlags = 0
60
+ self.testedFlags = 0
61
+ self.undefinedFlags = 0
62
+ if len(self.operands) == 3:
63
+ self.flags |= InstFlag.USE_OP3
64
+ elif len(self.operands) == 4:
65
+ self.flags |= InstFlag.USE_OP3 | InstFlag.USE_OP4
66
+ if isModRMIncluded:
67
+ self.flags |= InstFlag.MODRM_INCLUDED
68
+ # Does it use any of the VEX.vvvv field to describe an operand?
69
+ if len(filter(lambda x: x in [OperandType.VXMM, OperandType.VYMM, OperandType.VYXMM], self.operands)) == 0:
70
+ self.flags |= InstFlag.VEX_V_UNUSED
71
+ self.VEXtag = ""
72
+ # Special treatment for VEX instructions:
73
+ if self.flags & InstFlag.PRE_VEX:
74
+ # We assume that for now VEX.vvvv and MODRR are coupled in some instructions, it might not be the case in the future.
75
+ # VEX instructions have 3 options (only latter two can collide), so we will have to split them:
76
+ # 1) Normal VEX instruction is mapped to range 4 - 8 by default.
77
+ # 2) Instruction which is MODRR based and uses VEX.vvvv is mapped to range 4 - 8.
78
+ # 3) Instruction which is MODRR based and doesn't use VEX.vvvv is mapped to range 8 - 12.
79
+ self.entryNo += 4 # For VEX
80
+ self.VEXtag = "V"
81
+ if self.flags & (InstFlag.MODRR_BASED | InstFlag.VEX_V_UNUSED) == (InstFlag.MODRR_BASED | InstFlag.VEX_V_UNUSED):
82
+ self.entryNo += 4 # MODRR without VEX.VVVV
83
+ self.VEXtag += "RR"
84
+ # Assert that OL is < OL_33:
85
+ if self.OL >= OpcodeLength.OL_33:
86
+ raise DBException("Instruction OL is bigger than OL_33.")
87
+
88
+ class InstructionsTable:
89
+ """ A table contains all instructions under its index. The number of instructions varyies and depends on its type.
90
+ Note that a table be nested in itself.
91
+ Every table has its position beginning in the db.root.
92
+ So all opcodes that begin with first byte with the value of 0x0f, will be in the 0x0f table (which has a tag "0f"). """
93
+ Full = 256
94
+ Divided = 72
95
+ Group = 8
96
+ Prefixed = 12
97
+
98
+ def __init__(self, size, tag, pos):
99
+ self.list = {}
100
+ self.size = size
101
+ # The type of the table is determined by its actual size.
102
+ # Set up a limit number for the table, used in the iterator.
103
+ if size == self.Full:
104
+ self.type = NodeType.LIST_FULL
105
+ self.limit = self.Full
106
+ elif size == self.Divided:
107
+ # Since the ranges of Divided instructions are 0x0-0x7 and 0xc0-0xff, we limit it as a full sized table.
108
+ self.type = NodeType.LIST_DIVIDED
109
+ self.limit = self.Full
110
+ elif size == self.Group:
111
+ self.type = NodeType.LIST_GROUP
112
+ self.limit = size
113
+ elif size == self.Prefixed:
114
+ self.type = NodeType.LIST_PREFIXED
115
+ self.limit = size
116
+ self.tag = tag
117
+ self.pos = pos
118
+
119
+ def __iter__(self):
120
+ """ This is the "ctor" of the iterator. """
121
+ # Begin with the first opcode.
122
+ self.__iterIndex = -1
123
+ return self
124
+
125
+ def next(self):
126
+ """ This is the core of the iterator, return the next instruction or halt. """
127
+ # Get next instruction.
128
+ self.__iterIndex += 1
129
+ # Optimization: skip unused entries in Divided tables.
130
+ if self.type == NodeType.LIST_DIVIDED and self.__iterIndex == 8:
131
+ # Actually it must be done, because the iterator-user assumes Divided table is 72 entries long.
132
+ # So it happens that len(list(divided-table)) == 72 !
133
+ self.__iterIndex = 0xc0
134
+ # Stop the iteration in case we reached the limit.
135
+ if self.__iterIndex == self.limit:
136
+ raise StopIteration
137
+ # If we have the key return its corresponding opcode,
138
+ # it might be that we return an object of another nested InstructionTable as well.
139
+ if self.list.has_key(self.__iterIndex):
140
+ item = self.list[self.__iterIndex]
141
+ return item
142
+ # In case no InstructionInfo or InstructionsTable were found, return None (this doesn't stop the iteration!).
143
+ return None
144
+
145
+ class GenBlock:
146
+ """ There are some special instructions which have the operand encoded in the code byte itself.
147
+ For instance: 40: INC EAX 41: ECX. push/pop/dec, etc...
148
+ Therefore, these instructions can be treated specially in the tables, so instead of generating a unique instruction-info per such instruction.
149
+ We "cheat" by making some entries in the table point to the same instruction-info.
150
+ Following the last example, all instructions in the range of 0x40-0x47 point to the instruction-info 0x40, which means INC <REG-FROM-SAME-BYTE>.
151
+ This means that we don't call SetInstruction for the range 0x40-0x47, only a single set instruction per this block
152
+ (8 instructions which their REG field is extracted from their own byte code).
153
+ So in order to simulate the real case where there are actually 8 instructions that were set using SetInstruction,
154
+ this class handles this special flag and returns the same first instruction for its corresponding block at runtime. """
155
+
156
+ # Number of generated instructions in a row.
157
+ Block = 8
158
+
159
+ def __init__(self, list):
160
+ if isinstance(list, InstructionsTable) == False:
161
+ raise DBException("List must be InstructionsTable object")
162
+ self.list = list
163
+
164
+ def __iter__(self):
165
+ """ This is the "ctor" of the iterator. """
166
+ # Count up to Block instructions.
167
+ self.counter = 0
168
+ # This is the item we're going to return every iteration.
169
+ self.item = None
170
+ # Start the InstructionsTable internal iterator as well.
171
+ self.list.__iter__()
172
+ return self
173
+
174
+ def next(self):
175
+ # Get next item from internal iterator.
176
+ i = self.list.next()
177
+ # If there's an item set, it means we hit the special opcode before.
178
+ if self.item != None:
179
+ # Countup
180
+ self.counter += 1
181
+ # If we reached the limit, stop.
182
+ if self.counter == self.Block:
183
+ self.counter = 0
184
+ self.item = None
185
+ # See if the item we read is an opcode and whether it has the GEN_BLOCK.
186
+ if isinstance(i, InstructionInfo) and i.flags & InstFlag.GEN_BLOCK:
187
+ # Store this instruction for next 8 iterations.
188
+ self.item = i
189
+ return i
190
+ elif i == None and self.item != None:
191
+ # Return the stored item, only if there's no opcode set.
192
+ # Sometimes, there are GEN_BLOCK instructions and the next instruction is also set (means it has its own OpcodeInfo)
193
+ # so we have to return real instructions first, and then generated instructions.
194
+ return self.item
195
+ # Return the instruction we read from the real list.
196
+ return i
197
+
198
+ class InstructionsDB:
199
+ """ The Instructions Data Base holds all instructions under it.
200
+ The self.root is where all instructions begin, so instructions that are 1 byte long, will be set directly there.
201
+ But instructions that are 2 instructions long, will be set under another InstructionsTable nested inside the self.root.
202
+
203
+ The DB is actually the root of a Trie. (For more info about Trie see diStorm's instructions.h). """
204
+ def __init__(self):
205
+ # Root contains a Full InstructionsTable with 256 entries.
206
+ self.root = InstructionsTable(InstructionsTable.Full, "", [])
207
+ # Special instructions that diStorm has to use manually and can look them up using the tables.
208
+ self.exportedInstructions = []
209
+
210
+ def getExportedInstructions(self):
211
+ return self.exportedInstructions
212
+
213
+ def HandleMandatoryPrefix(self, type, o, pos, ii, tag):
214
+ if ii.prefixed:
215
+ ii.tag = "_%02X%s" % (ii.prefix, ii.tag)
216
+ if ii.flags & InstFlag.PRE_VEX:
217
+ ii.tag = "_%s%s" % (ii.VEXtag, ii.tag)
218
+ # If there is nothing at this index, create a prefixed table.
219
+ if o.list.has_key(pos[0]) == False:
220
+ o.list[pos[0]] = InstructionsTable(InstructionsTable.Prefixed, tag, "")
221
+ # If there's a table constructred already (doesn't matter if by last line).
222
+ if isinstance(o.list[pos[0]], InstructionsTable) and o.list[pos[0]].type == NodeType.LIST_PREFIXED:
223
+ # Check for obvious collision.
224
+ if o.list[pos[0]].list.has_key(ii.entryNo):
225
+ raise DBException("Collision in prefix table.")
226
+ # Link the instruction to its index.
227
+ o.list[pos[0]].list[ii.entryNo] = ii
228
+ # The index is taken and it's not a prefixed table, we will have to convert it then.
229
+ else:
230
+ # Store current instruction which we are going to replace with a table.
231
+ tmp = o.list[pos[0]]
232
+ # Check for collision first, if it's a prefixed instruction at all.
233
+ if (not ii.prefixed and ii.pos[0] != 0x0f) or (tmp.entryNo == ii.entryNo):
234
+ msg = "Instruction Collision: %s" % str(o.list[pos[0]])
235
+ raise DBException(msg)
236
+ # Create a prefixed table.
237
+ o.list[pos[0]] = InstructionsTable(InstructionsTable.Prefixed, tag, "")
238
+ # Link the previous instruction.
239
+ o.list[pos[0]].list[tmp.entryNo] = tmp
240
+ # Link new instruction.
241
+ o.list[pos[0]].list[ii.entryNo] = ii
242
+
243
+ def CreateSet(self, type, o, pos, ii, tag = "", level = 0):
244
+ """ This is the most improtant function in the whole project.
245
+ It builds and links a new InstructionsTable if required and
246
+ afterwards sets the given InstructionInfo object in its correct place.
247
+ It knows to generate the nested lists dynamically, building a Trie DB.
248
+
249
+ The algorithm for building the nested tables is as follows:
250
+ See if you got to the last byte code of the instruction, if so, link the instruction info and exit.
251
+ Try to enter the first index in the list, if it doesn't exist, create it.
252
+ If it exists, take off the first index from its array, (since we already entered it), and RECURSE with the new(/existing) list now.
253
+
254
+ In practice it's a bit more complex since there are 3 types of tables we can create, and we have to take care of it.
255
+ Let's see two examples of how it really works with the following input (assuming root is empty):
256
+ 0: OL_3, root, [0x67, 0x69, 0x6c], II_INST
257
+
258
+ 1: Create Table - with size of 256 at index 0x67
259
+ Recurse - OL_2, root[0x67], [0x69, 0x6c], II_INST
260
+
261
+ 2: Create Table - with size of 256 at index 0x69
262
+ Recurse - OL_1, root[0x67][0x69], [0x6c], II_INST
263
+
264
+ 3: Link Instruction Information - at index 0x6c, since type is OL_1
265
+ root[0x67][0x69][0x6c] = II_INST
266
+ exit
267
+
268
+ Second example:
269
+ 0: OL_23, root, [0x0f, 0xb0, 0x03], II_INST2
270
+
271
+ 1: Create Table - with size of 256 at index 0x0f
272
+ Recurse - OL_13, root[0x0f], [0xb0, 0x03], II_INST2
273
+
274
+ 2: Create Table - with size of 8(GROUP) at index 0xb0, since input type is OL_13
275
+ Recurse - OL_1, root[0x0f][0xb0], [0x03], II_INST2
276
+
277
+ 3: Link Instruction Information - at index 0x03, since type is OL_1
278
+ root[0x0f][0xb0][0x03] = II_INST2
279
+ exit
280
+
281
+ Every table we create is usually a Full sized table (256 entries), since it can point to next 256 instructions.
282
+ If the input type is OL_13 or OL_1d we know we have to create a Group sized table or Divided sized table, correspondingly.
283
+ OL_13/OL_1d means its the last table to build in the sequence of byte codes of the given instruction.
284
+
285
+ OL_1 always means that we just have to link the instruction information and that all tables are built already.
286
+ Therefore the "next" of OL_13/OL_1d is always OL_1.
287
+
288
+ Special case for mandatory prefixed instructions:
289
+ If the instruction's first opcode byte is a mandatory prefix (0x66, 0xf2, 0xf3), then we will skip it in the root.
290
+ However, it will be set in the same table of that instruction without the prefix byte.
291
+ Therefore if there are a few instructions that the only difference among them is the mandatory prefix byte,
292
+ they will share a special table. This "PREFIXED" table points to the Instruction Information of those possible instructions.
293
+ Also the information for the same instruction without any mandatory prefix will be stored in this table.
294
+ Entries order: None, 0x66, 0xf2, 0xf3.
295
+
296
+ Example: [0x0f, 0x2a], ["CVTPI2PS"]
297
+ [0x66, 0x0f, 0x2a], ["CVTPI2PD"]
298
+ [0xf3, 0x0f, 0x2a], ["CVTSI2SS"]
299
+
300
+ When there is a collision with the same instruction, we will know to change it into a PREFIXED table.
301
+ """
302
+
303
+ # Keep track of the index we scan.
304
+ tag += "_%02X" % pos[0]
305
+ # If the type is OL_1, it means we have to set the InstructionInfo in o.list, which is the last table we need.
306
+ if type == OpcodeLength.OL_1:
307
+ # Set the tag (position formatted in text) of the Instruction itself.
308
+ ii.tag = tag
309
+ # If the instruction is already set, there's something wrong with the DB initializer,
310
+ # probably a collision inside x86sets.py.
311
+ if ii.prefixed:
312
+ self.HandleMandatoryPrefix(type, o, pos, ii, tag)
313
+ return
314
+ if o.list.has_key(pos[0]) == True:
315
+ self.HandleMandatoryPrefix(type, o, pos, ii, tag)
316
+ return
317
+ # Link the instruction info in its place.
318
+ o.list[pos[0]] = ii
319
+ # Stop recursion.
320
+ return
321
+ # See whether we have to create a nested table.
322
+ if o.list.has_key(pos[0]) == False:
323
+ # All tables are full sized.
324
+ tableType = InstructionsTable.Full
325
+ if type == OpcodeLength.OL_13:
326
+ # Except 1.3 - Create a Group table.
327
+ tableType = InstructionsTable.Group
328
+ elif type == OpcodeLength.OL_1d:
329
+ # And except 1.d - Create a Divided table.
330
+ tableType = InstructionsTable.Divided
331
+ # Create and link the new table at the same time in its place.
332
+ o.list[pos[0]] = InstructionsTable(tableType, tag, ii.pos[:-1])
333
+ # This is the tricky recursive call,
334
+ # 1) Get the next OL we need, so we know what to do next, place an instruction info, or create another table.
335
+ # 2) Return the current table, which must be created, because it's either was just created or exists from before.
336
+ # 3) Since pos is a list with the indexes of the instruction, and we just used pos[0], we move to the next indexes,
337
+ # by removing the first item.
338
+ # 4) The instruction info to set when we reach its place.
339
+ # 5) The updated tag with the indexes of the instruction.
340
+ self.CreateSet(OpcodeLength.NextOL[type], o.list[pos[0]], pos[1:], ii, tag, level + 1)
341
+
342
+ def SetInstruction(self, *args):
343
+ """ This function is used in order to insert an instruction info into the DB. """
344
+ if (args[4] & InstFlag.EXPORTED) != 0:
345
+ ii = InstructionInfo(args[0], OpcodeLength.OL_1, [0], False, args[2], args[3], args[4])
346
+ self.exportedInstructions.append(ii)
347
+ return
348
+ # *args = ISetClass, OL, pos, mnemonics, operands, flags
349
+ # Construct an Instruction Info object with the info given in args.
350
+ opcode = args[1].replace(" ", "").split(",")
351
+ # The number of bytes is the base length, now we need to check the last entry.
352
+ pos = [int(i[:2], 16) for i in opcode]
353
+ last = opcode[-1][2:] # Skip hex of last full byte
354
+ isModRMIncluded = False # Indicates whether 3 bits of the REG field in the ModRM byte were used.
355
+ if last[:2] == "//": # Divided Instruction
356
+ pos.append(int(last[2:], 16))
357
+ isModRMIncluded = True
358
+ try:
359
+ OL = {1:OpcodeLength.OL_1d, 2:OpcodeLength.OL_2d}[len(opcode)]
360
+ except KeyError:
361
+ raise DBException("Invalid divided instruction opcode")
362
+ elif last[:1] == "/": # Group Instruction
363
+ isModRMIncluded = True
364
+ pos.append(int(last[1:], 16))
365
+ try:
366
+ OL = {1:OpcodeLength.OL_13, 2:OpcodeLength.OL_23, 3:OpcodeLength.OL_33}[len(opcode)]
367
+ except KeyError:
368
+ raise DBException("Invalid group instruction opcode")
369
+ elif len(last) != 0:
370
+ raise DBException("Invalid last byte in opcode")
371
+ # Normal full bytes instruction
372
+ else:
373
+ try:
374
+ OL = {1:OpcodeLength.OL_1, 2:OpcodeLength.OL_2, 3:OpcodeLength.OL_3, 4:OpcodeLength.OL_4}[len(opcode)]
375
+ except KeyError:
376
+ raise DBException("Invalid normal instruction opcode")
377
+ ii = InstructionInfo(args[0], OL, pos, isModRMIncluded, args[2], args[3], args[4])
378
+ # Insert the instruction into the table, take care of nested tables, etc...
379
+ self.CreateSet(ii.OL, self.root, ii.pos, ii)
380
+
381
+ def GenerateTables(self, filter):
382
+ """ GenerateTables is a generator function that iterates over an InstructionsTable,
383
+ it returns all nested tables in the DB.
384
+ The tables are returned in BFS order!
385
+ If you pass a filter, that filter will be called for every table and
386
+ should return True for letting the generator return it. """
387
+ # Start with the root, if no list was specified.
388
+ list = self.root
389
+ list.tag = "ROOT"
390
+ # Return the root first.
391
+ stack = [list]
392
+ while len(stack) > 0:
393
+ list = stack.pop(0)
394
+ yield list
395
+ for i in list:
396
+ if isinstance(i, InstructionsTable):
397
+ if filter is not None:
398
+ # If a filter is set, call it with the table.
399
+ if filter(i):
400
+ # If the return value was True, return this same InstructionInfo.
401
+ stack.append(i)
402
+ else:
403
+ # If no filter was set, just push this table.
404
+ stack.append(i)
@@ -0,0 +1,247 @@
1
+ #
2
+ # x86header.py
3
+ #
4
+ # Copyright (C) 2009 Gil Dabah, http://ragestorm.net/disops/
5
+ #
6
+
7
+ class OperandType:
8
+ """ Types of possible operands in an opcode.
9
+ Refer to the diStorm's documentation or diStorm's instructions.h
10
+ for more explanation about every one of them. """
11
+ (NONE,
12
+ IMM8,
13
+ IMM16,
14
+ IMM_FULL,
15
+ IMM32,
16
+ SEIMM8,
17
+ IMM16_1, # NEW
18
+ IMM8_1, # NEW
19
+ IMM8_2, # NEW
20
+ REG8,
21
+ REG16,
22
+ REG_FULL,
23
+ REG32,
24
+ REG32_64,
25
+ FREG32_64_RM,
26
+ RM8,
27
+ RM16,
28
+ RM_FULL,
29
+ RM32_64,
30
+ RM16_32,
31
+ FPUM16,
32
+ FPUM32,
33
+ FPUM64,
34
+ FPUM80,
35
+ R32_M8,
36
+ R32_M16,
37
+ R32_64_M8,
38
+ R32_64_M16,
39
+ RFULL_M16,
40
+ CREG,
41
+ DREG,
42
+ SREG,
43
+ SEG,
44
+ ACC8,
45
+ ACC16,
46
+ ACC_FULL,
47
+ ACC_FULL_NOT64,
48
+ MEM16_FULL,
49
+ PTR16_FULL,
50
+ MEM16_3264,
51
+ RELCB,
52
+ RELC_FULL,
53
+ MEM,
54
+ MEM_OPT, # NEW
55
+ MEM32,
56
+ MEM32_64, # NEW
57
+ MEM64,
58
+ MEM128,
59
+ MEM64_128,
60
+ MOFFS8,
61
+ MOFFS_FULL,
62
+ CONST1,
63
+ REGCL,
64
+ IB_RB,
65
+ IB_R_FULL,
66
+ REGI_ESI,
67
+ REGI_EDI,
68
+ REGI_EBXAL,
69
+ REGI_EAX,
70
+ REGDX,
71
+ REGECX,
72
+ FPU_SI,
73
+ FPU_SSI,
74
+ FPU_SIS,
75
+ MM,
76
+ MM_RM,
77
+ MM32,
78
+ MM64,
79
+ XMM,
80
+ XMM_RM,
81
+ XMM16,
82
+ XMM32,
83
+ XMM64,
84
+ XMM128,
85
+ REGXMM0,
86
+ # Below new for AVX:
87
+ RM32,
88
+ REG32_64_M8,
89
+ REG32_64_M16,
90
+ WREG32_64,
91
+ WRM32_64,
92
+ WXMM32_64,
93
+ VXMM,
94
+ XMM_IMM,
95
+ YXMM,
96
+ YXMM_IMM,
97
+ YMM,
98
+ YMM256,
99
+ VYMM,
100
+ VYXMM,
101
+ YXMM64_256,
102
+ YXMM128_256,
103
+ LXMM64_128,
104
+ LMEM128_256) = range(93)
105
+
106
+ class OpcodeLength:
107
+ """ The length of the opcode in bytes.
108
+ Where a suffix of '3' means we have to read the REG field of the ModR/M byte (REG size is 3 bits).
109
+ Suffix of 'd' means it's a Divided instruction (see documentation),
110
+ tells the disassembler to read the REG field or the whole next byte.
111
+
112
+ OL_33 and OL_4 are used in raw opcode bytes, they include the mandatory prefix,
113
+ therefore when they are defined in the instruction tables, the mandatory prefix table is added,
114
+ and they become OL_23 and OL_3 correspondingly. There is no effective opcode which is more than 3 bytes. """
115
+ (OL_1, # 0
116
+ OL_13, # 1
117
+ OL_1d, # 2 - Can be prefixed (only by WAIT/9b)
118
+ OL_2, # 3 - Can be prefixed
119
+ OL_23, # 4 - Can be prefixed
120
+ OL_2d, # 5
121
+ OL_3, # 6 - Can be prefixed
122
+ OL_33, # 7 - Internal only
123
+ OL_4 # 8 - Internal only
124
+ ) = range(9)
125
+
126
+ """ Next-Opcode-Length dictionary is used in order to recursively build the instructions' tables dynamically.
127
+ It is used in such a way that it indicates how many more nested tables
128
+ we have to build and link starting from a given OL. """
129
+ NextOL = {OL_13: OL_1, OL_1d: OL_1, OL_2: OL_1, OL_23: OL_13,
130
+ OL_2d: OL_1d, OL_3: OL_2, OL_33: OL_23, OL_4: OL_3}
131
+
132
+ class InstFlag:
133
+ """ Instruction Flag contains all bit mask constants for describing an instruction.
134
+ You can bitwise-or the flags. See diStorm's documentation for more explanation.
135
+
136
+ The GEN_BLOCK is a special flag, it is used in the tables generator only;
137
+ See GenBlock class inside x86db.py. """
138
+ FLAGS_EX_START_INDEX = 32
139
+ INST_FLAGS_NONE = 0
140
+ (MODRM_REQUIRED, # 0
141
+ NOT_DIVIDED, # 1
142
+ _16BITS, # 2
143
+ _32BITS, # 3
144
+ PRE_LOCK, # 4
145
+ PRE_REPNZ, # 5
146
+ PRE_REP, # 6
147
+ PRE_CS, # 7
148
+ PRE_SS, # 8
149
+ PRE_DS, # 9
150
+ PRE_ES, # 10
151
+ PRE_FS, # 11
152
+ PRE_GS, # 12
153
+ PRE_OP_SIZE, # 13
154
+ PRE_ADDR_SIZE, # 14
155
+ NATIVE, # 15
156
+ USE_EXMNEMONIC, # 16
157
+ USE_OP3, # 17
158
+ USE_OP4, # 18
159
+ MNEMONIC_MODRM_BASED, # 19
160
+ MODRR_REQUIRED, # 20
161
+ _3DNOW_FETCH, # 21
162
+ PSEUDO_OPCODE, # 22
163
+ INVALID_64BITS, # 23
164
+ _64BITS, # 24
165
+ PRE_REX, # 25
166
+ USE_EXMNEMONIC2, # 26
167
+ _64BITS_FETCH, # 27
168
+ FORCE_REG0, # 28
169
+ PRE_VEX, # 29
170
+ MODRM_INCLUDED, # 30
171
+ DST_WR, # 31
172
+ VEX_L, # 32 From here on: flagsEx.
173
+ VEX_W, # 33
174
+ MNEMONIC_VEXW_BASED, # 34
175
+ MNEMONIC_VEXL_BASED, # 35
176
+ FORCE_VEXL, # 36
177
+ MODRR_BASED, # 37
178
+ VEX_V_UNUSED, # 38
179
+ GEN_BLOCK, # 39 From here on: internal to disOps.
180
+ EXPORTED # 40
181
+ ) = [1 << i for i in xrange(41)]
182
+ # Nodes are extended if they have any of the following flags:
183
+ EXTENDED = (PRE_VEX | USE_EXMNEMONIC | USE_EXMNEMONIC2 | USE_OP3 | USE_OP4)
184
+ SEGMENTS = (PRE_CS | PRE_SS | PRE_DS | PRE_ES | PRE_FS | PRE_FS)
185
+
186
+ class ISetClass:
187
+ """ Instruction-Set-Class indicates to which set the instruction belongs.
188
+ These types are taken from the documentation of Intel/AMD. """
189
+ (INTEGER,
190
+ FPU,
191
+ P6,
192
+ MMX,
193
+ SSE,
194
+ SSE2,
195
+ SSE3,
196
+ SSSE3,
197
+ SSE4_1,
198
+ SSE4_2,
199
+ SSE4_A,
200
+ _3DNOW,
201
+ _3DNOWEXT,
202
+ VMX,
203
+ SVM,
204
+ AVX,
205
+ FMA,
206
+ CLMUL,
207
+ AES) = range(1, 20)
208
+
209
+ class FlowControl:
210
+ """ The flow control instruction will be flagged in the lo nibble of the 'meta' field in _InstInfo of diStorm.
211
+ They are used to distinguish between flow control instructions (such as: ret, call, jmp, jz, etc) to normal ones. """
212
+ (CALL,
213
+ RET,
214
+ SYS,
215
+ UNC_BRANCH,
216
+ CND_BRANCH,
217
+ INT,
218
+ CMOV) = range(1, 8)
219
+
220
+ class NodeType:
221
+ """ A node can really be an object holder for an instruction-info object or
222
+ another table (list) with a different size.
223
+
224
+ GROUP - 8 entries in the table
225
+ FULL - 256 entries in the table.
226
+ Divided - 72 entries in the table (ranges: 0x0-0x7, 0xc0-0xff).
227
+ Prefixed - 12 entries in the table (none, 0x66, 0xf2, 0xf3). """
228
+ (NONE, # 0
229
+ INFO, # 1
230
+ INFOEX, # 2
231
+ LIST_GROUP, # 3
232
+ LIST_FULL, # 4
233
+ LIST_DIVIDED, # 5
234
+ LIST_PREFIXED # 6
235
+ ) = range(0, 7)
236
+
237
+ class CPUFlags:
238
+ """ Specifies all the flags that the x86/x64 CPU supports, in a special compact order. """
239
+ (CF, # 0
240
+ IF, # 1
241
+ PF, # 2
242
+ DF, # 3
243
+ AF, # 4
244
+ OF, # 5
245
+ ZF, # 6
246
+ SF # 7
247
+ ) = [1 << i for i in xrange(8)]