librex 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (434) hide show
  1. data/lib/rex/LICENSE +29 -0
  2. data/lib/rex/arch.rb +103 -0
  3. data/lib/rex/arch/sparc.rb +75 -0
  4. data/lib/rex/arch/sparc.rb.ut.rb +18 -0
  5. data/lib/rex/arch/x86.rb +513 -0
  6. data/lib/rex/arch/x86.rb.ut.rb +93 -0
  7. data/lib/rex/assembly/nasm.rb +104 -0
  8. data/lib/rex/assembly/nasm.rb.ut.rb +22 -0
  9. data/lib/rex/codepage.map +104 -0
  10. data/lib/rex/compat.rb +311 -0
  11. data/lib/rex/constants.rb +113 -0
  12. data/lib/rex/elfparsey.rb +11 -0
  13. data/lib/rex/elfparsey/elf.rb +123 -0
  14. data/lib/rex/elfparsey/elfbase.rb +258 -0
  15. data/lib/rex/elfparsey/exceptions.rb +27 -0
  16. data/lib/rex/elfscan.rb +12 -0
  17. data/lib/rex/elfscan/scanner.rb +207 -0
  18. data/lib/rex/elfscan/search.rb +46 -0
  19. data/lib/rex/encoder/alpha2.rb +31 -0
  20. data/lib/rex/encoder/alpha2/alpha_mixed.rb +68 -0
  21. data/lib/rex/encoder/alpha2/alpha_upper.rb +79 -0
  22. data/lib/rex/encoder/alpha2/generic.rb +114 -0
  23. data/lib/rex/encoder/alpha2/unicode_mixed.rb +117 -0
  24. data/lib/rex/encoder/alpha2/unicode_upper.rb +129 -0
  25. data/lib/rex/encoder/ndr.rb +89 -0
  26. data/lib/rex/encoder/ndr.rb.ut.rb +44 -0
  27. data/lib/rex/encoder/nonalpha.rb +61 -0
  28. data/lib/rex/encoder/nonupper.rb +64 -0
  29. data/lib/rex/encoder/xdr.rb +106 -0
  30. data/lib/rex/encoder/xdr.rb.ut.rb +29 -0
  31. data/lib/rex/encoder/xor.rb +69 -0
  32. data/lib/rex/encoder/xor/dword.rb +13 -0
  33. data/lib/rex/encoder/xor/dword_additive.rb +13 -0
  34. data/lib/rex/encoders/xor_dword.rb +35 -0
  35. data/lib/rex/encoders/xor_dword_additive.rb +53 -0
  36. data/lib/rex/encoders/xor_dword_additive.rb.ut.rb +12 -0
  37. data/lib/rex/encoding/xor.rb +20 -0
  38. data/lib/rex/encoding/xor.rb.ts.rb +14 -0
  39. data/lib/rex/encoding/xor/byte.rb +15 -0
  40. data/lib/rex/encoding/xor/byte.rb.ut.rb +21 -0
  41. data/lib/rex/encoding/xor/dword.rb +21 -0
  42. data/lib/rex/encoding/xor/dword.rb.ut.rb +15 -0
  43. data/lib/rex/encoding/xor/dword_additive.rb +92 -0
  44. data/lib/rex/encoding/xor/dword_additive.rb.ut.rb +15 -0
  45. data/lib/rex/encoding/xor/exceptions.rb +17 -0
  46. data/lib/rex/encoding/xor/generic.rb +146 -0
  47. data/lib/rex/encoding/xor/generic.rb.ut.rb +120 -0
  48. data/lib/rex/encoding/xor/qword.rb +15 -0
  49. data/lib/rex/encoding/xor/word.rb +21 -0
  50. data/lib/rex/encoding/xor/word.rb.ut.rb +13 -0
  51. data/lib/rex/exceptions.rb +275 -0
  52. data/lib/rex/exceptions.rb.ut.rb +44 -0
  53. data/lib/rex/exploitation/cmdstager.rb +9 -0
  54. data/lib/rex/exploitation/cmdstager/base.rb +175 -0
  55. data/lib/rex/exploitation/cmdstager/debug_asm.rb +142 -0
  56. data/lib/rex/exploitation/cmdstager/debug_write.rb +136 -0
  57. data/lib/rex/exploitation/cmdstager/tftp.rb +63 -0
  58. data/lib/rex/exploitation/cmdstager/vbs.rb +128 -0
  59. data/lib/rex/exploitation/egghunter.rb +277 -0
  60. data/lib/rex/exploitation/egghunter.rb.ut.rb +25 -0
  61. data/lib/rex/exploitation/encryptjs.rb +77 -0
  62. data/lib/rex/exploitation/heaplib.js.b64 +331 -0
  63. data/lib/rex/exploitation/heaplib.rb +94 -0
  64. data/lib/rex/exploitation/javascriptosdetect.rb +897 -0
  65. data/lib/rex/exploitation/obfuscatejs.rb +335 -0
  66. data/lib/rex/exploitation/omelet.rb +320 -0
  67. data/lib/rex/exploitation/omelet.rb.ut.rb +13 -0
  68. data/lib/rex/exploitation/opcodedb.rb +818 -0
  69. data/lib/rex/exploitation/opcodedb.rb.ut.rb +279 -0
  70. data/lib/rex/exploitation/seh.rb +92 -0
  71. data/lib/rex/exploitation/seh.rb.ut.rb +19 -0
  72. data/lib/rex/file.rb +112 -0
  73. data/lib/rex/file.rb.ut.rb +16 -0
  74. data/lib/rex/image_source.rb +12 -0
  75. data/lib/rex/image_source/disk.rb +60 -0
  76. data/lib/rex/image_source/image_source.rb +46 -0
  77. data/lib/rex/image_source/memory.rb +37 -0
  78. data/lib/rex/io/bidirectional_pipe.rb +157 -0
  79. data/lib/rex/io/datagram_abstraction.rb +35 -0
  80. data/lib/rex/io/ring_buffer.rb +364 -0
  81. data/lib/rex/io/stream.rb +319 -0
  82. data/lib/rex/io/stream_abstraction.rb +197 -0
  83. data/lib/rex/io/stream_server.rb +211 -0
  84. data/lib/rex/job_container.rb +187 -0
  85. data/lib/rex/logging.rb +4 -0
  86. data/lib/rex/logging/log_dispatcher.rb +179 -0
  87. data/lib/rex/logging/log_sink.rb +42 -0
  88. data/lib/rex/logging/sinks/flatfile.rb +55 -0
  89. data/lib/rex/logging/sinks/stderr.rb +43 -0
  90. data/lib/rex/machparsey.rb +9 -0
  91. data/lib/rex/machparsey/exceptions.rb +34 -0
  92. data/lib/rex/machparsey/mach.rb +209 -0
  93. data/lib/rex/machparsey/machbase.rb +408 -0
  94. data/lib/rex/machscan.rb +9 -0
  95. data/lib/rex/machscan/scanner.rb +217 -0
  96. data/lib/rex/mime.rb +9 -0
  97. data/lib/rex/mime/header.rb +77 -0
  98. data/lib/rex/mime/message.rb +144 -0
  99. data/lib/rex/mime/part.rb +20 -0
  100. data/lib/rex/nop/opty2.rb +108 -0
  101. data/lib/rex/nop/opty2.rb.ut.rb +23 -0
  102. data/lib/rex/nop/opty2_tables.rb +300 -0
  103. data/lib/rex/ole.rb +205 -0
  104. data/lib/rex/ole/clsid.rb +47 -0
  105. data/lib/rex/ole/difat.rb +141 -0
  106. data/lib/rex/ole/directory.rb +231 -0
  107. data/lib/rex/ole/direntry.rb +240 -0
  108. data/lib/rex/ole/docs/dependencies.txt +8 -0
  109. data/lib/rex/ole/docs/references.txt +1 -0
  110. data/lib/rex/ole/fat.rb +99 -0
  111. data/lib/rex/ole/header.rb +204 -0
  112. data/lib/rex/ole/minifat.rb +77 -0
  113. data/lib/rex/ole/propset.rb +144 -0
  114. data/lib/rex/ole/samples/create_ole.rb +27 -0
  115. data/lib/rex/ole/samples/dir.rb +35 -0
  116. data/lib/rex/ole/samples/dump_stream.rb +34 -0
  117. data/lib/rex/ole/samples/ole_info.rb +23 -0
  118. data/lib/rex/ole/storage.rb +395 -0
  119. data/lib/rex/ole/stream.rb +53 -0
  120. data/lib/rex/ole/substorage.rb +49 -0
  121. data/lib/rex/ole/util.rb +157 -0
  122. data/lib/rex/parser/arguments.rb +97 -0
  123. data/lib/rex/parser/arguments.rb.ut.rb +67 -0
  124. data/lib/rex/parser/ini.rb +185 -0
  125. data/lib/rex/parser/ini.rb.ut.rb +29 -0
  126. data/lib/rex/parser/ip360_aspl_xml.rb +102 -0
  127. data/lib/rex/parser/ip360_xml.rb +97 -0
  128. data/lib/rex/parser/nessus_xml.rb +118 -0
  129. data/lib/rex/parser/netsparker_xml.rb +94 -0
  130. data/lib/rex/parser/nexpose_xml.rb +136 -0
  131. data/lib/rex/parser/nmap_xml.rb +137 -0
  132. data/lib/rex/parser/retina_xml.rb +109 -0
  133. data/lib/rex/payloads.rb +1 -0
  134. data/lib/rex/payloads/win32.rb +2 -0
  135. data/lib/rex/payloads/win32/common.rb +26 -0
  136. data/lib/rex/payloads/win32/kernel.rb +53 -0
  137. data/lib/rex/payloads/win32/kernel/common.rb +54 -0
  138. data/lib/rex/payloads/win32/kernel/migration.rb +12 -0
  139. data/lib/rex/payloads/win32/kernel/recovery.rb +50 -0
  140. data/lib/rex/payloads/win32/kernel/stager.rb +194 -0
  141. data/lib/rex/peparsey.rb +12 -0
  142. data/lib/rex/peparsey/exceptions.rb +32 -0
  143. data/lib/rex/peparsey/pe.rb +212 -0
  144. data/lib/rex/peparsey/pe_memdump.rb +63 -0
  145. data/lib/rex/peparsey/pebase.rb +1680 -0
  146. data/lib/rex/peparsey/section.rb +136 -0
  147. data/lib/rex/pescan.rb +13 -0
  148. data/lib/rex/pescan/analyze.rb +309 -0
  149. data/lib/rex/pescan/scanner.rb +206 -0
  150. data/lib/rex/pescan/search.rb +56 -0
  151. data/lib/rex/platforms.rb +1 -0
  152. data/lib/rex/platforms/windows.rb +51 -0
  153. data/lib/rex/poly.rb +132 -0
  154. data/lib/rex/poly/block.rb +477 -0
  155. data/lib/rex/poly/register.rb +100 -0
  156. data/lib/rex/poly/register/x86.rb +40 -0
  157. data/lib/rex/post.rb +8 -0
  158. data/lib/rex/post/dir.rb +51 -0
  159. data/lib/rex/post/file.rb +172 -0
  160. data/lib/rex/post/file_stat.rb +220 -0
  161. data/lib/rex/post/gen.pl +13 -0
  162. data/lib/rex/post/io.rb +182 -0
  163. data/lib/rex/post/meterpreter.rb +4 -0
  164. data/lib/rex/post/meterpreter/channel.rb +445 -0
  165. data/lib/rex/post/meterpreter/channel_container.rb +54 -0
  166. data/lib/rex/post/meterpreter/channels/pool.rb +160 -0
  167. data/lib/rex/post/meterpreter/channels/pools/file.rb +62 -0
  168. data/lib/rex/post/meterpreter/channels/pools/stream_pool.rb +103 -0
  169. data/lib/rex/post/meterpreter/channels/stream.rb +87 -0
  170. data/lib/rex/post/meterpreter/client.rb +364 -0
  171. data/lib/rex/post/meterpreter/client_core.rb +274 -0
  172. data/lib/rex/post/meterpreter/dependencies.rb +3 -0
  173. data/lib/rex/post/meterpreter/extension.rb +32 -0
  174. data/lib/rex/post/meterpreter/extensions/espia/espia.rb +58 -0
  175. data/lib/rex/post/meterpreter/extensions/espia/tlv.rb +16 -0
  176. data/lib/rex/post/meterpreter/extensions/incognito/incognito.rb +94 -0
  177. data/lib/rex/post/meterpreter/extensions/incognito/tlv.rb +21 -0
  178. data/lib/rex/post/meterpreter/extensions/networkpug/networkpug.rb +57 -0
  179. data/lib/rex/post/meterpreter/extensions/networkpug/tlv.rb +15 -0
  180. data/lib/rex/post/meterpreter/extensions/priv/fs.rb +118 -0
  181. data/lib/rex/post/meterpreter/extensions/priv/passwd.rb +61 -0
  182. data/lib/rex/post/meterpreter/extensions/priv/priv.rb +111 -0
  183. data/lib/rex/post/meterpreter/extensions/priv/tlv.rb +28 -0
  184. data/lib/rex/post/meterpreter/extensions/sniffer/sniffer.rb +101 -0
  185. data/lib/rex/post/meterpreter/extensions/sniffer/tlv.rb +26 -0
  186. data/lib/rex/post/meterpreter/extensions/stdapi/constants.rb +333 -0
  187. data/lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb +282 -0
  188. data/lib/rex/post/meterpreter/extensions/stdapi/fs/file.rb +266 -0
  189. data/lib/rex/post/meterpreter/extensions/stdapi/fs/file_stat.rb +103 -0
  190. data/lib/rex/post/meterpreter/extensions/stdapi/fs/io.rb +48 -0
  191. data/lib/rex/post/meterpreter/extensions/stdapi/net/config.rb +144 -0
  192. data/lib/rex/post/meterpreter/extensions/stdapi/net/interface.rb +73 -0
  193. data/lib/rex/post/meterpreter/extensions/stdapi/net/route.rb +56 -0
  194. data/lib/rex/post/meterpreter/extensions/stdapi/net/socket.rb +137 -0
  195. data/lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_client_channel.rb +180 -0
  196. data/lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_server_channel.rb +167 -0
  197. data/lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/udp_channel.rb +208 -0
  198. data/lib/rex/post/meterpreter/extensions/stdapi/railgun.rb.ts.rb +6 -0
  199. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/api_constants.rb +38106 -0
  200. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/api_constants.rb.ut.rb +31 -0
  201. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/buffer_item.rb +47 -0
  202. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/buffer_item.rb.ut.rb +36 -0
  203. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/def/def_advapi32.rb +1818 -0
  204. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/def/def_iphlpapi.rb +96 -0
  205. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/def/def_kernel32.rb +3848 -0
  206. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/def/def_netapi32.rb +26 -0
  207. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/def/def_ntdll.rb +153 -0
  208. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/def/def_shell32.rb +21 -0
  209. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/def/def_user32.rb +3169 -0
  210. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/def/def_ws2_32.rb +599 -0
  211. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/dll.rb +318 -0
  212. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/dll_function.rb +100 -0
  213. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/dll_function.rb.ut.rb +42 -0
  214. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/dll_helper.rb +148 -0
  215. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/dll_helper.rb.ut.rb +127 -0
  216. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/multicall.rb +309 -0
  217. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb +204 -0
  218. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/tlv.rb +51 -0
  219. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/util.rb +630 -0
  220. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/win_const_manager.rb +75 -0
  221. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/win_const_manager.rb.ut.rb +103 -0
  222. data/lib/rex/post/meterpreter/extensions/stdapi/stdapi.rb +149 -0
  223. data/lib/rex/post/meterpreter/extensions/stdapi/sys/config.rb +97 -0
  224. data/lib/rex/post/meterpreter/extensions/stdapi/sys/event_log.rb +192 -0
  225. data/lib/rex/post/meterpreter/extensions/stdapi/sys/event_log_subsystem/event_record.rb +41 -0
  226. data/lib/rex/post/meterpreter/extensions/stdapi/sys/power.rb +61 -0
  227. data/lib/rex/post/meterpreter/extensions/stdapi/sys/process.rb +370 -0
  228. data/lib/rex/post/meterpreter/extensions/stdapi/sys/process_subsystem/image.rb +129 -0
  229. data/lib/rex/post/meterpreter/extensions/stdapi/sys/process_subsystem/io.rb +55 -0
  230. data/lib/rex/post/meterpreter/extensions/stdapi/sys/process_subsystem/memory.rb +336 -0
  231. data/lib/rex/post/meterpreter/extensions/stdapi/sys/process_subsystem/thread.rb +141 -0
  232. data/lib/rex/post/meterpreter/extensions/stdapi/sys/registry.rb +279 -0
  233. data/lib/rex/post/meterpreter/extensions/stdapi/sys/registry_subsystem/registry_key.rb +193 -0
  234. data/lib/rex/post/meterpreter/extensions/stdapi/sys/registry_subsystem/registry_value.rb +102 -0
  235. data/lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb +180 -0
  236. data/lib/rex/post/meterpreter/extensions/stdapi/tlv.rb +211 -0
  237. data/lib/rex/post/meterpreter/extensions/stdapi/ui.rb +227 -0
  238. data/lib/rex/post/meterpreter/extensions/stdapi/webcam/webcam.rb +63 -0
  239. data/lib/rex/post/meterpreter/inbound_packet_handler.rb +30 -0
  240. data/lib/rex/post/meterpreter/object_aliases.rb +83 -0
  241. data/lib/rex/post/meterpreter/packet.rb +688 -0
  242. data/lib/rex/post/meterpreter/packet_dispatcher.rb +431 -0
  243. data/lib/rex/post/meterpreter/packet_parser.rb +94 -0
  244. data/lib/rex/post/meterpreter/packet_response_waiter.rb +83 -0
  245. data/lib/rex/post/meterpreter/ui/console.rb +137 -0
  246. data/lib/rex/post/meterpreter/ui/console/command_dispatcher.rb +62 -0
  247. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/core.rb +730 -0
  248. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/espia.rb +108 -0
  249. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/incognito.rb +241 -0
  250. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb +231 -0
  251. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/priv.rb +61 -0
  252. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/priv/elevate.rb +98 -0
  253. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/priv/passwd.rb +51 -0
  254. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/priv/timestomp.rb +132 -0
  255. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb +187 -0
  256. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi.rb +65 -0
  257. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb +442 -0
  258. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb +298 -0
  259. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb +486 -0
  260. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/ui.rb +315 -0
  261. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/webcam.rb +157 -0
  262. data/lib/rex/post/meterpreter/ui/console/interactive_channel.rb +95 -0
  263. data/lib/rex/post/permission.rb +26 -0
  264. data/lib/rex/post/process.rb +57 -0
  265. data/lib/rex/post/thread.rb +57 -0
  266. data/lib/rex/post/ui.rb +52 -0
  267. data/lib/rex/proto.rb +13 -0
  268. data/lib/rex/proto.rb.ts.rb +8 -0
  269. data/lib/rex/proto/dcerpc.rb +6 -0
  270. data/lib/rex/proto/dcerpc.rb.ts.rb +9 -0
  271. data/lib/rex/proto/dcerpc/client.rb +361 -0
  272. data/lib/rex/proto/dcerpc/client.rb.ut.rb +491 -0
  273. data/lib/rex/proto/dcerpc/exceptions.rb +150 -0
  274. data/lib/rex/proto/dcerpc/handle.rb +47 -0
  275. data/lib/rex/proto/dcerpc/handle.rb.ut.rb +85 -0
  276. data/lib/rex/proto/dcerpc/ndr.rb +72 -0
  277. data/lib/rex/proto/dcerpc/ndr.rb.ut.rb +41 -0
  278. data/lib/rex/proto/dcerpc/packet.rb +253 -0
  279. data/lib/rex/proto/dcerpc/packet.rb.ut.rb +56 -0
  280. data/lib/rex/proto/dcerpc/response.rb +187 -0
  281. data/lib/rex/proto/dcerpc/response.rb.ut.rb +15 -0
  282. data/lib/rex/proto/dcerpc/uuid.rb +84 -0
  283. data/lib/rex/proto/dcerpc/uuid.rb.ut.rb +46 -0
  284. data/lib/rex/proto/dhcp.rb +7 -0
  285. data/lib/rex/proto/dhcp/constants.rb +33 -0
  286. data/lib/rex/proto/dhcp/server.rb +292 -0
  287. data/lib/rex/proto/drda.rb +5 -0
  288. data/lib/rex/proto/drda.rb.ts.rb +17 -0
  289. data/lib/rex/proto/drda/constants.rb +49 -0
  290. data/lib/rex/proto/drda/constants.rb.ut.rb +23 -0
  291. data/lib/rex/proto/drda/packet.rb +252 -0
  292. data/lib/rex/proto/drda/packet.rb.ut.rb +109 -0
  293. data/lib/rex/proto/drda/utils.rb +123 -0
  294. data/lib/rex/proto/drda/utils.rb.ut.rb +84 -0
  295. data/lib/rex/proto/http.rb +5 -0
  296. data/lib/rex/proto/http.rb.ts.rb +12 -0
  297. data/lib/rex/proto/http/client.rb +821 -0
  298. data/lib/rex/proto/http/client.rb.ut.rb +95 -0
  299. data/lib/rex/proto/http/handler.rb +46 -0
  300. data/lib/rex/proto/http/handler/erb.rb +128 -0
  301. data/lib/rex/proto/http/handler/erb.rb.ut.rb +21 -0
  302. data/lib/rex/proto/http/handler/erb.rb.ut.rb.rhtml +1 -0
  303. data/lib/rex/proto/http/handler/proc.rb +60 -0
  304. data/lib/rex/proto/http/handler/proc.rb.ut.rb +24 -0
  305. data/lib/rex/proto/http/header.rb +161 -0
  306. data/lib/rex/proto/http/header.rb.ut.rb +46 -0
  307. data/lib/rex/proto/http/packet.rb +407 -0
  308. data/lib/rex/proto/http/packet.rb.ut.rb +165 -0
  309. data/lib/rex/proto/http/request.rb +356 -0
  310. data/lib/rex/proto/http/request.rb.ut.rb +214 -0
  311. data/lib/rex/proto/http/response.rb +90 -0
  312. data/lib/rex/proto/http/response.rb.ut.rb +149 -0
  313. data/lib/rex/proto/http/server.rb +369 -0
  314. data/lib/rex/proto/http/server.rb.ut.rb +79 -0
  315. data/lib/rex/proto/ntlm.rb +7 -0
  316. data/lib/rex/proto/ntlm.rb.ut.rb +177 -0
  317. data/lib/rex/proto/ntlm/base.rb +326 -0
  318. data/lib/rex/proto/ntlm/constants.rb +74 -0
  319. data/lib/rex/proto/ntlm/crypt.rb +415 -0
  320. data/lib/rex/proto/ntlm/exceptions.rb +16 -0
  321. data/lib/rex/proto/ntlm/message.rb +536 -0
  322. data/lib/rex/proto/ntlm/utils.rb +764 -0
  323. data/lib/rex/proto/proxy/socks4a.rb +440 -0
  324. data/lib/rex/proto/rfb.rb +19 -0
  325. data/lib/rex/proto/rfb.rb.ut.rb +37 -0
  326. data/lib/rex/proto/rfb/cipher.rb +84 -0
  327. data/lib/rex/proto/rfb/client.rb +207 -0
  328. data/lib/rex/proto/rfb/constants.rb +52 -0
  329. data/lib/rex/proto/smb.rb +7 -0
  330. data/lib/rex/proto/smb.rb.ts.rb +8 -0
  331. data/lib/rex/proto/smb/client.rb +1952 -0
  332. data/lib/rex/proto/smb/client.rb.ut.rb +223 -0
  333. data/lib/rex/proto/smb/constants.rb +1047 -0
  334. data/lib/rex/proto/smb/constants.rb.ut.rb +18 -0
  335. data/lib/rex/proto/smb/crypt.rb +36 -0
  336. data/lib/rex/proto/smb/evasions.rb +66 -0
  337. data/lib/rex/proto/smb/exceptions.rb +858 -0
  338. data/lib/rex/proto/smb/simpleclient.rb +306 -0
  339. data/lib/rex/proto/smb/simpleclient.rb.ut.rb +128 -0
  340. data/lib/rex/proto/smb/utils.rb +103 -0
  341. data/lib/rex/proto/smb/utils.rb.ut.rb +20 -0
  342. data/lib/rex/proto/sunrpc.rb +1 -0
  343. data/lib/rex/proto/sunrpc/client.rb +195 -0
  344. data/lib/rex/proto/tftp.rb +12 -0
  345. data/lib/rex/proto/tftp/constants.rb +39 -0
  346. data/lib/rex/proto/tftp/server.rb +497 -0
  347. data/lib/rex/proto/tftp/server.rb.ut.rb +28 -0
  348. data/lib/rex/script.rb +42 -0
  349. data/lib/rex/script/base.rb +59 -0
  350. data/lib/rex/script/meterpreter.rb +15 -0
  351. data/lib/rex/script/shell.rb +9 -0
  352. data/lib/rex/service.rb +48 -0
  353. data/lib/rex/service_manager.rb +141 -0
  354. data/lib/rex/service_manager.rb.ut.rb +32 -0
  355. data/lib/rex/services/local_relay.rb +423 -0
  356. data/lib/rex/socket.rb +684 -0
  357. data/lib/rex/socket.rb.ut.rb +107 -0
  358. data/lib/rex/socket/comm.rb +119 -0
  359. data/lib/rex/socket/comm/local.rb +412 -0
  360. data/lib/rex/socket/comm/local.rb.ut.rb +75 -0
  361. data/lib/rex/socket/ip.rb +130 -0
  362. data/lib/rex/socket/parameters.rb +345 -0
  363. data/lib/rex/socket/parameters.rb.ut.rb +51 -0
  364. data/lib/rex/socket/range_walker.rb +346 -0
  365. data/lib/rex/socket/range_walker.rb.ut.rb +55 -0
  366. data/lib/rex/socket/ssl_tcp.rb +184 -0
  367. data/lib/rex/socket/ssl_tcp.rb.ut.rb +39 -0
  368. data/lib/rex/socket/ssl_tcp_server.rb +122 -0
  369. data/lib/rex/socket/ssl_tcp_server.rb.ut.rb +61 -0
  370. data/lib/rex/socket/subnet_walker.rb +75 -0
  371. data/lib/rex/socket/subnet_walker.rb.ut.rb +28 -0
  372. data/lib/rex/socket/switch_board.rb +278 -0
  373. data/lib/rex/socket/switch_board.rb.ut.rb +52 -0
  374. data/lib/rex/socket/tcp.rb +76 -0
  375. data/lib/rex/socket/tcp.rb.ut.rb +64 -0
  376. data/lib/rex/socket/tcp_server.rb +66 -0
  377. data/lib/rex/socket/tcp_server.rb.ut.rb +44 -0
  378. data/lib/rex/socket/udp.rb +164 -0
  379. data/lib/rex/socket/udp.rb.ut.rb +44 -0
  380. data/lib/rex/struct2.rb +5 -0
  381. data/lib/rex/struct2/c_struct.rb +181 -0
  382. data/lib/rex/struct2/c_struct_template.rb +39 -0
  383. data/lib/rex/struct2/constant.rb +26 -0
  384. data/lib/rex/struct2/element.rb +44 -0
  385. data/lib/rex/struct2/generic.rb +73 -0
  386. data/lib/rex/struct2/restraint.rb +54 -0
  387. data/lib/rex/struct2/s_string.rb +72 -0
  388. data/lib/rex/struct2/s_struct.rb +111 -0
  389. data/lib/rex/sync.rb +6 -0
  390. data/lib/rex/sync/event.rb +94 -0
  391. data/lib/rex/sync/read_write_lock.rb +176 -0
  392. data/lib/rex/sync/ref.rb +57 -0
  393. data/lib/rex/sync/thread_safe.rb +82 -0
  394. data/lib/rex/test.rb +35 -0
  395. data/lib/rex/text.rb +1163 -0
  396. data/lib/rex/text.rb.ut.rb +190 -0
  397. data/lib/rex/thread_factory.rb +42 -0
  398. data/lib/rex/time.rb +65 -0
  399. data/lib/rex/transformer.rb +115 -0
  400. data/lib/rex/transformer.rb.ut.rb +38 -0
  401. data/lib/rex/ui.rb +21 -0
  402. data/lib/rex/ui/interactive.rb +298 -0
  403. data/lib/rex/ui/output.rb +78 -0
  404. data/lib/rex/ui/output/none.rb +18 -0
  405. data/lib/rex/ui/progress_tracker.rb +96 -0
  406. data/lib/rex/ui/subscriber.rb +149 -0
  407. data/lib/rex/ui/text/color.rb +97 -0
  408. data/lib/rex/ui/text/color.rb.ut.rb +18 -0
  409. data/lib/rex/ui/text/dispatcher_shell.rb +467 -0
  410. data/lib/rex/ui/text/input.rb +117 -0
  411. data/lib/rex/ui/text/input/buffer.rb +75 -0
  412. data/lib/rex/ui/text/input/readline.rb +129 -0
  413. data/lib/rex/ui/text/input/socket.rb +95 -0
  414. data/lib/rex/ui/text/input/stdio.rb +45 -0
  415. data/lib/rex/ui/text/irb_shell.rb +57 -0
  416. data/lib/rex/ui/text/output.rb +80 -0
  417. data/lib/rex/ui/text/output/buffer.rb +61 -0
  418. data/lib/rex/ui/text/output/file.rb +43 -0
  419. data/lib/rex/ui/text/output/socket.rb +43 -0
  420. data/lib/rex/ui/text/output/stdio.rb +40 -0
  421. data/lib/rex/ui/text/progress_tracker.rb +56 -0
  422. data/lib/rex/ui/text/progress_tracker.rb.ut.rb +34 -0
  423. data/lib/rex/ui/text/shell.rb +328 -0
  424. data/lib/rex/ui/text/table.rb +279 -0
  425. data/lib/rex/ui/text/table.rb.ut.rb +55 -0
  426. data/lib/rex/zip.rb +93 -0
  427. data/lib/rex/zip/archive.rb +184 -0
  428. data/lib/rex/zip/blocks.rb +182 -0
  429. data/lib/rex/zip/entry.rb +104 -0
  430. data/lib/rex/zip/samples/comment.rb +32 -0
  431. data/lib/rex/zip/samples/mkwar.rb +138 -0
  432. data/lib/rex/zip/samples/mkzip.rb +19 -0
  433. data/lib/rex/zip/samples/recursive.rb +58 -0
  434. metadata +434 -1
