-
Notifications
You must be signed in to change notification settings - Fork 225
/
functions
1957 lines (1820 loc) · 65.1 KB
/
functions
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
##
# Linux Malware Detect v1.5
# (C) 2002-2016, R-fx Networks <proj@r-fx.org>
# (C) 2016, Ryan MacDonald <ryan@r-fx.org>
# This program may be freely redistributed under the terms of the GNU GPL v2
##
#
prerun() {
if [ ! "$(whoami)" == "root" ]; then
if [ -z "$scan_user_access" ] || [ "$scan_user_access" == "0" ]; then
args="$@"
if [[ "$args" =~ "modsec" ]]; then
echo "1 maldet: OK"
exit
fi
header
echo "public scanning is currently disabled, please contact your system administrator to enable scan_user_access in $cnffile."
exit 1
fi
pub=1
user=`whoami`
quardir="$userbasedir/$user/quar"
sessdir="$userbasedir/$user/sess"
tmpdir="$userbasedir/$user/tmp"
maldet_log="$userbasedir/$user/event_log"
mkdir -p $quardir >> /dev/null 2>&1
mkdir -p $sessdir >> /dev/null 2>&1
mkdir -p $tmpdir >> /dev/null 2>&1
touch $maldet_log
cd $tmpdir
else
echo $ver > $lmd_versionsion_file
fi
if [ ! -d "$sigdir" ]; then
mkdir -p $sigdir
chmod 755 $sigdir
fi
if [ ! -d "$logdir" ]; then
mkdir -p $logdir
chmod 755 $logdir
fi
if [ ! -d "$tmpdir" ]; then
mkdir -p $tmpdir
chmod 755 $tmpdir
else
chmod 755 $tmpdir
fi
if [ ! -d "$sessdir" ]; then
mkdir -p $sessdir
chmod 750 $sessdir
fi
if [ ! -d "$quardir" ]; then
mkdir -p $quardir
chmod 750 $quardir
fi
if [ -z "$wget" ]; then
header
echo "could not find required binary wget, aborting."
exit 1
fi
if [ -z "$md5sum" ]; then
header
echo "could not find required binary md5sum, aborting."
exit 1
fi
if [ -z "$od" ]; then
header
echo "could not find required binary od, aborting."
exit 1
fi
if [ -z "$find" ]; then
header
echo "could not find required binary find, aborting."
exit 1
fi
if [ -z "$perl" ]; then
header
echo "could not find required binary perl, aborting."
exit 1
fi
if [ ! -f "$mail" ]; then
email_alert=0
fi
if [ ! -f "$sig_cust_hex_file" ]; then
touch $sig_cust_hex_file
chmod 644 $sig_cust_hex_file
fi
if [ ! -f "$sig_cust_md5_file" ]; then
touch $sig_cust_md5_file
chmod 644 $sig_cust_md5_file
fi
if [ "$scan_hexfifo" == "1" ]; then
mkfifo=`which mkfifo 2> /dev/null`
if [ ! -f "$mkfifo" ]; then
scan_hexfifo=0
else
if [ -f "$mkfifo" ] && [ ! -p "$hex_fifo_path" ]; then
$mkfifo -m 666 $hex_fifo_path
fi
fi
fi
sed -i '/^$/d' $ignore_paths $ignore_sigs $ignore_inotify $ignore_file_ext
if [ -z "$EDITOR" ]; then
defedit=`which nano 2> /dev/null`
if [ -z "$defedit" ]; then
EDITOR=vi
else
EDITOR=nano
fi
fi
if [ ! "$scan_cpunice" ]; then
scan_cpunice=19
fi
if [ ! "$scan_ionice" ]; then
scan_ionice=6
fi
if [ -f "$nice" ]; then
nice_command="$nice -n $scan_cpunice"
fi
if [ -f "$ionice" ] && [ ! -d "/proc/vz" ]; then
nice_command="$nice_command $ionice -c2 -n $scan_ionice"
fi
if [ -f "$cpulimit" ] && [ "$scan_cpulimit" -gt 2> /dev/null "0" ]; then
max_cpulimit=$[$(egrep -w processor /proc/cpuinfo -c)*100]
if [ "$scan_cpulimit" -gt "$max_cpulimit" ]; then
scan_cpulimit="0"
else
nice_command="$cpulimit -l $scan_cpulimit $nice_command"
fi
fi
}
eout() {
arg="$1"
stdout="$2"
appn=maldet
if [ ! -d "$logdir" ]; then
mkdir -p $logdir ; chmod 700 $logdir
fi
if [ ! -f "$maldet_log" ]; then
touch $maldet_log
fi
log_size=`$wc -l $maldet_log | awk '{print$1}'`
if [ "$log_size" -ge "20000" ]; then
trim=1000
printf "%s\n" "$trim,${log_size}d" w | ed -s $maldet_log
fi
if [ ! "$arg" == "" ]; then
echo "$(date +"%b %d %H:%M:%S") $(hostname -s) $appn($$): $arg" >> $maldet_log
if [ ! -z "$stdout" ]; then
echo "$appn($$): $arg"
fi
fi
}
trap_exit() {
if [ "$svc" == "m" ]; then
echo
eout "{glob} monitor interrupt by user, sending kill." 1
monitor_kill
exit 1
elif [ "$svc" == "a" ] || [ "$svc" == "r" ] || [ "$svc" == "f" ]; then
echo
gen_report
if [ ! "$tot_hits" == "0" ]; then
if [ "$email_ignore_clean" == "1" ] && [ ! "$tot_hits" == "$tot_cl" ]; then
genalert file $nsess
elif [ "$email_ignore_clean" == "0" ]; then
genalert file $nsess
fi
fi
mv $scan_session $nsess_hits 2> /dev/null
rm -f $clamscan_results $find_results $runtime_hdb $runtime_hexstrings $runtime_ndb $scan_session $tmpdir/.find_killed.$scanid $tmpdir/.tmp* $tmpdir/.tmpf* $tmpf
eout "{glob} scan interrupt by user, aborting scan..." 1
eout "{scan} scan report saved, to view run: maldet --report $datestamp.$$" 1
if [ "$quarantine_hits" == "0" ] && [ ! "$tot_hits" == "0" ]; then
eout "{glob} quarantine is disabled! set quarantine_hits=1 in $cnffile or to quarantine results run: maldet -q $datestamp.$$" 1
fi
exit
fi
}
clean_exit() {
mv -f $scan_session $nsess_hits 2> /dev/null
rm -f $clamscan_results $find_results $list $runtime_hdb $runtime_hexstrings $runtime_ndb $scan_session $tmpdir/.find_killed.$scanid $tmpdir/.tmp* $tmpdir/.tmpf* $tmpf 0 2> /dev/null
}
get_remote_file() {
# $1 = URI, $2 = service identifier, $3 boolean do referrer identify, $4 boolean verbose
get_uri="$1"
service="$2"
doid="$3"
verbose="$4"
unset return_file
if [ -z "$get_uri" ]; then
eout "{internal} missing or invalid URI passed to get_remote_files" 1
break
fi
if [ -z "$service" ]; then
svc="internal"
else
svc="$service"
fi
if [ "$doid" == "1" ]; then
id="--referer=\"$lmd_referer\""
else
id=""
fi
tmpf="$tmpdir/.tmpf_get.${RANDOM}"
touch $tmpf ; chmod 700 $tmpf
get_file=`echo "$get_uri" | tr '/' '\n' | tail -n1`
$wget $id -T$wget_timeout -t$wget_retries -q $get_uri -O $tmpf || wget_return=$?
if [ ! -f "$tmpf" ] || [ ! -s "$tmpf" ]; then
eout "{$svc} could not download $get_uri, please try again later." $verbose
unset return_file
else
eout "{$svc} downloaded $get_uri" $verbose
return_file="$tmpf"
fi
}
import_user_sigs() {
if [ "$import_sigs_md5_url" ]; then
get_remote_file "$import_sigs_md5_url" "importsigs" "0" "1"
if [ -f "$return_file" ]; then
cp -f $return_file $sig_cust_md5_file
eout "{importsigs} imported custom signature data from $import_sigs_md5_url" 1
fi
fi
if [ "$import_sigs_hex_url" ]; then
get_remote_file "$import_sigs_hex_url" "importsigs" "0" "1"
if [ -f "$return_file" ]; then
cp -f $return_file $sig_cust_hex_file
eout "{importsigs} imported custom signature data from $import_sigs_hex_url" 1
fi
fi
}
import_conf() {
current_utime=`date +"%s"`
if [ -f "$sessdir/.import_conf.utime" ]; then
import_utime=`cat $sessdir/.import_conf.utime`
if [ -z "$import_utime" ]; then
import_utime="0"
fi
import_diff=$[current_utime-import_utime]
if [ "$import_diff" -lt "import_config_expire" ]; then
import_config_skip="1"
eout "{importconf} configuration expire value has not lapsed ($import_dif/${import_config_expire}), using cache." 1
fi
fi
if [ "$import_config_url" ]; then
if [ -z "$import_config_skip" ]; then
get_remote_file "$import_config_url" "importconf" "0" "1"
if [ -f "$return_file" ]; then
cp -f $return_file $sessdir/.import_conf.cache
echo "$current_utime" > $sessdir/.import_conf.utime
fi
fi
if [ -f "$sessdir/.import_conf.cache" ]; then
source $cnf
source $intcnf
source $sessdir/.import_conf.cache
eout "{importconf} imported configuration from $import_config_uri" 1
fi
fi
}
clamav_linksigs() {
cpath="$1"
if [ -d "$cpath" ]; then
rm -f $cpath/rfxn.* ; cp -f $sigdir/rfxn.ndb $sigdir/rfxn.hdb $cpath/ 2> /dev/null
rm -f $cpath/lmd.user.* ; cp -f $sigdir/lmd.user.ndb $sigdir/lmd.user.hdb $cpath/ 2> /dev/null
fi
}
usage_short() {
cat <<EOF
signature set: $sig_version
usage maldet [-h|--help] [-a|--scan-all PATH] [-r|--scan-recent PATH DAYS]
[-f|--file-list PATH] [-i|--include-regex] [-x|--exclude-regex]
[-b|--background] [-m|--monitor] [-k|--kill-monitor] [-c|--checkout]
[-q|--quarantine] [-s|--restore] [-n|--clean] [-l|--log] [-e|--report]
[-u|--update-sigs] [-d|--update-ver]
EOF
}
usage_long() {
cat<<EOF
signature set: $sig_version
usage $0 [ OPTION ]
-b, --background
Execute operations in the background, ideal for large scans
e.g: maldet -b -r /home/?/public_html 7
-u, --update-sigs
Update malware detection signatures from rfxn.com
-d, --update-ver
Update the installed version from rfxn.com
-f, --file-list
Scan files or paths defined in line spaced file
e.g: maldet -f /root/scan_file_list
-r, --scan-recent PATH DAYS
Scan files created/modified in the last X days (default: 7d, wildcard: ?)
e.g: maldet -r /home/?/public_html 2
-a, --scan-all PATH
Scan all files in path (default: /home, wildcard: ?)
e.g: maldet -a /home/?/public_html
-i, --include-regex REGEX
Include paths/files from file list based on supplied posix-egrep regular
expression.
e.g: To include only paths named wp-content and files ending in .php:
--include-regex ".*/wp-content/.*|.*.php$"
-x, --exclude-regex REGEX
Exclude paths/files from file list based on supplied posix-egrep regular
expression.
e.g: To exclude paths containing 'wp-content/w3tc/' and core files:
--exclude-regex ".*wp-content/w3tc/.*|.*core.[0-9]+$"
-m, --monitor USERS|PATHS|FILE|RELOAD
Run maldet with inotify kernel level file create/modify monitoring
If USERS is specified, monitor user homedirs for UID's > 500
If FILE is specified, paths will be extracted from file, line spaced
If PATHS are specified, must be comma spaced list, NO WILDCARDS!
e.g: maldet --monitor users
e.g: maldet --monitor /root/monitor_paths
e.g: maldet --monitor /home/mike,/home/ashton
-k, --kill-monitor
Terminate inotify monitoring service
-c, --checkout FILE
Upload suspected malware to rfxn.com for review & hashing into signatures
-l, --log
View maldet log file events
-e, --report SCANID email
View scan report of most recent scan or of a specific SCANID and optionally
e-mail the report to a supplied e-mail address
e.g: maldet --report
e.g: maldet --report list
e.g: maldet --report 050910-1534.21135
e.g: maldet --report SCANID user@domain.com
-s, --restore FILE|SCANID
Restore file from quarantine queue to orginal path or restore all items from
a specific SCANID
e.g: maldet --restore $varlibpath/quarantine/config.php.23754
e.g: maldet --restore 050910-1534.21135
-q, --quarantine SCANID
Quarantine all malware from report SCANID
e.g: maldet --quarantine 050910-1534.21135
-n, --clean SCANID
Try to clean & restore malware hits from report SCANID
e.g: maldet --clean 050910-1534.21135
-U, --user USER
Set execution under specified user, ideal for restoring from user quarantine or
to view user reports.
e.g: maldet --user nobody --report
e.g: maldet --user nobody --restore 050910-1534.21135
-co, --config-option VAR1=VALUE,VAR2=VALUE,VAR3=VALUE
Set or redefine the value of $cnffile config options
e.g: maldet --config-option email_addr=you@domain.com,quarantine_hits=1
-p, --purge
Clear logs, quarantine queue, session and temporary data.
EOF
}
clean() {
file="$1"
file_signame="$2"
file_owner="$3"
file_chmod="$4"
file_size="$5"
file_md5="$6"
v="$3"
sh_hitname=`echo $hitname | sed -e 's/{HEX}//' -e 's/{MD5}//' | tr '.' ' ' | awk '{print$1"."$2"."$3}'`
if [ -d "$cldir" ] && [ "$quarantine_clean" == "1" ] && [ "$quarantine_hits" == "1" ] && [ -f "$file" ]; then
if [ -f "$cldir/$sh_hitname" ] || [ -f "$cldir/custom.$sh_hitname" ] && [ -f "$file.info" ]; then
file_path=`cat $file.info | egrep -v '#' | awk '{print$6}'`
eout "{clean} restoring $file for cleaning attempt" $v
restore "$file" >> /dev/null 2>&1
if [ -f "$cldir/$sh_hitname" ]; then
eout "{clean} attempting to clean $file_path with $sh_hitname rule" $v
$cldir/$sh_hitname "$file_path" "$file_signame" "$file_owner" "$file_chmod" "$file_size" "$file_md5"
fi
if [ -f "$cldir/custom.$sh_hitname" ]; then
eout "{clean} attempting to clean $file_path with custom.$sh_hitname rule" $v
$cldir/custom.$sh_hitname "$file_path" "$file_signame" "$file_owner" "$file_chmod" "$file_size" "$file_md5"
fi
eout "{clean} rescanning $file_path for malware hits" $v
cleanst="1"
scan_stage1 "$file_path" >> /dev/null 2>&1
unset cleanst
if [ -f "$file_path" ]; then
echo "$file_path" >> $sessdir/clean.$$
echo "$file_path" >> $sessdir/.clean.hist
eout "{clean} clean successful on $file_path" $v
else
eout "{clean} clean failed on $file_path and returned to quarantine" $v
fi
elif [ -f "$cldir/$sh_hitname" ] || [ -f "$cldir/custom.$sh_hitname" ] && [ -f "$file" ]; then
file_path="$file"
if [ -f "$cldir/$sh_hitname" ]; then
eout "{clean} attempting to clean $file with $sh_hitname rule" $v
$cldir/$sh_hitname "$file_path"
fi
if [ -f "$cldir/custom.$sh_hitname" ]; then
eout "{clean} attempting to clean $file with custom.$sh_hitname rule" $v
$cldir/custom.$sh_hitname "$file_path"
fi
eout "{clean} scanning $file for malware hits" $v
cleanst="1"
unset clean_failed
scan_stage1 "$file_path" 1 >> /dev/null 2>&1
unset cleanst
if [ "$clean_failed" == "1" ]; then
eout "{clean} clean failed on $file" $v
else
echo "$file" >> $sessdir/clean.$$
echo "$file_path" >> $sessdir/.clean.hist
eout "{clean} clean successful on $file" $v
fi
fi
else
if [ "$quarantine_clean" == "1" ] && [ "$quarantine_hits" == "1" ]; then
eout "file path error on $file, aborting." $v
exit
else
eout "malware cleaning disabled - skipping $file" $v
fi
fi
}
restore() {
file="$1"
fname=`basename $file`
if [ -f "$quardir/$file" ] && [ -f "$quardir/$file.info" ]; then
file_owner=`cat $quardir/$file.info | egrep -v '#' | awk '{print$1}'`
file_group=`cat $quardir/$file.info | egrep -v '#' | awk '{print$2}'`
file_mode=`cat $quardir/$file.info | egrep -v '#' | awk '{print$3}'`
file_size=`cat $quardir/$file.info | egrep -v '#' | awk '{print$4}'`
md5_hash=`cat $quardir/$file.info | egrep -v '#' | awk '{print$5}'`
file_path=`cat $quardir/$file.info | egrep -v '#' | awk '{print$6}'`
chown $file_owner.$file_group "$quardir/$file" >> /dev/null 2>&1
chmod $file_mode "$quardir/$file" >> /dev/null 2>&1
mv -f "$quardir/$file" "$file_path"
eout "{restore} quarantined file $file restored to $file_path" 1
elif [ -f "$file" ] && [ -f "$file.info" ]; then
file_owner=`cat $file.info | egrep -v '#' | awk '{print$1}'`
file_group=`cat $file.info | egrep -v '#' | awk '{print$2}'`
file_mode=`cat $file.info | egrep -v '#' | awk '{print$3}'`
file_size=`cat $file.info | egrep -v '#' | awk '{print$4}'`
md5_hash=`cat $file.info | egrep -v '#' | awk '{print$5}'`
file_path=`cat $file.info | egrep -v '#' | awk '{print$6}'`
chown $file_owner.$file_group "$file" >> /dev/null 2>&1
chmod $file_mode "$file" >> /dev/null 2>&1
mv -f "$file" "$file_path"
eout "{restore} quarantined file $file restored to $file_path" 1
else
eout "{restore} invalid file or could not be found" 1
fi
}
restore_hitlist() {
hitlist="$sessdir/session.hits.$1"
if [ -f "$hitlist" ]; then
val_aquar=`tail -n1 $hitlist | awk '{print$5}'`
val_mquar=`tail -n1 $hitlist | awk '{print$3}'`
if [ "$val_aquar" ]; then
for file in `cat $hitlist | awk '{print$5}'`; do
if [ -f "$file" ]; then
restore $file
fi
done
elif [ "$val_mquar" ]; then
for file in `cat $hitlist | awk '{print$3}'`; do
quar_file=`cat $maldet_log | grep -w "$file" | tail -n1 | awk '{print$12}' | tr -d "'"`
restore $quar_file
done
else
eout "{restore} could not find a valid hit list to restore." 1
fi
fi
}
clean_hitlist() {
hitlist="$sessdir/session.hits.$1"
if [ -f "$hitlist" ]; then
for file in `cat $hitlist | awk '{print$3}'`; do
if [ -f "$file" ]; then
hitname=`cat $hitlist | grep $file | awk '{print$1}'`
clean "$quardir/$file_name.$rnd" "$hitname" "$file_owner.$file_group" "$file_mode" "$file_size" "$md5_hah" "$file"
else
hitname=`cat $hitlist | grep $file | awk '{print$1}'`
quarfile=`cat $hitlist | grep $file | awk '{print$5}'`
clean "$quardir/$file_name.$rnd" "$hitname" "$file_owner.$file_group" "$file_mode" "$file_size" "$md5_hah" "$file"
fi
done
else
eout "{clean} invalid SCANID, aborting." 1
exit
fi
}
view_report() {
rid="$1"
if [ "$rid" == "list" ]; then
tmpf="$tmpdir/.areps$$"
for file in `ls $sessdir/session.[0-9]* 2> /dev/null`; do
SCANID=`cat $file | grep "SCAN ID"`
FILES=`cat $file | grep "TOTAL FILES" | sed 's/TOTAL //'`
HITS=`cat $file | grep "TOTAL HITS" | sed 's/TOTAL //'`
CLEAN=`cat $file | grep "TOTAL CLEANED" | sed 's/TOTAL //'`
TIME=`cat $file | grep -iE "TIME|STARTED" | sed -e 's/TIME: //' -e 's/STARTED: //' | awk '{print$1,$2,$3,$4}'`
TIME_U=`date -d "$TIME" "+%s" 2> /dev/null`
if [ ! -z "$SCANID" ] && [ ! -z "$TIME" ]; then
clean_zero=`echo $CLEAN | awk '{print$2}'`
if [ -z "$clean_zero" ]; then
CLEAN="CLEANED: 0"
fi
echo "$TIME_U | $TIME | $SCANID | $FILES | $HITS | $CLEAN" >> $tmpf
fi
done
if [ -f "$tmpf" ]; then
cat $tmpf | sort -k1 -n | tac | cut -d'|' -f2-6 | column -t | more
rm -f $tmpf
exit 0
else
echo "error no report data found"
exit 1
fi
fi
if [ -f "$sessdir/session.$rid" ] && [ ! -z "$(echo $2 | grep '\@')" ]; then
cat $sessdir/session.$rid | $mail -s "$email_subj" "$2"
eout "{report} report ID $rid sent to $2" 1
exit
fi
if [ "$rid" == "" ] && [ -f "$sessdir/session.last" ]; then
rid=`cat $sessdir/session.last`
$EDITOR $sessdir/session.$rid
elif [ -f "$sessdir/session.$rid" ]; then
$EDITOR $sessdir/session.$rid
else
echo "{report} no report found, aborting."
exit
fi
}
view() {
echo "Viewing last 50 lines from $maldet_log:"
tail -n 50 $maldet_log
}
purge() {
:> $maldet_log
rm -f $tmpdir/* $quardir/* $sessdir/*
eout "{glob} logs and quarantine data cleared by user request (-p)" 1
}
quarantine_suspend_user() {
file="$1"
user=`$stat -c "%U" "$file"`
user_id=`id -u $user`
if [ ! "$user" == "" ] && [ "$user_id" -ge "$quarantine_suspend_user_minuid" ]; then
if [ -f "/scripts/suspendacct" ]; then
if [ ! -f "/var/cpanel/suspended/$user" ]; then
/scripts/suspendacct $user "maldet --report $datestamp.$$" >> /dev/null 2>&1
eout "{quar} account $user cpanel suspended" 1
echo "$user" >> $sessdir/suspend.users.$$
echo "$user" >> $sessdir/.susp.hist
fi
else
if [ "$(grep $user /etc/passwd | cut -d':' -f7 | grep /bin/false)" == "" ]; then
/usr/sbin/usermod -s /bin/false $user >> /dev/null 2>&1
eout "{quar} account $user suspended; set 'usermod -s /bin/false'"
echo "$user" >> $sessdir/suspend.users.$$
echo "$user" >> $sessdir/.susp.hist
fi
fi
fi
}
record_hit() {
file="$1"
hitname="$2"
if [ -f "$file" ]; then
file_owner=`$stat -c "%U" "$file"`
file_group=`$stat -c "%G" "$file"`
file_mode=`$stat -c "%a" "$file"`
file_size=`$stat -c "%s" $file`
# atime mtime ctime (since epoch)
file_times=`$stat -c "%X %Y %Z" $file`
file_name=`basename "$file"`
if [ ! "$md5_hash" ]; then
md5_hash=`$md5sum $file | awk '{print$1}'`
fi
eout "{hit} malware hit $hitname found for $file"
echo "${utime}:${hostid}:${hitname}:${md5_hash}:${file_size}:${file_owner}.${file_group}:${file}" >> $hits_history
fi
}
quarantine() {
file="$1"
hitname="$2"
file_name=`basename $file`
if [ -f "$file" ] && [ -d "$quardir" ]; then
if [ "$quarantine_hits" == "1" ]; then
file_namewc=`echo $file_name | $wc -m`
rnd="${RANDOM}${RANDOM}"
if [ "$quarantine_suspend_user" == "1" ]; then
quarantine_suspend_user "$file"
fi
chattr -ia "$file"
mv "$file" "$quardir/$file_name.$rnd"
if [ "$pub" == "1" ]; then
chmod 400 "$quardir/$file_name.$rnd"
else
chmod 000 "$quardir/$file_name.$rnd"
chown root.root "$quardir/$file_name.$rnd"
fi
echo -e "# owner group mode size(b) md5 path atime(epoch) mtime(epoch) ctime(epoch)\n$file_owner $file_group $file_mode $file_size $md5_hash $file $file_times" > $quardir/$file_name.$rnd.info
eout "{quar} malware quarantined from '$file' to '$quardir/$file_name.$rnd'"
echo "$utime:$hitname:$file:$file_owner:$file_group:$md5_hash:$file_size:$quardir/$file_name.$rnd" >> $quar_history
if [ ! -z "$scan_session" ]; then
echo "$hitname : $file => $quardir/$file_name.$rnd" >> $scan_session
fi
if [ "$quarantine_clean" == "1" ] && [ ! "$cleanst" == "1" ]; then
unset cleanst
clean "$quardir/$file_name.$rnd" "$hitname" "$file_owner.$file_group" "$file_mode" "$file_size" "$md5_hash" "$file"
fi
else
if [ ! -z "$scan_session" ]; then
echo "$hitname : $file" >> $scan_session
fi
fi
else
eout "{quar} fatal error handling '$file'"
fi
}
quar_hitlist() {
hitlist="$sessdir/session.hits.$1"
if [ -f "$hitlist" ]; then
for file in `cat $hitlist | awk '{print$3}'`; do
if [ -f "$file" ]; then
file_owner=`$stat -c "%U" "$file"`
file_group=`$stat -c "%G" "$file"`
file_mode=`$stat -c "%a" "$file"`
file_size=`$stat -c "%s" $file`
file_name=`basename "$file"`
file_namewc=`echo $file_name | $wc -m`
if [ ! "$md5_hash" ]; then
md5_hash=`$md5sum $file | awk '{print$1}'`
fi
if [ "$pub" == "1" ]; then
chattr -ia "$file" >> /dev/null 2>&1
chmod 400 "$file"
else
chattr -ia "$file"
chmod 000 "$file"
fi
rnd="${RANDOM}${RANDOM}"
mv "$file" "$quardir/$file_name.$rnd"
echo -e "# owner group mode size(b) md5 path atime(epoch) mtime(epoch) ctime(epoch)\n$file_owner $file_group $file_mode $file_size $md5_hash $file $file_times" > $quardir/$file_name.$rnd.info
eout "{quar} malware quarantined from '$file' to '$quardir/$file_name.$rnd'" 1
echo "$utime:$hitname:$file:$file_owner:$file_group:$md5_hash:$file_size:$quardir/$file_name.$rnd" >> $quar_history
if [ "$quarantine_suspend_user" == "1" ]; then
quarantine_suspend_user "$file"
fi
if [ "$quarantine_clean" == "1" ] && [ ! "$cleanst" == "1" ]; then
unset cleanst
hitname=`cat $hitlist | grep $file | awk '{print$1}'`
clean "$quardir/$file_name.$rnd" "$hitname" "$file_owner.$file_group" "$file_mode" "$file_size" "$md5_hah" "$file"
fi
fi
done
else
echo "{quar} invalid quarantine hit list, aborting."
exit
fi
}
scan() {
scan_start_hr=`date +"%b %e %Y %H:%M:%S %z"`
scan_start=`date +"%s"`
spath=`echo $1 | tr '?' '*' | tr ',' ' '`
days="$2"
scanid="$datestamp.$$"
if [ "$file_list" ]; then
spath="$file_list"
fi
if [ -f "$spath" ] && [ -z "$file_list" ]; then
single_filescan=1
fi
if [ "$scan_clamscan" == "1" ]; then
isclamd=`pgrep -x clamd 2> /dev/null`
isclamd_root=`pgrep -x -u root clamd 2> /dev/null`
if [ "$isclamd" ] && [ "$isclamd_root" ]; then
clambin="clamdscan"
else
clambin="clamscan"
fi
if [ -f "/usr/local/cpanel/3rdparty/bin/$clambin" ]; then
clamscan="/usr/local/cpanel/3rdparty/bin/$clambin"
elif [ -f "$(which $clambin 2> /dev/null)" ]; then
clamscan=`which $clambin 2> /dev/null`
else
scan_clamscan="0"
fi
fi
if [ ! -f "$sig_md5_file" ]; then
eout "{scan} required signature file not found ($sig_md5_file), try running -u|--update, aborting!" 1
clean_exit
exit 1
fi
if [ ! -f "$sig_hex_file" ]; then
eout "{scan} required signature file not found ($sig_hex_file), try running -u|--update, aborting!" 1
clean_exit
exit 1
fi
if [ ! -f "$find" ]; then
eout "{scan} could not locate find command" 1
clean_exit
exit 1
fi
if [ ! -f "$ignore_paths" ]; then
touch $ignore_paths
chmod 640 $ignore_paths
elif [ ! -f "$ignore_sigs" ]; then
touch $ignore_sigs
chmod 640 $ignore_sigs
fi
if [ ! "$days" == "all" ] && [ -z "$file_list" ]; then
val=`echo $days | grep "[[:alpha:]]"`
if [ ! -z "$val" ]; then
eout "{scan} days value must be numeric value in the range of 1 - 60, reverting to default (7)." 1
days=7
elif [ "$days" -gt "60" ]; then
eout "{scan} days value must be numeric value in the range of 1 - 60, reverting to default (7)." 1
days=7
fi
fi
if [ ! "${spath:0:1}" = "/" ]; then
eout "{scan} must use absolute path, provided relative path $spath" 1
exit
fi
if [ ! -d $(echo $spath | tr '*' ' ' | awk '{print$1}') ] && [ ! -f $(echo $spath | tr '*' ' ' | awk '{print$1}') ]; then
eout "{scan} invalid path $spath" 1
exit
fi
scan_session="$tmpdir/.sess.$$"
find_results="$tmpdir/.find.$$"
touch $find_results
touch $scan_session
sigignore
gensigs
hex_sigs=`$wc -l $sig_hex_file | awk '{print$1}'`
md5_sigs=`$wc -l $sig_md5_file | awk '{print$1}'`
custhex_sigs=`$wc -l $sig_cust_hex_file | awk '{print$1}'`
custmd5_sigs=`$wc -l $sig_cust_md5_file | awk '{print$1}'`
cust_sigs=$[custhex_sigs+custmd5_sigs]
tot_sigs=$[md5_sigs+hex_sigs+custhex_sigs+custmd5_sigs]
if [ -z "$hscan" ]; then
eout "{scan} signatures loaded: $tot_sigs ($md5_sigs MD5 / $hex_sigs HEX / $cust_sigs USER)" 1
fi
if [ -f "$ignore_file_ext" ]; then
if [ ! "$(cat $ignore_file_ext)" == "" ]; then
for i in `cat $ignore_file_ext`; do
if [ "$ignore_fext" == "" ]; then
ignore_fext="-not -iname \"*$i\""
else
ignore_fext="$ignore_fext -not -iname \"*$i\""
fi
done
fi
fi
if [ "$scan_ignore_root" == "1" ]; then
ignore_root="-not -uid 0 -not -gid 0"
fi
if [ "$scan_ignore_user" ]; then
for i in `echo $scan_ignore_user | tr ', ' ' '`; do
if [ "$ignore_user" == "" ]; then
ignore_user="-not -user $i"
else
ignore_user="$ignore_user -not -user $i"
fi
done
fi
if [ "$scan_ignore_group" ]; then
for i in `echo $scan_ignore_group | tr ', ' ' '`; do
if [ "$ignore_group" == "" ]; then
ignore_group="-not -group $i"
else
ignore_group="$ignore_group -not -group $i"
fi
done
fi
if [ "$scan_tmpdir_paths" ] && [ -z "$hscan" ] && [ -z "$single_filescan" ]; then
spath_tmpdirs="$scan_tmpdir_paths"
fi
if [ -f "$clamscan" ] && [ "$scan_clamscan" == "1" ]; then
scan_max_filesize=`cat $sig_md5_file | cut -d':' -f2 | sort -n | tail -n1`
if [ "$scan_max_filesize" -gt "1" 2> /dev/null ]; then
scan_max_filesize=$[scan_max_filesize+1]
scan_max_filesize="${scan_max_filesize}c"
else
scan_max_filesize="768k"
fi
fi
if [ "$file_list" ]; then
cat $file_list | grep -vf $ignore_paths > $find_results
else
if [ "$single_filescan" ]; then
find_recentops=""
elif [ "$days" == "all" ]; then
if [ -z "$hscan" ]; then
eout "{scan} building file list for $spath, this might take awhile..." 1
fi
find_recentops=""
else
rscan=1
if [ -z "$hscan" ]; then
eout "{scan} building file list for $spath of new/modified files from last $days days, this might take awhile..." 1
fi
find_recentopts="( -mtime -$days -o -ctime -$days )"
fi
if [ -z "$scan_find_timeout" ];then
scan_find_timeout=0
fi
if [ "$scan_find_timeout" -ge "60" ]; then
echo -e "sleep $scan_find_timeout\ntouch $tmpdir/.find_killed.$scanid\npkill -f lmd_find" > $tmpdir/.lmd_find_sleep.$$
sh -c "sh $tmpdir/.lmd_find_sleep.$$ >> /dev/null 2>&1 &" >> /dev/null 2>&1 &
rm -f $tmpdir/.lmd_find_sleep.$$
eout "{scan} setting maximum execution time for 'find' file list: ${scan_find_timeout}sec" 1
fi
if [ -z "$hscan" ]; then
eout "{scan} setting nice scheduler priorities for all operations: cpunice $scan_cpunice , ionice $scan_ionice" 1
fi
file_list_start=`date +"%s"`
tmpscandir="$tmpdir/scan.$RANDOM"
mkdir -p "$tmpscandir" ; chmod 700 $tmpscandir ; cd $tmpscandir
eout "{scan} executed $nice_command $find $spath $spath_tmpdirs -maxdepth $scan_max_depth $find_opts -type f $find_recentopts -size +${scan_min_filesize}c -size -$scan_max_filesize $include_regex -not -perm 000 -not -regex \"$exclude_regex\" $ignore_fext $ignore_root $ignore_user $ignore_group"
$nice_command $find /lmd_find/ $spath $spath_tmpdirs -maxdepth $scan_max_depth $find_opts -type f $find_recentopts -size +${scan_min_filesize}c -size -$scan_max_filesize $include_regex -not -perm 000 -not -regex "$exclude_regex" $ignore_fext $ignore_root $ignore_user $ignore_group 2> /dev/null | grep -vf $ignore_paths > $find_results
cd $tmpdir
rm -rf $tmpscandir
if [ "$rscan" = "1" ] && [ "$LMDCRON" == "1" ] && [ "$scan_export_filelist" == "1" ]; then
rm -f $tmpdir/.find_results.* ; cp $find_results $tmpdir/.find_results.shared.$$
ln -fs $tmpdir/.find_results.shared.$$ $tmpdir/find_results.last
fi
file_list_end=`date +"%s"`
file_list_et=$[file_list_end-file_list_start]
if [ -f "$tmpdir/.find_killed.$scanid" ]; then
rm -f $tmpdir/.find_killed.$scanid
echo && eout "{scan} file list 'find' operation reached maximum execution time (${scan_find_timeout}sec) and was terminated" 1
else
pkill -f lmd_find_sleep >> /dev/null 2>&1
fi
fi
if [ ! -f "$find_results" ] || [ -z "$(cat $find_results)" ]; then
if [ -z "$hscan" ]; then
if [ "$days" == "all" ]; then
eout "{scan} scan returned zero results, please provide a new path." 1
rm -f $find_results $scan_session $runtime_ndb $runtime_hdb $runtime_hexstrings $clamscan_results $tmpdir/.tmpf*
exit 0
else
eout "{scan} scan returned zero results, please increase days range or provide a new path." 1
rm -f $find_results $scan_session $runtime_ndb $runtime_hdb $runtime_hexstrings $clamscan_results
exit 0
fi
fi
fi
res_col="1"
move_to_col="echo -en \\033[${res_col}G"
tot_files=`$wc -l $find_results | awk '{print$1}'`
if [ -z "$hscan" ] && [ -z "$single_filescan" ]; then
if [ "$file_list" ]; then
eout "{scan} user supplied file list '$file_list', found $tot_files files..." 1
else
eout "{scan} file list completed in ${file_list_et}s, found $tot_files files..." 1
fi
fi
touch $sessdir/clean.$$
if [ ! -f "$scan_session" ]; then
touch $scan_session
fi
if [ ! -z "$hscan" ]; then
eout "{scan.hook} scan of $spath in progress (id: $datestamp.$$)"
fi
cnt=0
if [ -f "$clamscan" ] && [ "$scan_clamscan" == "1" ]; then
if [ -z "$hscan" ]; then
eout "{scan} found clamav binary at $clamscan, using clamav scanner engine..." 1
fi
if [ "$string_length_scan" == "1" ]; then
if [ -z "$hscan" ]; then
eout "{scan} preprocessing file list for string length hits..." 1
scan_strlen list "$find_results" >> /dev/null 2>&1
fi
fi
if [ -f "/usr/local/cpanel/3rdparty/share/clamav/main.cld" ] || [ -f "/usr/local/cpanel/3rdparty/share/clamav/main.cvd" ]; then
clamav_db="-d /usr/local/cpanel/3rdparty/share/clamav"
elif [ -f "/var/lib/clamav/main.cld" ] || [ -f "/var/lib/clamav/main.cvd" ]; then
clamav_db="-d /var/lib/clamav"
elif [ -f "/var/clamav/main.cld" ] || [ -f "/var/clamav/main.cvd" ]; then
clamav_db="-d /var/clamav"
elif [ -f "/usr/share/clamav/main.cld" ] || [ -f "/usr/share/clamav/main.cvd" ]; then
clamav_db="-d /usr/share/clamav"
fi
if [ -z "$hscan" ]; then
eout "{scan} scan of $spath ($tot_files files) in progress..." 1
fi
trim_log $clamscan_log 10000 1
echo "$(date +"%b %d %H:%M:%S") $(hostname -s) clamscan start" >> $clamscan_log
if [ "$isclamd" ] && [ "$isclamd_root" ]; then
clamopts=""
else
clamopts="--max-filesize=5M --max-scansize=5M -d $runtime_hdb -d $runtime_ndb $clamav_db -r"
fi
clamscan_results="$tmpdir/.clamscan.$$"
echo "$(date +"%b %d %H:%M:%S") $(hostname -s) executed: $nice_command $clamscan $clamopts --infected --no-summary -f $find_results" >> $clamscan_log
$nice_command $clamscan $clamopts --infected --no-summary -f $find_results > $clamscan_results 2>> $clamscan_log || clamscan_return=$?
if [ "$clamscan_return" == "2" ]; then
eout "{scan} clamscan returned an error, check $clamscan_log for more details!" 1
echo "$(date +"%b %d %H:%M:%S") $(hostname -s) clamscan end" >> $clamscan_log
fi
for hit in `cat $clamscan_results | egrep -v 'ERROR$|lstat()' | tr -d ':' | sed 's/.UNOFFICIAL//' | awk '{print$2":"$1}'`; do
file=`echo $hit | tr ':' ' ' | awk '{print$2}'`
signame=`echo $hit | tr ':' ' ' | awk '{print$1}'`
clamsig=`echo $signame | grep -iE "HEX|MD5"`
if [ -z "$clamsig" ]; then
signame="{CAV}$signame"
fi
record_hit "$file" "$signame"
quarantine "$file" "$signame"
if [ ! "$set_background" == "1" ]; then
tot_hits=`$wc -l $scan_session | awk '{print$1}'`
tot_cl=`$wc -l $sessdir/clean.$$ | awk '{print$1}'`
if [ -z "$hscan" ]; then
echo -en "\\033[${res_col}G" && echo -n "maldet($$): {scan} processing scan results for hits: $tot_hits hits $tot_cl cleaned"
fi
cnt="$tot_files"
fi
done
echo
echo "$(date +"%b %d %H:%M:%S") $(hostname -s) clamscan end" >> $clamscan_log
else
if [ -z "$hscan" ]; then
eout "{scan} scan of $spath ($tot_files files) in progress..." 1
fi
SAVEIFS="$IFS"
IFS=$(echo -en "\n\b")
if [ ! -f "$scan_session" ]; then
touch $scan_session
fi
for rpath in `cat $find_results`; do
((cnt++))