-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpl.h
89 lines (68 loc) · 1.6 KB
/
vpl.h
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
#include <dirent.h>
#include <dlfcn.h>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
void dumpVtable(char *file, void **vtable) {
FILE* dump = fopen(file, "w");
if (!dump) return;
int index = 0;
while (*vtable) {
Dl_info info;
dladdr(*vtable++, &info);
fprintf(dump, "%d %p %s \n", index++, info.dli_saddr, info.dli_sname);
}
fclose(dump);
}
void** getVtable(char *symbol) {
return (void **)((int)dlsym(RTLD_DEFAULT, symbol) + 8);
}
int getVtableIndex(void **vtable, char *symbol) {
int index = 0;
while (*vtable) {
Dl_info info;
dladdr(*vtable++, &info);
if (strstr(info.dli_sname, symbol))
return index;
index++;
}
return -1;
}
pid_t getPid(char *cmdline) {
pid_t pid = -1;
DIR *dp = opendir("/proc");
if (!dp) return -1;
dirent *dirp;
while (dirp = readdir(dp)) {
int id = atoi(dirp->d_name);
if (id <= 0) continue;
char buffer[1024] = { 0 };
sprintf(buffer, "/proc/%d/cmdline", id);
FILE *file = fopen(buffer, "rt");
if (!fgets(buffer, sizeof(buffer), file)) {
fclose(file);
continue;
}
fclose(file);
if (!strstr(buffer, cmdline)) continue;
pid = id;
break;
}
closedir(dp);
return pid;
}
uintptr_t findLibrary(char *library, pid_t pid = -1) {
char filename[0xFF] = { 0 }, buffer[1024] = { 0 };
FILE *fp = NULL;
sprintf(filename, "/proc/%d/maps", pid == -1 ? getpid() : pid);
fp = fopen(filename, "rt");
if (fp == NULL) return 0;
uintptr_t address = 0;
while (fgets(buffer, sizeof(buffer), fp)) {
if (!strstr(buffer, library)) continue;
address = (uintptr_t)strtoul(buffer, NULL, 16);
break;
}
fclose(fp);
return address;
}