-
Notifications
You must be signed in to change notification settings - Fork 3
/
procmodlib.c
235 lines (179 loc) · 4.29 KB
/
procmodlib.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
#include "procmodlib.h"
#define ATTACH(procid) \
long ret = ptrace(PTRACE_ATTACH, procid, NULL, 0); \
if (ret == -1) { \
printf("Failed to attach to process\n");\
return;\
}\
int status;\
int w = waitpid(procid, &status, WUNTRACED);\
if (w == -1) {\
printf("Waitpid failure");\
return;\
}\
if (WIFSTOPPED(status)) {\
} else {\
printf("Stop failure");\
return;\
}
#define DETACH(procid) \
ret = ptrace(PTRACE_DETACH, procid, NULL, 0);
/*
Replace bytes with alternatives
*/
void replacebytes(char *haystack, char *replace, int replacelen) {
int i = 0;
for (i = 0; i < replacelen; i++) {
haystack[i] = replace[i];
}
}
void dumpprocess(int procid, char *file) {
printf("Dumping process....\n");
char heappath[100];
sprintf(heappath, "/proc/%d/maps", procid);
FILE *hp = fopen(heappath, "r");
if (hp == NULL) {
printf("Heap not found");
return;
}
ATTACH(procid);
FILE *dump = fopen(file, "w+");
WORD to, from;
while (1) {
char string[300];
if (!fgets(string, 300, hp)) {
printf("NO LINE FOUND\n");
break;
}
char perm[10];
sscanf(string, WORDFORMAT "-" WORDFORMAT " %s ", &from, &to,
perm);
printf("FROM " WORDFORMAT " TO " WORDFORMAT " %s\n", from, to,
perm);
if (strstr(perm, "---p")) {
continue;
}
WORD off = 0;
for (; from < to; from += sizeof(WORD)) {
WORD tmp = ptrace(PTRACE_PEEKTEXT, procid, from, NULL);
fwrite(&tmp, sizeof(WORD), 1, dump);
}
}
fclose(dump);
DETACH(procid);
}
int procreplace(int procid, unsigned char *find, int findlen,
unsigned char *replace, int replacelen, WORD lowerbound,
WORD upperbound) {
printf("Finding bytes within 0x" WORDFORMAT " to 0x" WORDFORMAT "\n",
lowerbound, upperbound);
char heappath[100];
sprintf(heappath, "/proc/%d/maps", procid);
FILE *hp = fopen(heappath, "r");
if (hp == NULL) {
printf("Heap not found");
return;
}
ATTACH(procid);
WORD to, from;
while (1) {
char string[300];
if (!fgets(string, 300, hp)) {
printf("NO LINE FOUND\n");
break;
}
char perm[10];
sscanf(string, WORDFORMAT "-" WORDFORMAT " %s ", &from, &to,
perm);
printf("FROM " WORDFORMAT " TO " WORDFORMAT " %s\n", from, to,
perm);
if (strstr(perm, "---p")) {
continue;
}
// Don't attempt to write to read only memory
if (strstr(perm, "r--p"))
continue;
WORD off = 0;
WORD start = 0;
// 1 extra WORD because replacelen < 8 needs to give result of 1, and 2 extra words for good measure ;)
WORD mash[(replacelen / sizeof(WORD)) + 3];
int pos = 0;
int mval = 0;
int starti = 0;
for (; from < to; from += sizeof(WORD)) {
if (from >= lowerbound && from <= upperbound
|| to <= upperbound && to >= lowerbound) {
} else
continue;
WORD tmp = ptrace(PTRACE_PEEKTEXT, procid, from, NULL);
if (tmp == -1)
if (errno != 0) {
printf
("Can't read --------------------"
WORDFORMAT "\n", from);
return -1;
}
int i = 0;
for (i = 0; i < sizeof(WORD); i++) {
//printf("%u\n",((tmp >> (i * 8)) & 0xFF));
if (((tmp >> (i * 8)) & 0xFF) == find[pos]) {
// Matched charcter
//
if (pos == 0) {
start = from;
starti = i;
}
pos++;
if (i == sizeof(WORD) - 1
|| findlen == pos) {
mash[mval] = tmp;
mval++;
}
if (findlen == pos) {
printf
("-------------- FOUND @ "
WORDFORMAT "\n",
start + starti);
if (replace) {
replacebytes((char *)
mash +
starti,
replace,
replacelen);
int z = 0;
printf
("-------------- POKING\n");
for (z = 0; z < mval;
z++) {
if (ptrace
(PTRACE_POKETEXT,
procid,
start +
(z *
sizeof
(WORD)),
mash[z]) ==
-1) {
perror
("POKE failure");
return;
}
}
}
mval = 0;
pos = 0;
starti = 0;
break;
}
} else {
mval = 0;
pos = 0;
starti = 0;
continue;
}
}
}
}
fclose(hp);
DETACH(procid);
}