-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
SUDO_KILLERv3.sh
executable file
·2128 lines (1697 loc) · 88.5 KB
/
SUDO_KILLERv3.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 developed to check for common misconfigurations and vulnerabilities of the sudo
# V1: Date Created : 08/12/2018
# V2: Date Created : 11/02/2020
# V3: Date Created : 20/07/2023
# Date of last modification : 25/01/2024
# @TH3xACE - BLAIS David
version="version 3.0.1"
##### (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 " %s \n" "$version"
echo -e " Example: ./sudo_killer.sh -c -r report.txt -e /tmp/ \n"
echo "OPTIONS:"
echo "-c Includes CVEs related to sudo's version"
echo "-a Includes CVEs related to third party apps/devices"
echo "-i import (offline mode) from extract.sh"
echo "-e Include export of sudo rules / sudoers file"
echo "-r Enter report name"
echo "-p path where to save export and report"
echo "-s Supply user password for sudo checks (NOT SECURE)"
# echo "-t Include thorough (lengthy) tests"
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
echo -e "${BLUE} $version${RESET}\n"
# CANARY
}
#------------------------------------------------------
function versionToInt() {
# set -e
# local IFS=.
# local parts=($1)
# local val=$((1000000*parts[0]+1000*parts[1]+parts[2]))
# cnver=$val
local IFS=.
parts=($1)
let val=1000000*parts[0]+1000*parts[1]+parts[2]
cnver=$val
}
init() {
if [ -n "$import" ]; then
sudover=$(grep "Sudo version" "$import")
else
sudover=$(sudo -V 2>/dev/null | grep "Sudo version" 2>/dev/null)
fi
sudover1=$(echo "$sudover" | sed 's/Sudo version //g' | cut -d"p" -f 1)
if [ -n "$sudover1" ]; then
versionToInt "$sudover1"
fi
if [ -z "$cnver" ]; then
echo "Error: The tool has not been able to convert the sudo's version!"
fi
if [ -n "$path" ]; then
vpath="$path/sudo_killer-export-$(date +'%d-%m-%y')"
else
vpath="/tmp/sudo_killer-export-$(date +'%d-%m-%y')"
fi
# Create the directory
mkdir -p "$vpath"
} # init
#------------------------------------------------------
checksudoersize() {
file_path="/etc/sudoers"
# Check if the file exists
if [ -f "$file_path" ]; then
# Get the file size in bytes using ls and awk
file_size_bytes=$(ls -al "$file_path" | awk '{print $5}')
# Check if the file size is between 600 bytes and 770 bytes (inclusive)
if ((file_size_bytes >= 600 && file_size_bytes <= 770)); then
echo -e "${BOLD}${YELLOW}[+] Sudo's rules:${RESET} It seems there is no custom sudo's rules! (size) ${BOLD}${RED}[DEFAULT] ${RESET} \n"
#echo "The file size is between 600 bytes and 770 bytes."
else
#echo "The file size is not between 600 bytes and 770 bytes."
echo -e "${BOLD}${YELLOW}[+] Sudo's rules:${RESET} It seems that custom sudo's rules for the current user exists! (size) ${BOLD}${RED}[CUSTOM]${RESET} \n"
fi
else
echo "File not found: $file_path"
fi
}
checksudoerstimestamp() {
maccrdate=$(ls -al /etc/ssh/ | grep -iw "ssh_host_rsa_key.pub" | awk '{print $6,$7,$8}')
sudoerscrdate=$(ls -al /etc/sudoers | awk '{print $6,$7,$8}')
if ((maccrdate == sudoerscrdate)); then
echo -e "${BOLD}${YELLOW}[+] Sudo's rules:${RESET} It seems there is no custom sudo's rules! (timestamp) ${BOLD}${RED}[DEFAULT] ${RESET} \n"
else
echo -e "${BOLD}${YELLOW}[+] Sudo's rules:${RESET} It seems that custom sudo's rules for the current user exists! (timestamp) ${BOLD}${RED}[CUSTOM]${RESET} \n"
fi
}
checkcustomsecurepath()
{
sudosecpacth=$(echo "$cmd" 2>/dev/null | grep "secure_path=" | cut -d= -f 2 | sed 's/:/\n/g' | grep -v "bin")
if [ "$sudosecpacth" ]; then
echo -e "${BOLD}${YELLOW}[+] Custom Secure Path:${RESET} It seems that the secure path defined in sudoers includes custom path[s]! ${BOLD}${RED}[CUSTOM] ${RESET}"
echo -e "[*] secure_path: $sudosecpacth \n"
fi
}
intro() {
who=$(whoami 2>/dev/null)
where=$(hostname 2>/dev/null)
echo -e "${BLUE} @TH3xACE - BLAIS David"
echo -e "${BLUE} Contribute and collaborate on the KILLER project @ https://github.com/TH3xACE"
echo -e "${RED} Please consider giving a +1 star on GitHub to show your support! "
echo -e "\n"
echo -e "${RED} IMPORTANT! Always run the latest version [Current: $version]. Run 'git pull' or download the project again. ${RESET}"
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 "Current host: $where"
echo -e "\n"
if [ "$import" ]; then
cmd=$(cat "$import" | grep -v "Sudo version")
elif [ "$sudopass" ]; then
echo -e "${RED} [+] Please enter the password of the current user: ${RESET}"
read -s -p "[+] Password: " userpassword
echo -e "\n*********"
cmdwp=$(echo "$userpassword" | sudo -S -l -k 2>/dev/null)
else
cmd=$(sudo -l -k)
if [ -z "$cmd" ]; then
echo -e "${BOLD}${RED}[**] It seems that sudo's rules cannot be accessed without a password."
echo -e "Try using the '-s' argument and provide the current user's password.${RESET}\n"
echo -e "${BOLD}${YELLOW}[+] This can occur when there is no rule with NOPASSWD or when root has explicitly configured sudo to ask for a password to list rules.${RESET}\n"
exit
fi
fi
if [ "$report" ]; then
echo -e "${BOLD}${YELLOW}[+] Report saved: ${RESET} $vpath/$report"
fi
if [ "$exports" ]; then
echo -e "${BOLD}${YELLOW}[+] Sudo rules exported: ${RESET} $vpath/sudo_export.txt"
fi
checksudoersize
checksudoerstimestamp
checkcustomsecurepath
echo -e "\n"
} # intro
#------------------------------------------------------
# Helper function to check sudo permissions
check_sudo_permissions() {
if [ -z "$sudoperms" ]; then
sudoperms=$(sudo -S -l -k 2>/dev/null)
fi
}
# Helper function to print sudo pwnage
print_sudo_pwnage() {
sudopwnage=$(echo "$sudoperms" | 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"
fi
}
# Helper function to print sudo users
print_sudo_users() {
sudo_user=$(getent group sudo | cut -d":" -f 4)
if [ "$sudo_user" ]; then
echo -e "${BOLD}${GREEN}[+] All users found in sudo group: ${RESET}\n$sudo_user"
echo -e "\n"
fi
}
checkinitial() {
#echo -e "${BOLD}${YELLOW}================== Initial check - Quick overview ========================= ${RESET} \n"
echo -e "${BOLD}${YELLOW}[1/21] ====== Initial check - Quick overview ====== ${RESET} \n"
# useful binaries (thanks to https://gtfobins.github.io/)
binarylist='cp\|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|grep\|csplit\|csvtool'
##### sudo version - check to see if there are any known vulnerabilities with this - CVE
if [ "$sudover" ]; then
echo -e "${BOLD}${GREEN}[+] Sudo version:${RESET}\n$sudover"
echo -e "\n"
fi
###check the timestamp
timestamp=$(sudo -l | grep -i timestamp_timeout | sed 's/,/\n/g' | grep -i timestamp_timeout | cut -d "=" -f 2)
echo -e "${BOLD}${GREEN}[+] Timestamp:${RESET}"
echo -e "Timestamp is the amount of time in minutes between instances of sudo before it will re-prompt for a password."
echo -e "${timestamp:-5} mins"
echo -e "\n"
# Check if sudo is possible without supplying a password
check_sudo_permissions
#sudoperms=$(sudo -S -l -k 2>/dev/null)
if [ "$sudoperms" ]; then
echo -e "${BOLD}${GREEN}[+] SUDO possible without a password!${RESET}\n\n$sudoperms"
echo -e "\n"
fi
# Check if sudo is possible with a password supplied
if [ "$sudopass" ]; then
if [ -z "$sudoperms" ]; then
sudoauth=$(sudo -S -l -k 2>/dev/null)
if [ "$sudoauth" ]; then
echo -e "${BOLD}${GREEN}[+] SUDO possible with a password supplied!${RESET}\n\n$sudoauth"
echo -e "\n"
fi
fi
fi
# Known 'good' breakout binaries (cleaned to parse /etc/sudoers for comma-separated values) - authenticated
print_sudo_pwnage
# sudopwnage=$(echo "$sudoperms" | 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"
# 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"
fi
# Sudo users
print_sudo_users
# sudo_user=$(getent group sudo | cut -d":" -f 4)
# if [ "$sudo_user" ]; then
# echo -e "${BOLD}${GREEN}[+] All users found in sudo group: ${RESET}\n$sudo_user"
# echo -e "\n"
# fi
# # Check if SELinux is enabled
# sestatus=`sestatus 2>/dev/null`
# if [ "$sestatus" ]; then
# echo -e "[-] SELinux seems to be present: $sestatus, can execute /CVE/CVE-2017-1000367-2.c if vulnerable (Check CVEs)."
# echo -e "\n"
# fi
# Exporting sudo rules
if [ "$exports" ]; then
echo "$cmd" >"$vpath/sudo_export.txt" 2>/dev/null
echo -e "${BOLD}${GREEN}[+] Sudo rules exported!${RESET} \n$vpath/sudo_export.txt "
echo -e "\n"
local cmddt=$(sudo -ll)
echo "$cmddt" >"$vpath/sudo_export_list.txt" 2>/dev/null
echo -e "${BOLD}${GREEN}[+] Detailed Sudo rules exported!${RESET} \n$vpath/sudo_export_list.txt "
echo -e "\n"
fi
# pull out vital sudoers info
# sudoers=$(grep -v -e '^$' /etc/sudoers 2>/dev/null | grep -v "#" 2>/dev/null)
# # Export sudoers file to export location
# if [ "$exports" ] && [ "$sudoers" ]; then
# echo -e "${BOLD}${GREEN}[+] Sudoers configuration exported:${RESET}\n$sudoers"
# echo -e "\n"
# echo "$sudoers" > "$vpath/sudoers_export.txt" 2>/dev/null
# fi
if [ -r "/etc/sudoers" ]; then
echo -e "${BOLD}${RED}[+] The file /etc/sudoers is readable by current user.${RESET}"
#export sudoers file to export location
if [ "$exports" ] && [ "$sudoers" ]; then
cp /etc/sudoers $vpath/sudoers_export.txt 2>/dev/null
else
:
fi
fi
} # checkinitial
#------------------------------------------------------
# Function to print vulnerability information
check_cve_version() {
echo -e "${BOLD}${GREEN}[+] Sudo version is vulnerable to the following CVEs:${RESET}"
echo -e "${BOLD}${GREEN}[+] Despite the version being vulnerable to a CVE or several,${RESET}"
echo -e "${BOLD}${GREEN}[+] some requirements might be needed for exploitation.\n${RESET}"
sver_tmp=$(sudo -V 2>/dev/null | grep "Sudo version" 2>/dev/null | cut -d" " -f 3 2>/dev/null)
version=$(echo $sver_tmp | tr -d ' ' | sed 's/P/p/g')
# Display CVEs vulnerable based on version
cat $PWD/CVE/cve.sudover.vuln.txt | grep "$version" | cut -d"+" -f 1,2 | awk '{print $0,"\n"}'
cve_vuln=$(cat $PWD/CVE/cve.sudover.vuln.txt | grep "$version" | cut -d"+" -f 1)
if [ "$cve_vuln" ]; then
echo -e "\n[+] Please find the following exploit(s) for some of the detected CVEs\n"
while read -r line; do
cvepath=$(ls -al $PWD/CVE/ | grep "$line" | tr -s " " | cut -d " " -f 9)
if [ "$cvepath" ]; then
echo -e " [*] $PWD/CVE/${BOLD}${RED}$cvepath${RESET} \n"
fi
done <<<"$cve_vuln"
fi
}
# function check_and_print_cve() {
# local cve_cond="$1"
# local cve_number="$2"
# local sudo_intversion="$3"
# local sudo_version="$3"
# local description="$4"
# local exploit_file="$5"
# if [ "$cnver" -lt "$sudo_intversion" ]; then
# cve_vulnerable=$(eval "echo \"\$cmd\" 2>/dev/null | $cve_cond")
# #echo -e "$cve_cond"
# if [ "$cve_vulnerable" ]; then
# echo -e "${BOLD}${GREEN}[+] Checking for the vulnerability $cve_number: ${RESET}"
# echo -e "${BOLD}${RED}[-] Vulnerable to $cve_number${RESET}"
# echo -e "[-] Current Sudo version: $sudover | Vulnerable version: <=$sudo_version"
# echo -e "[-] Description: $description"
# echo -e "[-] Exploit: /CVE/$exploit_file"
# echo -e "\n"
# fi
# fi
# }
checkcve() {
# Check for sudo version vulnerability based on CVEs
if [ "$sudocve" ]; then
echo -e "${BOLD}${YELLOW}[2/21] ====== Checking for disclosed vulnerabilities (CVEs) - version based ====== ${RESET} \n"
check_cve_version
echo -e "${BOLD}${BLUE}[@] Check [2/21] Completed!${RESET} \n"
# ---------------------------------------------
echo -e "${BOLD}${YELLOW}[3/21] ====== Checking for disclosed vulnerabilities (CVE) ====== ${RESET} \n"
echo -e "${BOLD}${GREEN}[+] The prerequisites for the below CVEs have been checked (not all CVEs checked - refer to readme):${RESET}"
echo -e "${BOLD}${RED}[+] Highly probable that sudo is VULNERABLE to the below CVEs:${RESET} \n"
# Check for specific CVE vulnerabilities as well as their requirements
##### 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).
# check_and_print_cve 'grep "(root) NOPASSWD: sudoedit" | grep -F $"/*/*/"' "CVE-2015-5602" 1008015 "The bug was found in sudoedit, which does not check the full path if a wildcard is used twice." "CVE-2015-5602.sh"
if [ "$cnver" -lt "1008015" ]; then
sudodblwildcard=$(echo "$cmd" 2>/dev/null | grep "(root) NOPASSWD: sudoedit" | grep -F $"/*/*/")
if [ "$sudodblwildcard" ]; then
echo -e "${BOLD}${RED}[-] Vulnerable to CVE-2015-5602${RESET}"
echo -e "${BOLD}${GREEN}[+] Sudoedit with double wildcard was detected. The bug was found in sudoedit, which does not check the full path if a wildcard is used twice. (CVE-2015-5602): ${RESET}"
echo -e "$sudodblwildcard"
echo -e "[-] current $sudover | vuln version: <=1.8.14"
echo -e "[*] Exploit: /CVE/CVE-2015-5602.sh"
echo -e "\n"
# echo -e "[-] run the command: sudo ./CVE-2015-5602.sh then su [RANDOM PASSWORD GENERATED]\n"
fi
fi # check version
##### CVE-2019-14287
if [ "$cnver" -lt "1008027" ]; then
sudorunas=$(echo "$cmd" 2>/dev/null | grep "(ALL, \!root)")
if [ "$sudorunas" ]; then
cmdi=$(echo "$cmd" 2>/dev/null | grep "(ALL, \!root)" | sed 's/NOPASSWD//g' | sed 's/://g' | cut -d ")" -f 2)
echo -e "${BOLD}${RED}[-] Vulnerable to CVE-2019-14287${RESET}"
echo -e "${BOLD}${GREEN}[+] The vulnerability allows users with sudo permissions to execute arbitrary commands as root. (CVE-2019-14287): ${RESET}"
echo -e "[-] current $sudover | vuln version: <=1.8.27"
echo -e "[-] Example : sudo -u#-1 /usr/bin/id"
echo -e "[-] Run command : sudo -u#-1 <cmd>"
echo -e "[-] where <cmd> is one of the following:"
echo -e "$cmdi"
echo -e "[*] Exploit: /CVE/CVE-2019-14287.txt"
echo -e "\n"
fi
fi
##### CVE-2019-18634
if [ "$cnver" -lt "1008026" ] && [ "$cnver" -gt "1007001" ]; then
sudopwfeedback=$(echo "$cmd" 2>/dev/null | grep " pwfeedback")
if [ "$sudopwfeedback" ]; then
echo -e "${BOLD}${RED}[-] Vulnerable to CVE-2019-18634${RESET}"
echo -e "${BOLD}${GREEN}[+] The vulnerability is caused by a heap-based buffer overflow condition in the sudo command. (CVE-2019-18634): ${RESET}"
echo -e "[-] current $sudover | vuln version: 1.7.1 to 1.8.25p1 inclusive"
# echo -e "[-] Run command : perl -e 'print(("A" x 100 . "\x{00}") x 50)' | sudo -S id"
echo -e "[-] Run command : perl -e 'print((\"A\" x 100 . \"\\x{00}\") x 50)' | sudo -S id"
echo -e "[-] if you have a segmentation fault then sudo is vulnerable"
echo -e "[*] Notes: /exploits/pwfeedback.txt"
echo -e "[*] Exploit: /CVE/CVE-2019-16634-pwfeedback/CVE-2019-18634.sh"
echo -e "\n"
fi
fi
#### CVE-2021-23240
sudoedit_selinux=$(cat $PWD/CVE/cve.sudover.vuln.txt | grep "$(echo $sver)" | grep "CVE-2021-23240" | cut -d"+" -f 1)
if [ "$sudoedit_selinux" ]; then
#check_psymlinks=$(cat /proc/sys/fs/protected_symlinks | grep 0)
#if [ "$check_psymlinks" ]; then
echo -e "${BOLD}${RED}[-] Vulnerable to CVE-2021-23240${RESET}"
echo -e "${BOLD}${GREEN}[+] The vulnerability is caused by a heap-based buffer overflow condition in the sudo command. (CVE-2021-23240): ${RESET}"
echo -e "[-] The version of sudo is vulnerable and symlinks is not protected (set to 0)"
echo -e "[-] Provided that SELinux is in permissive (not enforcing or disables) mode (Refer to the file /etc/selinux/) "
echo "or the invoking user is in an unconfined domain, then only all requirements will be met for exploitation."
echo "Permissive mode: SELinux prints warnings instead of enforcing."
echo -e "[*] M1 : Run command: sudoedit /path then :e /etc/sudoers or :e /etc/shadow"
echo -e "[*] M2 : Run command: S1 -> sudoedit /path then :call libcallnr("libc.so.6","setuid",0)"
echo -e " S2 -> then run ::!bash"
echo -e "[*] M3 : Notes: /CVE/CVE-2021-23240.txt"
echo -e "\n"
#fi
fi
#### CVE-2021-3156
sudoescapevschk=$(cat $PWD/CVE/cve.sudover.vuln.txt | grep "$(echo $sver)" | grep "CVE-2021-3156" | cut -d"+" -f 1)
if [ "$sudoescapevschk" ]; then
sudounescapeof=$(echo "$cmd" 2>/dev/null | grep -w "root) NOPASSWD:\|ALL) NOPASSWD:" | grep "sudoedit /")
if [ "$sudounescapeof" ]; then
sudo_escape1=$(sudoedit -s / 2>&1)
sudo_escape=$(echo "$sudo_escape1" | grep -w "sudoedit: /: not a regular file")
#sudo_escape=$("sudoedit -s /")
if [ "$sudo_escape" ]; then
echo -e "${BOLD}${RED}[-] Vulnerable to CVE-2021-3156${RESET}"
echo -e "${BOLD}${GREEN}[+] The vulnerability is caused by a heap-based buffer overflow vulnerability in sudo, allowing attackers to gain root-level privileges on Unix-like systems. (CVE-2021-3156): ${RESET}"
echo -e "[-] current $sudover | vuln version: 1.7.7-1.7.10p9, 1.8.2-1.8.31p2, and 1.9.0-1.9.5p1"
#echo -e "[*] Run command: sudoedit -s / - If output starts with { sudoedit: } vulnerable else { usage: } not vulnerable "
#echo -e "Example of output: { sudoedit: /: not a regular file } means it is Vulnerable to CVE-2021-3156"
echo -e "[*] Notes: CVE/CVE-2021-3156.txt"
echo -e "[*] Exploit: refer to CVE/CVE-2021-3156/, several exploits are provided and be aware then some of them can pose some risks"
echo -e " to be run on production environment and most of them are version specific... read the readme/note."
echo -e "\n"
fi
fi
fi
### CVE-2023-22809
sudoeditrockchk=$(cat $PWD/CVE/cve.sudover.vuln.txt | grep "$(echo $sver)" | grep "CVE-2023-22809" | cut -d"+" -f 1)
if [ "$sudoeditrockchk" ]; then
#sudoeditrock=$(echo "$cmd" 2>/dev/null | grep -i "(root) NOPASSWD: sudoedit /")
#sudoeditrock=$(echo "$cmd" 2>/dev/null | grep -i "(root) NOPASSWD: sudoedit /" | sed -e "s/(root) NOPASSWD: /EDITOR='vi -- \/etc\/shadow' /g" )
sudoeditrock=$(echo "$cmd" 2>/dev/null | grep -i "(root) NOPASSWD: sudoedit /\|(ALL : ALL) NOPASSWD: sudoedit\|(ALL) NOPASSWD: sudoedit" | sed -e "s/(root) NOPASSWD: /EDITOR='vi -- \/etc\/shadow' /g" | sed -e "s/(ALL : ALL) NOPASSWD: /EDITOR='vi -- \/etc\/shadow' /g" | sed -e "s/(ALL) NOPASSWD: /EDITOR='vi -- \/etc\/shadow' /g")
if [ "$sudoeditrock" ]; then
#sudo_escape=$(sudoedit -s / | grep "sudoedit:")
#sudo_escape=$("sudoedit -s /")
#if [ "$sudo_escape" ]; then
echo -e "${BOLD}${RED}[-] Vulnerable to CVE-2023-22809${RESET}"
echo -e "${BOLD}${GREEN}[+] The vulnerability allows users to run arbitrary commands by abusing sudoedit as root without authentication. (CVE-2023-22809):${RESET}"
echo -e "[-] current $sudover | vuln version: 1.8.0 to 1.9.12p1 inclusive"
echo -e "[*] Run one of the command (No Password Required): "
echo -e "$sudoeditrock"
echo -e "[+] Tested editor: vi and vim, the file is /etc/shadow here but can be any file"
echo -e "[+] The variable EDITOR is used as default but can be also SUDO_EDITOR or VISUAL"
echo -e "[*] Notes: /CVE/CVE-2023-22809.txt"
#echo -e "[*] Exploit: "
echo -e "\n"
#fi
fi
sudoeditrocknp=$(echo "$cmd" 2>/dev/null | grep -i "(root) sudoedit /\|(ALL : ALL) sudoedit\|(ALL) sudoedit" | sed -e "s/(root) /EDITOR='vi -- \/etc\/shadow' /g" | sed -e "s/(ALL : ALL) /EDITOR='vi -- \/etc\/shadow' /g" | sed -e "s/(ALL) /EDITOR='vi -- \/etc\/shadow' /g")
if [ "$sudoeditrocknp" ]; then
echo -e "${BOLD}${RED}[-] Vulnerable to CVE-2023-22809${RESET}"
echo -e "${BOLD}${GREEN}[-] The vulnerability allows users to run arbitrary commands by abusing sudoedit as root ${BLUE}with user authentication (password needed) ${RESET}. (CVE-2023-22809):${RESET}"
echo -e "[-] current $sudover | vuln version: 1.8.0 to 1.9.12p1 inclusive"
echo -e "[*] Run one of the command ${BLUE}(User's password required)${RESET}: "
echo -e "$sudoeditrocknp"
echo -e "[+] Tested editor: vi and vim, the file is /etc/shadow here but can be any file"
echo -e "[+] The variable EDITOR is used as default but can be also SUDO_EDITOR or VISUAL"
echo -e "[*] Notes: /CVE/CVE-2023-22809.txt"
#echo -e "[*] Exploit: "
echo -e "\n"
fi
##### Check for absolute path to sudoedit
if [ "$cnver" -lt "1008030" ]; then
sudoeditpathcmd=$(echo "$cmd" 2>/dev/null | grep -E "(/bin/|/usr/bin/|/usr/local/bin/)sudoedit" | cut -d " " -f 8)
sudoeditpath=$(echo "$cmd" 2>/dev/null | grep -Eo "(/bin/|/usr/bin/|/usr/local/bin/)sudoedit")
if [ "$sudoeditpath" ]; then
echo -e "${BOLD}${RED}[-] Vulnerable to sudoedit absolute path vuln${RESET}"
echo -e "${BOLD}${GREEN}[+] Absolute path to sudoedit was found in the sudoers file: ${RESET}"
echo -e "[-] Privilege escalation is possible if the sudo version is < 1.8.30"
echo -e "[*] Run the command sudo $sudoeditpath <file> to invoke a file editor as root"
echo -e "[*] where <file> is as below:"
echo -e "$sudoeditpathcmd"
echo -e "[-] Once you are in the editor, type the following command in command mode to get a shell"
echo -e "[-] Run command : :set shell=/bin/sh"
echo -e "[-] :shell"
echo -e "[*] Then use the appropriate exploit from res/absolute_path-sudoedit.txt for the editor you invoked \n"
fi
fi
fi
echo -e "${BOLD}${BLUE}[@] Check [3/21] Completed!${RESET} \n"
else
echo -e "${BOLD}${YELLOW}[2/21] ====== Checking for disclosed vulnerabilities (CVEs) - version based ====== ${RESET} \n"
echo -e "${BOLD}${RED}Checks related to CVEs were skipped. To include them use the flag -c ${RESET} \n"
echo -e "${BOLD}${YELLOW}[3/21] ====== Checking for disclosed vulnerabilities (CVE) ====== ${RESET} \n"
echo -e "${BOLD}${RED}Checks related to CVEs were skipped. To include them use the flag -c ${RESET} \n"
fi # cve flag check
} # checkcve
#------------------------------------------------------
fn_excess_priv() {
# echo -e "$cmd"
# Detects variants of the execessive sudo rule "ALL (ALL) NOPASSWD: ALL"
# Variant 1: user (ALL) NOPASSWD: ALL
# regex for /etc/sudoers | grep -E '^\S+\s+\(ALL\)\s+NOPASSWD:\s+ALL'
exprv1=$(echo "$cmd" 2>/dev/null | grep -E '\(ALL\)\s+NOPASSWD:\s+ALL')
if [ "$exprv1" ]; then
echo -e "${BOLD}${RED}[+] Excessive Privilege detected [Variant 1]${RESET}"
echo -e "[-] Variant 1: $who (ALL) NOPASSWD: ALL"
echo -e "[-] Identified rule: \n$exprv1"
echo -e "[-] Impersonate: ${BOLD}${BLUE}Any user [incl root]${RESET}"
echo -e "[-] Password required: ${BOLD}${BLUE}No${RESET}"
echo -e "[-] Command/bin: ${BOLD}${BLUE}Any${RESET}"
echo -e "[*] Notes: notes/execessive_priv.txt \n"
fi
# # Variant 3: user (root) NOPASSWD: ALL
# regex for /etc/sudoers | grep -E '^\S+\s+\(root\)\s+NOPASSWD:\s+ALL'
exprv3=$(echo "$cmd" 2>/dev/null | grep -E '\(root\)\s+NOPASSWD:\s+ALL')
if [ "$exprv3" ]; then
echo -e "${BOLD}${RED}[+] Excessive Privilege detected [Variant 3]${RESET}"
echo -e "[-] Variant 3: $who (root) NOPASSWD: ALL"
echo -e "[-] Identified rule: \n$exprv3"
echo -e "[-] Impersonate: ${BOLD}${BLUE}root${RESET}"
echo -e "[-] Password required: ${BOLD}${BLUE}No${RESET}"
echo -e "[-] Command/bin: ${BOLD}${BLUE}Any${RESET}"
echo -e "[*] Notes: notes/execessive_priv.txt \n"
fi
# # Variant 4/5: user (ALL) NOPASSWD: /usr/bin/command
# regex for /etc/sudoers | grep -E '^\S+\s+\(ALL\)\s+NOPASSWD:\s+/usr/bin/\w+'
exprv45=$(echo "$cmd" 2>/dev/null | grep -E '\(ALL\)\s+NOPASSWD:\s+/*bin*')
if [ "$exprv45" ]; then
echo -e "${BOLD}${RED}[+] Excessive Privilege detected [Variant 4/5]${RESET}"
echo -e "[-] Variant 4/5: $who (ALL) NOPASSWD: */s|bin/*"
echo -e "[-] Identified rule: \n$exprv45"
echo -e "[-] Impersonate: ${BOLD}${BLUE}Any user [incl root]${RESET}"
echo -e "[-] Password required: ${BOLD}${BLUE}No${RESET}"
echo -e "[-] Command/bin: ${BOLD}${BLUE}Listed one from identified rule${RESET}"
echo -e "[*] Notes: notes/execessive_priv.txt \n"
fi
# # # Variant 5: user (ALL) NOPASSWD: /bin/*
# regex for /etc/sudoers | grep -E '^\S+\s+\(ALL\)\s+NOPASSWD:\s+/bin/\*'
# exprv5=$(echo "$cmd" 2>/dev/null | grep -E '\(ALL\)\s+NOPASSWD:\s+ALL')
# if [ "$exprv5" ]; then
# echo -e "${BOLD}${RED}[+] Excessive Privilege detected [Variant 5]${RESET}"
# echo -e "[-] Variant 5: <user> (ALL) NOPASSWD: /bin/*"
# echo -e "[-] Identified rule: $exprv5"
# echo -e "[*] Notes: notes/execessive_priv.txt"
# fi
# # Variant 2: %group (ALL) NOPASSWD: ALL - (ALL : ALL) NOPASSWD: ALL
# # regex for /etc/sudoers | grep -E '^%\w+\s+\(ALL\)\s+NOPASSWD:\s+ALL'
exprv2=$(echo "$cmd" 2>/dev/null | grep -E '\(ALL \: ALL\)\s+NOPASSWD:\s+ALL')
if [ "$exprv2" ]; then
echo -e "${BOLD}${RED}[+] Excessive Privilege detected [Variant 2]${RESET}"
echo -e "[-] Variant 2: %group (ALL) NOPASSWD: ALL"
echo -e "[-] Identified rule: \n$exprv2"
echo -e "[-] Impersonate: ${BOLD}${BLUE}Any user [incl root] or group${RESET}"
echo -e "[-] Password required: ${BOLD}${BLUE}No${RESET}"
echo -e "[-] Command/bin: ${BOLD}${BLUE}Any${RESET}"
echo -e "[*] Notes: notes/execessive_priv.txt \n"
fi
# # Variant 6: %group (ALL) NOPASSWD: /sbin/reboot, /sbin/shutdown
# regex for /etc/sudoers | grep -E '^%\w+\s+\(ALL\)\s+NOPASSWD:\s+/*bin/\*'
exprv6=$(echo "$cmd" 2>/dev/null | grep -E '\(ALL \: ALL\)\s+NOPASSWD:\s+/*bin*')
if [ "$exprv6" ]; then
echo -e "${BOLD}${RED}[+] Excessive Privilege detected [Variant 6]${RESET}"
echo -e "[-] Variant 6: %group (ALL) NOPASSWD: */s|bin/*"
echo -e "[-] Identified rule: \n$exprv6"
echo -e "[-] Impersonate: ${BOLD}${BLUE}Any user [incl root] or group${RESET}"
echo -e "[-] Password required: ${BOLD}${BLUE}No${RESET}"
echo -e "[-] Command/bin: ${BOLD}${BLUE}Listed one from identified rule${RESET}"
echo -e "[*] Notes: notes/execessive_priv.txt \n"
fi
# # Variant 7: %group (ALL:ALL) NOPASSWD: ALL
# echo "Variant 7: %group (ALL:ALL) NOPASSWD: ALL"
# sudo -l | grep -E '^%\w+\s+\(ALL:\w+\)\s+NOPASSWD:\s+ALL'
# # Variant 8: user (root) NOPASSWD: */s|bin/*
# regex for /etc/sudoers | grep -E '^\S+\s+\(root\)\s+NOPASSWD:\s+ALL'
exprv8=$(echo "$cmd" 2>/dev/null | grep -E '\(root\)\s+NOPASSWD:\s+/*bin/')
if [ "$exprv8" ]; then
echo -e "${BOLD}${RED}[+] Excessive Privilege detected [Variant 8]${RESET}"
echo -e "[-] Variant 8: $who (root) NOPASSWD: */|bin/*"
echo -e "[-] Identified rule: \n$exprv8"
echo -e "[-] Impersonate: ${BOLD}${BLUE}root${RESET}"
echo -e "[-] Password required: ${BOLD}${BLUE}No${RESET}"
echo -e "[-] Command/bin: ${BOLD}${BLUE}Listed one from identified rule${RESET}"
echo -e "[*] Notes: notes/execessive_priv.txt \n"
fi
}
fn_excess_priv_pwd() {
# Variant 1: user (ALL) ALL
# regex for /etc/sudoers | grep -E '^\S+\s+\(ALL\)\s+ALL'
exprv11=$(echo "$cmd" 2>/dev/null | grep -E '\(ALL\)\s+ALL' | grep -v "PASSWD")
if [ "$exprv11" ]; then
echo -e "${BOLD}${RED}[+] Excessive Privilege detected [Variant 1]${RESET}"
echo -e "[-] Variant 1: $who (ALL) ALL"
echo -e "[-] Identified rule: \n$exprv11"
echo -e "[-] Impersonate: ${BOLD}${BLUE}Any user${RESET}"
echo -e "[-] Password required: ${BOLD}${BLUE}Yes (any account)${RESET}"
echo -e "[-] Command/bin: ${BOLD}${BLUE}Any${RESET}"
echo -e "[*] Notes: notes/execessive_priv.txt \n"
fi
# Variant 4/5: user (ALL) /usr/bin/command
# regex for /etc/sudoers | grep -E '^\S+\s+\(ALL\)\s+/usr/bin/\w+'
exprv451=$(echo "$cmd" 2>/dev/null | grep -E '\(ALL\)\s+/*bin*' | grep -v "PASSWD")
if [ "$exprv451" ]; then
echo -e "${BOLD}${RED}[+] Excessive Privilege detected [Variant 4/5]${RESET}"
echo -e "[-] Variant 4/5: $who (ALL) */s|bin/*"
echo -e "[-] Identified rule: \n$exprv451"
echo -e "[-] Impersonate: ${BOLD}${BLUE}Any user${RESET}"
echo -e "[-] Password required: ${BOLD}${BLUE}Yes (any account)${RESET}"
echo -e "[-] Command/bin: ${BOLD}${BLUE}Listed one from identified rule${RESET}"
echo -e "[*] Notes: notes/execessive_priv.txt \n"
fi
# # Variant 2: %group (ALL) ALL - (ALL : ALL) ALL
# # regex for /etc/sudoers | grep -E '^%\w+\s+\(ALL\)\s+ALL'
exprv21=$(echo "$cmd" 2>/dev/null | grep -E '\(ALL \: ALL\)\s+ALL' | grep -v "PASSWD")
if [ "$exprv21" ]; then
echo -e "${BOLD}${RED}[+] Excessive Privilege detected [Variant 2]${RESET}"
echo -e "[-] Variant 2: %group (ALL) ALL"
echo -e "[-] Identified rule: \n$exprv21"
echo -e "[-] Impersonate: ${BOLD}${BLUE}Any user or group${RESET}"
echo -e "[-] Password required: ${BOLD}${BLUE}Yes (any account)${RESET}"
echo -e "[-] Command/bin: ${BOLD}${BLUE}Any${RESET}"
echo -e "[*] Notes: notes/execessive_priv.txt \n"
fi
# # Variant 6: %group (ALL) /sbin/reboot, /sbin/shutdown
# regex for /etc/sudoers | grep -E '^%\w+\s+\(ALL\)\s+/*bin/\*'
exprv61=$(echo "$cmd" 2>/dev/null | grep -E '\(ALL \: ALL\)\s+/*bin*' | grep -v "PASSWD")
if [ "$exprv61" ]; then
echo -e "${BOLD}${RED}[+] Excessive Privilege detected [Variant 6]${RESET}"
echo -e "[-] Variant 6: %group (ALL) */s|bin/*"
echo -e "[-] Identified rule: \n$exprv61"
echo -e "[-] Impersonate: ${BOLD}${BLUE}Any user or group${RESET}"
echo -e "[-] Password required: ${BOLD}${BLUE}Yes (any account)${RESET}"
echo -e "[-] Command/bin: ${BOLD}${BLUE}Listed one from identified rule${RESET}"
echo -e "[*] Notes: notes/execessive_priv.txt \n"
fi
}
fn_chown() {
#sudochownrec=`echo '' | sudo -S -l -k 2>/dev/null | grep "(root) NOPASSWD:" | grep "/bin/chown -hR"`
sudochownrec=$(echo "$cmd" 2>/dev/null | grep "(root) NOPASSWD:" | grep "/bin/chown -hR")
if [ "$sudochownrec" ]; then
echo -e "${BOLD}${GREEN}[+] Sudo chown with recursive, was found: ${RESET}"
echo -e "$sudochownrec"
echo -e "\n[-] You can change the owner of directories, refer to notes/chown-hR.txt for exploitation\n"
echo -e "[-] run the command: sudo chown -hR [new_owner:old_owner] [/parent/children] "
echo -e "[-] Can be combined with other misconfig for complete privilege escalation such as wildcard(script)/missing script \n"
# echo -e "[-] you can then modify or create .sh script that can be run with root right "
# echo -e "[-] #! /bin/bash "
# echo -e "[-] bash "
# echo -e "[-] sudo ./[appp].sh \n"
fi
sudochown=$(echo "$cmd" 2>/dev/null | grep "(root) NOPASSWD:" | grep "/bin/chown")
if [ "$sudochown" ]; then
echo -e "${BOLD}${GREEN}[+] Sudo chown, was found: ${RESET}"
echo -e "$sudochown"
echo -e "\n[-] You can change the owner of directories, refer to notes/chown-hR.txt for exploitation\n "
fi
}
fn_impersonate() {
#sudoimpuser=$(echo "$cmd" 2>/dev/null | grep "(root) NOPASSWD:" | grep -w "/bin/su")
#sudoimpuser=$(echo "$cmd" 2>/dev/null | grep "NOPASSWD:" | grep -iw "/*bin/su *")
sudoimpuser=$(echo "$cmd" 2>/dev/null | grep "NOPASSWD:" | grep -iw "/*bin/su *" | grep -v "su root\|su - root" | grep -v "/*bin/su -$\|/*bin/su \*$\|/*bin/su \- \*$")
if [ "$sudoimpuser" ]; then
echo -e "${BOLD}${RED}[-] Potential user impersonation detected!${RESET}"
echo -e "${BOLD}${GREEN}[+] User Impersonation : command su found within sudo's rules: ${RESET}"
echo -e "$sudoimpuser"
echo -e "\n[-] You can impersonate users, by running the cmd: sudo su - [USER] "
echo -e "[+] Run the SUDO_KILLER AGAIN after impersonating a user!"
echo -e "[*] Notes: notes/user_impersonation.txt \n"
fi
sudoimproot=$(echo "$cmd" 2>/dev/null | grep "NOPASSWD:" | grep -iw "/bin/su -$\|/bin/su - \*$\|/bin/su \*$\|/bin/su root$\|/*bin/su - root,")
if [ "$sudoimproot" ]; then
echo -e "${BOLD}${RED}[-] Potential root impersonation detected!${RESET}"
echo -e "${BOLD}${GREEN}[+] Root Impersonation : command su found within sudo's rules: ${RESET}"
echo -e "$sudoimproot"
echo -e "\n[-] You can impersonate root, by running the cmd:\n ${BOLD}${BLUE}sudo su -${RESET} or ${BOLD}${BLUE}sudo su - root${RESET} or ${BOLD}${BLUE}sudo su${RESET} or ${BOLD}${BLUE}sudo su root${RESET}"
echo -e "[+] Run the SUDO_KILLER AGAIN after impersonating a user!"
echo -e "[*] Notes: notes/user_impersonation.txt \n"
fi
# sudo -l | grep "NOPASSWD"| grep -i "/bin/su -$"
# sudo -l | grep "NOPASSWD"| grep -i "/bin/su - \*$"
# check1=$(sudo -l | grep "NOPASSWD: /bin/su - root -c")
# check2=$(sudo -l | grep "NOPASSWD: /bin/su - root -c" | cut -d/ -f 5- | cut -d " " -f 1 | grep "\*")
# check3=$(sudo -l | grep "NOPASSWD: /bin/su - root -c" | cut -d/ -f 5- | cut -d " " -f 2 | grep "\*")
# if [ -n "$check1" ] && [ -n "$check2" ] && [ -n "$check3" ]; then
# var1="/bin/su - root -c "
# var2=$(sudo -l | grep "NOPASSWD: /bin/su - root -c" | cut -d ":" -f 2 | sed 's/ \/bin\/su - root -c //g' | sed 's/\*/DUMP/g')
# var2=\"${var2}\;id\"
# echo -e "${BOLD}${RED}[-] Vulnerable to a misconfiguration (wildcard + impersonation) ${RESET}"
# echo -e "[+] This misconfiguration which includes wildcard and impersonation allow to PE to root "
# echo -e "$check1"
# echo -e "[*] Exploit: run the command below:"
# echo -e "[*] sudo $var1$var2"
# echo -e "\n"
# fi
}
fn_userimp() {
# comment due to issue > Checking sudo without password #9
#sudonopassuser==`echo '' | sudo -S -l -k 2>/dev/null | grep "NOPASSWD:" | grep "/bin\|/sbin"`
#sudonopassuser=$(echo "$cmd" 2>/dev/null | grep "NOPASSWD:" | grep -v "root" | sed 's/NOPASSWD//g' | sed 's/(//g' | sed 's/)//g' | sed 's/://g')
sudonopassuser=$(echo "$cmd" 2>/dev/null | grep "NOPASSWD:" | grep -v "(root)\|(ALL" | sed 's/NOPASSWD//g' | sed 's/(//g' | sed 's/)//g' | sed 's/://g' | sort -u)
if [ "$sudonopassuser" ]; then
echo -e "${BOLD}${GREEN}[+] Can impersonate non-root user: ${RESET}"
echo -e "$sudonopassuser"
echo -e "[-] You can impersonate users, by running the cmd: sudo -u [USER] /path/bin"
echo -e "[-] Refer to section [Dangerous bins to escalate to other users] for the exact commands \n"
fi
}
fn_wildcard() {
# grep '*/\|/*\|*' or | grep '*/"\|"/*"\|"*''
#sudowildcard=$(echo "$cmd" 2>/dev/null | grep "(root) NOPASSWD:" | grep '*/\|/*\|*' )
#sudowildcard=$(echo "$cmd" 2>/dev/null | grep "(root) NOPASSWD:" | grep '\*' )
sudowildcard=$(echo "$cmd" 2>/dev/null | grep "NOPASSWD:" | grep "(root)\|(ALL)\|(ALL : ALL)" | grep '\*')
if [ "$sudowildcard" ]; then
echo -e "${BOLD}${GREEN}[+] Wildcard was found in sudo's rules: ${RESET}"
echo -e "$sudowildcard \n"
fi
#sudowildcardsh=$(echo "$cmd" 2>/dev/null | grep "(root) NOPASSWD:" | grep "\*" | grep ".sh")
sudowildcardsh=$(echo "$cmd" 2>/dev/null | grep "NOPASSWD:" | grep "(root)\|(ALL)\|(ALL : ALL)" | grep '\*' | grep ".sh")
if [ "$sudowildcardsh" ]; then
echo -e "${BOLD}${GREEN}[+] Wildcard with a bash script was found in sudo's rules: ${RESET}"
echo -e "$sudowildcardsh \n"
fi
sudowildcardless=$(echo "$cmd" 2>/dev/null | grep "less")
if [ "$sudowildcardless" ]; then
exploit1_sudowildcardless=$(echo "$cmd" 2>/dev/null | grep less | cut -d ":" -f 2- | sed 's/*$/dump \/etc\/shadow/g' | sed s'/*/dump/g')
exploit2_sudowildcardless=$(echo "$cmd" 2>/dev/null | grep less | cut -d ":" -f 2- | sed 's/*$/..\/..\/..\/..\/..\/etc\/shadow/g' | sed s'/*/dump/g')
sudowildcardlesspwd=$(echo "$sudowildcardless" 2>/dev/null | grep "PASSWD")
if [ "$sudowildcardlesspwd" ]; then
echo -e "${BOLD}${GREEN}[+] Wildcard with less command detected ${RESET}${BLUE}(No password required!): ${RESET}"
echo -e "$sudowildcardless \n"
echo -e "Exploit1: sudo$exploit1_sudowildcardless"
echo -e "Exploit2: sudo$exploit2_sudowildcardless\n"
else
echo -e "${BOLD}${GREEN}[+] Wildcard with less command detected (Password required!): ${RESET}"
echo -e "$sudowildcardless \n"
echo -e "Exploit1: sudo$exploit1_sudowildcardless"
echo -e "Exploit2: sudo$exploit2_sudowildcardless\n"
fi
fi
sudowildcardimpr=$(echo "$cmd" 2>/dev/null | grep "/bin/su" | grep " -c " | grep "*$" )
if [ "$sudowildcardimpr" ]; then
sudowildcardimprpwd=$(echo "$sudowildcardimpr" 2>/dev/null | grep "PASSWD")
if [ "$sudowildcardimprpwd" ]; then
sudowildcardimprpwd1=$(echo "$cmd" 2>/dev/null | grep "/bin/su" | grep " -c " | cut -d ":" -f 2 | grep "*$" | sed 's/ -c / -c \"/g' |sed 's/*$/dump;id\"/g' | sed 's/*/dump/g')
echo -e "${BOLD}${GREEN}[+] Wildcard with su detected ${RESET}${BLUE}(No password required!): ${RESET}"
echo -e "$sudowildcardimpr \n"
echo -e "Exploit: sudo$sudowildcardimprpwd1"
else
sudowildcardimprpwd2=$(echo "$cmd" 2>/dev/null | grep "/bin/su" | grep " -c " | grep "*$" | sed 's/ -c / -c \"/g' | sed 's/*$/dump;id\"/g' | sed 's/*/dump/g')
echo -e "${BOLD}${GREEN}[+] Wildcard with su detected (Password required!): ${RESET}"
echo -e "$sudowildcardimpr \n"
echo -e "Exploit: sudo$sudowildcardimprpwd2"
fi
fi
}
fn_sinject() {
#### Sudo Injection
sudoinj=$(cat /proc/sys/kernel/yama/ptrace_scope | grep 0 2>/dev/null)
if [ "$sudoinj" ]; then
echo -e "${BOLD}${GREEN}[+] Ptrace is set to zero: ${RESET}"
echo -e "[-] All processes can be debugged, as long as they have same uid"
echo -e "[-] It is possible to inject process that have valid sudo token and activate our own sudo token."
echo -e "[*] Notes: refer to: https://github.com/nongiach/sudo_inject for more information"
echo -e "[*] Exploit: res/sudo_injec \n"
fi
}
fn_scache() {
sudocache=$(echo "$cmd" 2>/dev/null | grep " !tty_tickets")
if [ "$sudocache" ]; then
echo -e "${BOLD}${GREEN}[+] Checking whether sudo caching is possible: ${RESET}"
echo -e "[-] Potentially vulnerable to MITRE Attack TTP T1548.003"
echo -e "[*] Notes: notes/sudo_caching.txt"
echo -e "\n"
fi
}
fn_fileownhijack() {
##### Chown file reference trick (file owner hijacking)
#sudowildcardchown=$(echo "$cmd" 2>/dev/null | grep "(root) NOPASSWD:" | grep "*" | grep "chown")
sudowildcardchown=$(echo "$cmd" 2>/dev/null | grep "NOPASSWD:" | grep "*" | grep "*bin/chown\|chown ")
if [ "$sudowildcardchown" ]; then
echo -e "${BOLD}${GREEN}[+] Wildcard with chown was found in sudo's rules: ${RESET} "
echo -e "$sudowildcardchown"
echo -e "\n[-] ${BOLD}${RED}File owner hijacking possible.${RESET} "
echo -e "[*] Exploit: notes/file_owner_hijacking(chown).txt \n"
fi
##### tar file reference trick (file owner hijacking)
#sudowildcardtar=$(echo "$cmd" 2>/dev/null | grep "(root) NOPASSWD:" | grep "*" | grep "tar")
sudowildcardtar=$(echo "$cmd" 2>/dev/null | grep "NOPASSWD:" | grep "*" | grep "*bin/tar\|tar ")
if [ "$sudowildcardtar" ]; then
echo -e "${BOLD}${GREEN}[+] Wildcard with tar was found in sudo's rules: ${RESET}"
echo -e "$sudowildcardtar"
echo -e "\n[-] ${BOLD}${RED}File owner hijacking possible.${RESET} "
echo -e "[*] Exploit: notes/file_owner_hijacking(tar).txt \n"
fi
##### rsync file reference trick (file owner hijacking)
#sudowildcardrsync=$(echo "$cmd" 2>/dev/null | grep "(root) NOPASSWD:" | grep "*" | grep "rsync")
sudowildcardrsync=$(echo "$cmd" 2>/dev/null | grep "NOPASSWD:" | grep "*" | grep "*bin/rsync\|rsync ")
if [ "$sudowildcardtar" ]; then
echo -e "${BOLD}${GREEN} [+] Wildcard with rsync was found in sudo's rules: ${RESET}"
echo -e "$sudowildcardrsync"
echo -e "\n[-] ${BOLD}${RED}File owner hijacking possible.${RESET} "
echo -e "[*] Exploit: notes/file_owner_hijacking(rsync).txt \n"
fi
}
fn_filepermhijack() {
##### Chmod file reference trick(file permission hijacking)
#sudowildcardchmod=$(echo "$cmd" 2>/dev/null | grep "(root) NOPASSWD:" | grep "*" | grep "chmod")
sudowildcardchmod=$(echo "$cmd" 2>/dev/null | grep "NOPASSWD:" | grep "*" | grep "*bin/chmod\|chmod ")
if [ "$sudowildcardchmod" ]; then
echo -e "${BOLD}${GREEN} [+] Wildcard with chmod was found in the sudoers file: ${RESET}"
echo -e "$sudowildcardchmod"
echo -e "\n[-] ${BOLD}${RED}File permission hijacking possible.${RESET} "
echo -e "[*] Exploit: notes/file_permission_hijacking.txt \n"
fi
}
checkmisconfig() {
echo -e "${BOLD}${YELLOW}[4/21] ====== Checking for excessive privilege ====== ${RESET} \n"
fn_excess_priv
echo -e "${BOLD}${BLUE}\n[@] Check [4/21] Completed!${RESET} \n"
echo -e "${BOLD}${YELLOW}[5/21] ====== Checking for excessive privilege (a password required) ====== ${RESET} \n"
fn_excess_priv_pwd
echo -e "${BOLD}${BLUE}\n[@] Check [5/21] Completed!${RESET} \n"
echo -e "${BOLD}${YELLOW}[6/21] ====== Checking for Common Misconfiguration (User impersonation) ====== ${RESET} \n"
fn_impersonate
echo -e "${BOLD}${BLUE}\n[@] Check [6/21] Completed!${RESET} \n"
echo -e "${BOLD}${YELLOW}[7/21] ====== Checking for Common Misconfiguration (Change owner) ====== ${RESET} \n"
fn_chown
echo -e "${BOLD}${BLUE}\n[@] Check [7/21] Completed!${RESET} \n"
echo -e "${BOLD}${YELLOW}[8/21] ====== Checking for Common Misconfiguration (Wildcard) ====== ${RESET} \n"
fn_wildcard
echo -e "${BOLD}${BLUE}\n[@] Check [8/21] Completed!${RESET} \n"
echo -e "${BOLD}${YELLOW}[9/21] ====== Checking for Common Misconfiguration (Sudo Injection) ====== ${RESET} \n"
fn_sinject
echo -e "${BOLD}${BLUE}\n[@] Check [9/21] Completed!${RESET} \n"
echo -e "${BOLD}${YELLOW}[10/21] ====== Checking for Common Misconfiguration (Sudo Cache) ====== ${RESET} \n"
fn_scache
echo -e "${BOLD}${BLUE}\n[@] Check [10/21] Completed!${RESET} \n"
echo -e "${BOLD}${YELLOW}[11/21] ====== Checking for Common Misconfiguration (File Owner Hijacking) ====== ${RESET} \n"
fn_fileownhijack
echo -e "${BOLD}${BLUE}\n[@] Check [11/21] Completed!${RESET} \n"
echo -e "${BOLD}${YELLOW}[12/21] ====== Checking for Common Misconfiguration (File Permission Hijacking) ====== ${RESET} \n"
fn_filepermhijack
echo -e "${BOLD}${BLUE}\n[@] Check [12/21] Completed!${RESET} \n"
} # checkmisconfig
#------------------------------------------------------
fn_miss_scripts() {
# offline mode check
if [ "$import" ]; then
echo -e "${BOLD}${GREEN}[/] This check is excluded in the offline mode for now. ${RESET}"
else
:
current_user="$(whoami)"
groups >/tmp/groups.txt
# issue #10 > missing check on NOPAASWD
#sudo -S -l -k | grep "NOPASSWD" | sed 's/(root) //g' | sed 's/NOPASSWD: //g' | sed 's/,/\n/g' | sed -e 's/ *$//' | awk '$1=$1' | cut -d " " -f 1 | grep .sh > $vpath/script_list
#echo "$cmd" | grep "NOPASSWD" | sed 's/(root) //g' | sed 's/NOPASSWD: //g' | sed 's/,/\n/g' | sed -e 's/ *$//' | awk '$1=$1' | cut -d " " -f 1 | grep .sh > $vpath/script_list
echo "$cmd" | sed 's/(root) //g' | sed 's/NOPASSWD: //g' | sed 's/,/\n/g' | sed -e 's/ *$//' | awk '$1=$1' | cut -d " " -f 1 | grep .sh >$vpath/script_list
#echo -e "${BOLD}${GREEN}[+] The script/s found in sudoers can be found at: $vpath/script_list ${RESET}"
echo -e "${BOLD}${GREEN}[+] The script/s found in sudoers can be found at: $vpath/script_list ${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:"
#echo -e "\n --------------------------------------------------------------"
#cat $vpath/script_list | while read line
cat $vpath/script_list | while read line; do
#test
#echo $line
# 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 -e "\n"
echo -e "------------------------------------------------------------------"
echo -e "[++] Missing script found:"
echo $line
echo -e "\n"
echo -e ">>> Checking Directory User Ownership of the missing script"