forked from CpanelInc/tech-CSI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsi.pl
2193 lines (1933 loc) · 66.2 KB
/
csi.pl
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
#!/usr/bin/perl
# Copyright(c) 2013 cPanel, Inc.
# All rights Reserved.
# http://cpanel.net
# Unauthorized copying is prohibited
# Tested on cPanel 11.30 - 11.48
# Maintainer: Samir Jafferali
use strict;
use warnings;
use Cwd 'abs_path';
use File::Basename;
use File::Spec;
use POSIX;
use Time::Local;
use Getopt::Long;
use IO::Socket::INET;
use Term::ANSIColor qw(:constants);
$Term::ANSIColor::AUTORESET = 1;
my $version = '3.3.4';
###################################################
# Check to see if the calling user is root or not #
###################################################
if ( $> != 0 ) {
die "This script needs to be ran as the root user\n";
}
###########################################################
# Parse positional parameters for flags and set variables #
###########################################################
# Set defaults for positional parameters
my $no3rdparty = 0; # Default to running 3rdparty scanners
my $short=0;
my $debug=0;
my $binscan=0;
my $fh = ' ';
my $scan = 0;
my $a_type = 0; # Defaults to searching for only POST requests
my $range = "60"; # Defaults to 60 seconds
my $owner = "owner";
my $epoc_time = '0';
my @process_list = get_process_list();
my %process; &get_process_pid_hash(\%process);
my %ipcs; &get_ipcs_hash(\%ipcs);
GetOptions(
'no3rdparty' => \$no3rdparty,
'file=s' => \$fh,
'rootkitscan' => \$scan,
'get' => \$a_type,
'range=i' => \$range,
'timestamp=i' => \$epoc_time,
'user=s' => \$owner,
'short' => \$short,
'bincheck' => \$binscan,
'bugreport' => \$debug,
);
#######################################
# Set variables needed for later subs #
#######################################
chomp( my $wget = qx(which wget) );
chomp( my $make = qx(which make) );
my $top = File::Spec->curdir();
my $csidir = File::Spec->catdir( $top, 'CSI' );
my $rkhunter_bin = File::Spec->catfile( $csidir, 'rkhunter', 'bin', 'rkhunter' );
my $chkrootkit_bin = File::Spec->catfile( $csidir, 'chkrootkit', 'chkrootkit' );
my $CSISUMMARY;
my @SUMMARY;
my $touchfile = '/var/cpanel/perl/easy/Cpanel/Easy/csi.pm';
my @logfiles = (
'/usr/local/apache/logs/access_log',
'/usr/local/apache/logs/error_log',
'/var/log/messages',
'/var/log/maillog',
'/var/log/wtmp',
'/root/.bash_history',
);
my $systype;
my $os;
my $linux;
my $freebsd;
my $filename;
my $epoc_mtime;
my $epoc_ctime;
my @mbash;
my @mmessages;
my @mftp;
my @mcpanel;
my @maccess;
my %mon2num = qw(
jan 1 feb 2 mar 3 apr 4 may 5 jun 6
jul 7 aug 8 sep 9 oct 10 nov 11 dec 12
);
######################
# Run code main body #
######################
# Checks if the disclaimer has already been shown on this machine
if (!-e '/usr/share/doc/.csidisclaimer') {
qx(/bin/touch /usr/share/doc/.csidisclaimer);
disclaimer ();
} else {
my $disclaimertime = (stat('/usr/share/doc/.csidisclaimer'))[9];
my $currenttime = qx(date +%s);
if (($currenttime - $disclaimertime) > 86400) {
qx(/bin/touch /usr/share/doc/.csidisclaimer);
disclaimer ();
}
}
if ($fh ne " ") {
logfinder();
exit;
}
if ($epoc_time != "0" ) {
time_logfinder();
exit;
}
if ($scan == "1" ) {
scan();
exit;
}
if ($binscan == "1" ) {
bincheck();
exit;
}
show_help();
########
# Subs #
########
sub show_help {
print_header("\ncPanel Security Inspection Version $version");
print_header("Usage: perl csi.pl [options] [function]\n");
print_header("Functions");
print_header("=================");
print_status("--rootkitscan Performs a variety of checks to detect root level compromises.");
print_status("--bincheck Performs RPM verification on core system binaries and prints active aliases.");
print_status("--file [file/directory] Searches all available log files for the change and modify timestamp of the file/directory");
print_status(" provided in effort to determine how a file was modified or changed. ");
print_status("--timestamp [timestamp] Similar to --file, but allows you to specify a epoch timestamp if the file is no longer");
print_status(" available. The --user flag is required when using this function.\n");
print_header("Options (rootkitscan)");
print_header("=================");
print_status("--no3rdparty Disables running of 3rdparty scanners.\n");
print_header("Options (file/timestamp)");
print_header("=================");
print_status("--user [user] Override detected owner of file with custom user to search for.");
print_status("--range [seconds] Specify search range in seconds. Default is 60 seconds.");
print_status("--short Do not print verbose output.");
print_status("--get By default, CSI only searches for POST requests. This option enables searching of GET requests as well.");
print_normal(" ");
print_header("Options (bincheck)");
print_header("=================");
print_status("--bug Generates bug report for RPM verification failed files.");
print_normal(" ");
print_header("Examples");
print_header("=================");
print_status("Timestamp: ");
print_status(" csi.pl --timestamp 1407114375 --user cpuser");
print_status(" csi.pl --timestamp 1407114375 --user cpuser --range 120");
print_status("File: ");
print_status(" csi.pl --file /home/zyreperm/ii.php");
print_status(" csi.pl --file /home/zyreperm/ii.php --user ipadbest --short");
print_status("Rootkitscan: ");
print_status(" csi.pl --rootkitscan");
print_status(" csi.pl --rootkitscan --no3rdparty");
print_status("Bincheck: ");
print_status(" csi.pl --bincheck");
print_normal(" ");
}
sub bincheck {
detect_system();
print_normal('');
print_header('[ Starting cPanel Security Inspection: Bincheck Mode ]');
print_header("[ Version $version ]");
print_header("[ System Type: $systype ]");
print_header("[ OS: $os ]");
print_normal('');
print_header("[ Available flags when running csi.pl --bincheck: ]");
print_header('[ --bug (generates a bug report for invalid output) ]');
print_normal('');
my @rpms = qw(
abrt
abrt-addon-ccpp
abrt-addon-kerneloops
abrt-addon-python
abrt-tui
acl
acpid
alsa-lib
alsa-utils
aspell
aspell-devel
at
attr
audit
augeas
authconfig
autoconf
automake
b43-fwcutter
bash
bc
bind
bind-devel
bind-utils
binutils
bison
blktrace
bridge-utils
btparser
busybox
bzip2
ca-certificates
checkpolicy
chkconfig
cl-MySQL55-client
cl-MySQL55-devel
cl-MySQL55-server
cloog-ppl
compat-db42
compat-db43
ConsoleKit
coreutils
cpanel-userperl
cpio
cpp
cpuspeed
cracklib
cracklib-dicts
crda
cronie
cronie-anacron
crontabs
cryptsetup-luks
curl
cvs
cyrus-sasl
cyrus-sasl-lib
dash
db4-utils
dbus
dbus-glib
desktop-file-utils
device-mapper
device-mapper-event
device-mapper-persistent-data
dhclient
diffutils
dmidecode
dmraid
dmraid-events
dos2unix
dosfstools
dovecot
dracut
e2fsprogs
ed
efibootmgr
eject
elfutils
elinks
ethtool
exim
expat
expect
file
filesystem
findutils
fipscheck
flex
fontconfig
fprintd
freetype-devel
ftp
gawk
gcc
gcc-c++
GConf2
gdb
gd-devel
gdk-pixbuf2
gd-progs
gettext
ghostscript
ghostscript-devel
glib2
glibc
glibc-common
gnupg2
governor-mysql
grep
groff
grub
grubby
gtk2
gzip
hal
hdparm
hunspell
ImageMagick
ImageMagick-c++-devel
ImageMagick-devel
info
initscripts
iotop
iproute
iptables
iptables-ipv6
iputils
irqbalance
iw
jwhois
kbd
kernelcare
kexec-tools
kpartx
krb5-devel
lcms
less
libcap
libcgroup
libcom_err-devel
libcroco
libgcj
libgpg-error
libhugetlbfs-utils
libidn
libjpeg-turbo
libpng-devel
libproxy-bin
libreport
libreport-cli
libreport-compat
libreport-plugin-kerneloops
libreport-plugin-logger
libreport-plugin-mailx
libreport-plugin-reportuploader
libreport-plugin-rhtsupport
librsvg2
libselinux
libselinux-utils
libtar
libtiff
libtool
libuser
libwmf
libxml2
libxml2-devel
libXpm-devel
logrotate
lsof
lua
lve
lvemanager
lve-stats
lve-utils
lvm2
lynx
m4
mailx
make
MAKEDEV
man
mdadm
microcode_ctl
mingetty
mlocate
module-init-tools
mtr
nano
nc
ncurses
ncurses-devel
net-tools
newt
ngrep
nss-sysinit
nss-tools
ntsysv
numactl
openssh
openssh-clients
openssh-server
openssl
p11-kit
pam
pango
parted
passwd
patch
pciutils
pcmciautils
pcre
pcre-devel
perl
perl-CPANPLUS
perl-DBI
perl-devel
perl-libwww-perl
perl-Module-CoreList
pinentry
pinfo
pkgconfig
plymouth
plymouth-scripts
pm-utils
policycoreutils
polkit
portreserve
ppl
prelink
procps
psacct
psmisc
pure-ftpd
python
python-devel
python-ethtool
python-tools
python-urlgrabber
quota
rcs
rdate
readahead
rfkill
rhn-check
rhn-client-tools
rhnsd
rhn-setup
rng-tools
rpm
rsync
rsyslog
screen
sed
setserial
setuptool
sgml-common
sgpio
shadow-utils
shared-mime-info
sharutils
smartmontools
sos
sqlite
strace
stunnel
sudo
sysstat
system-config-firewall-base
system-config-firewall-tui
system-config-network-tui
systemtap-runtime
sysvinit-tools
tar
tcl
tcpdump
tcp_wrappers
tcsh
telnet
time
tk
tmpwatch
traceroute
udev
unixODBC
unzip
upstart
usbutils
usermode
util-linux-ng
vconfig
vim-common
vim-enhanced
vim-minimal
wget
which
wireless-tools
xdg-utils
xorg-x11-font-utils
xz
xz-lzma-compat
yum
yum-utils
zip
);
my %okbins = (
'/bin/su', '.M....G..',
'/bin/ping', '.M.......',
'/bin/ping6', '.M.......',
'/usr/bin/locate', '.M.......',
'/usr/bin/quota', '.M.......',
'/usr/bin/screen', '.M.......',
'/usr/sbin/userhelper', '.M.......',
'/usr/bin/chsh', '.M.......',
'/usr/bin/ld', '.M....G..',
'/usr/bin/c99', '.M....G..',
'/usr/bin/gcc', '.M....G..',
'/usr/bin/x86_64-redhat-linux-gcc', '.M....G..',
'/usr/bin/c++', '.M....G..',
'/usr/bin/g++', '......G..',
'/usr/bin/x86_64-redhat-linux-c++', '......G..',
'/usr/bin/x86_64-redhat-linux-g++', '......G..',
);
my @badbins ;
my @warnbins;
print "\n[ Running RPM Checks ]\n ";
my $x=0 ;
for my $rpm (@rpms) {
$x++;
push @badbins, qx(rpm -V $rpm 2> /dev/null | egrep "/(s)?bin");
if ($x=="10") {
print "=";
$x=0;
}
}
my @debuglist;
chomp (@badbins);
print "\n";
foreach (@badbins) {
if ($_ =~ m/(S.*|.*5.*|.*L.*) \//) {
push @warnbins, $_;
my $index = 0;
$index++ until $badbins[$index] eq "$_";
splice(@badbins, $index, 1);
} elsif ($_ =~ m/missing \//) {
my $index = 0;
$index++ until $badbins[$index] eq "$_";
splice(@badbins, $index, 1);
}
}
foreach (@badbins) {
my $binary=(split (/ /, $_))[1];
my $verify_string=(split (/ /, $_))[0];
if (exists $okbins{$binary}) {
my $verify_okstring= $okbins{$binary};
if ($verify_string ne $verify_okstring) {
print BOLD YELLOW ON_BLACK "[INFO] * Modified Attribute: ".$binary."\n";
if ($debug) {
push @debuglist, "RPM: ".qx(rpm -qf $binary);
push @debuglist, "File: ".$_;
push @debuglist, "Classification: [INFO] * Modified Attribute";
push @debuglist, "=============";
}
}
} else {
print BOLD YELLOW ON_BLACK "[INFO] * Modified Attribute: ".$binary."\n";
if ($debug) {
push @debuglist, "RPM: ".qx(rpm -qf $binary);
push @debuglist, "File: ".$_;
push @debuglist, "Classification: [INFO] * Modified Attribute";
push @debuglist, "=============";
}
}
}
foreach (@warnbins) {
my $binary=(split (/ /, $_))[1];
my $verify_string=(split (/ /, $_))[0];
if (exists $okbins{$binary}) {
my $verify_okstring= $okbins{$binary};
if ($verify_string ne $verify_okstring) {
print BOLD RED ON_BLACK "[WARN] * Modified Binary: ".$binary."\n";
if ($debug) {
push @debuglist, "RPM: ".qx(rpm -qf $binary);
push @debuglist, "File: ".$_;
push @debuglist, "Classification: [WARN] * Modified Binary";
push @debuglist, "=============";
}
}
} else {
print BOLD RED ON_BLACK "[WARN] * Modified Binary: ".$binary."\n";
if ($debug) {
push @debuglist, "RPM: ".qx(rpm -qf $binary);
push @debuglist, "File: ".$_;
push @debuglist, "Classification: [INFO] * Modified Attribute";
push @debuglist, "=============";
}
}
}
my @aliases=grep /^alias/, qx{/bin/bash -ic alias};
print BOLD GREEN ON_BLACK "[ALIASES] * ".scalar @aliases." Found\n";
foreach (@aliases) {
print BOLD CYAN ON_BLACK "\t- ";
print BOLD CYAN ON_BLACK $_;
}
print BOLD GREEN ON_BLACK '[!] Run "unalias -a" to unset all aliases'."\n";
print BOLD MAGENTA '[NOTE] * If any of the above binaries should not be showing up in the above list, run this script with --bug to generate a bug report'."\n\n";
if ($debug) {
chomp (@debuglist);
my $ticket = "";
if (exists $ENV{HISTFILE}) {
if ( $ENV{HISTFILE} =~ /ticket.(\d+)$/ ) {
$ticket = $1;
}
}
my $date=qx(date);
my $kernel=qx(uname -r);
my $arch=qx(uname -p);
my $cp_version=qx(cat /usr/local/cpanel/version);
my $os=qx(cat /etc/redhat-release);
chomp ($ticket);
chomp ($date);
my $message = " =============================================\n CSI Bug Report: $date\n =============================================\n Kernel: $kernel Arch: $arch OS: $os cPanel: $cp_version Ticket: $ticket\n\n\n";
print "Please send the below report to samir.jafferali\@cpanel.net.\n\n";
print " --REPORT START--\n";
print $message ;
print " -DEBUG START-\n";
foreach (@debuglist) {
print " ".$_."\n";
}
print " -DEBUG STOP-\n";
print " --REPORT END--\n\n\n";
}
}
sub disclaimer {
print_normal('');
print_header('########################################################################');
print_header('### DISCLAIMER! cPanel\'s Technical Support does not provide #');
print_header('### security consultation services. The only support services we #');
print_header('### can provide at this time is to perform a minimal analysis of the #');
print_header('### possible security breach solely for the purpose of determining if #');
print_header('### cPanel\'s software was involved or used in the security breach. #');
print_header('########################################################################');
print_header('### If it is suspected to be root compromised, only Level III Analysts #');
print_header('### should be handling the issue. Account level compromises are #');
print_header('### investigated as a courtesy and carry no guarantees. #');
print_header('########################################################################');
print_normal('');
}
sub logfinder {
detect_system();
print_normal('') if (!$short);
print_header('[ Starting cPanel Security Inspection: Logfinder Mode ]') if (!$short);
print_header("[ Version $version ]") if (!$short);
print_header("[ System Type: $systype ]") if (!$short);
print_header("[ OS: $os ]") if (!$short);
print_normal('') if (!$short);
print_header("[ Available flags when running csi.pl --file: ]") if (!$short);
print_header('[ --range (specify custom search range in seconds) ]') if (!$short);
print_header('[ --get (show GET requests as well as POST) ]') if (!$short);
print_header('[ --user (force user) ]') if (!$short);
print_header('[ --short (do not print verbose output) ]') if (!$short);
print_normal('') if (!$short);
if (!-e $fh) {
print_error("$fh not found. Exiting... \n");
exit ;
}
$filename= basename $fh ;
$epoc_mtime= (stat($fh))[9];
$epoc_ctime= (stat($fh))[10];
if ($owner eq "owner") {
my $file_uid= (stat($fh))[4];
$owner= (getpwuid $file_uid)[0];
}
print_filestats();
my $differ="1";
if ($epoc_ctime != $epoc_mtime) {
$differ="2";
}
while ($differ > 0 ) {
search_logs();
print_matches();
print "\n" if (!$short);
$epoc_mtime=$epoc_ctime ;
--$differ ;
}
}
sub time_logfinder {
detect_system();
print_normal('') if (!$short);
print_header('[ Starting cPanel Security Inspection: Logfinder Mode ]') if (!$short);
print_header("[ Version $version ]") if (!$short);
print_header("[ System Type: $systype ]") if (!$short);
print_header("[ OS: $os ]") if (!$short);
print_normal('') if (!$short);
print_header("[ Available flags when running csi.pl --timestamp: ]") if (!$short);
print_header('[ --range (specify custom search range in seconds) ]') if (!$short);
print_header('[ --get (show GET requests as well as POST) ]') if (!$short);
print_header('[ --user (force user) ]') if (!$short);
print_header('[ --short (do not print verbose output) ]') if (!$short);
print_normal('') if (!$short);
$epoc_mtime=$epoc_time ;
$epoc_ctime=$epoc_time ;
$filename = ".";
if ($owner eq "owner") {
print_error("Providing the user via --user is required when using --timestamp. Exiting... \n");
exit ;
}
print_filestats () ;
search_logs();
print_matches();
}
sub search_logs {
my $tmpepoc= $epoc_mtime - $range;
my $searchmbash= "^#$tmpepoc";
my $searchmmessages= "^".strftime("%b %d %H:%M:%S",localtime($tmpepoc));
my $searchmftp= "^".strftime("%a %b %d %H:%M:%S %Y",localtime($tmpepoc));
my $searchmcpanel= strftime("%m/%d/%Y:%H:%M:%S",gmtime($tmpepoc));
my $searchmaccess= strftime("%d/%b/%Y:%H:%M:%S",localtime($tmpepoc));
@mbash=();
@mmessages=();
@mftp=();
@mcpanel=();
@maccess =();
for (my $i=0; $i <= $range * 2 ; $i++) {
$tmpepoc++ ;
$searchmbash= "$searchmbash|^#$tmpepoc";
$searchmmessages= "$searchmmessages|^".strftime("%b %d %H:%M:%S",localtime($tmpepoc));
$searchmftp= "$searchmftp|^".strftime("%a %b %d %H:%M:%S %Y",localtime($tmpepoc));
$searchmcpanel= "$searchmcpanel|".strftime("%m/%d/%Y:%H:%M:%S",gmtime($tmpepoc));
$searchmaccess= "$searchmaccess|".strftime("%d/%b/%Y:%H:%M:%S",localtime($tmpepoc));
}
print_header("Searching for: ".localtime($epoc_mtime)." ($epoc_mtime)") if (!$short);
print_header("----------------------------------------------------") if (!$short);
print_normal_chomped("[+] Checking .bash_history files... ") if (!$short);
push @mbash, qx(egrep -HA1 "$searchmbash" /root/.bash_history);
if (-e "/home/$owner/.bash_history") {
push @mbash, qx(egrep -HA1 "$searchmbash" /home/$owner/.bash_history);
}
chomp(@mbash);
print_normal("Done. ".scalar @mbash / "2". " results found.") if (!$short);
print_normal_chomped("[+] Checking /var/log/messages... ") if (!$short);
my ($second, $minute, $hour, $dayofmonth, $month, $year, $dayofweek, $dayofyear, $daylightsavings) = localtime();
opendir(DIR, "/var/log/");
my @files = grep(/^messages/,readdir(DIR));
closedir(DIR);
foreach my $file (@files) {
my $firstline ;
my $lastline ;
if ($file =~ /\.gz$/) {
chomp($firstline=qx(zcat /var/log/$file | head -1 | awk '{print\$1" "\$2" "\$3}'));
chomp($lastline=qx(zcat /var/log/$file | tail -1 | awk '{print\$1" "\$2" "\$3}'));
} else {
chomp($firstline=qx(head -1 /var/log/$file | awk '{print\$1" "\$2" "\$3}'));
chomp($lastline=qx(tail -1 /var/log/$file | awk '{print\$1" "\$2" "\$3}'));
}
$firstline =~ s/:/ /g;
$lastline =~ s/:/ /g;
my @first= split(/ /, $firstline);
my @last= split(/ /, $lastline);
$firstline = timelocal($first[4],$first[3],$first[2],$first[1],$mon2num{ lc substr($first[0], 0, 3) }-1,$year);
$lastline = timelocal($last[4],$last[3],$last[2],$last[1],$mon2num{ lc substr($last[0], 0, 3) }-1,$year);
if ($firstline < $epoc_mtime && $lastline > $epoc_mtime) {
push @mmessages, qx(zegrep -H "$searchmmessages" /var/log/$file | grep $filename);
}
}
chomp(@mmessages);
print_normal("Done. ".scalar @mmessages. " results found.") if (!$short);
print_normal_chomped("[+] Checking ftpxferlog... ") if (!$short);
if (-s '/usr/local/apache/domlogs/ftpxferlog') {
my $firstline ;
my $lastline ;
chomp($firstline=qx(head -1 /usr/local/apache/domlogs/ftpxferlog | awk '{print\$2" "\$3" "\$4" "\$5}'));
chomp($lastline=qx(tail -1 /usr/local/apache/domlogs/ftpxferlog | awk '{print\$2" "\$3" "\$4" "\$5}'));
$firstline =~ tr/\/|:/ / ;
$lastline =~ tr/\/|:/ / ;
my @first= split(/ /, $firstline);
my @last= split(/ /, $lastline);
$firstline = timelocal($first[4],$first[3],$first[2],$first[1],$mon2num{ lc substr($first[0], 0, 3) }-1,$first[5]);
$lastline = timelocal($last[4],$last[3],$last[2],$last[1],$mon2num{ lc substr($last[0], 0, 3) }-1,$last[5]);
if ($firstline < $epoc_mtime && $lastline > $epoc_mtime) {
push @mftp, qx(egrep -H "$searchmftp" /usr/local/apache/domlogs/ftpxferlog);
}
}
if ($owner ne "root") {
@files=();
opendir(DIR, "/home/$owner/logs");
@files = grep(/^ftp.*/,readdir(DIR));
closedir(DIR);
foreach my $file (@files) {
my $filesize = -s "/home/$owner/logs/$file";
if ( $filesize > 80 ) {
my $firstline ;
my $lastline ;
chomp($firstline=qx(zcat /home/$owner/logs/$file | head -1 | awk '{print\$2" "\$3" "\$4" "\$5}'));
chomp($lastline=qx(zcat /home/$owner/logs/$file | tail -1 | awk '{print\$2" "\$3" "\$4" "\$5}'));
$firstline =~ tr/\/|:/ / ;
$lastline =~ tr/\/|:/ / ;
my @first= split(/ /, $firstline);
my @last= split(/ /, $lastline);
$firstline = timelocal($first[4],$first[3],$first[2],$first[1],$mon2num{ lc substr($first[0], 0, 3) }-1,$first[5]);
$lastline = timelocal($last[4],$last[3],$last[2],$last[1],$mon2num{ lc substr($last[0], 0, 3) }-1,$last[5]);
if ($firstline < $epoc_mtime && $lastline > $epoc_mtime) {
push @mftp, qx(zegrep -H "$searchmftp" /home/$owner/logs/$file);
}
}
}
}
chomp(@mftp);
print_normal("Done. ".scalar @mftp. " results found.") if (!$short);
print_normal_chomped("[+] Checking cPanel access logs... ") if (!$short);
my $type;
if ( $a_type == "1" ) {
$type="POST|GET";
} else {
$type="POST";
}
if (-d "/usr/local/cpanel/logs/archive") {
@files=();
opendir(DIR, "/usr/local/cpanel/logs/archive");
@files = grep(/^access_log/,readdir(DIR));
closedir(DIR);
foreach my $file (@files) {
my $firstline ;
my $lastline ;
chomp($firstline=qx(zcat /usr/local/cpanel/logs/archive/$file | head -1 | awk -F'[' '{print\$2}' | awk '{print\$1}'));
chomp($lastline=qx(zcat /usr/local/cpanel/logs/archive/$file | tail -1 | awk -F'[' '{print\$2}' | awk '{print\$1}'));
$firstline =~ tr/\/|:/ / ;
$lastline =~ tr/\/|:/ / ;
my @first= split(/ /, $firstline);
my @last= split(/ /, $lastline);
$firstline = timelocal($first[5],$first[4],$first[3],$first[1],$first[0]-1,$first[2]);
$lastline = timelocal($last[5],$last[4],$last[3],$last[1],$last[0]-1,$last[2]);
if ($firstline < $epoc_mtime && $lastline > $epoc_mtime) {
push @mcpanel, qx(zegrep -H "$searchmcpanel" /usr/local/cpanel/logs/archive/$file | egrep "$type");
}
}
}
my $firstline ;
my $lastline ;
chomp($firstline=qx(head -1 /usr/local/cpanel/logs/access_log | awk -F'[' '{print\$2}' | awk '{print\$1}'));
chomp($lastline=qx(tail -1 /usr/local/cpanel/logs/access_log | awk -F'[' '{print\$2}' | awk '{print\$1}'));
$firstline =~ tr/\/|:/ / ;
$lastline =~ tr/\/|:/ / ;
my @first ;
my @last ;
@first= split(/ /, $firstline);
@last= split(/ /, $lastline);
$firstline = timelocal($first[5],$first[4],$first[3],$first[1],$first[0]-1,$first[2]);
$lastline = timelocal($last[5],$last[4],$last[3],$last[1],$last[0]-1,$last[2]);
if ($firstline < $epoc_mtime && $lastline > $epoc_mtime) {
push @mcpanel, qx(egrep -H "$searchmcpanel" /usr/local/cpanel/logs/access_log | egrep "$type");
}
chomp(@mcpanel);
print_normal("Done. ".scalar @mcpanel. " results found.") if (!$short);
if ($owner ne "root") {
print_normal_chomped("[+] Checking Apache access logs... ") if (!$short);
opendir(DIR, "/home/$owner/access-logs");
my @files = grep(/^(?!(ftp|\.))/,readdir(DIR));
closedir(DIR);
foreach my $file (@files) {
if (-z "/home/$owner/access-logs/$file") {
next ;
}
my $firstline ;
my $lastline ;
chomp($firstline=qx(head -1 /home/$owner/access-logs/$file | awk -F'[' '{print\$2}' | awk '{print\$1}'));
chomp($lastline=qx(tail -1 /home/$owner/access-logs/$file | awk -F'[' '{print\$2}' | awk '{print\$1}'));
$firstline =~ tr/\/|:/ / ;
$lastline =~ tr/\/|:/ / ;
my @first= split(/ /, $firstline);
my @last= split(/ /, $lastline);
$firstline = timelocal($first[5],$first[4],$first[3],$first[0],$mon2num{ lc substr($first[1], 0, 3) }-1,$first[2]);
$lastline = timelocal($last[5],$last[4],$last[3],$last[0],$mon2num{ lc substr($last[1], 0, 3) }-1,$last[2]);
if ($firstline < $epoc_mtime && $lastline > $epoc_mtime) {
push @maccess, qx(egrep -H "$searchmaccess" /home/$owner/access-logs/$file | egrep "$type");
}
}
opendir(DIR, "/home/$owner/logs");
@files = grep(/^(?!(ftp|\.))/,readdir(DIR));
closedir(DIR);
foreach my $file (@files) {
my $firstline ;
my $lastline ;
chomp($firstline=qx(zcat /home/$owner/logs/$file | head -1 | awk -F'[' '{print\$2}' | awk '{print\$1}'));
chomp($lastline=qx(zcat /home/$owner/logs/$file | tail -1 | awk -F'[' '{print\$2}' | awk '{print\$1}'));
$firstline =~ tr/\/|:/ / ;
$lastline =~ tr/\/|:/ / ;
my @first= split(/ /, $firstline);
my @last= split(/ /, $lastline);
$firstline = timelocal($first[5],$first[4],$first[3],$first[0],$mon2num{ lc substr($first[1], 0, 3) }-1,$first[2]);
$lastline = timelocal($last[5],$last[4],$last[3],$last[0],$mon2num{ lc substr($last[1], 0, 3) }-1,$last[2]);
if ($firstline < $epoc_mtime && $lastline > $epoc_mtime) {
push @maccess, qx(zegrep -H "$searchmaccess" /home/$owner/logs/$file | egrep "$type");
}
}
chomp (@maccess);
print_normal("Done. ".scalar @maccess. " results found.") if (!$short);
}
print_normal(" ") if (!$short);
}
sub print_matches {
print_header("Matches: ".localtime($epoc_mtime)." ($epoc_mtime)") if (!$short);
print_header("---------------------------------------------") if (!$short);
if (scalar @mbash > 0) {
print MAGENTA ".bash_history\n" if (!$short);
foreach (@mbash) {
print_status($_);
}
print_normal (" ") if (!$short);
}
if (scalar @mmessages > 0) {
print MAGENTA "/var/log/messages\n" if (!$short);
foreach (@mmessages) {
print_status($_);
}
print_normal (" ") if (!$short);
}
if (scalar @mftp > 0) {
print MAGENTA "ftpxferlog\n" if (!$short);
foreach (@mftp) {
print_status($_);
}
print_normal (" ") if (!$short);
}
if (scalar @mcpanel > 0) {
print MAGENTA "cPanel Access Logs\n" if (!$short);
foreach (@mcpanel) {
print_status($_);
}
print_normal (" ") if (!$short);
}
if (scalar @maccess > 0) {
print MAGENTA "Apache Access Logs\n" if (!$short);
foreach (@maccess) {
print_status($_);
}
print_normal (" ") if (!$short);
}
}
sub print_filestats {
print_header( "\nStatistics: ") if (!$short);
print_header( "---------------------------------------------------------") if (!$short);
print_status( "File: " . File::Spec->rel2abs($fh) ) if (!$short && !$epoc_time);
print_status( "Size: " . (stat($fh))[7] ) if (!$short && !$epoc_time );
print_status( "Modify Time: " . localtime($epoc_mtime) . " ($epoc_mtime)" ) if (!$short && !$epoc_time);
print_status( "Change Time: " . localtime($epoc_ctime) . " ($epoc_ctime)" ) if (!$short && !$epoc_time);
print_status( "Timestamp: " . localtime($epoc_time) . " ($epoc_time)" ) if (!$short && $epoc_time);
print_status( "User: " . $owner ) if (!$short);
print_status("Search Range: $range seconds ") if (!$short);
print CYAN "---------------------------------------------------------\n" if (!$short);
if ($epoc_ctime != $epoc_mtime) {
print_warn("Change and modify timestamps are different. Will search logs twice.") if (!$short);
}
if ($owner eq "root") {
print_warn("User root detected. Skipping some checks.") if (!$short);
}
if (! -e "/var/cpanel/users/$owner" && $owner ne "root") {
print_error("User ($owner) not found. Exiting...\n") if (!$short);
exit;