-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon
3475 lines (3288 loc) · 105 KB
/
common
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
################################################################################
# #
# Copyright (C) 2008-2015 LABBE Corentin <[email protected]>
#
# YASAT 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 3 of the License, or
# (at your option) any later version.
#
# YASAT 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 YASAT. If not, see <http://www.gnu.org/licenses/>.
# #
################################################################################
#
# The display function is originated from lynis Copyright 2007-2009, Michael Boelen ([email protected]), The Netherlands
# Web site: http://www.rootkit.nl
#
#################################################################################
#
# Common functions for YASAT
#
#################################################################################
#
NORMAL="[0;39m"
WARNING="[1;31m"
YELLOW="[1;33m"
BLUE="[0;36m"
WHITE="[1;37m"
GREEN="[0;32m"
RED="[1;31m"
ORANGE="[0;33m"
NOIRGRAS="[1;39m"
#All results in orange/yellow is a optional warning
#All results in red must be corrected
################################################################################
################################################################################
print_color_chart()
{
echo "Color chart"
echo "$GREEN GREEN $NORMAL is for good configuration or information"
echo "$RED RED $NORMAL is for configuration that must be corrected"
echo "$ORANGE ORANGE $NORMAL is for optional configuration that can be done"
#ugly color, do not use:)
# echo "$YELLOW YELLOW $NORMAL is for optional configuration that can be done"
echo "$BLUE BLUE $NORMAL is for information"
}
################################################################################
################################################################################
Debug()
{
if [ $DEBUG -eq 1 ] ; then
ECHOCMD="echo -e"
if [ "`echo -e plop`" = '-e plop' ] ;then
#with /bin/sh or zsh no -e
ECHOCMD='echo'
fi
$ECHOCMD "$1"
fi
}
################################################################################
################################################################################
# Display an error, by error I mean internal YASAT error
Display_error() {
echo "$1" >&2
echo "$1" >> $ERROR_OUTPUT_FILE
if [ ! -z "$2" ];then
echo "$2" >> $ERROR_OUTPUT_FILE
fi
}
################################################################################
################################################################################
# create a tmp file
create_tmp()
{
local suffix=""
if [ $# -ge 1 ];then
suffix="$1"
Debug "DEBUG: Setting suffix to $suffix"
fi
#TODO check is suffix is handled on all OS
if [ -z "$suffix" ];then
TMP_FILE="`mktemp --suffix=$suffix`"
else
TMP_FILE="`mktemp`"
fi
if [ $? -ne 0 -o ! -e $TMP_FILE ];then
Display_error "ERROR: Cannot create a tmp file"
return 1
fi
Debug "DEBUG: Created tmp $TMP_FILE"
TMP_FILE_CREATED="$TMP_FILE_CREATED $TMP_FILE"
return 0
}
################################################################################
################################################################################
Display()
{
Debug "DEBUG: Display begin"
DEBUG_ARGS="$*"
if [ -z "$1" ];then
Display_error "ERROR: Display need at least one argument"
return 1;
fi
INDENT=0; TEXT=''; RESULT=''; COLOR=''; ADVICE=''
ECHOCMD="echo -e"
if [ "`echo -e plop`" = '-e plop' ] ;then
#with /bin/sh or zsh no -e
ECHOCMD='echo'
fi
while [ $# -ge 1 ]; do
case $1 in
--color)
shift
case $1 in
GREEN)
COLOR=$GREEN;HTMLCOLOR='GREEN'
if [ $PRINT_LEVEL -ge 2 ] ;then
return 1;
fi
YASAT_STAT_GREEN=$(($YASAT_STAT_GREEN+1))
;;
RED)
COLOR=$RED;HTMLCOLOR='RED'
YASAT_STAT_RED=$(($YASAT_STAT_RED+1))
;;
WHITE)
COLOR=$WHITE
;;
YELLOW)
COLOR=$YELLOW;HTMLCOLOR='YELLOW'
if [ $PRINT_LEVEL -ge 3 ];then
return 1;
fi
;;
ORANGE)
COLOR=$ORANGE;HTMLCOLOR='ORANGE'
if [ $PRINT_LEVEL -ge 3 ] ;then
return 1;
fi
YASAT_STAT_ORANGE=$(($YASAT_STAT_ORANGE+1))
;;
BLUE)
COLOR=$BLUE;HTMLCOLOR='BLUE'
if [ $PRINT_LEVEL -ge 1 ];then
return 1;
fi
;;
*)
Display_error "ERROR: unknown color $1 for Display()"
return 1;
;;
esac
;;
--indent)
shift
INDENT=$1
;;
--no-break | --nobreak | -nb)
ECHOCMD="echo -en"
;;
--result)
shift
if [ -z "$1" ];then
Display_error "ERROR : missing parameters to --result"
return 1;
fi
RESULT=$1
;;
--advice)
shift
if [ -z "$1" ];then
Display_error "ERROR : missing parameters to --advice" "$DEBUG_ARGS"
return 1;
fi
ADVICE=$1
;;
--text)
shift
#clean possible double slash
TEXT="`echo $1 | sed 's,//,/,g'`"
if [ $HIDESR -ge 1 ];then
#I use | because it is the only separator that I will be sure to be not present
TEXT=`echo $1 | sed "s|$SCAN_ROOT||"`
fi
;;
--comp)
shift
if [ -z "$1" -o -z "$2" ];then
Display_error "ERROR : missing parameters to --comp"
return 1;
fi
Compliance --result "$1" --plugin "$2" --color "$HTMLCOLOR" --yasatresult "$RESULT"
shift
;;
*)
Display_error "INVALID OPTION (Display): $1, it is usually a bug of yasat (shame on me)"
exit 1
;;
esac
# Go to next parameter
shift
done
if [ -z "${ADVICE}" -o "${ADVICE}" = 'NONE' ] ;then
ADVICEVALUE=''
else
ADVICEVALUE="`grep ${ADVICE}= ${YASAT_ROOT}/yasat.advices | cut -d\= -f2-`"
if [ -z "$ADVICEVALUE" ] ;then
Display --indent 2 --text "BUG ADVICEVALUE is empty for ${ADVICE}" --result WARNING --color RED --advice YASAT_BUG
fi
# if [ -z "`echo ${RESULT} | grep -vEi 'warning$|found$'`" ] ;then
# echo "= ${TEXT}" >> $REPORT_OUTPUT
# else
echo "= ${TEXT} Result=${RESULT}" >> $REPORT_OUTPUT
# fi
echo " $ADVICEVALUE" >> $REPORT_OUTPUT
report_add "${ADVICE}" TEXT $REPORT_OUTPUT
fi
if [ ! -z "$HTML_OUTPUT" ] ;then
echo "<tr><td><b>${TEXT}</b></td><td> <FONT color=$HTMLCOLOR >${RESULT}</font></td><td>$ADVICEVALUE</td></tr>" >> "$HTML_OUTPUT"
if [ ! -z "$ADVICE" ] ;then
report_add "${ADVICE}" HTML "$HTML_OUTPUT"
fi
fi
if [ -z "${RESULT}" ];then
Display_error 'ERROR: No --result'
return 1;
fi
RESULTPART=" [ ${COLOR}${RESULT}${NORMAL} ]"
#size of result is 5 ( [ ]) + 8 (NOTFOUND/WARNING is the greatest result)
MAXLINESIZE=67
if [ ! -z "${TEXT}" ] ;then
# Display
LINESIZE=`echo "${TEXT}" | wc -c | tr -d ' '`
#SPACES=`expr ${MAXLINESIZE} - ${INDENT} - ${LINESIZE}`
SPACES=$((${MAXLINESIZE}-${INDENT}-${LINESIZE}))
if [ "$SPACES" -le 0 ] ;then
TEXT1=`echo ${TEXT} | cut -b -50`
LINESIZE=`echo "${TEXT1}" | wc -c | tr -d ' '`
#SPACES=`expr ${MAXLINESIZE} - ${INDENT} - ${LINESIZE}`
SPACES=$((${MAXLINESIZE}-${INDENT}-${LINESIZE}))
${ECHOCMD} "\033[${INDENT}C${TEXT1}\033[${SPACES}C${RESULTPART}\t${ADVICEVALUE}"
TEXT2=`echo ${TEXT} | cut -b 51-`
${ECHOCMD} "!!\033[${INDENT}C${TEXT2}"
else
#SPACES=`expr ${MAXLINESIZE} - ${INDENT} - ${LINESIZE}`
SPACES=$((${MAXLINESIZE}-${INDENT}-${LINESIZE}))
ADVICE_LINE_SIZE=0
if [ -z "${ADVICEVALUE}" ] ;then
FULLLINESIZE=$LINESIZE
else
ADVICE_LINE_SIZE=`echo "${ADVICEVALUE}" | wc -c | tr -d ' '`
#FULLLINESIZE=`expr ${ADVICE_LINE_SIZE} + 80`
FULLLINESIZE=$((${ADVICE_LINE_SIZE}+80))
fi
Debug "FULL $FULLLINESIZE $LINESIZE $SPACES adv=$ADVICE_LINE_SIZE COL_WIDTH=$COL_WIDTH"
if [ $FULLLINESIZE -gt $COL_WIDTH ]
then
${ECHOCMD} "\033[${INDENT}C${TEXT}\033[${SPACES}C${RESULTPART}"
${ECHOCMD} "\033[${INDENT}C\t-> ${ADVICEVALUE}"
else
${ECHOCMD} "\033[${INDENT}C${TEXT}\033[${SPACES}C${RESULTPART}\t${ADVICEVALUE}"
fi
fi
else
echo "ERROR: Display: Missing parameter --text"
return 1
fi
}
################################################################################
################################################################################
# does $1 is equal to $2 ?
# optionnal parameter [-f filter] [-i]
# -f filter: filter $1
# -i case insesitive test but $2 need to be lowercase
# -2 add a second value accepted
# return 0 if yes
# return 1 if no
is_equal() {
local cmp=$1
local cmp2=$2
local cmp3=''
local tmp
shift
shift
if [ $# -ge 1 ];then
while [ $# -ge 1 ]
do
case $1 in
-i)
tmp=`echo $cmp | tr 'A-Z' 'a-z'`
#echo "DEBUG: $cmp to $tmp"
cmp=$tmp
shift
;;
-f)
shift
if [ -z "$1" ];then
Display_error "ERROR: is_equal() -f need an argument"
return 2
fi
tmp=`echo $cmp | sed "s,$1,,g"`
#echo "DEBUG: $cmp to $tmp"
cmp=$tmp
shift
;;
-2)
shift
if [ -z "$1" ];then
Display_error "ERROR: is_equal() -2 need an argument"
return 2
fi
cmp3="$1"
shift
;;
*)
Display_error "ERROR: invalid arg $1 for is_equal()"
return 2
;;
esac
done
fi
if [ "$cmp" = "$cmp2" ];then
return 0
fi
if [ ! -z "$cmp3" -a "$cmp" = "$cmp3" ];then
return 0
fi
return 1
}
################################################################################
################################################################################
report_add()
{
if [ -z "$1" ];then
Display --indent 2 --text "Missing argument #1 for report_add" --result WARNING --color RED --advice YASAT_BUG
return 1;
fi
if [ -z "$2" ];then
Display --indent 2 --text "Missing argument #2 (type of output) for report_add" --result WARNING --color RED --advice YASAT_BUG
return 1;
fi
if [ -z "$3" ];then
Display --indent 2 --text "Missing argument #3 (name of the output file) for report_add" --result WARNING --color RED --advice YASAT_BUG
return 1;
fi
Debug "report_add() called with $1 $2 $3"
# if [ $2 = "TEXT" ]
# then
# echo "" >> $3
# echo "== `cat yasat.advices |grep $ADVICE | cut -d\= -f2-` ==" >> $3
# echo "" >> $3
# fi
if [ $2 = "HTML" ];then
echo "<tr><td>" >> "$3"
fi
ADVICEFOUND=0
LISTE_ADVICE="`ls ${PLUGINS_REP}/*.advice`"
cat $LISTE_ADVICE |
while read line
do
if [ "$line" = "ADVICEEND" ];then
ADVICEFOUND=0
fi
#temporary
if [ ! -z "`echo $line |grep ^${ADVICELANG},`" ];then
ADVICEFOUND=0
fi
if [ $ADVICEFOUND -eq 1 ];then
if [ $2 = "TEXT" ];then
echo " $line" | sed 's/<[^>]*>//g'>> "$3"
fi
if [ $2 = "HTML" ];then
echo " $line" >> "$3"
echo "<br>" >> "$3"
fi
fi
if [ ! -z "`echo $line |grep ${ADVICELANG},$1`" ];then
ADVICEFOUND=1
fi
done
if [ $2 = "HTML" ];then
echo "<br></td><td></td><td></td></tr>" >> "$3"
fi
}
################################################################################
################################################################################
#arg 1 is the path to file created by prepare_apache_conf()
#arg2 is the value found by FindValueOf
Check_apache_user()
{
export RESULTAT=''
export FINDERROR=''
if [ -z "$1" ] ; then
Display --indent 2 --text "Missing argument #1 for Check_apache_user" --result WARNING --color RED --advice YASAT_BUG
return 2;
fi
if [ -z "$2" ] ; then
Display --indent 2 --text "Missing argument #2 for Check_apache_user" --result WARNING --color RED --advice YASAT_BUG
return 2;
fi
if [ "`echo $2 | cut -b1`" = '$' ] ;then
Display --indent 2 --text "Apache user is a variable" --result INFO --color BLUE
#we ll find the value of this variable (only debian do that, and values can be found in /etc/apache2/envvars)
if [ -e /etc/apache2/envvars ] ; then
export RESULTAT="`grep APACHE_RUN_USER /etc/apache2/envvars | cut -d\= -f2`"
if [ -z "$RESULTAT" ] ; then
Display --indent 4 --text "Fallback to www-data" --result INFO --color BLUE
export RESULTAT='www-data'
fi
else
#TODO fallback to a common value
Display --indent 4 --text "Fallback to www-data" --result INFO --color BLUE
export RESULTAT='www-data'
fi
return 0;
Display --indent 2 --text "Apache user is " --result "$RESULTAT" --color BLUE
fi
export RESULTAT="$2"
}
################################################################################
################################################################################
#arg 1 is the path to file created by prepare_apache_conf()
#arg2 is the value found by FindValueOf
Check_apache_group()
{
export RESULTAT=''
export FINDERROR=''
if [ -z "$1" ] ; then
Display --indent 2 --text "Missing argument #1 for Check_apache_group" --result WARNING --color RED --advice YASAT_BUG
return 2;
fi
if [ -z "$2" ] ; then
Display --indent 2 --text "Missing argument #2 for Check_apache_group" --result WARNING --color RED --advice YASAT_BUG
return 2;
fi
if [ "`echo $2 | cut -b1`" = '$' ] ; then
Display --indent 2 --text "Apache group is a variable" --result INFO --color BLUE
#we ll find the value of this variable (only debian do that, and values can be found in /etc/apache2/envvars)
if [ -e /etc/apache2/envvars ] ; then
export RESULTAT="`grep APACHE_RUN_GROUP /etc/apache2/envvars | cut -d\= -f2`"
if [ -z "$RESULTAT" ] ; then
Display --indent 4 --text "Fallback to www-data" --result INFO --color BLUE
export RESULTAT='www-data'
fi
else
#TODO fallback to a common value
Display --indent 4 --text "Fallback to www-data" --result INFO --color BLUE
export RESULTAT='www-data'
fi
return 0;
Display --indent 2 --text "Apache group is " --result "$RESULTAT" --color BLUE
fi
export RESULTAT="$2"
}
################################################################################
################################################################################
#Find value of a directive separated by space "$2 value"
# #1 is the file to scan
# #2 is the directive to seek
# #3 is a sort of error reporting, possible values JUSTTEST(what a bad name choice) and MULTIPLE(allow multiple value)
# #4 is case sensitivity flag (nothing = sensitive, INSENSITIVE otherwise)
FindValueOf()
{
export RESULTAT=''
export FINDERROR=''
if [ -z "$1" ] ; then
Display --indent 2 --text "Missing argument FindValueOf() #1 (conf file to scan)" --result WARNING --color RED --advice YASAT_BUG
return 2;
fi
if [ -z "$2" ] ; then
Display --indent 2 --text "Missing argument FindValueOf #2 (directive to seek)" --result WARNING --color RED --advice YASAT_BUG
return 2;
fi
TEMP=""
Debug "Seek value of $2 in $1"
if [ ! -e "$1" ] ;then
echo "Error $1 do not exist"
return 1;
fi
#End of checks of parameters
DO_INSENSITIVE=0
if [ $# -ge 4 ] ; then
if [ "$4" = 'INSENSITIVE' ] ; then
DO_INSENSITIVE=1
fi
fi
if [ $DO_INSENSITIVE -ge 1 ] ; then
TEMP=`grep -rih "^[[:space:]]*$2[[:space:]]" $1 |grep -v '^[[:space:]]*#' |tr '[:upper:]' '[:lower:]' |sed "s/^[[:space:]]*$2[[:space:]]*//g" |sed 's/#.*//g'|sed 's,[[:space:]]*,,'`
else
TEMP=`grep -rih "^[[:space:]]*$2[[:space:]]" $1 |grep -v '^[[:space:]]*#' |sed "s/^[[:space:]]*$2[[:space:]]*//g" |sed 's/#.*//g'|sed 's,[[:space:]]*,,'`
fi
if [ -z "$TEMP" ] ;then
if [ $# -le 2 ] ; then
Display --indent 2 --text "No declaration of $2" --result WARNING --color RED
else
if [ -z "$3" ] ;then
Display --indent 2 --text "No declaration of $2" --result WARNING --color RED
return 1;
fi
fi
fi
if [ `echo "$TEMP" | wc -l` -ge 2 ];then
if [ "$3" = 'MULTIPLE' ] ; then
# export RESULTAT="`echo $TEMP | sed "s/[[:space:]]*$2[[:space:]]*//g"`"
export RESULTAT="$TEMP"
return 0;
fi
Display --indent 2 --text "Error multiple declarations of $2" --result WARNING --color RED --advice GLOBAL_MULTIPLE_DECLARATIONS
grep -ri "^[[:space:]]*$2[[:space:]]" $1 |grep -v '^[[:space:]]*#' |
while read line
do
echo " ==> $line"
done
FINDERROR='MULTIPLE'
RESULTAT=`echo "$TEMP" | sort | uniq | head -n 1 |sed "s/^[[:space:]]*$2[[:space:]]*//g" | sed 's/#.*//g'`
return 3;
fi
if [ `echo "$TEMP" | wc -l` -eq 0 ] ; then
if [ -z "$3" ] ;then
Display --indent 2 --text "No declaration of $2" --result WARNING --color RED
return 1;
fi
fi
if [ `echo "$TEMP" | wc -l` -eq 1 ] ;then
# export RESULTAT=`echo "${TEMP}" | sed "s/^[[:space:]]*[a-zA-Z0-9][a-zA-Z0-9]*[[:space:]]*//g" | sed 's/#.*//g'`
# export RESULTAT=`echo "${TEMP}" | sed "s/^[[:space:]]*$2[[:space:]]*//g" | sed 's/#.*//g'`
export RESULTAT="${TEMP}"
fi
return 0;
}
#========================================================================================
#========================================================================================
#Find value type "$2 = value"
FindValueOfEqual()
{
RESULTAT=''
if [ "$1x" = "x" ]
then
Display --indent 2 --text "Missing argument #1 (conf file to scan) of FindValueOfEqual" --result WARNING --color RED --advice YASAT_BUG
return 2;
fi
if [ "$2x" = "x" ]
then
Display --indent 2 --text "Missing argument #2 (directive to scan) of FindValueOfEqual" --result WARNING --color RED --advice YASAT_BUG
return 2;
fi
TEMP=""
Debug "cherche la valeur de $2 dans $1"
TEMP=`grep -rih "^[[:space:]]*$2[[:space:]]*=" $1`
if [ -z "$TEMP" ]
then
if [ "$3x" = "x" ]
then
Display --indent 2 --text "No declarations of $2 " --result WARNING --color RED
return 1;
fi
fi
if [ `echo "$TEMP" | wc -l` -ge 2 ]
then
Display --indent 2 --text "Error multiple declarations of $2 " --result WARNING --color RED --advice GLOBAL_MULTIPLE_DECLARATIONS
return 3;
fi
if [ `echo "$TEMP" | wc -l` -eq 0 ]
then
if [ "$3x" = "x" ]
then
Display --indent 2 --text "No declarations of $2 " --result WARNING --color RED
return 1;
fi
fi
if [ `echo "$TEMP" | wc -l` -eq 1 ]
then
export RESULTAT=`echo $TEMP | sed "s/^[[:space:]]*$2[[:space:]]*=[[:space:]]*//g" | sed 's/[#;].*//g'`
fi
return 0;
}
#========================================================================================
#========================================================================================
#Find value type "$2 : value"
FindValueOfDDot()
{
RESULTAT=''
if [ "$1x" = "x" ]
then
Display --indent 2 --text "Missing argument #1" --result WARNING --color RED --advice YASAT_BUG
return 2;
fi
if [ "$2x" = "x" ]
then
Display --indent 2 --text "Missing argument #2" --result WARNING --color RED --advice YASAT_BUG
return 2;
fi
TEMP=""
Debug "cherche la valeur de $2 dans $1"
TEMP=`grep -rih "^[[:space:]]*$2[[:space:]]*:" $1`
if [ -z "$TEMP" ]
then
if [ "$3x" = "x" ]
then
Display --indent 2 --text "No declarations of $2 " --result WARNING --color RED
return 1;
fi
fi
if [ `echo "$TEMP" | wc -l` -ge 2 ]
then
Display --indent 2 --text "Error multiple declarations of $2 " --result WARNING --color RED --advice GLOBAL_MULTIPLE_DECLARATIONS
return 3;
fi
if [ `echo "$TEMP" | wc -l` -eq 0 ]
then
if [ "$3x" = "x" ]
then
Display --indent 2 --text "No declarations of $2 " --result WARNING --color RED
return 1;
fi
fi
if [ `echo "$TEMP" | wc -l` -eq 1 ]
then
export RESULTAT=`echo $TEMP | sed "s/^[[:space:]]*$2[[:space:]]*:[[:space:]]*//g" | sed 's/#.*//g'`
fi
return 0;
}
################################################################################
################################################################################
CheckPresenceOf()
{
if [ "$1x" = "x" ]
then
Display --indent 2 --text "Missing argument #1" --result WARNING --color RED --advice YASAT_BUG
fi
if [ "$2x" = "x" ]
then
Display --indent 2 --text "Missing argument #1" --result WARNING --color RED --advice YASAT_BUG
fi
TEMP=""
Debug "cherche si $2 est dans $1"
TEMP=`grep -rih "^ *$2" $1`
if [ -z "$TEMP" ]
then
if [ "$3x" = "x" ]
then
Display --indent 2 --text "No declarations of $2 " --result WARNING --color RED
fi
fi
if [ `echo "$TEMP" | wc -l` -ge 2 ]
then
Display --indent 2 --text "Error multiple declarations of $2 " --result WARNING --color RED --advice GLOBAL_MULTIPLE_DECLARATIONS
fi
if [ `echo "$TEMP" | wc -l` -eq 0 ]
then
if [ "$3x" = "x" ]
then
Display --indent 2 --text "No declarations of $2 " --result WARNING --color RED
fi
fi
if [ `echo "$TEMP" | wc -l` -eq 1 ]
then
export RESULTAT=`echo $TEMP | sed "s/^.*$2\ //g" | cut -d\ -f1`
## echo "=>$RESULTAT<="
fi
}
################################################################################
################################################################################
Title()
{
if [ -z "$1" ]
then
echo "Error missing parameter for Title()"
return 1;
fi
echo "=== $1 ==="
if [ ! -z "$HTML_OUTPUT" ]
then
echo "</table><h1>$1</h1><br><table border='1'>" >> "$HTML_OUTPUT"
fi
if [ ! -z "$REPORT_OUTPUT" ]
then
echo '' >> $REPORT_OUTPUT
echo "=== $1 ===" >> $REPORT_OUTPUT
fi
}
################################################################################
################################################################################
#affiche_rouge()
#{
# echo -e "\033[31m $1 \033[0m "
#}
################################################################################
################################################################################
#affiche_vert()
#{
# echo -e "\033[0;32m $1 \033[0m "
#}
################################################################################
################################################################################
#affiche_orange()
#{
# echo -e "\033[0;33m $1 \033[0m "
#}
################################################################################
################################################################################
print_help()
{
echo "====================================="
echo "== YASAT =="
echo "== Yet Another Stupid Audit Tool =="
echo "== =="
echo "== Copyright (C) 2008-2015 =="
echo "== LABBE Corentin =="
echo "============================================================"
echo "|Available options |"
echo "| |"
echo "| --standard (-s) Do standard test ====="
echo "| --list (-l) List plugins available |"
echo "| --debug (-d) print debug informations |"
echo "| --help (-h) show this help ====="
echo "| --html (-H) export YASAT 's results in html |"
echo "| default to ~/yasat/yasat.html |"
echo "| --html-output PATH PATH is the name of html file to write |"
echo "| --advice-lang LANG LANG is the 2letter digit of the lang |"
echo "| (default is EN ) |"
echo "| --full-scan (-f) Do extra (long) tests (lots of find) |"
echo "| --plugins-dir PATH (-P) Set the path to the plugins to use |"
echo "| (default is ./plugins ) |"
echo "| --nopause (-a) Do not make a pause after plugin's end |"
echo "| --plugin PATH (-1) Just use the plugin pointed by PATH |"
echo "| --Plugin NAME (-p) Just use the plugin named NAME |"
echo "| --scanroot PATH (-r) Scan PATH instead of / (WorkInProgress)|"
echo "| --compliance type Check for a specific compliance. |"
echo "| type could be cce, nsa or all |"
echo "| --print-level X Just print infos equal or above the |"
echo "| level X (All = 0 (default), infos = 1 |"
echo "| warnings(orange) = 2, errors(red) = 3 |"
echo "| --skip Test(s) to skip, without the .test |"
echo "| (ex: --skip nfs,ntp) comma separated |"
echo "| --check-update Check if an update of YASAT exists |"
echo "| --send-support Same as --check-support but you will |"
echo "| send also your OS version as parameter |"
echo "| --desktop Use the desktop profile |"
echo "| --server Use the server profile |"
echo "| --version (-V) Print yasat version |"
echo "| |"
echo "| Thanks for using YASAT. |"
echo "| |"
echo "===================================================================="
}
################################################################################
################################################################################
#compare 2 right
#example compare_right 666 640 YES said bad
#example compare_right 666 640 YES said bad
#example compare_right 400 640 NO said bad
#example compare_right 400 640 YES said good
#if arg #3 is NO, we want that $1 and $2 is strictly equal
compare_right()
{
# RESULTAT='ERROR'
if [ -z "$1" ] ; then
echo "ERROR compare_right() missing arg #1 (right to test)"
return 1;
fi
if [ -z "$2" ] ; then
echo "ERROR compare_right() missing arg #2 (right wanted)"
return 1;
fi
if [ -z "$3" ] ; then
echo "ERROR compare_right() missing arg #3 (accept or not more restricted right)"
return 1;
fi
WANT_U="`echo $2 | cut -c1`"
TEST_U="`echo $1 | cut -c1`"
WANT_G="`echo $2 | cut -c2`"
TEST_G="`echo $1 | cut -c2`"
WANT_O="`echo $2 | cut -c3`"
TEST_O="`echo $1 | cut -c3`"
# echo "$WANT_U vs $TEST_U"
if [ $TEST_U -gt $WANT_U ] ; then
return 2
else
if [ "$3" = 'NO' -a $TEST_U -lt $WANT_U ]; then
return 3
fi
fi
# echo "$WANT_G vs $TEST_G"
if [ $TEST_G -gt $WANT_G ] ; then
return 2
else
if [ "$3" = 'NO' -a $TEST_G -lt $WANT_G ]; then
return 3
fi
fi
# echo "$WANT_O vs $TEST_O"
if [ $TEST_O -gt $WANT_O ] ; then
return 2
else
if [ "$3" = 'NO' -a $TEST_O -lt $WANT_O ]; then
return 3
fi
fi
# RESULTAT='GOOD'
return 0
}
################################################################################
################################################################################
check_whiteliste_cert()
{
if [ ! -e "$1" ] ; then
echo "ERROR check_certificate() $1 do not exist"
return 1;
fi
FINGERPRINT="` openssl x509 -in \"$1\" -fingerprint -noout | cut -d\= -f2`"
if [ -z "`grep $FINGERPRINT certdata.txt`" ];then
echo "$FINGERPRINT $1 $line" >> xp-cert.out
else
echo "$FINGERPRINT $1 $line" >> xp-cert.in
fi
return 0;
TMPF=`mktemp`
openssl x509 -in "$1" -text > $TMPF
CERT_CN="`grep 'Subject:.*CN=' $TMPF | sed 's,.*CN=,,' |sed 's/,.*//' |sed 's,/emailAddress=.*,,'`"
if [ ! -z "$CERT_CN" ];then
echo "$CERT_CN" >> xp-cert.log
echo "Found xxx${CERT_CN}xxx"
if [ -z "`grep \"CN=${CERT_CN},\" certdata.txt`" ];then
echo "$line" >> xp-cert.out
else
echo "$line" >> xp-cert.in
fi
else
CERT_OU="`grep 'Subject:.*OU=' $TMPF | sed 's,.*\,[[:space:]]*OU=,,' | sed 's,/emailAddress=.*,,'`"
echo "$CERT_OU" >> xp-cert.log
echo "Found xxx${CERT_OU}xxx"
if [ -z "`grep \"OU=${CERT_OU},\" certdata.txt`" ];then
echo "$line" >> xp-cert.out
else
echo "$line" >> xp-cert.in
fi
fi
rm $TMPF
}
################################################################################
################################################################################
#864000s = 1DAY
#2592000 = 30DAYS
check_certificate()
{
RESULTAT='GOOD'
if [ ! -e "$1" ] ; then
echo "ERROR check_certificate() $1 do not exist"
return 1;
fi
if [ -z "$2" ] ; then
echo "ERROR check_certificate() missing #2 indent"
return 1;
fi
INDENT="$2"
CHECK_CERT_NAME="$1"
if [ ! -z "$3" ] ; then
CHECK_CERT_NAME="$3"
fi
#check_whiteliste_cert "$1"
#command not found return error code 127
openssl version> /dev/null 2>> $ERROR_OUTPUT_FILE
if [ $? -eq 127 ] ; then
Display --indent $INDENT --text "No openssl binary" --result WARNING --color RED
return 1;
fi
Display --indent $INDENT --text "Check $CHECK_CERT_NAME" --result INFO --color BLUE
INDENT=$(($INDENT+2))
$ECHOCMD -n "\033[${INDENT}C" && openssl x509 -in $1 -noout -enddate
BADTIME=1000000
for check_time in 0 604800 2592000
do
PERIOD_NAME='now'
if [ $check_time -eq 604800 ] ; then
PERIOD_NAME='week'
fi
if [ $check_time -eq 2592000 ] ; then
PERIOD_NAME='month'
fi
openssl x509 -in $1 -noout -checkend $check_time
if [ $? -eq 1 ] ; then
if [ $BADTIME -eq 1000000 ] ; then
if [ $check_time -eq 0 ] ; then
Display --indent $INDENT --text "Cert is outdated " --result WARNING --color RED --advice CERTIFICATE_OUTDATED
else
Display --indent $INDENT --text "Cert < 1 $PERIOD_NAME " --result WARNING --color RED --advice CERTIFICATE_OUTDATED
fi
RESULTAT='BAD'
BADTIME=$check_time
fi
fi
done
if [ $BADTIME -eq 1000000 ] ; then
Display --indent $INDENT --text "Cert expiration date > 1 month " --result OK --color GREEN
fi
#Get signature algorithm, we do not want MD5 hash http://www.win.tue.nl/hashclash/rogue-ca/
#md2WithRSAEncryption is bad
#md5WithRSAEncryption is bad
#sha1WithRSAEncryption is good
#sha256WithRSAEncryption is good
#ecdsa-with-SHA384 is ?
#dsaWithSHA1 ?
CERT_TMP_RESULT="${TEMPYASATDIR}/cert.out"
openssl x509 -in $1 -text > $CERT_TMP_RESULT