-
Notifications
You must be signed in to change notification settings - Fork 2
/
rkhunter
15419 lines (12070 loc) · 416 KB
/
rkhunter
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
#!/bin/sh
#
# rkhunter -- Scan the system for rootkits and other known security issues.
#
# Copyright (c) 2003-2009, Michael Boelen ( michael AT rootkit DOT nl )
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
#
#
# Unfortunately we must do some O/S checks at the very beginning,
# otherwise SunOS will complain about some of the ksh/bash syntax.
# By default the SunOS root account uses a simple Bourne shell,
# which does not work with RKH. So we exec to use the Bash shell
# if it is present, or the Korn shell which is usually installed
# by default on Solaris systems.
#
OPERATING_SYSTEM=`uname 2>/dev/null`
if [ "${OPERATING_SYSTEM}" = "SunOS" ]; then
# Simple SunOS test of RANDOM to see if we are now running bash or ksh.
if [ -z "$RANDOM" ]; then
# If the 'which' output contains a space, then it is probably an error.
if [ -n "`which bash 2>/dev/null | grep -v ' '`" ]; then
exec bash $0 $*
elif [ -n "`which ksh 2>/dev/null | grep -v ' '`" ]; then
exec ksh $0 $*
else
echo "Unable to find the bash or ksh shell to run rkhunter."
exit 1
fi
exit 0
fi
fi
#
# Check to see if we are using the '--debug' option. If so, then
# we exec to log everything to the debug file.
#
if [ -n "`echo \"$*\" | grep '\-\-debug'`" ]; then
RKHDEBUGBASE="/tmp/rkhunter-debug"
#
# Ensure we create a random file name.
#
if [ -n "`which mktemp 2>/dev/null | grep -v ' '`" ]; then
RKHDEBUGFILE=`mktemp ${RKHDEBUGBASE}.XXXXXXXXXX`
elif [ -n "$RANDOM" ]; then
RKHDEBUGFILE="${RKHDEBUGBASE}.$RANDOM"
elif [ -n "`date +%N%s 2>/dev/null | grep '^[0-9][0-9]*$'`" ]; then
RKHDEBUGFILE="${RKHDEBUGBASE}.`date +%N%s%N`"
else
RKHDEBUGFILE="${RKHDEBUGBASE}.`date +%Y%m%d%H%M%S`"
fi
if [ -e "${RKHDEBUGFILE}" ]; then
if [ -f "${RKHDEBUGFILE}" -a ! -h "${RKHDEBUGFILE}" ]; then
rm -f ${RKHDEBUGFILE} >/dev/null 2>&1
else
echo "Cannot use '--debug' option. \"${RKHDEBUGFILE}\" already exists, but it is not a file."
exit 1
fi
fi
DEBUG_OPT=1
exec 1>${RKHDEBUGFILE} 2>&1
chmod 600 ${RKHDEBUGFILE} >/dev/null 2>&1
set -x
else
DEBUG_OPT=0
fi
#
# Now we must determine if we are using the Korn shell or not. If so,
# then we alias the 'echo' command and set ECHOOPT. For other shells,
# we try and determine the real shell being used, and test to see if
# the 'echo -e' command is valid or not. We set ECHOOPT accordingly.
#
if [ "`print "rkh-ksh-string-test" 2>/dev/null`" = "rkh-ksh-string-test" ]; then
alias echo='print'
ECHOOPT="--"
MYSHELL=ksh
elif [ "${OPERATING_SYSTEM}" = "SunOS" ]; then
# For Solaris, if we are not running ksh, then it must be bash.
MYSHELL=bash
ECHOOPT="-e"
else
#
# We want to get the actual shell used by this program, and
# so we need to test /bin/sh.
#
MYSHELL=/bin/sh
test -h ${MYSHELL} && MYSHELL=`readlink ${MYSHELL} 2>/dev/null`
MYSHELL=`basename ${MYSHELL} 2>/dev/null`
# Assume 'bash' if we have problems finding the real shell.
test -z "${MYSHELL}" && MYSHELL=bash
#
# Now test the 'echo -e' command.
#
if [ "`echo -e \"rkh-ksh-string-test\" 2>/dev/null`" = "rkh-ksh-string-test" ]; then
ECHOOPT="-e"
else
ECHOOPT=""
fi
fi
#
# We now perform a similar test to see if 'echo -n' is valid
# or not. We set ECHON accordingly.
#
if [ "`echo -n \"rkh-ksh-string-test\" 2>/dev/null`" = "rkh-ksh-string-test" ]; then
ECHON="-n"
else
ECHON=""
fi
######################################################################
#
# Global function definitions
#
######################################################################
display() {
#
# This function is used to display text messages on to the
# users screen, as well as in to the log file. The same
# message is written to both. However, the screen may have
# a coloured result (green for good, red for bad, etc), and
# the log file will have the time prefixed to the message and,
# optionally, additional information messages after the main
# message. All the messages are indexed in the language file.
#
# Syntax: display --to <destination> --type <type>
# [--screen-indent <n>] [--log-indent <n>]
# [--nl [<n>]] [--nl-after] [--log-nl] [--nonl]
# [--result <result> --color <colour>]
# <message index> [optional message arguments]
#
# where the destination can be one of SCREEN, LOG or SCREEN+LOG.
# The type can be one of PLAIN, INFO or WARNING.
# The language file will have all the current values.
#
# The --screen-indent and --log-indent options are used to
# forcibly indent a message.
# The --nl option causes a blank-line to be output before the
# message both on the screen and in the log file. A following
# number can be used to indicate how many blank lines should
# be displayed on the screen.
# The --log-nl option outputs a blank line only in the log file.
# The --nl-after option outputs a blank line on the screen after
# the message.
# The --nonl option is only to be used in special cases where we
# want the output of more than one message to appear on the same
# line. This is currently only used when trying to obtain the
# lock file. It only applies to PLAIN messages, and may not be
# supported on all systems (depending on whether 'echo -n' works
# or not).
#
#
# We first initialize some variables and then
# process the switches used.
#
WARN_MSG=0; NL=0; NLAFTER=0; LOGINDENT=0; SCREENINDENT=0
LOGNL=0
WRITETO=''; TYPE=''; RESULT=''; COLOR=''; MSG=''
LINE1=''; LOGLINE1=''; SPACES=''; NONL=''
DISPLAY_LINE="display $*"
if [ $# -le 0 ]; then
echo "Error: Invalid display call - no arguments given"
return
fi
while [ $# -ge 1 ]; do
case "$1" in
--to)
case "$2" in
SCREEN|LOG|SCREEN+LOG)
WRITETO=$2
;;
*)
echo "Error: Invalid display destination: $2 Display line: ${DISPLAY_LINE}"
return
;;
esac
shift
;;
--type)
TYPE=`eval echo "\\$MSG_TYPE_$2"`
if [ -z "${TYPE}" -a "$2" != "PLAIN" ]; then
if [ $RKHLANGUPDT -eq 0 ]; then
echo "Error: Invalid display type: $2 Display line: ${DISPLAY_LINE}"
return
fi
fi
test "$2" = "WARNING" && WARN_MSG=1
shift
;;
--result)
RESULT=`eval echo "\\$MSG_RESULT_$2"`
if [ -z "${RESULT}" ]; then
if [ $RKHLANGUPDT -eq 0 ]; then
echo "Error: Invalid display result: $2 Display line: ${DISPLAY_LINE}"
return
fi
fi
shift
;;
--color)
if [ $COLORS -eq 1 ]; then
test -n "$2" && COLOR=`eval "echo \\${$2}"`
if [ -z "${COLOR}" ]; then
echo "Error: Invalid display color: $2 Display line: ${DISPLAY_LINE}"
return
fi
fi
shift
;;
--log-indent)
LOGINDENT=$2
if [ -z "${LOGINDENT}" ]; then
echo "Error: No --log-indent value given. Display line: ${DISPLAY_LINE}"
return
elif [ -z "`echo ${LOGINDENT} | grep '^[0-9]*$'`" ]; then
echo "Error: Invalid '--log-indent' value given: $2 Display line: ${DISPLAY_LINE}"
return
fi
shift
;;
--screen-indent)
SCREENINDENT=$2
if [ -z "${SCREENINDENT}" ]; then
echo "Error: No --screen-indent value given. Display line: ${DISPLAY_LINE}"
return
elif [ -z "`echo ${SCREENINDENT} | grep '^[0-9]*$'`" ]; then
echo "Error: Invalid '--screen-indent' value given: $2 Display line: ${DISPLAY_LINE}"
return
fi
shift
;;
--nl)
NL=1
case "$2" in
[0-9])
NL=$2
shift
;;
esac
;;
--log-nl)
LOGNL=1
;;
--nl-after)
NLAFTER=1
;;
--nonl)
NONL=$ECHON
;;
-*)
echo "Error: Invalid display option given: $1 Display line: ${DISPLAY_LINE}"
return
;;
*)
MSG=$1
shift
break
;;
esac
shift
done
#
# Before anything we must record if this is a warning message.
#
test $WARN_MSG -eq 1 && WARNING_COUNT=`expr ${WARNING_COUNT} + 1`
#
# For simplicity we now set variables as to whether the output
# goes to the screen and/or the log file. In some cases we do
# not need to output anything, and so can just return.
#
if [ $NOLOG -eq 1 ]; then
test "${WRITETO}" = "LOG" && return
test "${WRITETO}" = "SCREEN+LOG" && WRITETO="SCREEN"
fi
if [ $NOTTY -eq 1 ]; then
test "${WRITETO}" = "SCREEN" && return
test "${WRITETO}" = "SCREEN+LOG" && WRITETO="LOG"
fi
test "${WRITETO}" = "SCREEN" -o "${WRITETO}" = "SCREEN+LOG" && WRITETOTTY=1 || WRITETOTTY=0
test "${WRITETO}" = "LOG" -o "${WRITETO}" = "SCREEN+LOG" && WRITETOLOG=1 || WRITETOLOG=0
#
# Now check that the options we have been given make sense.
#
if [ $WRITETOTTY -eq 0 -a $WRITETOLOG -eq 0 ]; then
echo "Error: Invalid display destination: Display line: ${DISPLAY_LINE}"
return
elif [ $WRITETOTTY -eq 1 -a $COLORS -eq 1 -a -n "${RESULT}" -a -z "${COLOR}" ]; then
echo "Error: Invalid display - no color given: Display line: ${DISPLAY_LINE}"
return
fi
#
# We only allow no newline for PLAIN messages.
#
test -n "${TYPE}" && NONL=""
#
# If we want whitelisted results to be shown as white, or
# black for colour set two users, then change the colour now.
#
if [ $WLIST_IS_WHITE -eq 1 -a $WRITETOTTY -eq 1 -a $COLORS -eq 1 -a "${RESULT}" = "${MSG_RESULT_WHITELISTED}" ]; then
COLOR=$WHITE
fi
#
# We set the variable LINE1 to contain the first line of the message.
# For the log file we use the variable LOGLINE1. We also set
# where the language file is located. If a message cannot be found
# in the file, then we look in the English file. This will allow RKH
# to still work even when the language files change.
#
LANG_FILE="${DB_PATH}/i18n/${LANGUAGE}"
if [ -n "${MSG}" ]; then
LINE1=`grep "^${MSG}:" ${LANG_FILE} 2>/dev/null | head -n 1 | cut -d: -f2-`
if [ -z "${LINE1}" ]; then
LANG_FILE="${DB_PATH}/i18n/en"
LINE1=`grep "^${MSG}:" ${LANG_FILE} 2>/dev/null | head -n 1 | cut -d: -f2-`
if [ -z "${LINE1}" ]; then
echo "Error: Invalid display - language keyword cannot be found: Display line: ${DISPLAY_LINE}"
return
fi
else
LINE1=`echo "${LINE1}" | sed -e 's/\`/\\\\\`/g'`
fi
test -n "${LINE1}" && LINE1=`eval "echo \"${LINE1}\" | sed -e 's/;/\\;/g'"`
fi
#
# At this point LINE1 is the text of the message. We have to
# see if the message is to be indented, and must prefix the
# time to log file messages. We must do the log file first
# because it uses LINE1.
#
if [ $WRITETOLOG -eq 1 ]; then
LOGLINE1=`date '+[%H:%M:%S]'`
test $NL -gt 0 -o $LOGNL -eq 1 && echo "${LOGLINE1}" >>${RKHLOGFILE}
if [ -n "${TYPE}" ]; then
LOGLINE1="${LOGLINE1} ${TYPE}: ${LINE1}"
else
test $LOGINDENT -gt 0 && SPACES=`echo "${BLANK_LINE}" | cut -c1-$LOGINDENT`
LOGLINE1="${LOGLINE1} ${SPACES}${LINE1}"
fi
fi
if [ $WRITETOTTY -eq 1 -a $SCREENINDENT -gt 0 ]; then
SPACES=`echo "${BLANK_LINE}" | cut -c1-$SCREENINDENT`
LINE1="${SPACES}${LINE1}"
fi
#
# We now check to see if a result is to be output. If it is,
# then we need to space-out the line and color the result.
#
if [ -n "${RESULT}" ]; then
if [ $WRITETOTTY -eq 1 ]; then
LINE1_NUM=`echo "${LINE1}" | wc -c | tr -d ' '`
NUM_SPACES=`expr 62 - ${LINE1_NUM}`
test $NUM_SPACES -lt 1 && NUM_SPACES=1
if [ $COLORS -eq 0 ]; then
SPACES=`echo "${BLANK_LINE}" | cut -c1-$NUM_SPACES`
LINE1="${LINE1}${SPACES}[ ${RESULT} ]"
else
LINE1="${LINE1}\033[${NUM_SPACES}C[ ${COLOR}${RESULT}${NORMAL} ]"
fi
fi
if [ $WRITETOLOG -eq 1 ]; then
LOGLINE1_NUM=`echo "${LOGLINE1}" | wc -c | tr -d ' '`
NUM_SPACES=`expr 62 - ${LOGLINE1_NUM}`
test $NUM_SPACES -lt 1 && NUM_SPACES=1
SPACES=`echo "${BLANK_LINE}" | cut -c1-$NUM_SPACES`
LOGLINE1="${LOGLINE1}${SPACES}[ ${RESULT} ]"
fi
elif [ $WRITETOTTY -eq 1 -a -n "${COLOR}" ]; then
LINE1="${COLOR}${LINE1}${NORMAL}"
fi
#
# We can now output the message. We start with any required blank
# lines, and then the first line. If this is a warning message we
# write to the log file any additional lines.
#
if [ $WRITETOTTY -eq 1 ]; then
NLLOOP=$NL
while test $NLLOOP -gt 0; do
echo ""
NLLOOP=`expr ${NLLOOP} - 1`
done
echo $NONL $ECHOOPT "${LINE1}"
fi
if [ $WRITETOLOG -eq 1 ]; then
echo $ECHOOPT "${LOGLINE1}" >>${RKHLOGFILE}
if [ $WARN_MSG -eq 1 ]; then
test $SHOWWARNINGSONLY -eq 1 && echo $ECHOOPT "${LOGLINE1}" | cut -d' ' -f2-
LINE1=1
OLDIFS="${IFS}"
IFS=$IFSNL
for LOGLINE1 in `grep "^${MSG}:" ${LANG_FILE} 2>/dev/null | cut -d: -f2-`; do
if [ $LINE1 -eq 1 ]; then
LINE1=0
continue
else
test $SHOWWARNINGSONLY -eq 1 && echo $ECHOOPT " ${LOGLINE1}"
echo $ECHOOPT " ${LOGLINE1}" >>${RKHLOGFILE}
fi
done
IFS="${OLDIFS}"
elif [ $SHOWWARNINGSONLY -eq 1 -a -n "`echo \"${LOGLINE1}\" | grep '^\[[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\] '`" ]; then
echo $ECHOOPT "${LOGLINE1}" | cut -d' ' -f2-
fi
fi
#
# Output a final blank line if requested to do so.
#
test $WRITETOTTY -eq 1 -a $NLAFTER -eq 1 && echo ""
return
}
keypresspause() {
#
# This function will display a prompt message to the user.
#
if [ $SKIP_KEY_PRESS -eq 0 -a $QUIET -eq 0 ]; then
display --to SCREEN --type PLAIN --nl PRESSENTER
read RKHTMPVAR
test "${RKHTMPVAR}" = "s" -o "${RKHTMPVAR}" = "S" && SKIP_KEY_PRESS=1
fi
return
}
get_option() {
#
# This function is used to process configuration file options.
#
# Syntax: get_option <option type> [single | multi] <option name>
#
# Since different options require different needs, the first
# argument is the 'type' of option we are processing. The second
# argument is the word 'single' or 'multi'. This indicates if
# the option can occur on one or more lines in the configuration
# file. The third argument is the option name.
#
# There are currently two types defined:
#
# Type 1: A number, single word or pathname.
# Type 2: A space-separated word list.
#
# Typically, single and double-quotes, spaces and tabs will be
# removed. For type 2 options, tabs are converted to spaces, and
# all spaces are squeezed into one. Leading and trailing spaces
# are removed. All other types of options are processed separately.
#
# The function will output the final modified option.
#
# NOTE: This function is currently implemented such that if it returns
# a non-zero code, then RKH will exit at the relevant point with a
# return code of 1. However, the function does not currently return a
# non-zero code at any time.
#
OPTTYPE="$1"
OPTMULTI="$2"
OPTV="$3"
#
# First see if the option is in the configuration file, and if
# it is then process it according to the multi-line argument.
#
if [ -z "`grep -h \"^${OPTV}=\" ${CONFIGFILE} ${LOCALCONFIGFILE}`" ]; then
echo ""
return 0
else
case "${OPTMULTI}" in
single)
OPTVAR=`grep -h "^${OPTV}=" ${CONFIGFILE} ${LOCALCONFIGFILE} | tail -1 | sed -e "s/${OPTV}=//"`
;;
multi)
OPTVAR=`grep -h "^${OPTV}=" ${CONFIGFILE} ${LOCALCONFIGFILE} | sed -e "s/${OPTV}=//"`
;;
*)
echo "Error: Invalid multi-line argument in get_option function: $*" >&2
# Treat this as a single-line option.
OPTVAR=`grep -h "^${OPTV}=" ${CONFIGFILE} ${LOCALCONFIGFILE} | tail -1 | sed -e "s/${OPTV}=//"`
;;
esac
fi
#
# Now process the option.
#
case "$OPTTYPE" in
1)
OPTVAR=`echo "${OPTVAR}" | tr -d '" ' | tr -d "'"`
;;
2)
OPTVAR=`echo "${OPTVAR}" | tr ',' ' ' | tr ' ' ' ' | tr -s ' '`
#
# We must handle the MAIL_CMD option specially because it may
# contain quote characters, and we do not want to remove these.
#
if [ "${OPTV}" != "MAIL_CMD" ]; then
OPTVAR=`echo "${OPTVAR}" | sed -e 's/^ *"* *//; s/ *"* *$//' | sed -e "s/^ *'* *//; s/ *'* *$//"`
fi
OPTVAR=`echo ${OPTVAR}`
;;
*)
echo "Error: Invalid option type in get_option function: $*" >&2
;;
esac
echo "${OPTVAR}"
return 0
}
get_temp_file() {
#
# This function will create an empty, unique temporary file.
#
# It takes one argument which is the pathname for the file,
# excluding the suffix. The function will return the pathname
# in TEMPFILE.
#
TEMPFILE=""
TEMPFILE_BASE=$1
if [ -n "${MKTEMP_CMD}" ]; then
TEMPFILE=`${MKTEMP_CMD} ${TEMPFILE_BASE}.XXXXXXXXXX`
elif [ -n "$RANDOM" ]; then
TEMPFILE="${TEMPFILE_BASE}.$RANDOM"
elif [ $BSDOS -eq 1 ]; then
TEMPFILE="${TEMPFILE_BASE}.`date +%s`"
elif [ -n "`date +%N%s 2>/dev/null | grep '^[0-9][0-9]*$'`" ]; then
TEMPFILE="${TEMPFILE_BASE}.`date +%N%s%N`"
else
TEMPFILE="${TEMPFILE_BASE}.`date +%Y%m%d%H%M%S`"
fi
#
# Remove the file just in case it already exists!
#
rm -f ${TEMPFILE} >/dev/null 2>&1
return
}
suckit_extra_checks() {
#
# This function carries out some extra checks of the suckit rootkit.
# There are 3 extra checks, but we only display the result after
# all the checks have completed. As such we store the result of
# each check in a variable, and display the final result based on
# the value of those variables.
#
if [ $VERBOSE_LOGGING -eq 1 ]; then
display --to LOG --type PLAIN --log-indent 2 --nl ROOTKIT_ADD_SUCKIT_LOG
fi
ROOTKIT_COUNT=`expr ${ROOTKIT_COUNT} + 1`
#
# The first check tests the link count of the /sbin/init file.
# We use the NLINKS variable to indicate the test result:
# -1 means that no stat command was available
# 0 means that the stat command gave an error
# 1 is okay
# >1 means that suckit may be installed
#
NLINKS=-1
if [ -n "${STAT_CMD}" ]; then
if [ -n "`echo \"${STAT_CMD}\" | grep '\.pl$'`" ]; then
NLINKS=`${STAT_CMD} --nlink /sbin/init 2>/dev/null`
else
NLINKS=`${STAT_CMD} -c %h /sbin/init 2>/dev/null`
fi
test -z "${NLINKS}" && NLINKS=0
if [ $VERBOSE_LOGGING -eq 1 ]; then
if [ $NLINKS -eq 0 ]; then
display --to LOG --type PLAIN --result WARNING --log-indent 4 ROOTKIT_LINK_COUNT "/sbin/init"
elif [ $NLINKS -eq 1 ]; then
display --to LOG --type PLAIN --result OK --log-indent 4 ROOTKIT_LINK_COUNT "/sbin/init"
else
display --to LOG --type PLAIN --result WARNING --log-indent 4 ROOTKIT_LINK_COUNT "/sbin/init"
fi
fi
else
display --to LOG --type PLAIN --result SKIPPED --log-indent 4 ROOTKIT_LINK_COUNT "/sbin/init"
fi
#
# The next test checks to see if certain files are being
# hidden. These files have the '.xrk' or '.mem' suffix.
# The HIDDEN variable will be used to indicate the result:
# <null> is okay
# 'xrk' means that the 'xrk' suffix is hidden
# 'mem' means that the 'mem' suffix is hidden
#
HIDDEN=""
for EXT in xrk mem; do
get_temp_file "${RKHTMPDIR}/suckitexttest"
touch ${TEMPFILE}
rm -f ${TEMPFILE}.${EXT} >/dev/null 2>&1
mv ${TEMPFILE} ${TEMPFILE}.${EXT}
if [ ! -f "${TEMPFILE}.${EXT}" ]; then
if [ -n "${HIDDEN}" ]; then
HIDDEN="${HIDDEN} and ${EXT}"
else
HIDDEN=${EXT}
fi
fi
rm -f "${TEMPFILE}.${EXT}" >/dev/null 2>&1
done
if [ $VERBOSE_LOGGING -eq 1 ]; then
if [ -z "${HIDDEN}" ]; then
display --to LOG --type PLAIN --result NONE_FOUND --log-indent 4 ROOTKIT_ADD_SUCKIT_EXT
else
display --to LOG --type PLAIN --result FOUND --log-indent 4 ROOTKIT_ADD_SUCKIT_EXT
fi
fi
#
# Finally we perform a check using the skdet command, if it
# is present. The SKDET variable will be used to indicate
# the result:
# -1 means that skdet is not available
# 0 means that skdet found nothing
# 1 means that skdet found something
# 2 means that the version of skdet is unknown
#
# The variable SKDET_OUTPUT will contain any output from
# the command.
#
SKDET=-1
SKDET_OUTPUT=""
SKDET_CMD=`find_cmd skdet`
if [ -n "${SKDET_CMD}" ]; then
#
# We need to check the skdet version first.
#
SKDET=0
SKDETOPT=""
SKDETVER=`${SKDET_CMD} -v 2>&1 | grep '^skdet.v' | awk -F'.' '{ print $1 }'`
case "${SKDETVER}" in
*v0)
SKDETOPT="-a"
;;
*v1)
SKDETOPT="-c"
;;
*)
SKDET=2
SKDET_OUTPUT=`${SKDET_CMD} -v 2>&1`
;;
esac
if [ $SKDET -eq 0 ]; then
SKDET_OUTPUT=`${SKDET_CMD} ${SKDETOPT} 2>&1 | tr -s ' ' | grep -i 'invis'`
test -n "${SKDET_OUTPUT}" && SKDET=1
fi
if [ $VERBOSE_LOGGING -eq 1 ]; then
if [ $SKDET -eq 0 ]; then
display --to LOG --type PLAIN --result OK --log-indent 4 ROOTKIT_ADD_SUCKIT_SKDET
else
display --to LOG --type PLAIN --result WARNING --log-indent 4 ROOTKIT_ADD_SUCKIT_SKDET
fi
fi
elif [ $VERBOSE_LOGGING -eq 1 ]; then
display --to LOG --type PLAIN --result SKIPPED --log-indent 4 ROOTKIT_ADD_SUCKIT_SKDET
display --to LOG --type INFO NOT_FOUND_CMD "skdet"
fi
#
# Now we can display the results.
#
if [ $NLINKS -eq 1 -a -z "${HIDDEN}" -a $SKDET -le 0 ]; then
display --to SCREEN+LOG --type PLAIN --result OK --color GREEN --screen-indent 4 --log-indent 2 ROOTKIT_ADD_SUCKIT
else
ROOTKIT_FAILED_COUNT=`expr ${ROOTKIT_FAILED_COUNT} + 1`
ROOTKIT_FAILED_NAMES="${ROOTKIT_FAILED_NAMES}Suckit Rookit (additional checks), "
display --to SCREEN+LOG --type WARNING --result WARNING --color RED --screen-indent 4 --log-indent 2 ROOTKIT_ADD_SUCKIT
if [ $NLINKS -eq -1 ]; then
display --to LOG --type PLAIN --log-indent 9 ROOTKIT_ADD_SUCKIT_LINK_NOCMD
elif [ $NLINKS -eq 0 ]; then
display --to LOG --type PLAIN --log-indent 9 ROOTKIT_LINK_COUNT_CMDERR "${STAT_CMD}" "/sbin/init"
elif [ $NLINKS -gt 1 ]; then
display --to LOG --type PLAIN --log-indent 9 ROOTKIT_ADD_SUCKIT_LINK_FOUND "$NLINKS"
fi
if [ -n "${HIDDEN}" ]; then
display --to LOG --type PLAIN --log-indent 9 ROOTKIT_ADD_SUCKIT_EXT_FOUND "${HIDDEN}"
fi
if [ $SKDET -eq 1 ]; then
display --to LOG --type PLAIN --log-indent 9 ROOTKIT_ADD_SUCKIT_SKDET_FOUND "${SKDET_OUTPUT}"
elif [ $SKDET -eq 2 ]; then
display --to LOG --type PLAIN --log-indent 9 ROOTKIT_ADD_SUCKIT_SKDET_VER "${SKDET_OUTPUT}"
fi
fi
return
}
scanrootkit() {
#
# This function performs the actual check for a rootkit.
# It uses the variables SCAN_ROOTKIT, SCAN_FILES, SCAN_DIRS
# and SCAN_KSYMS. These will have been set before the
# function is called.
#
SCAN_STATUS=0
ROOTKIT_COUNT=`expr ${ROOTKIT_COUNT} + 1`
if [ $VERBOSE_LOGGING -eq 1 ]; then
display --to LOG --type PLAIN --nl ROOTKIT_FILES_DIRS_NAME_LOG "${SCAN_ROOTKIT}"
fi
#
# First check to see if any of the known files exist.
#
FILE_FOUND=""
for RKHTMPVAR2 in ${SCAN_FILES}; do
RKHTMPVAR=`echo "${RKHTMPVAR2}" | tr '%' ' '`
#
# If the option SCANROOTKITMODE is set to "THOROUGH" the scanrootkit() function
# will search (on a per rootkit basis) for filenames in all of the directories (as defined
# by the result of running 'find "${RKHROOTDIR}/" -xdev'). While still not optimal, as it
# still searches for only file names as opposed to file contents, this is one step away
# from the rigidity of searching in known (evidence) or default (installation) locations.
#
# THIS OPTION SHOULD NOT BE ENABLED BY DEFAULT
# You should only activate this feature as part of a more thorough investigation which
# should be based on relevant best practices and procedures.
# Apart from ameliorating the case with respect to functionality (e.g. reporting) this feature does not
# concern itself with efficiency so asking for improvements like whitelisting, de-duping or false positives is
# out of the question.
# Enabling this feature implies you have the knowledge to interprete results properly.
#
case "${SCANROOTKITMODE}" in
THOROUGH) # Search the whole filesystem
RKHTMPVAR=`basename "${RKHTMPVAR}"`
RKHTMPVAR=`find "${RKHROOTDIR}/" -xdev -iname "${RKHTMPVAR}"`
for RKHTMPVARITEM in $RKHTMPVAR; do
if [ -f "${RKHTMPVAR}" -o -b "${RKHTMPVAR}" -o -c "${RKHTMPVAR}" -o -L "${RKHTMPVAR}" -o -p "${RKHTMPVAR}" -o -S "${RKHTMPVAR}" ]; then
SCAN_STATUS=1
FILE_FOUND="${FILE_FOUND} ${RKHTMPVAR2}"
test $VERBOSE_LOGGING -eq 1 && display --to LOG --type PLAIN --result FOUND --log-indent 2 ROOTKIT_FILES_DIRS_FILE "${RKHTMPVAR}"
fi
done
;;
*) # Scan ze old vay
if [ -f "${RKHTMPVAR}" ]; then
#
# We first check to see if the file is whitelisted. Note that we use
# the un-translated file name. This allows us to check for filenames
# with spaces, but without causing problems for our space-delimited test.
#
RKHTMPVAR3=`echo "${RKHTMPVAR2}" | sed -e 's/\./\\\./g'`
if [ -n "`echo \"${RTKT_FILE_WHITELIST}\" | grep \" ${RKHTMPVAR3} \"`" ]; then
display --to LOG --type INFO FILE_PROP_WL "${RKHTMPVAR}" rootkit
else
SCAN_STATUS=1
FILE_FOUND="${FILE_FOUND} ${RKHTMPVAR2}"
fi
test $VERBOSE_LOGGING -eq 1 && display --to LOG --type PLAIN --result FOUND --log-indent 2 ROOTKIT_FILES_DIRS_FILE "${RKHTMPVAR}"
elif [ $VERBOSE_LOGGING -eq 1 ]; then
display --to LOG --type PLAIN --result NOT_FOUND --log-indent 2 ROOTKIT_FILES_DIRS_FILE "${RKHTMPVAR}"
fi
;;
esac
done
#
# Next check to see if any of the directories exist.
#
DIR_FOUND=""
for RKHTMPVAR2 in ${SCAN_DIRS}; do
RKHTMPVAR=`echo "${RKHTMPVAR2}" | tr '%' ' '`
if [ -d "${RKHTMPVAR}" ]; then
#
# We first check to see if the directory is whitelisted. Note that we use
# the un-translated directory name. This allows us to check for directory
# names with spaces, but without causing problems for our space-delimited test.
#
RKHTMPVAR3=`echo "${RKHTMPVAR2}" | sed -e 's/\./\\\./g'`
if [ -n "`echo \"${RTKT_DIR_WHITELIST}\" | grep \" ${RKHTMPVAR3} \"`" ]; then
display --to LOG --type INFO FILE_PROP_WL_DIR "${RKHTMPVAR}" rootkit
else
SCAN_STATUS=1
DIR_FOUND="${DIR_FOUND} ${RKHTMPVAR2}"
fi
test $VERBOSE_LOGGING -eq 1 && display --to LOG --type PLAIN --result FOUND --log-indent 2 ROOTKIT_FILES_DIRS_DIR "${RKHTMPVAR}"
elif [ $VERBOSE_LOGGING -eq 1 ]; then
display --to LOG --type PLAIN --result NOT_FOUND --log-indent 2 ROOTKIT_FILES_DIRS_DIR "${RKHTMPVAR}"
fi
done
#
# Next check the ksyms or kallsyms file.
#
KSYM_FOUND=""
if [ -n "${SCAN_KSYMS}" ]; then
for KS in ${SCAN_KSYMS}; do
if [ -n "${KSYMS_FILE}" ]; then
KSYM=`echo "${KS}" | sed -e 's/\./\\\./g'`
if [ -n "`grep \"${KSYM}\" ${KSYMS_FILE}`" ]; then
SCAN_STATUS=1
KSYM_FOUND="${KSYM_FOUND} ${KS}"
test $VERBOSE_LOGGING -eq 1 && display --to LOG --type PLAIN --result FOUND --log-indent 2 ROOTKIT_FILES_DIRS_KSYM "${KS}"
elif [ $VERBOSE_LOGGING -eq 1 ]; then
display --to LOG --type PLAIN --result NOT_FOUND --log-indent 2 ROOTKIT_FILES_DIRS_KSYM "${KS}"
fi
elif [ $VERBOSE_LOGGING -eq 1 ]; then
display --to LOG --type PLAIN --result SKIPPED --log-indent 2 ROOTKIT_FILES_DIRS_KSYM "${KS}"
fi
done
fi
#
# Now display the results.
#
if [ $SCAN_STATUS -eq 0 ]; then
display --to SCREEN+LOG --type PLAIN --result NOT_FOUND --color GREEN --screen-indent 4 NAME "${SCAN_ROOTKIT}"
else
ROOTKIT_FAILED_COUNT=`expr ${ROOTKIT_FAILED_COUNT} + 1`