-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_detection.c
More file actions
349 lines (289 loc) · 6.96 KB
/
process_detection.c
File metadata and controls
349 lines (289 loc) · 6.96 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wait.h>
#include <sys/resource.h>
#include <errno.h>
#include <dirent.h>
#include <sched.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <ctype.h>
#include <time.h>
// External commands
#define COMMAND "ps --no-header -p %i o pid"
// we are looking for session ID one by one
#define SESSION "ps --no-header -s %i o sess"
// We are looking for group ID one by one
#define PGID "ps --no-header -eL o pgid"
// We are looking for all processes even threads
#define THREADS "ps --no-header -eL o lwp"
// Masks for the checks to do in checkps()
#define PS_PROC 0x00000001
#define PS_THREAD 0x00000002
#define PS_MORE 0x00000004
int checkps(int tmppid, int checks) ;
void printbadpid (int tmppid) ;
void brute(int maxpid, int check);
char * find_hidden_process_name(int hiddenPID);
long int findSize(char file_name[]);
int counter = 0;
int main(int argc, char *argv[])
{
printf("==== Start hidden process detection app ====\n");
// first parameter should be your system's max PID,
// found at /proc/sys/kernel/pid_max
// 0 for a second check (leave it as 0)
brute(131072, 0);
printf("==== Exit hidden process detection app ====\n");
return 0;
}
/*
* Brute force the pid space via vfork. All PIDs which
* can't be obtained are checked against ps output
*/
void brute(int maxpid, int check)
{
int i=0;
int allpids[maxpid] ;
int allpids2[maxpid] ;
int x;
int y;
int z;
printf("==== Brute force PID scan with fork() range 301 to %d ====\n\n", maxpid);
// PID under 301 are reserved for kernel
// fill them up with zeros
for(x=0; x < 301; x++)
{
allpids[x] = 0 ;
allpids2[x] = 0 ;
}
for(z=301; z < maxpid; z++)
{
allpids[z] = z ;
allpids2[z] = z ;
}
for (i=301; i < maxpid; i++)
{
int vpid;
int status;
errno= 0 ;
if ((vpid = vfork()) == 0)
{
_exit(0);
}
if (0 == errno)
{
allpids[vpid] = 0;
waitpid(vpid, &status, 0);
}
}
if(0 == check) // Do the scan a second time
{
// printf("DOING double check ...\n") ;
for (i=301; i < maxpid; i++)
{
int vpid;
int status;
errno= 0 ;
if ((vpid = vfork()) == 0)
{
_exit(0);
}
if (0 == errno)
{
allpids2[vpid] = 0;
waitpid(vpid, &status, 0);
}
}
}
/* processes that quit at this point in time create false positives */
for(y=0; y < maxpid; y++)
{
if ((allpids[y] != 0) && ((0 == check) || (allpids2[y] != 0)))
{
//printf("Check PID : %d\n", y);
if(!checkps(allpids[y],PS_PROC | PS_THREAD | PS_MORE) )
{
printbadpid(allpids[y]);
}
}
}
if (counter == 0)
{
printf("Result: No hidden processes found.\n\n");
}
else
{
printf("Result: %d hidden process(es) found.\n\n", counter);
}
}
void printbadpid(int badPid)
{
char * processName = "err";
processName = find_hidden_process_name(badPid);
if(processName != "fail" && processName != "nonExist")
{
printf("Suspicious PID [%d]: %s\n", badPid, processName);
counter++;
}
else
{
//printf("process name is %s", processName); //debug
}
}
// head the PIDs maps file and read the last part, output to HPN.txt and return the string.
// if there are no errors (file exists), procNameErr will be cleared
char * find_hidden_process_name(int hiddenPID){
char command[1024];
snprintf(command, sizeof(command), "head -1 /proc/%d/maps 2> procNameErr.txt | rev | cut -d ' ' -f 1 | rev > HPN.txt",hiddenPID);
// printf("command is: %s \n",command); //debug print the command executed
system(command);
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("HPN.txt", "r");
if (fp == NULL){
printf("HPN.txt file is empty");
return "empty";
}
if (findSize("procNameErr.txt") > 1){
return "nonExist";
}
while ((read = getline(&line, &len, fp)) != -1){
if(line == NULL){
return "NULL";
}
return line;
}
//if fail
return "fail";
}
long int findSize(char file_name[])
{
// opening the file in read mode
FILE* fp = fopen(file_name, "r");
// checking if the file exist or not
if (fp == NULL) {
printf("File Not Found!\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
// calculating the size of the file
long int res = ftell(fp);
// closing the file
fclose(fp);
return res;
}
// verify if ps sees the pid
int checkps(int tmppid, int checks)
{
int ok = 0;
char pids[30];
char compare[100];
char command[60];
FILE *fich_tmp ;
// The compare string is the same for all test
sprintf(compare,"%i\n",tmppid);
if (PS_PROC == (checks & PS_PROC))
{
sprintf(command,COMMAND,tmppid) ;
fich_tmp=popen (command, "r") ;
if (fich_tmp == NULL)
{
return(0);
}
{
char* tmp_pids = pids;
if (NULL != fgets(pids, 30, fich_tmp))
{
pids[29] = 0;
while( *tmp_pids == ' ' && tmp_pids <= pids+29)
{
tmp_pids++;
}
if (strncmp(tmp_pids, compare, 30) == 0) {ok = 1;}
}
}
if (NULL != fich_tmp){
pclose(fich_tmp);
}
if (1 == ok) return(ok) ; // pid is found, no need to go further
}
if (PS_THREAD == (checks & PS_THREAD))
{
FILE *fich_thread ;
fich_thread=popen (THREADS, "r") ;
if (NULL == fich_thread)
{
return(0);
}
while ((NULL != fgets(pids, 30, fich_thread)) && ok == 0)
{
char* tmp_pids = pids;
pids[29] = 0;
while( *tmp_pids == ' ' && tmp_pids <= pids+29)
{
tmp_pids++;
}
if (strncmp(tmp_pids, compare, 30) == 0) {ok = 1;}
}
if (fich_thread != NULL)
pclose(fich_thread);
if (1 == ok) return(ok) ; // thread is found, no need to go further
}
if (PS_MORE == (checks & PS_MORE))
{
FILE *fich_session ;
sprintf(command,SESSION,tmppid) ;
fich_session=popen (command, "r") ;
if (fich_session == NULL)
{
return(0);
}
while ((NULL != fgets(pids, 30, fich_session)) && ok == 0)
{
char* tmp_pids = pids;
pids[29] = 0;
while( *tmp_pids == ' ' && tmp_pids <= pids+29)
{
tmp_pids++;
}
if (strncmp(tmp_pids, compare, 30) == 0)
{
ok = 1;
}
}
pclose(fich_session);
if (1 == ok)
return(ok) ; // session is found, no need to go further
FILE *fich_pgid ;
fich_pgid=popen (PGID, "r") ;
if (NULL == fich_pgid)
{
return(0);
}
while ((NULL != fgets(pids, 30, fich_pgid)) && ok == 0)
{
char* tmp_pids = pids;
pids[29] = 0;
while( *tmp_pids == ' ' && tmp_pids <= pids+29)
{
tmp_pids++;
}
if (strncmp(tmp_pids, compare, 30) == 0)
{
ok = 1;
}
}
pclose(fich_pgid);
}
return ok;
}