-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlsrootkit.c
1297 lines (1103 loc) · 37.6 KB
/
lsrootkit.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Warning!!: the code is bullshit (is only a beta prototype).
-
Compile: gcc -lpthread -o lsrootkit lsrootkit.c
If fails try: gcc -pthread -o lsrootkit lsrootkit.c
Execute: ./lsrootkit
Very Important: if lsrootkit process crash you can have a rootkit in the system with some bugs: memory leaks etc.
-
MIT LICENSE - Copyright (c) lsrootkit - 2013
by: David Reguera Garcia aka Dreg - dreg@fr33project.org
https://github.com/David-Reguera-Garcia-Dreg
http://www.fr33project.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define _POSIX_C_SOURCE 200809L
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/types.h>
#include <signal.h>
#include <argp.h>
#include <sys/time.h>
#include <signal.h>
#define PROC_GID_BYTES 1000
#define LSROOT_TMP_TEMPLATE "/lsroot.XXXXXX"
#define NUM_THREADS 16 /* MUST BE POWER OF 2 */
#define EACH_DISPLAY 60000
#define MAX_VALUE(a) (((unsigned long long)1 << (sizeof(a) * CHAR_BIT)) - 1)
#define MYARRAYSIZE(a) (sizeof(a) / sizeof(*(a)))
#define SPEED_TEST "<60 hours in a QUADCORE CPU 3100.000 MHz (NO SSD)"
#define _PCRED "\x1B[31m"
#define _PCGRN "\x1B[32m"
#define _PCYEL "\x1B[33m"
#define _PCBLU "\x1B[34m"
#define _PCMAG "\x1B[35m"
#define _PCCYN "\x1B[36m"
#define _PCWHT "\x1B[37m"
#define _PCRESET "\x1B[0m"
int disable_colors = 0;
#define DPRINT_MSG(oustream, colour, tag, ...) if (disable_colors == 0) { fprintf(oustream, colour tag _PCRESET __VA_ARGS__ ); } else { fprintf(oustream, tag __VA_ARGS__ ); }
#define DERROR_MSG(...) DPRINT_MSG(stdout, _PCRED, " [ERROR!!] ", __VA_ARGS__) // fprintf(stderr, _PCRED " [ERROR!!] " _PCRESET __VA_ARGS__ );
#define DWARNING_MSG(...) DPRINT_MSG(stdout, _PCYEL, " [Warning] ", __VA_ARGS__) // fprintf(stdout, _PCYEL " [Warning] " _PCRESET __VA_ARGS__ );
#define DOK_MSG(...) DPRINT_MSG(stdout, _PCGRN, " [ok] ", __VA_ARGS__) // fprintf(stdout, _PCGRN " [ok] " _PCRESET __VA_ARGS__ );
#define DINFO_MSG(...) DPRINT_MSG(stdout, _PCCYN, " [info] ", __VA_ARGS__) // fprintf(stdout, _PCCYN " [info] " _PCRESET __VA_ARGS__ );
#define DDETECTED_MSG(...) DPRINT_MSG(stdout, _PCRED, " [rootkit_detected!!] ", __VA_ARGS__) // fprintf(stdout, _PCRED " [rootkit_detected!!] " _PCRESET __VA_ARGS__ );
#define DNODETECTED_MSG(...) DPRINT_MSG(stdout, _PCBLU, " [NO_rootkits_detected] ", __VA_ARGS__) // fprintf(stdout, _PCBLU " [NO_rootkits_detected] " _PCRESET __VA_ARGS__ );
#define DRAW_MSG(...) fprintf(stdout, __VA_ARGS__ );
struct arguments
{
char* tmp_path;
char* report_path;
int disable_each_display;
int only_processes_gid;
int only_files_gid;
int only_processes_kill;
int disable_colors;
};
typedef struct THS_DAT_s
{
char* tmp_dir;
FILE* report_path;
pthread_mutex_t* mutex;
unsigned int first_gid;
unsigned int last_gid;
int detected;
struct arguments* arguments;
} THD_DAT_t;
char* CreateTempDir(void);
void* BruteForceGIDProcesses(void* arg);
void* BruteForceGIDFiles(void* arg);
int main(int argc, char* argv[]);
int CheckProcAccess(void);
typedef void* (*THREAD_FUNC_t)(void* arg);
int CheckRights(char* tmp_path)
{
char test_file[PATH_MAX];
FILE* file = NULL;
int retf = -1;
struct stat statbuf;
gid_t actual_gid = 8;
DINFO_MSG("Checking this-process rights\n");
if (CheckProcAccess() == -1)
{
return -1;
}
memset(test_file, 0, sizeof(test_file));
memset(&statbuf, 0, sizeof(statbuf));
sprintf(test_file, "%s/_test", tmp_path);
DINFO_MSG("Checking rights with file: %s\n", test_file);
retf = -1;
file = fopen(test_file, "wb+");
if (file == NULL)
{
perror(test_file);
}
else
{
if (chown(test_file, 0, 0) == 0)
{
statbuf.st_gid = 8;
if (stat(test_file, &statbuf) == 0)
{
if (statbuf.st_gid == 0)
{
if (chown(test_file, 1, 1) == 0)
{
statbuf.st_gid = 8;
if (stat(test_file, &statbuf) == 0)
{
if (statbuf.st_gid == 1)
{
actual_gid = getgid();
if (setgid(0) == 0)
{
if (getgid() == 0)
{
if (setgid(1) == 0)
{
if (getgid() == 1)
{
DOK_MSG("Rights ok!!\n");
retf = 0;
}
}
}
}
setgid(actual_gid);
}
}
}
}
}
}
fclose(file);
unlink(test_file);
}
if (retf != 0)
{
DERROR_MSG("Rights fail!!\n");
}
return retf;
}
static inline int ExistStartNumericInDir(char* path, char* pid_string, int* exist)
{
DIR* dir;
struct dirent* ent;
register char c;
*exist = 0;
if ((dir = opendir(path)) != NULL)
{
while ((ent = readdir(dir)) != NULL)
{
c = ent->d_name[0];
if (c >= '0' && c <= '9' && strcmp(ent->d_name, pid_string) == 0)
{
*exist = 1;
break;
}
}
closedir(dir);
}
else
{
return -1;
}
return 0;
}
// NOTE: /proc/[pid] group is real gid unless process' dumpable attribute is other than 1
static inline int GetGIDFromStatProcPID(unsigned int* gid, char* procfs_pid_dir_name)
{
int stat_ret = 0;
struct stat statbuf;
*gid = 0;
stat_ret = stat(procfs_pid_dir_name, &statbuf);
if (stat_ret == -1)
{
return -1;
}
*gid = statbuf.st_gid;
return 0;
}
static inline int GetGIDFromPID(unsigned int* gid, char* procfs_status_file_name)
{
char buf[PROC_GID_BYTES];
char* aux;
int procfs_pid;
int retf = -1;
ssize_t read_ret = 0;
*gid = 0;
procfs_pid = open(procfs_status_file_name, O_RDONLY);
if (procfs_pid == -1)
{
return -1;
}
read_ret = read(procfs_pid, buf, sizeof(buf) - 1);
if ((read_ret != -1) && (read_ret != 0))
{
buf[sizeof(buf) - 1] = '\0';
aux = buf;
do
{
aux = strchr(aux + 1, '\n');
if (aux != NULL)
{
aux++;
if (aux[0] == 'G')
{
if (sscanf(aux, "Gid:\t%u\t", gid) == 1)
{
retf = 0;
break;
}
}
}
} while ((aux != NULL) && (aux[0] != '\0'));
}
close(procfs_pid);
return retf;
}
int CheckProcAccess(void)
{
char pid_string[PATH_MAX];
char procpath[PATH_MAX];
unsigned int gid = 0;
pid_t child_pid = 0;
int retf = -1;
int exist = 0;
DINFO_MSG("Checking /proc access & info returned\n");
memset(pid_string, 0, sizeof(pid_string));
memset(procpath, 0, sizeof(procpath));
retf = -1;
child_pid = fork();
if (child_pid == 0)
{
while (1)
{
sleep(1);
}
}
else
{
DOK_MSG("created child pid: %d\n", child_pid)
sprintf(pid_string, "%d", (int)child_pid);
exist = 0;
if (ExistStartNumericInDir((char*) "/proc", pid_string, &exist) == -1)
{
DERROR_MSG("dont access to /proc\n");
}
else
{
DOK_MSG("accessing to /proc\n");
if (exist == 0)
{
DERROR_MSG("pid dont exist in /proc\n");
}
else
{
DOK_MSG("pid exist in /proc\n");
sprintf(procpath, "/proc/%s/status", pid_string);
if (GetGIDFromPID(&gid, procpath) == -1)
{
DERROR_MSG("%s DONT exist\n", procpath);
}
else
{
DOK_MSG("%s exist, parent child: %u, child gid: %u\n", procpath, (unsigned int) getgid(), gid);
if (gid != getgid())
{
DERROR_MSG("gid of child and parent is different\n");
}
else
{
DOK_MSG("gid of child and parent is the same\n");
retf = 0;
}
}
}
}
if (kill(child_pid, SIGKILL) == 0)
{
DOK_MSG("killed child: %d\n", child_pid);
}
else
{
DERROR_MSG("killing child: %d\n", child_pid)
}
}
return retf;
}
static inline char* CheckRootkitFilesGID(int chown_ret, int stat_ret, int exist_file_ret, int exist_in_tmp, unsigned int gid_detected, unsigned int actual_gid, unsigned int last_gid)
{
char* rootkit_msg_detection = NULL;
char* type = NULL;
if (exist_file_ret == -1)
{
type = (char*) "tmp dir innaccesible";
}
else if (exist_in_tmp == 0)
{
type = (char*) "gid hidden from readdir tmp";
}
else if (chown_ret == -1)
{
type = (char*) "chown hooked";
}
else if (stat_ret == -1)
{
type = (char*) "stat hooked";
}
else if (gid_detected != actual_gid)
{
type = (char*) "gid_detected != actual_gid";
}
else if (gid_detected == last_gid)
{
type = (char*) "gid_detected == last_gid";
}
else if (gid_detected == 0)
{
type = (char*) "gid_detected == 0";
}
else
{
return NULL;
}
if (type == NULL)
{
return NULL;
}
/* I am too lazy to include a portable POSIX asprintf x) */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
if (asprintf(&rootkit_msg_detection, "type: %s - extra info: chown_ret: %d, stat_ret: %d, exist_file_ret: %d, exist_in_tmp: %d, gid_detected: %u, actual_gid: %u, last_gid: %u\n",
type, chown_ret, stat_ret, exist_file_ret, exist_in_tmp, gid_detected, actual_gid, last_gid) == -1)
{
return NULL;
}
#pragma GCC diagnostic pop
return rootkit_msg_detection;
}
static inline void ShowEachDisplay(unsigned int* cnt, unsigned int* remain, struct timeval* tv1, char* tag, unsigned int actual_gid, unsigned int last_gid)
{
if ((*cnt % EACH_DISPLAY) == 0)
{
double estimated = 0;
struct timeval tv2;
unsigned int last_remain;
last_remain = *remain;
*remain = last_gid - actual_gid;
gettimeofday(&tv2, NULL);
if ((last_remain > 0) && ((last_remain - *remain) > 0))
{
estimated = (double)(tv2.tv_usec - tv1->tv_usec) / 1000000 + (double)(tv2.tv_sec - tv1->tv_sec);
estimated = (double)(((((double)last_remain / (double)(last_remain - *remain)) * estimated) / 60) / 60);
}
gettimeofday(tv1, NULL);
DINFO_MSG("t[%llu] - %s %s%u%s/%s%u%s remain: %s%u%s - aprox remain hours: %.2f %s\n",
(unsigned long long) pthread_self(),
tag,
disable_colors == 0 ? _PCCYN : "",
actual_gid,
disable_colors == 0 ? _PCRESET : "",
disable_colors == 0 ? _PCRED : "",
last_gid,
disable_colors == 0 ? _PCRESET : "",
disable_colors == 0 ? _PCGRN : "",
*remain,
disable_colors == 0 ? _PCRESET : "",
estimated,
estimated == 0 ? "calculating... be patient" : " ");
}
*cnt += 1;
}
static inline void RootkitDetected(char* tag, char* rootkit_msg_detection, THD_DAT_t* th_dat)
{
DDETECTED_MSG("t[%llu] - %s rootkit detected!! %s\n\n", (unsigned long long) pthread_self(), tag, rootkit_msg_detection);
pthread_mutex_lock(th_dat->mutex);
fprintf(th_dat->report_path, "\n%s\n", rootkit_msg_detection);
fflush(th_dat->report_path);
pthread_mutex_unlock(th_dat->mutex);
th_dat->detected = 1;
}
void* BruteForceGIDFiles(void* arg)
{
THD_DAT_t* th_dat = (THD_DAT_t*)arg;
char file_name[PATH_MAX];
char full_path[PATH_MAX];
unsigned int gid_detected = 0;
unsigned int actual_gid = 0;
unsigned int last_gid = 0;
uid_t my_uid;
struct stat statbuf;
unsigned int i = EACH_DISPLAY;
char* rootkit_msg_detection = NULL;
int chown_ret = 0;
int stat_ret = 0;
int exist_file_ret;
int exist_in_tmp;
char file_name_ext[PATH_MAX];
struct timeval tv1;
unsigned int remain = 0;
my_uid = getuid();
memset(file_name, 0, sizeof(file_name));
memset(full_path, 0, sizeof(full_path));
memset(file_name_ext, 0, sizeof(file_name_ext));
sprintf(file_name, "%llu", (unsigned long long) pthread_self());
sprintf(file_name_ext, "%s.files", file_name);
sprintf(full_path, "%s/%s", th_dat->tmp_dir, file_name_ext);
DOK_MSG("t[%llu] - BruteForceGIDFiles New thread! GID range: %u - %u\n\t%s\n", (unsigned long long) pthread_self(), th_dat->first_gid, th_dat->last_gid, full_path);
fclose(fopen(full_path, "wb+"));
gettimeofday(&tv1, NULL);
actual_gid = th_dat->first_gid;
do
{
chown_ret = chown(full_path, my_uid, actual_gid);
gid_detected = 0;
statbuf.st_gid = 0;
stat_ret = stat(full_path, &statbuf);
if (stat_ret != -1)
{
last_gid = gid_detected;
gid_detected = statbuf.st_gid;
}
exist_in_tmp = 0;
exist_file_ret = ExistStartNumericInDir(th_dat->tmp_dir, file_name_ext, &exist_in_tmp);
rootkit_msg_detection = CheckRootkitFilesGID(chown_ret, stat_ret, exist_file_ret, exist_in_tmp, gid_detected, actual_gid, last_gid);
if (rootkit_msg_detection != NULL)
{
RootkitDetected((char*)"BruteForceGIDFiles", rootkit_msg_detection, th_dat);
free(rootkit_msg_detection);
rootkit_msg_detection = NULL;
break;
}
if (th_dat->arguments->disable_each_display == 0)
{
ShowEachDisplay(&i, &remain, &tv1, (char*) "BruteForceGIDFiles", actual_gid, th_dat->last_gid);
}
} while (actual_gid++ != th_dat->last_gid);
unlink(full_path);
return NULL;
}
static inline char* CheckRootkitProcessesGID(int exist_in_proc_ret,
int exist_in_proc,
int proc_ret,
unsigned int gid_from_proc,
int statproc_ret,
unsigned int gid_from_statproc,
unsigned int gid_detected,
unsigned int actual_gid,
unsigned int last_gid)
{
char* rootkit_msg_detection = NULL;
char* type = NULL;
if (exist_in_proc_ret == -1)
{
type = (char*) "/proc dir innaccesible";
}
else if (exist_in_proc == 0)
{
type = (char*) "gid hidden from readdir proc";
}
else if (proc_ret == -1)
{
type = (char*) "gid hidden from open proc/pid/status";
}
else if (gid_from_proc != gid_detected)
{
type = (char*) "gid_from_proc != gid_detected";
}
else if (statproc_ret == -1)
{
type = (char*) "gid hidden from open proc/pid";
}
else if (gid_from_statproc != gid_detected)
{
type = (char*) "gid_from_statproc != gid_detected";
}
else if (gid_detected != actual_gid)
{
type = (char*) "gid_detected != actual_gid";
}
else if (gid_detected == last_gid)
{
type = (char*) "gid_detected == last_gid";
}
else if (gid_detected == 0)
{
type = (char*) "gid_detected == 0";
}
else
{
return NULL;
}
if (type == NULL)
{
return NULL;
}
/* I am too lazy to include a portable POSIX asprintf x) */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
if (asprintf(&rootkit_msg_detection, "type: %s - extra info: exist_in_proc_ret: %d, exist_in_proc: %d, proc_ret: %d, gid_from_proc: %u, statproc_ret: %d, gid_from_statproc: %u, gid_detected: %u, actual_gid: %u, last_gid: %u",
type, exist_in_proc_ret, exist_in_proc, proc_ret, gid_from_proc, statproc_ret, gid_from_statproc, gid_detected, actual_gid, last_gid) == -1)
{
return NULL;
}
#pragma GCC diagnostic pop
return rootkit_msg_detection;
}
void _Parent(pid_t child_pid, int fd_child, int fd_parent, THD_DAT_t* th_dat)
{
ssize_t write_ret = 0;
ssize_t read_ret = 0;
int proc_ret = 0;
int statproc_ret = 0;
unsigned int gid_detected = 0;
unsigned int last_gid = 0;
unsigned int actual_gid = 0;
int read_state = 0;
char procfs_status_file_name[PATH_MAX];
char procfs_childpid_dir_name[PATH_MAX];
unsigned int gid_from_proc = 0;
unsigned int gid_from_statproc = 0;
char* rootkit_msg_detection;
unsigned int i = 0;
char pid_string[PATH_MAX];
int exist_in_proc;
int exist_in_proc_ret;
unsigned int remain = 0;
struct timeval tv1;
memset(procfs_status_file_name, 0, sizeof(procfs_status_file_name));
sprintf(procfs_status_file_name, "/proc/%d/status", child_pid);
memset(procfs_childpid_dir_name, 0, sizeof(procfs_childpid_dir_name));
sprintf(procfs_childpid_dir_name, "/proc/%d", child_pid);
memset(pid_string, 0, sizeof(pid_string));
sprintf(pid_string, "%d", child_pid);
read_state = 1;
gettimeofday(&tv1, NULL);
actual_gid = th_dat->first_gid;
do
{
last_gid = gid_detected;
read_ret = read(fd_child, &gid_detected, sizeof(gid_detected));
gid_from_proc = 0;
proc_ret = GetGIDFromPID(&gid_from_proc, procfs_status_file_name);
gid_from_statproc = 0;
statproc_ret = GetGIDFromStatProcPID(&gid_from_statproc, procfs_childpid_dir_name);
exist_in_proc = 0;
exist_in_proc_ret = ExistStartNumericInDir((char*)"/proc/", pid_string, &exist_in_proc);
write_ret = write(fd_parent, &read_state, sizeof(read_state));
if ((read_ret == -1) || (read_ret == 0))
{
DWARNING_MSG("t[%llu] - BruteForceGIDProcesses broken read fd_child pipe with child!! the GID range of this thread will be stopped...\n", (unsigned long long) pthread_self());
break;
}
if ((write_ret == -1) || (write_ret == 0))
{
DWARNING_MSG("t[%llu] - BruteForceGIDProcesses broken write fd_parent pipe with child!! the GID range of this thread will be stopped... \n", (unsigned long long) pthread_self());
break;
}
rootkit_msg_detection = CheckRootkitProcessesGID(exist_in_proc_ret, exist_in_proc, proc_ret, gid_from_proc, statproc_ret, gid_from_statproc, gid_detected, actual_gid, last_gid);
if (rootkit_msg_detection != NULL)
{
RootkitDetected((char*)"BruteForceGIDProcesses", rootkit_msg_detection, th_dat);
free(rootkit_msg_detection);
rootkit_msg_detection = NULL;
break;
}
if (th_dat->arguments->disable_each_display == 0)
{
ShowEachDisplay(&i, &remain, &tv1, (char*) "BruteForceGIDProcesses", actual_gid, th_dat->last_gid);
}
} while (actual_gid++ != th_dat->last_gid);
}
void _Child(int fd_child, int fd_parent, THD_DAT_t* th_dat)
{
ssize_t write_ret = 0;
ssize_t read_ret = 0;
int set_gid_ret = 0;
unsigned int gid_detected = 0;
unsigned int last_gid = 0;
unsigned int actual_gid = 0;
int read_state = 0;
unsigned int gid_aux = 0;
actual_gid = th_dat->first_gid;
do
{
gid_detected = 0;
set_gid_ret = setgid(actual_gid);
last_gid = gid_detected;
gid_detected = getgid();
write_ret = write(fd_child, &gid_detected, sizeof(gid_detected));
read_ret = read(fd_parent, &read_state, sizeof(read_state));
if ((write_ret == 0) || (write_ret == -1))
{
break;
}
if ((read_ret == 0) || (read_ret == -1))
{
break;
}
if ((actual_gid != gid_detected) ||
(last_gid == gid_detected) ||
(set_gid_ret != 0))
{
/* possible rootkit detected */
gid_aux = 0;
write(fd_child, &gid_aux, sizeof(gid_aux));
break;
}
} while (actual_gid++ != th_dat->last_gid);
}
void Parent(pid_t child_pid, char* fifo_child, char* fifo_parent, THD_DAT_t* th_dat)
{
int fd_child = 0;
int fd_parent = 0;
fd_child = open(fifo_child, O_RDONLY);
if (fd_child != -1)
{
fd_parent = open(fifo_parent, O_WRONLY);
if (fd_parent != -1)
{
_Parent(child_pid, fd_child, fd_parent, th_dat);
close(fd_parent);
}
close(fd_child);
}
}
void Child(char* fifo_child, char* fifo_parent, THD_DAT_t* th_dat)
{
int fd_child = 0;
int fd_parent = 0;
fd_child = open(fifo_child, O_WRONLY);
if (fd_child != -1)
{
fd_parent = open(fifo_parent, O_RDONLY);
if (fd_parent != -1)
{
_Child(fd_child, fd_parent, th_dat);
close(fd_parent);
}
close(fd_child);
}
}
void* BruteForceGIDProcesses(void* arg)
{
THD_DAT_t* th_dat = (THD_DAT_t*)arg;
char fifo_name[PATH_MAX];
char fifo_parent[PATH_MAX];
char fifo_child[PATH_MAX];
pid_t child_pid;
memset(fifo_name, 0, sizeof(fifo_name));
memset(fifo_parent, 0, sizeof(fifo_parent));
memset(fifo_child, 0, sizeof(fifo_child));
sprintf(fifo_name, "%llu", (unsigned long long) pthread_self());
sprintf(fifo_parent, "%s/%s.parent_processes", th_dat->tmp_dir, fifo_name);
sprintf(fifo_child, "%s/%s.child_processes", th_dat->tmp_dir, fifo_name);
DOK_MSG("t[%llu] - BruteForceGIDProcesses New thread! GID range: %u - %u\n\t%s \n\t%s\n", (unsigned long long) pthread_self(), th_dat->first_gid, th_dat->last_gid, fifo_parent, fifo_child);
if (mkfifo(fifo_parent, 0666) == 0)
{
if (mkfifo(fifo_child, 0666) == 0)
{
child_pid = fork();
if (child_pid != -1)
{
if (child_pid == 0)
{
Child(fifo_child, fifo_parent, th_dat);
}
else
{
Parent(child_pid, fifo_child, fifo_parent, th_dat);
}
}
unlink(fifo_child);
}
unlink(fifo_parent);
}
return NULL;
}
void* BruteForceKillProcesses(void* arg)
{
THD_DAT_t* th_dat = (THD_DAT_t*)arg;
pid_t child_pid;
unsigned int actual_signal = 0;
char pid_string[PATH_MAX];
char detection_msg[PATH_MAX];
int exist = 0;
int exist_ret = 0;
unsigned int i = 0;
unsigned int remain = 0;
struct timeval tv1;
DOK_MSG("t[%llu] - BruteForceKillProcesses New thread! Signal range: %u - %u\n", (unsigned long long) pthread_self(), th_dat->first_gid, th_dat->last_gid);
child_pid = fork();
if (child_pid == 0)
{
while (1)
{
sleep(1);
}
}
else
{
gettimeofday(&tv1, NULL);
i = 0;
actual_signal = th_dat->first_gid;
remain = 0;
memset(pid_string, 0, sizeof(pid_string));
sprintf(pid_string, "%d", child_pid);
do
{
if ((actual_signal >= 1) && (actual_signal <= SIGUNUSED))
{
printf("t[%llu] - skipping signal: %d\n", (unsigned long long) pthread_self(), actual_signal);
}
else
{
kill(child_pid, actual_signal);
exist = 0;
exist_ret = 0;
exist_ret = ExistStartNumericInDir((char*)"/proc", pid_string, &exist);
if ((exist_ret == -1) || (exist == 0))
{
memset(detection_msg, 0, sizeof(detection_msg));
sprintf(detection_msg, "process hidding via: kill signal: %u", actual_signal);
RootkitDetected((char*)"BruteForceKillProcesses", detection_msg, th_dat);
break;
}
if (th_dat->arguments->disable_each_display == 0)
{
ShowEachDisplay(&i, &remain, &tv1, (char*) "BruteForceKillProcesses", actual_signal, th_dat->last_gid);
}
}
} while (actual_signal++ != th_dat->last_gid);
kill(child_pid, SIGKILL);
}
return NULL;
}
const char* argp_program_version = "lsrootkit beta0.1";
const char* argp_program_bug_address = "dreg@fr33project.org";
static char doc[] =
"lsrootkit options (all analysis are ON by default):\
\v-";
static char args_doc[] = " ";
enum CMD_OPT_e
{
OPT_EMPTY = 1,
OPT_TMP_PATH,
OPT_REPORT_PATH,
OPT_DISABLE_EACH_DISPLAY,
OPT_DISABLE_COLORS,
OPT_ONLY_PROCESSES_GID,
OPT_ONLY_FILES_GID,
OPT_ONLY_PROCESSES_KILL,
};
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
static struct argp_option options[] =
{
{
"tmp-path", OPT_TMP_PATH, "FILE", OPTION_ARG_OPTIONAL,
"Set new temp path dir. Example: --tmp-path=/var/tmp"
},
{
"report-path", OPT_REPORT_PATH, "FILE", OPTION_ARG_OPTIONAL,
"Set new report path. it needs also the name. Example: --report-path=/root/analysis.txt"
},
{ "disable-each-display", OPT_DISABLE_EACH_DISPLAY, 0, OPTION_ARG_OPTIONAL, "Disable each display messages" },
{ "disable-colors", OPT_DISABLE_COLORS, 0, OPTION_ARG_OPTIONAL, "Disable colours in output" },
{ "only-gid-processes", OPT_ONLY_PROCESSES_GID, 0, OPTION_ARG_OPTIONAL, "Only bruteforce processes GID" },
{ "only-gid-files", OPT_ONLY_FILES_GID, 0, OPTION_ARG_OPTIONAL, "Only bruteforce files GID" },
{ "only-kill-processes", OPT_ONLY_PROCESSES_KILL, 0, OPTION_ARG_OPTIONAL, "Only bruteforce processes Kill" },
{ 0 }
};
#pragma GCC diagnostic pop
static error_t parse_opt(int key, char* arg, struct argp_state* state)
{
struct arguments* arguments = (struct arguments*) state->input;
switch (key)
{
case OPT_TMP_PATH:
arguments->tmp_path = arg;
break;
case OPT_REPORT_PATH:
arguments->report_path = arg;
break;
case OPT_DISABLE_EACH_DISPLAY:
arguments->disable_each_display = 1;
break;
case OPT_DISABLE_COLORS:
arguments->disable_colors = 1;
break;
case OPT_ONLY_PROCESSES_GID:
arguments->only_processes_gid = 1;
break;
case OPT_ONLY_PROCESSES_KILL:
arguments->only_processes_kill = 1;
break;
case OPT_ONLY_FILES_GID:
arguments->only_files_gid = 1;
break;
case ARGP_KEY_ARG:
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
static struct argp argp = { options, parse_opt, args_doc, doc };
#pragma GCC diagnostic pop
int mainw(struct arguments* arguments);
int main(int argc, char* argv[])
{
struct arguments arguments;
memset(&arguments, 0, sizeof(arguments));
arguments.tmp_path = NULL;
arguments.report_path = NULL;
arguments.disable_each_display = 0;
arguments.only_processes_gid = 0;
arguments.only_files_gid = 0;
arguments.only_processes_kill = 0;
arguments.disable_colors = 0;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
disable_colors = arguments.disable_colors;
DRAW_MSG(" \n--\n"
"%s%s - Rootkit Detector for UNIX\n%s"
"-\n"
"MIT LICENSE - Copyright(c) 2013\n"
"by David Reguera Garcia aka Dreg - dreg@fr33project.org\n"