-
Notifications
You must be signed in to change notification settings - Fork 31
/
update.py
353 lines (332 loc) · 20 KB
/
update.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Typo3 Enumerator - Automatic Typo3 Enumeration Tool
# Copyright (c) 2014-2022 Jan Rude
#
# 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/](http://www.gnu.org/licenses/)
#-------------------------------------------------------------------------------
import os.path
from pkg_resources import parse_version
import xml.etree.ElementTree as ElementTree
import re, os, sys, gzip, urllib.request, sqlite3, requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
database = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'typo3scan.db')
conn = sqlite3.connect(database)
c = conn.cursor()
class Update:
"""
This class updates the extension and vulnerability database
It will download the extension file from the official repository,
unpack it and insert the extensions in the database.
Vulnerabilities will be parsed from the official homepage.
"""
def __init__(self):
self.load_core_vulns()
self.download_ext()
self.load_extensions()
self.load_extension_vulns()
def load_core_vulns(self):
"""
Grep the CORE vulnerabilities from the security advisory website
Search for advisories and maximum pages
Request every advisory and get:
Advisory Title
Vulnerability Type
Subcomponent(s)
Affected Versions
CVE Numbers
"""
print('\n[+] Searching for new CORE vulnerabilities...')
update_counter = 0
next_page = 2
last_page = 99
cHash = ''
for current_page in range(1, last_page+1):
if current_page == 1:
url = 'https://typo3.org/help/security-advisories/typo3-cms/'
else:
url = 'https://typo3.org/help/security-advisories/typo3-cms/page?tx_news_pi1%5BcurrentPage%5D={}&tx_sfeventmgt_pieventlist%5Baction%5D=list&tx_sfeventmgt_pieventlist%5Bcontroller%5D=Event&cHash={}'.format(current_page, cHash)
response = requests.get(url, timeout=6)
content = re.findall('<a class=\"page-link\" href=\"/help/security-advisories/typo3-cms/page\?tx_news_pi1%5BcurrentPage%5D=([0-9]+)&tx_sfeventmgt_pieventlist%5Baction%5D=list&tx_sfeventmgt_pieventlist%5Bcontroller%5D=Event&cHash=([0-9a-f]+)\"', response.text)
last_page = (content[-1])[0]
cHash = (content[0])[1]
print(' \u251c Page {}/{}'.format(current_page, last_page))
advisories = re.findall('TYPO3-CORE-SA-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]', response.text)
for advisory in advisories:
vulnerabilities = []
affected_version_max = '0.0.0'
affected_version_min = '0.0.0'
html = requests.get('https://typo3.org/security/advisory/{}'.format(advisory.lower()))
beauty_html = html.text
beauty_html = beauty_html[beauty_html.index('Component Type'):]
beauty_html = beauty_html[:beauty_html.index('General ')]
beauty_html = beauty_html.replace('\xa0', ' ')
beauty_html = beauty_html.replace('</strong>', '')
beauty_html = beauty_html.replace(' ', ' ')
beauty_html = beauty_html.replace('&', '&')
# set as global versions
advisory_items = {}
subcomponents = re.findall('([sS]ubcomponent\s?#?[0-9]?:\s?(.*?))<', beauty_html)
# if no subcomponent / CORE vuln
if len(subcomponents) == 0:
missed = re.search('Component Type:\s?(.*?)<', beauty_html).group(1)
advisory_items[missed] = []
advisory_items[missed].append(beauty_html)
subcomponents.reverse()
try:
for subcomponent in subcomponents:
index = beauty_html.rfind(subcomponent[0])
item_text = subcomponent[1]
if item_text in advisory_items:
item_text = item_text + ' (2)'
advisory_items[item_text] = []
advisory_items[item_text].append(beauty_html[index:])
beauty_html = beauty_html[:index]
for subcomponent, entry in advisory_items.items():
vulnerability_items = {}
vulnerability_type = re.findall('(Vulnerability Type:\s?(.*?)<)', entry[0])
vulnerability_type.reverse()
for type_entry in vulnerability_type:
index = entry[0].rfind(type_entry[0])
vulnerability_items[type_entry[1]] = []
vulnerability_items[type_entry[1]].append(entry[0][index:])
entry[0] = entry[0][:index]
for vuln_type, vuln_description in vulnerability_items.items():
cve = re.search(':\s?(CVE-.*?)(<|\"|\()', vuln_description[0])
if cve:
cve = cve.group(1)
else:
cve = 'None assigned'
search_affected = re.search('Affected Version[s]?:\s?(.+?)<', vuln_description[0])
if search_affected:
affected_versions = search_affected.group(1)
else:
affected_versions = re.search('Affected Version[s]?:\s?(.+?)<', beauty_html).group(1)
# separate versions
affected_versions = affected_versions.replace("and below", " - 0.0.0")
affected_versions = affected_versions.replace(";", ",")
affected_versions = affected_versions.replace(' and', ',')
versions = affected_versions.split(', ')
for version in versions:
version = re.findall('([0-9]+\.[0-9x]+\.?[0-9x]?[0-9x]?)', version)
if len(version) == 0:
print("[!] Unknown version info! Skipping...")
print(" \u251c Advisory:", advisory)
print(" \u251c Subcomponent:", subcomponent)
print(" \u251c Vulnerability:", vuln_type)
print(" \u251c Versions:", affected_versions)
break
elif len(version) == 1:
version = version[0]
if len(version) == 3: # e.g. version 6.2
version = version + '.0'
affected_version_max = version
affected_version_min = version
else:
if parse_version(version[0]) >= parse_version(version[1]):
affected_version_max = version[0]
affected_version_min = version[1]
else:
affected_version_max = version[1]
affected_version_min = version[0]
# add vulnerability
vulnerabilities.append([advisory, vuln_type, subcomponent, affected_version_max, affected_version_min, cve])
except Exception as e:
print("Error on receiving data for https://typo3.org/security/security-advisory/{}".format(advisory))
print(e)
exit(-1)
# Add vulnerability details to database
for core_vuln in vulnerabilities:
c.execute('SELECT * FROM core_vulns WHERE advisory=? AND vulnerability=? AND subcomponent=? AND affected_version_max=? AND affected_version_min=? AND cve=?', (core_vuln[0], core_vuln[1], core_vuln[2], core_vuln[3], core_vuln[4], core_vuln[5],))
data = c.fetchall()
if not data:
update_counter+=1
c.execute('INSERT INTO core_vulns VALUES (?,?,?,?,?,?)', (core_vuln[0], core_vuln[1], core_vuln[2], core_vuln[3], core_vuln[4], core_vuln[5]))
conn.commit()
else:
if update_counter == 0:
print('[!] Already up-to-date.\n')
else:
print(' \u2514 Done. Added {} new advisories to database.\n'.format(update_counter))
return True
def dlProgress(self, count, blockSize, totalSize):
"""
Progressbar for extension download
"""
percent = int(count*blockSize*100/totalSize)
sys.stdout.write('\r \u251c Downloading ' + '%d%%' % percent)
sys.stdout.flush()
def download_ext(self):
"""
Download extensions from server and unpack the ZIP
"""
print('[+] Getting extension file...')
try:
# Maybe someday we need to use mirrors: https://repositories.typo3.org/mirrors.xml.gz
urllib.request.urlretrieve('https://typo3.org/fileadmin/ter/extensions.xml.gz', 'extensions.xml.gz', reporthook=self.dlProgress)
with gzip.open('extensions.xml.gz', 'rb') as infile:
with open('extensions.xml', 'wb') as outfile:
for line in infile:
outfile.write(line)
infile.close()
outfile.close()
except Exception as e:
print('\n', e)
def load_extensions(self):
"""
Parse the extension file and add extensions in database
"""
print('\n \u251c Parsing extension file...')
tree = ElementTree.parse('extensions.xml')
root = tree.getroot()
c.execute('''DROP TABLE IF EXISTS extensions''')
c.execute('''CREATE TABLE IF NOT EXISTS extensions (extensionkey text PRIMARY KEY, title text, description text, version text, state text)''')
# for every extension get:
# title, extensionkey, description, version, state
counter = 0
for extensions in root:
extensionkey = extensions.get('extensionkey').lower()
if (extensionkey == 'aaaaa'):
continue
title = extensions[1][0].text
description = extensions[1][1].text
version = '0.0.0'
state = extensions[1][2].text
# search for current version
for extension in extensions.iter('version'):
if not(extension.attrib['version'] == ''):
try:
temp_ver=((extension.attrib['version']).split('-')[0]).strip()
if parse_version(temp_ver) > parse_version(version):
title = (extension.find('title')).text
description = (extension.find('description')).text
version = extension.attrib['version']
if ((extension.find('ownerusername').text == 'abandoned_extensions') or (extension.find('state').text == 'n/a') or (extension.find('state').text == 'test')):
state = 'obsolete'
else:
state = (extension.find('state')).text
except ValueError:
pass
c.execute('INSERT OR REPLACE INTO extensions VALUES (?,?,?,?,?)', (extensionkey, title, description, version, state))
counter += 1
conn.commit()
os.remove('extensions.xml.gz')
os.remove('extensions.xml')
print(' \u2514 Done. Added {} extensions to database'.format(counter))
def load_extension_vulns(self):
"""
Grep the EXTENSION vulnerabilities from the security advisory website
Search for advisories and maximum pages
Request every advisory and get:
Advisory Title
Extension Name
Vulnerability Type
Affected Versions
"""
print('\n[+] Searching for new extension vulnerabilities...')
update_counter = 0
next_page = 2
last_page = 99
cHash = ''
for current_page in range(1, last_page+1):
if current_page == 1:
url = 'https://typo3.org/help/security-advisories/typo3-extensions/'
else:
url = 'https://typo3.org/help/security-advisories/typo3-extensions/page?tx_news_pi1%5BcurrentPage%5D={}&tx_sfeventmgt_pieventlist%5Baction%5D=list&tx_sfeventmgt_pieventlist%5Bcontroller%5D=Event&cHash={}'.format(current_page, cHash)
response = requests.get(url, timeout=6)
content = re.findall('<a class=\"page-link\" href=\"/help/security-advisories/typo3-extensions/page\?tx_news_pi1%5BcurrentPage%5D=([0-9]+)&tx_sfeventmgt_pieventlist%5Baction%5D=list&tx_sfeventmgt_pieventlist%5Bcontroller%5D=Event&cHash=([0-9a-f]+)\"', response.text)
last_page = (content[-1])[0]
cHash = (content[0])[1]
print(' \u251c Page {}/{}'.format(current_page, last_page))
advisories = re.findall('TYPO3-EXT-SA-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]', response.text)
for advisory in advisories:
vulnerabilities = []
affected_version_max = '0.0.0'
affected_version_min = '0.0.0'
# adding vulns with odd stuff on website
if advisory == 'TYPO3-EXT-SA-2014-018':
vulnerabilities.append(['TYPO3-EXT-SA-2014-018', 'phpmyadmin', 'Cross-Site Scripting, Denial of Service, Local File Inclusion', '4.18.4', '4.18.0'])
elif advisory == 'TYPO3-EXT-SA-2014-015':
vulnerabilities.append(['TYPO3-EXT-SA-2014-015', 'dce', 'Information Disclosure', '0.11.4', '0.0.0'])
elif advisory == 'TYPO3-EXT-SA-2014-013':
vulnerabilities.append(['TYPO3-EXT-SA-2014-013', 'cal', 'Denial of Service', '1.5.8', '0.0.0'])
vulnerabilities.append(['TYPO3-EXT-SA-2014-013', 'cal', 'Denial of Service', '1.6.0', '1.6.0'])
elif advisory == 'TYPO3-EXT-SA-2014-009':
vulnerabilities.append(['TYPO3-EXT-SA-2014-009', 'news', 'Cross-Site Scripting', '3.0.0', '3.0.0'])
vulnerabilities.append(['TYPO3-EXT-SA-2014-009', 'news', 'Cross-Site Scripting', '2.3.0', '2.0.0'])
else:
try:
html = requests.get('https://typo3.org/security/advisory/{}'.format(advisory.lower()))
beauty_html = html.text.replace('\xa0', ' ')
beauty_html = beauty_html.replace('</strong>', '')
beauty_html = beauty_html.replace(' ', ' ')
beauty_html = beauty_html.replace('&', '&')
advisory_info = re.search('<title>(.*)</title>', beauty_html).group(1)
vulnerability = re.findall('Vulnerability Type[s]?:\s?(.*?)<', beauty_html)
affected_versions = re.findall('Affected Version[s]?:\s?(.+?)<', beauty_html)
extensionkey = re.findall('Extension[s]?:\s?(.*?)<', beauty_html)
# Sometimes there are multiple extensions in an advisory
if len(extensionkey) == 0: # If only one extension affected
if not '(' in advisory_info:
extensionkey = [advisory_info[advisory_info.rfind(' ')+1:]]
else:
extensionkey = [advisory_info[advisory_info.find('('):]]
for item in range (0, len(extensionkey)):
extensionkey_item = extensionkey[item]
if '(' in extensionkey_item:
extensionkey_item = extensionkey_item[extensionkey_item.rfind('(')+1:extensionkey_item.rfind(')')]
description = vulnerability[item]
version_item = affected_versions[item]
version_item = version_item.replace("and all versions below", "- 0.0.0")
version_item = version_item.replace("and all version below", "- 0.0.0") # typo
version_item = version_item.replace("and alll versions below", "- 0.0.0") # typo
version_item = version_item.replace("and below of", "-")
version_item = version_item.replace("and below", "- 0.0.0")
version_item = version_item.replace(" ", " ")
version_item = version_item.replace(";", ",")
version_item = version_item.replace(' and', ',')
versions = version_item.split(', ')
for version in versions:
version = re.findall('([0-9]+\.[0-9x]+\.[0-9x]+)', version)
if len(version) == 1:
affected_version_max = version[0]
affected_version_min = version[0]
else:
if parse_version(version[0]) >= parse_version(version[1]):
affected_version_max = version[0]
affected_version_min = version[1]
else:
affected_version_max = version[1]
affected_version_min = version[0]
vulnerabilities.append([advisory, extensionkey_item, description, affected_version_max, affected_version_min])
except Exception as e:
print("Error on receiving data for https://typo3.org/security/advisory/{}".format(advisory))
print(e)
exit(-1)
# Add vulnerability details to database
for ext_vuln in vulnerabilities:
c.execute('SELECT * FROM extension_vulns WHERE advisory=? AND extensionkey=? AND vulnerability=? AND affected_version_max=? AND affected_version_min=?', (ext_vuln[0], ext_vuln[1], ext_vuln[2], ext_vuln[3], ext_vuln[4],))
data = c.fetchall()
if not data:
update_counter+=1
c.execute('INSERT INTO extension_vulns VALUES (?,?,?,?,?)', (ext_vuln[0], ext_vuln[1], ext_vuln[2], ext_vuln[3], ext_vuln[4]))
conn.commit()
else:
if update_counter == 0:
print('[!] Already up-to-date.\n')
else:
print(' \u2514 Done. Added {} new advisories to database.\n'.format(update_counter))
return True