-
Notifications
You must be signed in to change notification settings - Fork 1k
/
check_rc_pids.c
329 lines (275 loc) · 8.8 KB
/
check_rc_pids.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
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
/* Copyright (C) 2009 Trend Micro Inc.
* All right reserved.
*
* This program is a free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License (version 2) as published by the FSF - Free Software
* Foundation
*/
#ifndef WIN32
#include "shared.h"
#include "rootcheck.h"
/* Prototypes */
static int proc_read(int pid);
static int proc_chdir(int pid);
static int proc_stat(int pid);
static void loop_all_pids(const char *ps, pid_t max_pid, int *_errors, int *_total);
/* Global variables */
static int noproc;
/* If /proc is mounted, check to see if the pid is present */
static int proc_read(int pid)
{
char dir[OS_SIZE_1024 + 1];
if (noproc) {
return (0);
}
snprintf(dir, OS_SIZE_1024, "%d", pid);
if (isfile_ondir(dir, "/proc")) {
return (1);
}
return (0);
}
/* If /proc is mounted, check to see if the pid is present */
static int proc_chdir(int pid)
{
int ret = 0;
char curr_dir[OS_SIZE_1024 + 1];
char dir[OS_SIZE_1024 + 1];
if (noproc) {
return (0);
}
if (getcwd(curr_dir, OS_SIZE_1024) == NULL) {
return (0);
}
if (chdir("/proc") == -1) {
return (0);
}
snprintf(dir, OS_SIZE_1024, "/proc/%d", pid);
if (chdir(dir) == 0) {
ret = 1;
}
/* Returning to the previous directory */
if (chdir(curr_dir) == -1) {
return (0);
}
return (ret);
}
/* If /proc is mounted, check to see if the pid is present there */
static int proc_stat(int pid)
{
char proc_dir[OS_SIZE_1024 + 1];
if (noproc) {
return (0);
}
snprintf(proc_dir, OS_SIZE_1024, "%s/%d", "/proc", pid);
if (is_file(proc_dir)) {
return (1);
}
return (0);
}
/* Check all the available PIDs for hidden stuff */
static void loop_all_pids(const char *ps, pid_t max_pid, int *_errors, int *_total)
{
int _kill0 = 0;
int _kill1 = 0;
int _gsid0 = 0;
int _gsid1 = 0;
int _gpid0 = 0;
int _gpid1 = 0;
int _ps0 = -1;
int _proc_stat = 0;
int _proc_read = 0;
int _proc_chdir = 0;
pid_t i = 1;
pid_t my_pid;
char command[OS_SIZE_1024 + 1];
my_pid = getpid();
for (;; i++) {
if ((i <= 0) || (i > max_pid)) {
break;
}
(*_total)++;
_kill0 = 0;
_kill1 = 0;
_gsid0 = 0;
_gsid1 = 0;
_gpid0 = 0;
_gpid1 = 0;
_ps0 = -1;
/* kill test */
if (!((kill(i, 0) == -1) && (errno == ESRCH))) {
_kill0 = 1;
}
/* getsid test */
if (!((getsid(i) == -1) && (errno == ESRCH))) {
_gsid0 = 1;
}
/* getpgid test */
if (!((getpgid(i) == -1) && (errno == ESRCH))) {
_gpid0 = 1;
}
/* /proc test */
_proc_stat = proc_stat(i);
_proc_read = proc_read(i);
_proc_chdir = proc_chdir(i);
/* If PID does not exist, move on */
if (!_kill0 && !_gsid0 && !_gpid0 &&
!_proc_stat && !_proc_read && !_proc_chdir) {
continue;
}
/* Ignore our own pid */
if (i == my_pid) {
continue;
}
/* Check the number of errors */
if ((*_errors) > 15) {
char op_msg[OS_SIZE_1024 + 1];
snprintf(op_msg, OS_SIZE_1024, "Excessive number of hidden processes"
". It maybe a false-positive or "
"something really bad is going on.");
notify_rk(ALERT_SYSTEM_CRIT, op_msg);
return;
}
/* Check if the process appears in ps(1) output */
if (*ps) {
snprintf(command, OS_SIZE_1024, "%s -p %d > /dev/null 2>&1", ps, (int)i);
_ps0 = 0;
if (system(command) == 0) {
_ps0 = 1;
}
}
/* If we are run in the context of OSSEC-HIDS, sleep here (no rush) */
#ifdef OSSECHIDS
debug1("%s: DEBUG: pause for %u", ARGV0, rootcheck.tsleep);
sleep(rootcheck.tsleep);
#endif
/* Everything fine, move on */
if (_ps0 && _kill0 && _gsid0 && _gpid0 && _proc_stat && _proc_read) {
continue;
}
/*
* If our kill or getsid system call got the PID but ps(1) did not,
* find out if the PID is deleted (not used anymore)
*/
if (!((getsid(i) == -1) && (errno == ESRCH))) {
_gsid1 = 1;
}
if (!((kill(i, 0) == -1) && (errno == ESRCH))) {
_kill1 = 1;
}
if (!((getpgid(i) == -1) && (errno == ESRCH))) {
_gpid1 = 1;
}
_proc_stat = proc_stat(i);
_proc_read = proc_read(i);
_proc_chdir = proc_chdir(i);
/* If it matches, process was terminated in the meantime, so move on */
if (!_gsid1 && !_kill1 && !_gpid1 && !_proc_stat &&
!_proc_read && !_proc_chdir) {
continue;
}
#ifdef AIX
/* Ignore AIX wait and sched programs */
if (_gsid0 == _gsid1 &&
_kill0 == _kill1 &&
_gpid0 == _gpid1 &&
_ps0 == 1 &&
_gsid0 == 1 &&
_kill0 == 0) {
/* The wait and sched programs do not respond to kill 0.
* So if everything else finds it, including ps, getpid, getsid,
* but not kill, we can safely ignore on AIX.
* A malicious program would specially try to hide from ps.
*/
continue;
}
#endif
if (_gsid0 == _gsid1 &&
_kill0 == _kill1 &&
_gsid0 != _kill0) {
/* If kill worked, but getsid and getpgid did not, it may
* be a defunct process -- ignore.
*/
if (! (_kill0 == 1 && _gsid0 == 0 && _gpid0 == 0) ) {
char op_msg[OS_SIZE_1024 + 1];
snprintf(op_msg, OS_SIZE_1024, "Process '%d' hidden from "
"kill (%d) or getsid (%d). Possible kernel-level"
" rootkit.", (int)i, _kill0, _gsid0);
notify_rk(ALERT_ROOTKIT_FOUND, op_msg);
(*_errors)++;
}
} else if (_kill1 != _gsid1 ||
_gpid1 != _kill1 ||
_gpid1 != _gsid1) {
/* See defunct process comment above */
if (! (_kill1 == 1 && _gsid0 == 0 && _gpid0 == 0 && _gsid1 == 0) ) {
char op_msg[OS_SIZE_1024 + 1];
snprintf(op_msg, OS_SIZE_1024, "Process '%d' hidden from "
"kill (%d), getsid (%d) or getpgid. Possible "
"kernel-level rootkit.", (int)i, _kill1, _gsid1);
notify_rk(ALERT_ROOTKIT_FOUND, op_msg);
(*_errors)++;
}
} else if (_proc_read != _proc_stat ||
_proc_read != _proc_chdir ||
_proc_stat != _kill1) {
/* Check if the pid is a thread (not showing in /proc */
if (!noproc && !check_rc_readproc((int)i)) {
char op_msg[OS_SIZE_1024 + 1];
snprintf(op_msg, OS_SIZE_1024, "Process '%d' hidden from "
"/proc. Possible kernel level rootkit.", (int)i);
notify_rk(ALERT_ROOTKIT_FOUND, op_msg);
(*_errors)++;
}
} else if (_gsid1 && _kill1 && !_ps0) {
/* checking if the pid is a thread (not showing on ps */
if (!check_rc_readproc((int)i)) {
char op_msg[OS_SIZE_1024 + 1];
snprintf(op_msg, OS_SIZE_1024, "Process '%d' hidden from "
"ps. Possible trojaned version installed.",
(int)i);
notify_rk(ALERT_ROOTKIT_FOUND, op_msg);
(*_errors)++;
}
}
}
}
/* Scan the whole filesystem looking for possible issues */
void check_rc_pids()
{
int _total = 0;
int _errors = 0;
char ps[OS_SIZE_1024 + 1];
char proc_0[] = "/proc";
char proc_1[] = "/proc/1";
pid_t max_pid = MAX_PID;
noproc = 1;
/* Checking where ps is */
memset(ps, '\0', OS_SIZE_1024 + 1);
strncpy(ps, "/bin/ps", OS_SIZE_1024);
if (!is_file(ps)) {
strncpy(ps, "/usr/bin/ps", OS_SIZE_1024);
if (!is_file(ps)) {
ps[0] = '\0';
}
}
/* Proc is mounted */
if (is_file(proc_0) && is_file(proc_1)) {
noproc = 0;
}
loop_all_pids(ps, max_pid, &_errors, &_total);
if (_errors == 0) {
char op_msg[OS_SIZE_1024 + 1];
snprintf(op_msg, OS_SIZE_1024, "No hidden process by Kernel-level "
"rootkits.\n %s is not trojaned. "
"Analyzed %d processes.", ps, _total);
notify_rk(ALERT_OK, op_msg);
}
return;
}
#else
void check_rc_pids()
{
return;
}
#endif