-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathkernelSymbolFinder.c
178 lines (144 loc) · 6.3 KB
/
kernelSymbolFinder.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// kernelSymbolFinder.c
// KernelSymbolFinder
//
// Created by Jake James on 8/21/18.
// Copyright © 2018 Jake James. All rights reserved.
//
#include "kernelSymbolFinder.h"
#define SWAP32(p) __builtin_bswap32(p)
static FILE *file;
uint32_t offset = 0;
static void *load_bytes(FILE *obj_file, off_t offset, uint32_t size) {
void *buf = calloc(1, size);
fseek(obj_file, offset, SEEK_SET);
fread(buf, size, 1, obj_file);
return buf;
}
uint64_t find_symbol(const char *symbol, bool verbose) {
//----This will store symbol address----//
uint64_t addr = 0;
//----This variable will hold the binary location as we move on through reading it----//
size_t offset = 0;
size_t sym_offset = 0;
int ncmds = 0;
struct load_command *cmd = NULL;
uint32_t *magic = load_bytes(file, offset, sizeof(uint32_t)); //at offset 0 we have the magic number
if (verbose) printf("[i] MAGIC = 0x%x\n", *magic);
//----64bit magic number----//
if (*magic == 0xFEEDFACF) {
if (verbose) printf("[i] 64bit binary\n");
struct mach_header_64 *mh64 = load_bytes(file, offset, sizeof(struct mach_header_64));
ncmds = mh64->ncmds;
free(mh64);
offset += sizeof(struct mach_header_64);
if (verbose) printf("[i] %d LOAD COMMANDS\n", ncmds);
for (int i = 0; i < ncmds; i++) {
cmd = load_bytes(file, offset, sizeof(struct load_command));
if (verbose) printf("[i] LOAD COMMAND %d = 0x%x\n", i, cmd->cmd);
if (cmd->cmd == LC_SYMTAB) {
if (verbose) printf("[+] Found LC_SYMTAB command!\n");
struct symtab_command *symtab = load_bytes(file, offset, cmd->cmdsize);
if (verbose) printf("\t[i] %d symbols\n", symtab->nsyms);
if (verbose) printf("\t[i] Symbol table at 0x%x\n", symtab->symoff);
for (int i = 0; i < symtab->nsyms; i++) {
struct symbol *sym = load_bytes(file, symtab->symoff + sym_offset, sizeof(struct symbol));
int symlen = 0;
int sym_str_addr = sym->table_index + symtab->stroff;
uint8_t *byte = load_bytes(file, sym_str_addr+symlen, 1);
//strings end with 0 so that's how we know it's over
while (*byte != 0) {
free(byte);
symlen++;
byte = load_bytes(file, sym_str_addr+symlen, 1);
}
free(byte);
char *sym_name = load_bytes(file, sym_str_addr, symlen + 1);
if (verbose) printf("\t%s: 0x%llx\n", sym_name, sym->address);
if (!strcmp(sym_name, symbol)) {
addr = sym->address;
if (!verbose) return addr;
}
free(sym_name);
sym_offset += sizeof(struct symbol);
free(sym);
}
free(symtab);
free(cmd);
break;
}
offset += cmd->cmdsize;
free(cmd);
}
}
//----32bit magic number----//
else if (*magic == 0xFEEDFACE) {
if (verbose) printf("[i] 32bit binary\n");
struct mach_header *mh = load_bytes(file, offset, sizeof(struct mach_header));
ncmds = mh->ncmds;
free(mh);
offset += sizeof(struct mach_header);
if (verbose) printf("[i] %d LOAD COMMANDS\n", ncmds);
for (int i = 0; i < ncmds; i++) {
cmd = load_bytes(file, offset, sizeof(struct load_command));
if (verbose) printf("[i] LOAD COMMAND %d = 0x%x\n", i, cmd->cmd);
offset += cmd->cmdsize;
if (cmd->cmd == LC_SYMTAB) {
if (verbose) printf("[+] Found LC_SYMTAB command!\n");
struct symtab_command *symtab = load_bytes(file, offset, cmd->cmdsize);
if (verbose) printf("\t[i] %d symbols\n", symtab->nsyms);
if (verbose) printf("\t[i] Symbol table at 0x%x\n", symtab->symoff);
for (int i = 0; i < symtab->nsyms; i++) {
struct symbol *sym = load_bytes(file, symtab->symoff + sym_offset, sizeof(struct symbol));
int symlen = 0;
int sym_str_addr = sym->table_index + symtab->stroff;
uint8_t *byte = load_bytes(file, sym_str_addr+symlen, 1);
while (*byte != 0) {
free(byte);
symlen++;
byte = load_bytes(file, sym_str_addr+symlen, 1);
}
free(byte);
char *sym_name = load_bytes(file, sym_str_addr, symlen + 1);
if (verbose) printf("\t%s: 0x%llx\n", sym_name, sym->address);
if (!strcmp(sym_name, symbol)) {
addr = sym->address;
if (!verbose) return addr;
}
free(sym_name);
sym_offset += sizeof(struct symbol);
free(sym);
}
free(symtab);
free(cmd);
break;
}
offset += cmd->cmdsize;
free(cmd);
}
}
else {
if (verbose) printf("[!] Unrecognized file\n");
return -1;
}
return addr;
}
int initWithKernelCache(const char *kernelcache) {
size_t pathlen = strlen(kernelcache);
char decomp[pathlen + 5];
memcpy(decomp, kernelcache, pathlen);
memcpy(decomp + pathlen, ".dec", 5);
extern int kerneldec(int argc, char**argv);
const char *args[6] = { "kerneldec", "-q", "-i", kernelcache, "-o", decomp };
int ret = kerneldec(6, (char **)args);
if (ret) {
printf("[-] Failed to decompress kernel\n");
return -1;
}
file = fopen(decomp, "rb");
if (!file) {
printf("[-] Failed to open decompressed kernelcache\n");
return -1;
}
return 0;
}