-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.c
285 lines (224 loc) · 5.96 KB
/
helper.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#define EI_NIDENT 16
#define SHT_SYMTAB 2
#define SHT_STRTAB 3
#define SHN_UNDEF 0
#define SHN_COMMON 0xFFF2
typedef struct {
uint32_t st_name;
uint8_t st_info;
uint8_t st_other;
uint16_t st_shndx;
uint64_t st_value;
uint64_t st_size;
} Elf64_Sym;
typedef struct {
unsigned char e_ident[EI_NIDENT];
uint16_t e_type;
uint16_t e_machine;
uint32_t e_version;
uint64_t e_entry;
uint64_t e_phoff;
uint64_t e_shoff;
uint32_t e_flags;
uint16_t e_ehsize;
uint16_t e_phentsize;
uint16_t e_phnum;
uint16_t e_shentsize;
uint16_t e_shnum;
uint16_t e_shstrndx;
} Elf64_Ehdr;
typedef struct {
uint32_t sh_name;
uint32_t sh_type;
uint64_t sh_flags;
uint64_t sh_addr;
uint64_t sh_offset;
uint64_t sh_size;
uint32_t sh_link;
uint32_t sh_info;
uint64_t sh_addralign;
uint64_t sh_entsize;
} Elf64_Shdr;
uintptr_t funcAddress;
void fixA(uintptr_t address, uint32_t ret) {
unsigned char push_opcode = 0x68;
unsigned char ret_opcode = 0xC3;
unsigned char ret_bytes[4];
ret_bytes[0] = (ret >> 0) & 0xFF;
ret_bytes[1] = (ret >> 8) & 0xFF;
ret_bytes[2] = (ret >> 16) & 0xFF;
ret_bytes[3] = (ret >> 24) & 0xFF;
int fd;
off_t offset = (off_t)address;
unsigned char buffer[6];
ssize_t bytesRead;
fd = open("/dev/kmem", O_RDWR);
if (fd < 0) {
perror("Error opening /dev/kmem");
return;
}
if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
perror("Error seeking /dev/kmem");
close(fd);
return;
}
bytesRead = read(fd, buffer, sizeof(buffer));
if (bytesRead < 0) {
perror("Error reading /dev/kmem");
close(fd);
return;
}
buffer[0] = push_opcode;
buffer[1] = ret_bytes[0];
buffer[2] = ret_bytes[1];
buffer[3] = ret_bytes[2];
buffer[4] = ret_bytes[3];
buffer[5] = ret_opcode;
if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
close(fd);
return;
}
if (write(fd, buffer, 6) != 1) {
close(fd);
return;
}
close(fd);
}
int validate_elf_header(Elf64_Ehdr *hdr) {
if (memcmp(hdr->e_ident, "\x7F" "ELF", 4) != 0) {
printf("Invalid ELF magic number\n");
return -1;
}
if (hdr->e_ident[4] != 2) {
fprintf(stderr, "Unsupported ELF class (not 64-bit)\n");
return -1;
}
if (hdr->e_ident[5] != 1) {
fprintf(stderr, "Unsupported ELF data encoding (not little-endian)\n");
return -1;
}
if (hdr->e_ident[6] != 1) {
fprintf(stderr, "Unsupported ELF version\n");
return -1;
}
return 0;
}
int parse_symbol_table(FILE *file, Elf64_Shdr *section_headers, int num_sections, Elf64_Shdr *string_table, char *search, char *ret) {
char *strtab = malloc(string_table->sh_size);
if (!strtab) {
printf("Failed to allocate memory for string table");
return -1;
}
fseek(file, string_table->sh_offset, SEEK_SET);
fread(strtab, 1, string_table->sh_size, file);
for (int i = 0; i < num_sections; i++) {
if (section_headers[i].sh_type == SHT_SYMTAB) {
Elf64_Sym *symtab = malloc(section_headers[i].sh_size);
if (!symtab) {
printf("Failed to allocate memory for symbol table");
free(strtab);
return -1;
}
fseek(file, section_headers[i].sh_offset, SEEK_SET);
fread(symtab, 1, section_headers[i].sh_size, file);
int num_symbols = section_headers[i].sh_size / sizeof(Elf64_Sym);
for (int j = 0; j < num_symbols; j++) {
if (symtab[j].st_shndx == SHN_UNDEF || symtab[j].st_shndx == SHN_COMMON)
continue;
if ((unsigned long)symtab[j].st_value == 0x00)
continue;
char *sym_name = strtab + symtab[j].st_name;
if (!strcmp(sym_name, search)) {
funcAddress = (unsigned long)symtab[j].st_value;
uint32_t retAddress = (uint32_t)strtoul(ret, NULL, 0);
fixA(funcAddress, retAddress);
}
}
free(symtab);
}
}
free(strtab);
return 0;
}
int read_elf_header(const char *filename, Elf64_Ehdr *header) {
FILE *file = fopen(filename, "rb");
if (!file) {
printf("Error opening file");
return -1;
}
if (fread(header, 1, sizeof(Elf64_Ehdr), file) != sizeof(Elf64_Ehdr)) {
printf("Error reading ELF header");
fclose(file);
return -1;
}
fclose(file);
return 0;
}
int read_section_headers(const char *filename, Elf64_Ehdr *elf_header, Elf64_Shdr **section_headers) {
FILE *file = fopen(filename, "rb");
if (!file) {
perror("Error opening file");
return -1;
}
*section_headers = malloc(elf_header->e_shentsize * elf_header->e_shnum);
if (!(*section_headers)) {
fprintf(stderr, "Failed to allocate memory for section headers\n");
fclose(file);
return -1;
}
fseek(file, elf_header->e_shoff, SEEK_SET);
if (fread(*section_headers, elf_header->e_shentsize, elf_header->e_shnum, file) != elf_header->e_shnum) {
perror("Error reading section headers");
free(*section_headers);
fclose(file);
return -1;
}
fclose(file);
return 0;
}
int main(int argc, char *argv[]) {
Elf64_Ehdr elf_header;
if (read_elf_header("/boot/kernel/kernel", &elf_header) != 0) {
printf("Failed to read ELF header\n");
return 1;
}
if (validate_elf_header(&elf_header) != 0) {
printf("ELF header validation failed\n");
return 1;
}
Elf64_Shdr *section_headers = NULL;
if (read_section_headers("/boot/kernel/kernel", &elf_header, §ion_headers) != 0) {
printf("Failed to read section headers\n");
return 1;
}
int symtab_index = -1;
int strtab_index = -1;
for (int i = 0; i < elf_header.e_shnum; i++) {
if (section_headers[i].sh_type == SHT_SYMTAB) {
symtab_index = i;
strtab_index = section_headers[i].sh_link;
break;
}
}
if (symtab_index == -1 || strtab_index == -1) {
printf("Symbol table or string table not found\n");
}
Elf64_Shdr *string_table = §ion_headers[strtab_index];
FILE *file = fopen("/boot/kernel/kernel", "rb");
if (!file) {
printf("Error opening file");
return 1;
}
if (parse_symbol_table(file, section_headers, elf_header.e_shnum, string_table, argv[1], argv[2]) != 0) {
printf("Failed to parse symbol table\n");
}
fclose(file);
free(section_headers);
return 0;
}