@@ -0,0 +1,204 @@
1
+ # Copyright (c) 2010, patrickHVE@googlemail.com
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are met:
6
+ # * Redistributions of source code must retain the above copyright
7
+ # notice, this list of conditions and the following disclaimer.
8
+ # * Redistributions in binary form must reproduce the above copyright
9
+ # notice, this list of conditions and the following disclaimer in the
10
+ # documentation and/or other materials provided with the distribution.
11
+ # * The names of the author may not be used to endorse or promote products
12
+ # derived from this software without specific prior written permission.
13
+ #
14
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ # DISCLAIMED. IN NO EVENT SHALL patrickHVE@googlemail.com BE LIABLE FOR ANY
18
+ # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
+ # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20
+ # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
+
25
+ #
26
+ # sf - Sept 2010 - Modified for x64 support and merged into the stdapi extension.
27
+ #
28
+
29
+ require 'pp'
30
+ require 'enumerator'
31
+
32
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/api_constants'
33
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/tlv'
34
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/util'
35
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/win_const_manager'
36
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/multicall'
37
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/dll'
38
+
39
+ module Rex
40
+ module Post
41
+ module Meterpreter
42
+ module Extensions
43
+ module Stdapi
44
+ module Railgun
45
+
46
+ #
47
+ # The Railgun class to dynamically expose the Windows API.
48
+ #
49
+ class Railgun
50
+ def initialize( client )
51
+
52
+ @client = client
53
+ @dll = ::Hash.new
54
+
55
+ @win_consts = WinConstManager.new()
56
+
57
+ @constants_loaded = false
58
+
59
+ # Load the multi-caller
60
+ @multicaller = MultiCaller.new( @client, self, @win_consts )
61
+
62
+ # Load utility class
63
+ @util = Util.new( self, @client.platform )
64
+ end
65
+
66
+ # read data from a memory address on the host (useful for working with LPVOID parameters)
67
+ def memread( address, length )
68
+
69
+ raise "Invalid parameters." if( not address or not length )
70
+
71
+ request = Packet.create_request( 'stdapi_railgun_memread' )
72
+
73
+ request.add_tlv( TLV_TYPE_RAILGUN_MEM_ADDRESS, address )
74
+ request.add_tlv( TLV_TYPE_RAILGUN_MEM_LENGTH, length )
75
+
76
+ response = client.send_request( request )
77
+ if( response.result == 0 )
78
+ return response.get_tlv_value( TLV_TYPE_RAILGUN_MEM_DATA )
79
+ end
80
+
81
+ return nil
82
+ end
83
+
84
+ # write data to a memory address on the host (useful for working with LPVOID parameters)
85
+ def memwrite( address, data, length )
86
+
87
+ raise "Invalid parameters." if( not address or not data or not length )
88
+
89
+ request = Packet.create_request( 'stdapi_railgun_memwrite' )
90
+
91
+ request.add_tlv( TLV_TYPE_RAILGUN_MEM_ADDRESS, address )
92
+ request.add_tlv( TLV_TYPE_RAILGUN_MEM_DATA, data )
93
+ request.add_tlv( TLV_TYPE_RAILGUN_MEM_LENGTH, length )
94
+
95
+ response = client.send_request( request )
96
+ if( response.result == 0 )
97
+ return true
98
+ end
99
+
100
+ return false
101
+ end
102
+
103
+ # adds a function to an existing DLL-definition
104
+ def add_function(dll_name, function_name, return_type, params, windows_name=nil)
105
+ raise "DLL #{dll_name} not found. Known DLLs: #{PP.pp(@dll.keys, "")}" unless @dll.has_key? dll_name
106
+ @dll[dll_name].add_function(function_name, return_type, params, windows_name)
107
+ end
108
+
109
+ # adds a function to an existing DLL-definition
110
+ # you can override the dll name if you want to include a path or the DLL name contains
111
+ # non-ruby-approved characters
112
+ def add_dll(dll_name, windows_name=nil)
113
+ raise "DLL #{dll_name} already exists. Existing DLLs: #{PP.pp(@dll.keys, "")}" unless not @dll.has_key? dll_name
114
+ if( windows_name == nil )
115
+ windows_name = dll_name
116
+ end
117
+ @dll[dll_name] = DLL.new(windows_name, @client, @win_consts)
118
+ end
119
+
120
+ def get_dll( dll_name )
121
+ # sf: we now lazy load the module definitions as needed to avoid the performance hit
122
+ # to stdapi if we do it upon initilization (the user may never use railgun or else
123
+ # require only a portion of the modules exposed by railgun so no need to pre load them)
124
+ if( not @dll.has_key?( dll_name ) )
125
+
126
+ # the constants are also lazy loaded the first time we call const() or any API function...
127
+ if( not @constants_loaded )
128
+ ApiConstants.add_constants( @win_consts )
129
+ @constants_loaded = true
130
+ end
131
+
132
+ case dll_name
133
+ when 'kernel32'
134
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/def/def_kernel32'
135
+ Def::Def_kernel32.add_imports(self)
136
+ when 'ntdll'
137
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/def/def_ntdll'
138
+ Def::Def_ntdll.add_imports(self)
139
+ when 'user32'
140
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/def/def_user32'
141
+ Def::Def_user32.add_imports(self)
142
+ when 'ws2_32'
143
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/def/def_ws2_32'
144
+ Def::Def_ws2_32.add_imports(self)
145
+ when 'iphlpapi'
146
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/def/def_iphlpapi'
147
+ Def::Def_iphlpapi.add_imports(self)
148
+ when 'netapi32'
149
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/def/def_netapi32'
150
+ Def::Def_netapi32.add_imports(self)
151
+ when 'advapi32'
152
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/def/def_advapi32'
153
+ Def::Def_advapi32.add_imports(self)
154
+ when 'shell32'
155
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/def/def_shell32'
156
+ Def::Def_shell32.add_imports(self)
157
+ end
158
+
159
+ if( @dll.has_key?( dll_name ) )
160
+ return @dll[dll_name]
161
+ end
162
+
163
+ else
164
+ return @dll[dll_name]
165
+ end
166
+
167
+ return nil
168
+ end
169
+
170
+ # we fake having members like user32 and kernel32.
171
+ # reason is that
172
+ # ...user32.MessageBoxW()
173
+ # is prettier than
174
+ # ...dlls["user32"].functions["MessageBoxW"]()
175
+ def method_missing(dll_symbol, *args)
176
+ dll_name = dll_symbol.to_s
177
+
178
+ self.get_dll( dll_name )
179
+
180
+ raise "DLL #{dll_name} not found. Known DLLs: #{PP.pp(@dll.keys, "")}" unless @dll.has_key? dll_name
181
+
182
+ return @dll[dll_name]
183
+ end
184
+
185
+ # Give the programmer access to constants
186
+ def const(str)
187
+ # the constants are also lazy loaded the first time we call const() or any API function...
188
+ if( not @constants_loaded )
189
+ ApiConstants.add_constants( @win_consts )
190
+ @constants_loaded = true
191
+ end
192
+ return @win_consts.parse(str)
193
+ end
194
+
195
+ # The multi-call shorthand ( ["kernel32", "ExitProcess", [0]] )
196
+ def multi(functions)
197
+ @multicaller.call(functions)
198
+ end
199
+
200
+ attr_accessor :client, :dll, :multicaller, :win_consts, :util
201
+
202
+ end
203
+
204
+ end; end; end; end; end; end
@@ -0,0 +1,51 @@
1
+ # Copyright (c) 2010, patrickHVE@googlemail.com
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are met:
6
+ # * Redistributions of source code must retain the above copyright
7
+ # notice, this list of conditions and the following disclaimer.
8
+ # * Redistributions in binary form must reproduce the above copyright
9
+ # notice, this list of conditions and the following disclaimer in the
10
+ # documentation and/or other materials provided with the distribution.
11
+ # * The names of the author may not be used to endorse or promote products
12
+ # derived from this software without specific prior written permission.
13
+ #
14
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ # DISCLAIMED. IN NO EVENT SHALL patrickHVE@googlemail.com BE LIABLE FOR ANY
18
+ # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
+ # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20
+ # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
+
25
+ module Rex
26
+ module Post
27
+ module Meterpreter
28
+ module Extensions
29
+ module Stdapi
30
+ module Railgun
31
+
32
+ TLV_TYPE_EXTENSION_RAILGUN = 0
33
+ TLV_TYPE_RAILGUN_SIZE_OUT = TLV_META_TYPE_UINT | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 1)
34
+ TLV_TYPE_RAILGUN_STACKBLOB = TLV_META_TYPE_RAW | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 2)
35
+ TLV_TYPE_RAILGUN_BUFFERBLOB_IN = TLV_META_TYPE_RAW | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 3)
36
+ TLV_TYPE_RAILGUN_BUFFERBLOB_INOUT = TLV_META_TYPE_RAW | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 4)
37
+
38
+ TLV_TYPE_RAILGUN_BACK_BUFFERBLOB_OUT = TLV_META_TYPE_RAW | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 5)
39
+ TLV_TYPE_RAILGUN_BACK_BUFFERBLOB_INOUT = TLV_META_TYPE_RAW | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 6)
40
+ TLV_TYPE_RAILGUN_BACK_RET = TLV_META_TYPE_QWORD | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 7)
41
+ TLV_TYPE_RAILGUN_BACK_ERR = TLV_META_TYPE_UINT | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 8)
42
+
43
+ TLV_TYPE_RAILGUN_DLLNAME = TLV_META_TYPE_STRING | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 9)
44
+ TLV_TYPE_RAILGUN_FUNCNAME = TLV_META_TYPE_STRING | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 10)
45
+ TLV_TYPE_RAILGUN_MULTI_GROUP = TLV_META_TYPE_GROUP | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 11)
46
+
47
+ TLV_TYPE_RAILGUN_MEM_ADDRESS = TLV_META_TYPE_QWORD | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 12 )
48
+ TLV_TYPE_RAILGUN_MEM_DATA = TLV_META_TYPE_RAW | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 13 )
49
+ TLV_TYPE_RAILGUN_MEM_LENGTH = TLV_META_TYPE_UINT | (TLV_TYPE_EXTENSION_RAILGUN + TLV_EXTENSIONS + 14 )
50
+
51
+ end; end; end; end; end; end
@@ -0,0 +1,630 @@
1
+ require 'rex/post/meterpreter/extensions/stdapi/railgun/dll_helper'
2
+
3
+ module Rex
4
+ module Post
5
+ module Meterpreter
6
+ module Extensions
7
+ module Stdapi
8
+ module Railgun
9
+ class Util
10
+
11
+ # Bring in some useful string manipulation utility functions
12
+ include DLLHelper
13
+
14
+ # Data type size info: http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.80).aspx
15
+ PRIMITIVE_TYPE_SIZES = {
16
+ :int => 4,
17
+ :__int8 => 1,
18
+ :__int16 => 2,
19
+ :__int32 => 4,
20
+ :__int64 => 8,
21
+ :bool => 1,
22
+ :char => 1,
23
+ :short => 2,
24
+ :long => 4,
25
+ :long_long => 8,
26
+ :float => 4,
27
+ :double => 8,
28
+ :long_double => 8,
29
+ :wchar_t => 2,
30
+ }
31
+
32
+ # Maps a data type to its corresponding primitive or special type :pointer
33
+ # Note, primitive types are mapped to themselves
34
+ # typedef info: http://msdn.microsoft.com/en-us/library/aa383751(v=vs.85).aspx
35
+ TYPE_DEFINITIONS = {
36
+ ##
37
+ # Primitives
38
+ ##
39
+ :int => :int,
40
+ :__int8 => :__int8,
41
+ :__int16 => :__int16,
42
+ :__int32 => :__int32,
43
+ :__int64 => :__int64,
44
+ :bool => :bool,
45
+ :char => :char,
46
+ :short => :short,
47
+ :long => :long,
48
+ :long_long => :long_long,
49
+ :float => :float,
50
+ :double => :double,
51
+ :long_double => :long_double,
52
+ :wchar_t => :wchar_t,
53
+ ##
54
+ # Non-pointers
55
+ ##
56
+ #typedef WORD ATOM;
57
+ :ATOM => :short,
58
+ #typedef int BOOL;
59
+ :BOOL => :int,
60
+ #typedef BYTE BOOLEAN;
61
+ :BOOLEAN => :char,
62
+ #typedef unsigned char BYTE;
63
+ :BYTE => :char,
64
+ #typedef char CHAR;
65
+ :CHAR => :char,
66
+ #typedef DWORD COLORREF;
67
+ :COLORREF => :long,
68
+ #typedef unsigned long DWORD;
69
+ :DWORD => :long,
70
+ #typedef unsigned int DWORD32;
71
+ :DWORD32 => :int,
72
+ #typedef unsigned __int64 DWORD64;
73
+ :DWORD64 => :__int64,
74
+ #typedef float FLOAT;
75
+ :FLOAT => :float,
76
+ #typedef int HFILE;
77
+ :HFILE => :int,
78
+ #typedef LONG HRESULT;
79
+ :HRESULT => :long,
80
+ #typedef int INT;
81
+ :INT => :int,
82
+ #typedef signed int INT32;
83
+ :INT32 => :int,
84
+ #typedef signed __int64 INT64;
85
+ :INT64 => :__int64,
86
+ #typedef WORD LANGID;
87
+ :LANGID => :short,
88
+ #typedef DWORD LCID;
89
+ :LCID => :long,
90
+ #typedef DWORD LCTYPE;
91
+ :LCTYPE => :long,
92
+ #typedef DWORD LGRPID;
93
+ :LGRPID => :long,
94
+ #typedef long LONG;
95
+ :LONG => :long,
96
+ #typedef signed int LONG32;
97
+ :LONG32 => :int,
98
+ #typedef __int64 LONG64;
99
+ :LONG64 => :__int64,
100
+ #typedef PDWORD PLCID;
101
+ :PLCID => :pointer,
102
+ #typedef LPVOID SC_LOCK;
103
+ :SC_LOCK => :pointer,
104
+ #typedef short SHORT;
105
+ :SHORT => :short,
106
+ #typedef unsigned char UCHAR;
107
+ :UCHAR => :char,
108
+ #typedef unsigned int UINT;
109
+ :UINT => :int,
110
+ #typedef unsigned int UINT32;
111
+ :UINT32 => :int,
112
+ #typedef unsigned long ULONG;
113
+ :ULONG => :long,
114
+ #typedef unsigned int ULONG32;
115
+ :ULONG32 => :int,
116
+ #typedef unsigned __int64 ULONG64;
117
+ :ULONG64 => :__int64,
118
+ #typedef unsigned short USHORT;
119
+ :USHORT => :short,
120
+ #typedef wchar_t WCHAR;
121
+ :WCHAR => :wchar_t,
122
+ #typedef unsigned short WORD;
123
+ :WORD => :short,
124
+ ##
125
+ # Pointers declared with *
126
+ ##
127
+ #typedef DWORD* LPCOLORREF;
128
+ :LPCOLORREF => :pointer,
129
+ #typedef void* LPCVOID;
130
+ :LPCVOID => :pointer,
131
+ #typedef WCHAR* LPCWSTR;
132
+ :LPCWSTR => :pointer,
133
+ #typedef DWORD* LPDWORD;
134
+ :LPDWORD => :pointer,
135
+ #typedef HANDLE* LPHANDLE;
136
+ :LPHANDLE => :pointer,
137
+ #typedef int* LPINT;
138
+ :LPINT => :pointer,
139
+ #typedef long* LPLONG;
140
+ :LPLONG => :pointer,
141
+ #typedef CHAR* LPSTR;
142
+ :LPSTR => :pointer,
143
+ #typedef void* LPVOID;
144
+ :LPVOID => :pointer,
145
+ #typedef WORD* LPWORD;
146
+ :LPWORD => :pointer,
147
+ #typedef WCHAR* LPWSTR;
148
+ :LPWSTR => :pointer,
149
+ #typedef BOOL* PBOOL;
150
+ :PBOOL => :pointer,
151
+ #typedef BOOLEAN* PBOOLEAN;
152
+ :PBOOLEAN => :pointer,
153
+ #typedef BYTE* PBYTE;
154
+ :PBYTE => :pointer,
155
+ #typedef CHAR* PCHAR;
156
+ :PCHAR => :pointer,
157
+ #typedef CHAR* PCSTR;
158
+ :PCSTR => :pointer,
159
+ #typedef WCHAR* PCWSTR;
160
+ :PCWSTR => :pointer,
161
+ #typedef DWORD* PDWORD;
162
+ :PDWORD => :pointer,
163
+ #typedef DWORDLONG* PDWORDLONG;
164
+ :PDWORDLONG => :pointer,
165
+ #typedef DWORD_PTR* PDWORD_PTR;
166
+ :PDWORD_PTR => :pointer,
167
+ #typedef DWORD32* PDWORD32;
168
+ :PDWORD32 => :pointer,
169
+ #typedef DWORD64* PDWORD64;
170
+ :PDWORD64 => :pointer,
171
+ #typedef FLOAT* PFLOAT;
172
+ :PFLOAT => :pointer,
173
+ #typedef HANDLE* PHANDLE;
174
+ :PHANDLE => :pointer,
175
+ #typedef HKEY* PHKEY;
176
+ :PHKEY => :pointer,
177
+ #typedef int* PINT;
178
+ :PINT => :pointer,
179
+ #typedef INT_PTR* PINT_PTR;
180
+ :PINT_PTR => :pointer,
181
+ #typedef INT32* PINT32;
182
+ :PINT32 => :pointer,
183
+ #typedef INT64* PINT64;
184
+ :PINT64 => :pointer,
185
+ #typedef LONG* PLONG;
186
+ :PLONG => :pointer,
187
+ #typedef LONGLONG* PLONGLONG;
188
+ :PLONGLONG => :pointer,
189
+ #typedef LONG_PTR* PLONG_PTR;
190
+ :PLONG_PTR => :pointer,
191
+ #typedef LONG32* PLONG32;
192
+ :PLONG32 => :pointer,
193
+ #typedef LONG64* PLONG64;
194
+ :PLONG64 => :pointer,
195
+ #typedef SHORT* PSHORT;
196
+ :PSHORT => :pointer,
197
+ #typedef SIZE_T* PSIZE_T;
198
+ :PSIZE_T => :pointer,
199
+ #typedef SSIZE_T* PSSIZE_T;
200
+ :PSSIZE_T => :pointer,
201
+ #typedef CHAR* PSTR;
202
+ :PSTR => :pointer,
203
+ #typedef TBYTE* PTBYTE;
204
+ :PTBYTE => :pointer,
205
+ #typedef TCHAR* PTCHAR;
206
+ :PTCHAR => :pointer,
207
+ #typedef UCHAR* PUCHAR;
208
+ :PUCHAR => :pointer,
209
+ #typedef UINT* PUINT;
210
+ :PUINT => :pointer,
211
+ #typedef UINT_PTR* PUINT_PTR;
212
+ :PUINT_PTR => :pointer,
213
+ #typedef UINT32* PUINT32;
214
+ :PUINT32 => :pointer,
215
+ #typedef UINT64* PUINT64;
216
+ :PUINT64 => :pointer,
217
+ #typedef ULONG* PULONG;
218
+ :PULONG => :pointer,
219
+ #typedef ULONGLONG* PULONGLONG;
220
+ :PULONGLONG => :pointer,
221
+ #typedef ULONG_PTR* PULONG_PTR;
222
+ :PULONG_PTR => :pointer,
223
+ #typedef ULONG32* PULONG32;
224
+ :PULONG32 => :pointer,
225
+ #typedef ULONG64* PULONG64;
226
+ :PULONG64 => :pointer,
227
+ #typedef USHORT* PUSHORT;
228
+ :PUSHORT => :pointer,
229
+ #typedef void* PVOID;
230
+ :PVOID => :pointer,
231
+ #typedef WCHAR* PWCHAR;
232
+ :PWCHAR => :pointer,
233
+ #typedef WORD* PWORD;
234
+ :PWORD => :pointer,
235
+ #typedef WCHAR* PWSTR;
236
+ :PWSTR => :pointer,
237
+ #typedef HANDLE HACCEL;
238
+ :HACCEL => :pointer,
239
+ ##
240
+ # Handles
241
+ ##
242
+ #typedef PVOID HANDLE;
243
+ :HANDLE => :pointer,
244
+ #typedef HANDLE HBITMAP;
245
+ :HBITMAP => :pointer,
246
+ #typedef HANDLE HBRUSH;
247
+ :HBRUSH => :pointer,
248
+ #typedef HANDLE HCOLORSPACE;
249
+ :HCOLORSPACE => :pointer,
250
+ #typedef HANDLE HCONV;
251
+ :HCONV => :pointer,
252
+ #typedef HANDLE HCONVLIST;
253
+ :HCONVLIST => :pointer,
254
+ #typedef HANDLE HDC;
255
+ :HDC => :pointer,
256
+ #typedef HANDLE HDDEDATA;
257
+ :HDDEDATA => :pointer,
258
+ #typedef HANDLE HDESK;
259
+ :HDESK => :pointer,
260
+ #typedef HANDLE HDROP;
261
+ :HDROP => :pointer,
262
+ #typedef HANDLE HDWP;
263
+ :HDWP => :pointer,
264
+ #typedef HANDLE HENHMETAFILE;
265
+ :HENHMETAFILE => :pointer,
266
+ #typedef HANDLE HFONT;
267
+ :HFONT => :pointer,
268
+ #typedef HANDLE HGDIOBJ;
269
+ :HGDIOBJ => :pointer,
270
+ #typedef HANDLE HGLOBAL;
271
+ :HGLOBAL => :pointer,
272
+ #typedef HANDLE HHOOK;
273
+ :HHOOK => :pointer,
274
+ #typedef HANDLE HICON;
275
+ :HICON => :pointer,
276
+ #typedef HANDLE HINSTANCE;
277
+ :HINSTANCE => :pointer,
278
+ #typedef HANDLE HKEY;
279
+ :HKEY => :pointer,
280
+ #typedef HANDLE HKL;
281
+ :HKL => :pointer,
282
+ #typedef HANDLE HLOCAL;
283
+ :HLOCAL => :pointer,
284
+ #typedef HANDLE HMENU;
285
+ :HMENU => :pointer,
286
+ #typedef HANDLE HMETAFILE;
287
+ :HMETAFILE => :pointer,
288
+ #typedef HANDLE HPALETTE;
289
+ :HPALETTE => :pointer,
290
+ #typedef HANDLE HPEN;
291
+ :HPEN => :pointer,
292
+ #typedef HANDLE HRGN;
293
+ :HRGN => :pointer,
294
+ #typedef HANDLE HRSRC;
295
+ :HRSRC => :pointer,
296
+ #typedef HANDLE HSZ;
297
+ :HSZ => :pointer,
298
+ #typedef HANDLE WINSTA;
299
+ :WINSTA => :pointer,
300
+ #typedef HANDLE HWND;
301
+ :HWND => :pointer,
302
+ #typedef HANDLE SC_HANDLE;
303
+ :SC_HANDLE => :pointer,
304
+ #typedef HANDLE SERVICE_STATUS_HANDLE;
305
+ :SERVICE_STATUS_HANDLE => :pointer,
306
+ }
307
+
308
+ # param 'railgun' is a Railgun instance.
309
+ # param 'platform' is a value like client.platform
310
+ def initialize(railgun, platform)
311
+ @railgun = railgun
312
+ @is_64bit = is_64bit_platform?(platform)
313
+ end
314
+
315
+ # Given a packed pointer, unpacks it according to architecture
316
+ def unpack_pointer(packed_pointer)
317
+ if is_64bit
318
+ # XXX: Only works if attacker and victim are like-endianed
319
+ packed_pointer.unpack('Q')[0]
320
+ else
321
+ packed_pointer.unpack('V')[0]
322
+ end
323
+ end
324
+
325
+ ###
326
+ # Summary: Returns true if pointer will be considered a 'null' pointer
327
+ #
328
+ # If given nil, returns true
329
+ # If given 0, returns true
330
+ # If given a string, if 0 after unpacking, returns true
331
+ # false otherwise
332
+ ##
333
+ def is_null_pointer(pointer)
334
+ if pointer.class == String
335
+ pointer = unpack_pointer(pointer)
336
+ end
337
+
338
+ return pointer.nil? || pointer == 0
339
+ end
340
+
341
+ ###
342
+ # Summary: Reads null-terminated unicode strings from memory.
343
+ #
344
+ # Given a pointer to a null terminated array of WCHARs, return a ruby string
345
+ # Null pointers cause an empty string to be returned
346
+ ##
347
+ def read_wstring(pointer, length = nil)
348
+ # Return an empty string for null pointers
349
+ if is_null_pointer(pointer)
350
+ return ''
351
+ end
352
+
353
+ # If length not provided, use lstrlenW
354
+ if length.nil?
355
+ length = railgun.kernel32.lstrlenW(pointer)['return']
356
+ end
357
+
358
+ # Retrieve the array of characters
359
+ chars = read_array(:WCHAR, length, pointer)
360
+
361
+ # Concatenate the characters and convert to a ruby string
362
+ str = uniz_to_str(chars.join(''))
363
+
364
+ return str
365
+ end
366
+
367
+ ###
368
+ # Summary: Read a given number of bytes from memory or from a provided buffer.
369
+ #
370
+ # If 'buffer' is not provided, read 'size' bytes from the client's memory
371
+ # If 'buffer' is provided, reads 'size' characters from the index of 'address'
372
+ ##
373
+ def memread(address, size, buffer = nil)
374
+ if buffer.nil?
375
+ return railgun.memread(address, size)
376
+ else
377
+ return buffer[address .. (address + size - 1)]
378
+ end
379
+ end
380
+
381
+ # Read and unpack a pointer from the given buffer at a given offset
382
+ def read_pointer(buffer, offset = 0)
383
+ unpack_pointer(buffer[offset, (offset + pointer_size)])
384
+ end
385
+
386
+ # Reads data structures and several windows data types
387
+ def read_data(type, position, buffer = nil)
388
+ if buffer.nil?
389
+ buffer = memread(position, sizeof_type(type))
390
+ position = 0
391
+ end
392
+
393
+ # If we're asked to read a data structure, deligate to read_struct
394
+ if is_struct_type?(type)
395
+ return read_struct(type, buffer, position)
396
+ end
397
+
398
+ # If the type is an array with a given size...
399
+ # BYTE[3] for example or BYTE[ENCRYPTED_PWLEN] or even PDWORD[23]
400
+ if is_array_type?(type)
401
+ # Separate the element type from the size of the array
402
+ element_type, length = split_array_type(type)
403
+
404
+ # Have read_array take care of the rest
405
+ return read_array(element_type, length, position, buffer)
406
+ end
407
+
408
+ size = sizeof_type(type)
409
+ raw = memread(position, size, buffer)
410
+
411
+ # read/unpack data for the types we have hard-coded support for
412
+ case type
413
+ when :LPWSTR
414
+ # null-terminated string of 16-bit Unicode characters
415
+ return read_wstring(read_pointer(raw))
416
+ when :DWORD
417
+ # Both on x86 and x64, DWORD is 32 bits
418
+ return raw.unpack('V').first
419
+ when :BOOL
420
+ return raw.unpack('l').first == 1
421
+ when :LONG
422
+ return raw.unpack('l').first
423
+ end
424
+
425
+ #If nothing worked thus far, return it raw
426
+ return raw
427
+ end
428
+
429
+ # Read 'length' number of instances of 'type' from 'bufptr'
430
+ # bufptr is an index in 'buffer' or, if buffer is nil, a memory address
431
+ def read_array(type, length, bufptr, buffer = nil)
432
+ if length <= 0
433
+ return []
434
+ end
435
+
436
+ size = sizeof_type(type)
437
+ # Grab the bytes that the array consists of
438
+ buffer = memread(bufptr, size * length, buffer)
439
+
440
+ offset = 0
441
+
442
+ 1.upto(length).map do |n|
443
+ data = read_data(type, offset, buffer)
444
+
445
+ offset = offset + size
446
+
447
+ data
448
+ end
449
+ end
450
+
451
+ # construct the data structure described in 'definition' from 'buffer'
452
+ # starting from the index 'offset'
453
+ def read_struct(definition, buffer, offset = 0)
454
+ data = {}
455
+
456
+ offsets = struct_offsets(definition, offset)
457
+
458
+ definition.each do |mapping|
459
+ key, data_type = mapping
460
+
461
+ data[key] = read_data(data_type, offsets.shift, buffer)
462
+ end
463
+
464
+ data
465
+ end
466
+
467
+
468
+ # Returns true if the data type is a pointer, false otherwise
469
+ def is_pointer_type?(type)
470
+ return TYPE_DEFINITIONS[type] == :pointer
471
+ end
472
+
473
+ # Returns whether the given type represents an array of another type
474
+ # For example BYTE[3], BYTE[ENCRYPTED_PWLEN], or even PDWORD[23]
475
+ def is_array_type?(type)
476
+ return type =~ /^\w+\[\w+\]$/ ? true : false
477
+ end
478
+
479
+ # Returns true if the type passed describes a data structure, false otherwise
480
+ def is_struct_type?(type)
481
+ return type.class == Array
482
+ end
483
+
484
+
485
+ # Returns the pointer size for this architecture
486
+ def pointer_size
487
+ is_64bit ? 8 : 4
488
+ end
489
+
490
+ # Return the size, in bytes, of the given type
491
+ def sizeof_type(type)
492
+ if is_pointer_type?(type)
493
+ return pointer_size
494
+ end
495
+
496
+ if is_array_type?(type)
497
+ element_type, length = split_array_type(type)
498
+
499
+ return length * sizeof_type(element_type)
500
+ end
501
+
502
+ if is_struct_type?(type)
503
+ return sizeof_struct(type)
504
+ end
505
+
506
+ if TYPE_DEFINITIONS.has_key?(type)
507
+ primitive = TYPE_DEFINITIONS[type]
508
+
509
+ if primitive == :pointer
510
+ return pointer_size
511
+ end
512
+
513
+ if PRIMITIVE_TYPE_SIZES.has_key?(primitive)
514
+ return PRIMITIVE_TYPE_SIZES[primitive]
515
+ else
516
+ raise "Type #{type} was mapped to non-existent primitive #{primitive}"
517
+ end
518
+ end
519
+
520
+ raise "Unable to determine size for type #{type}."
521
+ end
522
+
523
+ # Calculates the size of the struct after alignment
524
+ def sizeof_struct(struct)
525
+ offsets = struct_offsets(struct, 0)
526
+ last_data_size = sizeof_type(struct.last[1])
527
+ size_no_padding = offsets.last + last_data_size
528
+
529
+ return size_no_padding + calc_padding(size_no_padding)
530
+ end
531
+
532
+ # Given a description of a data structure, returns an array containing
533
+ # the offset from the beginning for each subsequent element, taking into
534
+ # consideration alignment and padding
535
+ def struct_offsets(definition, offset)
536
+ padding = 0
537
+ offsets = []
538
+
539
+ definition.each do |mapping|
540
+ key, data_type = mapping
541
+
542
+ if sizeof_type(data_type) > padding
543
+ offset = offset + padding
544
+ end
545
+
546
+ offsets.push(offset)
547
+
548
+ offset = offset + sizeof_type(data_type)
549
+
550
+ padding = calc_padding(offset)
551
+ end
552
+
553
+ offsets
554
+ end
555
+
556
+ # http://en.wikipedia.org/wiki/Data_structure_alignment
557
+ def required_alignment
558
+ is_64bit ? 8 : 4
559
+ end
560
+
561
+ # Bytes that needed to be added to be aligned
562
+ def calc_padding(offset)
563
+ align = required_alignment
564
+
565
+ # If offset is not aligned...
566
+ if (offset % align) != 0
567
+ # Calculate padding needed to be aligned
568
+ align - (offset & (align - 1))
569
+ else
570
+ 0
571
+ end
572
+ end
573
+
574
+ # Given an explicit array definition (e.g. BYTE[23]) return size (e.g. 23) and
575
+ # and type (e.g. BYTE). If a constant is given, attempt to resolve it
576
+ # that constant
577
+ def split_array_type(type)
578
+ if type =~ /^(\w+)\[(\w+)\]$/
579
+ element_type = $1
580
+ length = $2
581
+
582
+ unless length =~ /^\d+$/
583
+ length = railgun.const(length)
584
+ end
585
+
586
+ return element_type, length
587
+ else
588
+ raise "Can not split non-array type #{type}"
589
+ end
590
+ end
591
+
592
+ # Returns true if given platform has 64bit architecture
593
+ # expects client.platform
594
+ def is_64bit_platform?(platform)
595
+ platform =~ /win64/
596
+ end
597
+
598
+ ###
599
+ # Summary:
600
+ # Evaluates a bit field, returning a hash representing the meaning
601
+ # and state of each bit.
602
+ #
603
+ # Parameters:
604
+ # value: a bit field represented by a Fixnum
605
+ # mappings: { 'WINAPI_CONSTANT_NAME' => :descriptive_symbol, ... }
606
+ #
607
+ # Returns:
608
+ # { :descriptive_symbol => true/false, ... }
609
+ ##
610
+ def judge_bit_field(value, mappings)
611
+ flags = {}
612
+ rg = railgun
613
+
614
+ mappings.each do |constant_name, key|
615
+ flags[key] = (value & rg.const(constant_name)) != 0
616
+ end
617
+
618
+ flags
619
+ end
620
+
621
+ protected
622
+
623
+ attr_accessor :railgun, :is_64bit
624
+ end # Util
625
+ end # Railgun
626
+ end # Stdapi
627
+ end # Extensions
628
+ end # Meterpreter
629
+ end # Post
630
+ end # Rex