-
Notifications
You must be signed in to change notification settings - Fork 130
/
elf-parser.c
165 lines (158 loc) · 3.89 KB
/
elf-parser.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/elf.h>
int find_hook_target(const char *libcxx, uint64_t *hook_target, uint64_t *payload_target, uint32_t* first_instruction) {
Elf64_Ehdr hdr;
*hook_target = 0;
*payload_target = 0;
int fd = open(libcxx, O_RDONLY);
if(fd < 0){
perror("open libc++.so");
return 1;
}
if(read(fd, (char *)&hdr, sizeof(hdr)) < sizeof(hdr)){
perror("read libc++.so");
close(fd);
return 1;
}
if(strncmp((char *)hdr.e_ident, "\x7f""ELF", 4) != 0){
fprintf(stderr, "Invalid elf file: %s\n", libcxx);
close(fd);
return 1;
}
//printf("libc++.so Program header: %x %x %x\n", hdr.e_phoff, hdr.e_phnum, hdr.e_phentsize);
if(lseek64(fd, hdr.e_phoff, SEEK_SET) < 0){
perror("lseek64 e_phoff");
close(fd);
return 1;
}
if(hdr.e_phentsize != sizeof(Elf64_Phdr)) {
fprintf(stderr, "Invalid program header size: %d\n", hdr.e_phentsize);
close(fd);
return 1;
}
for(int i = 0; i < hdr.e_phnum; i++){
Elf64_Phdr phdr;
if(read(fd, (char *)&phdr, sizeof(phdr)) < 0){
perror("read phdr");
close(fd);
return 1;
}
if(phdr.p_type == PT_LOAD){
if(phdr.p_flags & PF_X){
*payload_target = phdr.p_offset + phdr.p_filesz;
}
}
}
if(lseek64(fd, hdr.e_shoff + hdr.e_shstrndx * sizeof(Elf64_Shdr), SEEK_SET) < 0){
perror("lseek64 shdr");
close(fd);
return 1;
}
Elf64_Shdr str_shdr;
if(read(fd, (char *)&str_shdr, sizeof(str_shdr)) < 0){
perror("read shdr");
close(fd);
return 1;
}
uint64_t dynstr = 0;
uint64_t dynsym_offset = 0;
uint64_t dynsym_size = 0;
for(int i = 0; i < hdr.e_shnum; i++){
Elf64_Shdr shdr;
if(lseek64(fd, hdr.e_shoff + i * sizeof(Elf64_Shdr), SEEK_SET) < 0){
perror("lseek64 shdr");
close(fd);
return 1;
}
if(read(fd, (char *)&shdr, sizeof(shdr)) < 0){
perror("read shdr");
close(fd);
return 1;
}
if(lseek64(fd, shdr.sh_name + str_shdr.sh_offset, SEEK_SET) < 0){
perror("lseek64 e_phoff");
close(fd);
return 1;
}
char name[100];
if(read(fd, name, sizeof(name) - 1) < 0){
perror("read sh_name");
close(fd);
return 1;
}
name[sizeof(name) - 1] = 0;
if(strcmp(name, ".dynstr") == 0){
dynstr = shdr.sh_offset;
}
if(strcmp(name, ".dynsym") == 0){
dynsym_offset = shdr.sh_offset;
dynsym_size = shdr.sh_size;
}
//printf("Section[%d] = %s\n", i, name);
}
if(dynstr == 0 || dynsym_offset == 0){
fprintf(stderr, ".dynstr or .dynsym not found\n");
close(fd);
return 1;
}
for(int i = 0; i < dynsym_size / sizeof(Elf64_Sym); i++){
Elf64_Sym sym;
if(lseek64(fd, dynsym_offset + i * sizeof(Elf64_Sym), SEEK_SET) < 0){
perror("lseek64 dynsym");
close(fd);
return 1;
}
if(read(fd, (char *)&sym, sizeof(sym)) < 0){
perror("read dynsym");
close(fd);
return 1;
}
if(lseek64(fd, dynstr + sym.st_name, SEEK_SET) < 0){
perror("lseek64 st_name");
close(fd);
return 1;
}
char name[200];
if(read(fd, name, sizeof(name) - 1) < 0){
perror("read st_name");
close(fd);
return 1;
}
name[sizeof(name) - 1] = 0;
if(strcmp(name, "_ZNSt3__115basic_streambufIcNS_11char_traitsIcEEEC2Ev") == 0){
*hook_target = sym.st_value;
}
//printf("Dymsym[%d] = %s\n", i, name);
}
if(*hook_target == 0){
fprintf(stderr, "Could not find symbol name for hook target.\n");
close(fd);
return 1;
}
// Extract first instruction
if(lseek64(fd, *hook_target, SEEK_SET) < 0){
perror("lseek64 hook_target");
close(fd);
return 1;
}
if(read(fd, first_instruction, sizeof(*first_instruction)) < 0){
perror("read first instruction");
close(fd);
return 1;
}
if(*first_instruction == 0xd503233fU){
// Hook next instruction if PACIASP is detected.
printf("d503233f PACIASP was found. Offset hook address by +4.\n");
*hook_target += 4UL;
if(read(fd, first_instruction, sizeof(*first_instruction)) < 0){
perror("read first instruction");
close(fd);
return 1;
}
}
close(fd);
return 0;
}