forked from hc0d3r/sshd-poison
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proc-info.c
105 lines (80 loc) · 1.82 KB
/
proc-info.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
#include <sys/types.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "proc-info.h"
static char *xreadlink(const char *link);
int re_exec(const char *name, pid_t pid){
char *buf, exe[32];
size_t len;
int ret;
len = strlen(name);
buf = malloc(len+1);
if(buf == NULL){
perror("re_exec() malloc()");
exit(1);
}
sprintf(exe, "/proc/%d/exe", pid);
if((size_t)readlink(exe, buf, len) != len){
ret = 0;
goto end;
}
ret = (memcmp(buf, name, len) == 0);
end:
free(buf);
return ret;
}
uint64_t get_elf_baseaddr(pid_t pid){
char buf[64];
int fd;
sprintf(buf, "/proc/%d/maps", pid);
fd = open(buf, O_RDONLY);
if(fd == -1){
perror("get_elf_baseaddr(), open()");
exit(1);
}
if(read(fd, buf, 16) != 16){
perror("get_elf_baseaddr(), read()");
exit(1);
}
close(fd);
return (uint64_t)strtol(buf, NULL, 16);
}
char *get_elf_name(pid_t pid){
char buf[32];
sprintf(buf, "/proc/%d/exe", pid);
return xreadlink(buf);
}
static char *xreadlink(const char *link){
char *ret = NULL, *aux;
ssize_t nread;
size_t len = 16;
while(1){
aux = realloc(ret, len);
if(aux == NULL){
free(ret);
ret = NULL;
break;
}
ret = aux;
if((nread = readlink(link, ret, len)) == -1){
free(ret);
ret = NULL;
break;
}
if((size_t)nread < len){
/* try dealloc unused space */
aux = realloc(ret, nread+1);
if(aux != NULL){
ret = aux;
}
ret[nread] = 0x0;
break;
}
len *= 2;
}
return ret;
}