-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
test_qgsserver_security.py
429 lines (340 loc) · 17.5 KB
/
test_qgsserver_security.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
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
# -*- coding: utf-8 -*-
"""QGIS Unit tests for server security.
.. note:: 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 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Paul Blottiere'
__date__ = '31/01/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
from qgis.utils import spatialite_connect
import os
# Needed on Qt 5 so that the serialization of XML is consistent among all executions
os.environ['QT_HASH_SEED'] = '1'
import time
import urllib.parse
from shutil import copyfile
from qgis.core import QgsApplication
from qgis.server import QgsServer
from qgis.testing import unittest
from utilities import unitTestDataPath
from test_qgsserver import QgsServerTestBase
class TestQgsServerSecurity(QgsServerTestBase):
@classmethod
def setUpClass(cls):
cls.testdatapath = unitTestDataPath('qgis_server_security') + '/'
cls.db = os.path.join(cls.testdatapath, 'db.sqlite')
cls.db_clone = os.path.join(cls.testdatapath, 'db_clone.sqlite')
cls.project = os.path.join(cls.testdatapath, 'project.qgs')
cls.app = QgsApplication([], False)
@classmethod
def tearDownClass(cls):
try:
os.remove(cls.db_clone)
except OSError:
pass
def setUp(self):
self.server = QgsServer()
copyfile(self.db, self.db_clone)
def test_wms_getfeatureinfo_filter_and_based_blind(self):
"""
And-based blind attack to check the kind of database currently used (if
the result is valid for the point named 'b', then sqlite_version()
function exist).
But does not work because of the allowlist.
If you remove the safety check, this is a valid injection.
"""
filter_sql = "point:\"name\" = 'b'"
injection_sql = ") and (select sqlite_version()"
query = "{0} {1}".format(filter_sql, injection_sql)
d, h = self.handle_request_wms_getfeatureinfo(query)
self.assertFalse(b"name = 'b'" in d)
def test_wms_getfeatureinfo_filter_time_based_blind(self):
"""
Time-based blind to check the current version of database. If the
server is too long to respond, then we have the answer!
But it does not work because of the allowlist.
If you remove the safety check, this is a valid injection.
"""
# first step, retrieve the version of sqlite by a regular way
conn = spatialite_connect(self.db_clone)
cur = conn.cursor()
sql = "select sqlite_version()"
sqlite_version = ''
for row in cur.execute(sql):
sqlite_version = row[0]
conn.close()
# second step, check the time of response for an invalid version
filter_sql = "point:\"name\" = 'b'"
injection_sql = ") and (select case sqlite_version() when '0.0.0' then substr(upper(hex(randomblob(99999999))),0,1) end)--"
query = "{0} {1}".format(filter_sql, injection_sql)
start = time.time()
d, h = self.handle_request_wms_getfeatureinfo(query)
duration_invalid_version = time.time() - start
# third step, check the time of response for a valid version
# maximum: several seconds
injection_sql = ") and (select case sqlite_version() when '{0}' then substr(upper(hex(randomblob(99999999))),0,1) end)--".format(
sqlite_version)
query = "{0} {1}".format(filter_sql, injection_sql)
start = time.time()
d, h = self.handle_request_wms_getfeatureinfo(query)
duration_valid_version = time.time() - start
# compare duration. On my computer when safety check is deactivated:
# duration_invalid_version: 0.012360334396362305
# duration_valid_version: 2.8810460567474365
self.assertAlmostEqual(duration_valid_version, duration_invalid_version, delta=0.5)
def test_wms_getfeatureinfo_filter_stacked(self):
"""
The aim is to execute some staked queries. Here the 'drop' function is
used but it could be done with create, insert, ...
But the filter string is split thanks to the semicolon so it seems
totally ignored whatever the query is (even without the safety check).
"""
filter_sql = "point:\"name\" = 'fake'"
injection_sql = "); drop table point"
query = "{0} {1}".format(filter_sql, injection_sql)
d, h = self.handle_request_wms_getfeatureinfo(query)
self.assertTrue(self.is_point_table_still_exist())
def test_wms_getfeatureinfo_filter_union_0(self):
"""
The aim is to retrieve name of tables within the database (like
'SpatialIndex').
But the allowlist blocks this request because of invalid tokens.
If you remove the safety check, this is a valid injection.
"""
filter_sql = "point:\"name\" = 'fake'"
injection_sql = ") union select 1,1,name,1,1 from sqlite_master where type = \"table\" order by name--"
query = "{0} {1}".format(filter_sql, injection_sql)
d, h = self.handle_request_wms_getfeatureinfo(query)
self.assertFalse(b'SpatialIndex' in d)
def test_wms_getfeatureinfo_filter_union_1(self):
"""
The aim is to retrieve data from an excluded layer.
But the allowlist blocks this request because of invalid tokens.
If you remove the safety check, this is a valid injection.
"""
filter_sql = "point:\"name\" = 'fake'"
injection_sql = ") union select 1,1,* from aoi--"
query = "{0} {1}".format(filter_sql, injection_sql)
d, h = self.handle_request_wms_getfeatureinfo(query)
self.assertFalse(b'private_value' in d)
def test_wms_getfeatureinfo_filter_unicode(self):
"""
The aim is to send some invalid token in unicode to bypass the
allowlist.
But unicode is interpreted and checked by the safety function.
"""
# %3B -> semicolon
filter_sql = "point:\"name\" = 'fake %3B'"
d, h = self.handle_request_wms_getfeatureinfo(filter_sql)
self.assertTrue(self.check_service_exception_report(d))
def test_wms_getfeatureinfo_filter_patternmatching(self):
"""
The aim is to retrieve the table's name thanks to pattern matching.
If you remove the safety check, this is a valid injection.
"""
filter_sql = "point:\"name\" = 'b'"
injection_sql = "or ( select name from sqlite_master where type='table' and name like '{0}') != ''"
query = "{0} {1}".format(filter_sql, injection_sql)
# there's no table named as 'az%'
name = "az%"
sql = query.format(name)
d, h = self.handle_request_wms_getfeatureinfo(sql)
# self.assertTrue(b"name = 'b'" in d) #true if sanity check deactivated
self.assertTrue(self.check_service_exception_report(d))
# a table named as 'ao%' exist
name = "ao%"
sql = query.format(name)
d, h = self.handle_request_wms_getfeatureinfo(sql)
# self.assertTrue(b"name = 'a'" in d) #true if sanity check deactivated
self.assertTrue(self.check_service_exception_report(d))
# a table named as 'aoi' exist
name = "aoi"
sql = query.format(name)
d, h = self.handle_request_wms_getfeatureinfo(sql)
# self.assertTrue(b"name = 'a'" in d) #true if sanity check deactivated
self.assertTrue(self.check_service_exception_report(d))
def test_wms_getfeatureinfo_filter_allowlist(self):
"""
The aim is to check that some tokens cannot pass the safety check
whatever their positions in the filter string.
"""
# create
filter_sql = "point:\"name\" = 'a'create"
d, h = self.handle_request_wms_getfeatureinfo(filter_sql)
self.assertTrue(self.check_service_exception_report(d))
filter_sql = "point:\"name\" = 'a' create"
d, h = self.handle_request_wms_getfeatureinfo(filter_sql)
self.assertTrue(self.check_service_exception_report(d))
# invalid token and escape single quote
filter_sql = "point:\"name\" = 'a\\'create"
d, h = self.handle_request_wms_getfeatureinfo(filter_sql)
self.assertTrue(self.check_service_exception_report(d))
# drop
filter_sql = "point:\"name\" = 'a' drop"
d, h = self.handle_request_wms_getfeatureinfo(filter_sql)
self.assertTrue(self.check_service_exception_report(d))
# select
filter_sql = "point:\"name\" = 'a' select"
d, h = self.handle_request_wms_getfeatureinfo(filter_sql)
self.assertTrue(self.check_service_exception_report(d))
# comments
# filter_sql = "point:\"name\" = 'a' #"
# d, h = self.handle_request_wms_getfeatureinfo(filter_sql)
# self.assertTrue(self.check_service_exception_report(d))
filter_sql = "point:\"name\" = 'a' -"
d, h = self.handle_request_wms_getfeatureinfo(filter_sql)
self.assertTrue(self.check_service_exception_report(d))
def test_wfs_getfeature_filter_stacked(self):
"""
The aim is to execute some staked queries within the 'Literal'
and 'PropertyName' field. Here the 'drop' function is used but it
could be done with create, insert, ...
But due to the implementation, these filters are not resolved on
database side but in server side with QgsExpression. So, there's no
'WHERE' clause and the query never reach the database. By the way,
it's exactly the same thing whatever the kind of attacks and for
the EXP_FILTER parameter too (filter described with QgsExpression).
It's typically the kind of SQL injection which has been fixed in
mapserver several years ago:
https://trac.osgeo.org/mapserver/ticket/3874
"""
# ogc:Literal / ogc:PropertyIsEqualTo
literal = "4')); drop table point --"
filter_xml = "<ogc:Filter%20xmlns:ogc=\"http://www.opengis.net/ogc\"><ogc:PropertyIsEqualTo><ogc:PropertyName>pkuid</ogc:PropertyName><ogc:Literal>{0}</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>".format(
literal)
self.handle_request_wfs_getfeature_filter(filter_xml)
self.assertTrue(self.is_point_table_still_exist())
# ogc:Literal / ogc:PropertyIsLike
literal = "4')); drop table point --"
filter_xml = "<ogc:Filter%20xmlns:ogc=\"http://www.opengis.net/ogc\"><ogc:PropertyIsLike><ogc:PropertyName>pkuid</ogc:PropertyName><ogc:Literal>{0}</ogc:Literal></ogc:PropertyIsLike></ogc:Filter>".format(
literal)
self.handle_request_wfs_getfeature_filter(filter_xml)
self.assertTrue(self.is_point_table_still_exist())
# ogc:PropertyName / ogc:PropertyIsLike
propname = "name = 'a')); drop table point --"
filter_xml = "<ogc:Filter%20xmlns:ogc=\"http://www.opengis.net/ogc\"><ogc:PropertyIsLike><ogc:PropertyName>{0}</ogc:PropertyName><ogc:Literal>4</ogc:Literal></ogc:PropertyIsLike></ogc:Filter>".format(
propname)
self.handle_request_wfs_getfeature_filter(filter_xml)
self.assertTrue(self.is_point_table_still_exist())
def test_wms_getmap_filter_stacked(self):
"""
The aim is to execute some staked queries within the 'Literal'
and 'PropertyName' field. Here the 'drop' function is used but it
could be done with create, insert, ...
But due to the implementation, these filters quoted before being sent to the DB.
It's typically the kind of SQL injection which has been fixed in
mapserver several years ago:
https://trac.osgeo.org/mapserver/ticket/3874
"""
# ogc:Literal / ogc:PropertyIsEqualTo
literal = "4')); drop table point --"
filter_xml = "<ogc:Filter%20xmlns:ogc=\"http://www.opengis.net/ogc\"><ogc:PropertyIsEqualTo><ogc:PropertyName>pkuid</ogc:PropertyName><ogc:Literal>{0}</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>".format(
literal)
self.handle_request_wms_getmap(filter=filter_xml)
self.assertTrue(self.is_point_table_still_exist())
# ogc:Literal / ogc:PropertyIsLike
literal = "4')); drop table point --"
filter_xml = "<ogc:Filter%20xmlns:ogc=\"http://www.opengis.net/ogc\"><ogc:PropertyIsLike><ogc:PropertyName>pkuid</ogc:PropertyName><ogc:Literal>{0}</ogc:Literal></ogc:PropertyIsLike></ogc:Filter>".format(
literal)
self.handle_request_wms_getmap(filter=filter_xml)
self.assertTrue(self.is_point_table_still_exist())
# ogc:PropertyName / ogc:PropertyIsLike
propname = "name = 'a')); drop table point --"
filter_xml = "<ogc:Filter%20xmlns:ogc=\"http://www.opengis.net/ogc\"><ogc:PropertyIsLike><ogc:PropertyName>{0}</ogc:PropertyName><ogc:Literal>4</ogc:Literal></ogc:PropertyIsLike></ogc:Filter>".format(
propname)
self.handle_request_wms_getmap(filter=filter_xml)
self.assertTrue(self.is_point_table_still_exist())
def test_wms_getmap_sld_stacked(self):
"""
The aim is to execute some staked queries within the 'Literal'
and 'PropertyName' field. Here the 'drop' function is used but it
could be done with create, insert, ...
However it's not working because special characters are duplicated. For
example, with 'Literal' as "4')); drop table point --", the underlying
query is:
SELECT .... AND (("pkuid" = '4'')); drop table point --'))
"""
literal = "4')); drop table point --"
sld = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><StyledLayerDescriptor xmlns=\"http://www.opengis.net/sld\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ogc=\"http://www.opengis.net/ogc\" xsi:schemaLocation=\"http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd\" version=\"1.1.0\" xmlns:se=\"http://www.opengis.net/se\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"> <NamedLayer> <se:Name>point</se:Name> <UserStyle> <se:Name>point</se:Name> <se:FeatureTypeStyle> <se:Rule> <se:Name>Single symbol</se:Name> <ogc:Filter xmlns:ogc=\"http://www.opengis.net/ogc\"> <ogc:PropertyIsEqualTo> <ogc:PropertyName>pkuid</ogc:PropertyName> <ogc:Literal>{0}</ogc:Literal> </ogc:PropertyIsEqualTo> </ogc:Filter> <se:PointSymbolizer> <se:Graphic> <se:Mark> <se:WellKnownName>circle</se:WellKnownName> <se:Fill><se:SvgParameter name=\"fill\">5e86a1</se:SvgParameter></se:Fill><se:Stroke><se:SvgParameter name=\"stroke\">000000</se:SvgParameter></se:Stroke></se:Mark><se:Size>7</se:Size></se:Graphic></se:PointSymbolizer></se:Rule></se:FeatureTypeStyle></UserStyle></NamedLayer></StyledLayerDescriptor>".format(
literal)
self.handle_request_wms_getmap(sld=sld)
self.assertTrue(self.is_point_table_still_exist())
def check_service_exception_report(self, d):
"""
Return True if a ServiceExceptionReport is raised, False otherwise
"""
if b'<ServiceExceptionReport' in d:
return True
else:
return False
def handle_request_wfs_getfeature_filter(self, filter_xml):
qs = "?" + "&".join(["%s=%s" % i for i in {
"MAP": urllib.parse.quote(self.project),
"SERVICE": "WFS",
"VERSION": "1.1.1",
"REQUEST": "GetFeature",
"TYPENAME": "point",
"STYLES": "",
"CRS": "EPSG:32613",
"FILTER": filter_xml}.items()])
return self._execute_request(qs)
def handle_request_wms_getfeatureinfo(self, filter_sql):
qs = "?" + "&".join(["%s=%s" % i for i in {
"MAP": urllib.parse.quote(self.project),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetFeatureInfo",
"QUERY_LAYERS": "point",
"LAYERS": "point",
"STYLES": "",
"FORMAT": "image/png",
"HEIGHT": "500",
"WIDTH": "500",
"BBOX": "606171,4822867,612834,4827375",
"CRS": "EPSG:32613",
"FILTER": filter_sql}.items()])
return self._result(self._execute_request(qs))
def handle_request_wms_getmap(self, sld=None, filter=None):
params = {
"MAP": urllib.parse.quote(self.project),
"SERVICE": "WMS",
"VERSION": "1.0.0",
"REQUEST": "GetMap",
"QUERY_LAYERS": "point",
"LAYERS": "point",
"STYLES": "",
"FORMAT": "image/png",
"HEIGHT": "500",
"WIDTH": "500",
"BBOX": "606171,4822867,612834,4827375",
"CRS": "EPSG:32613"
}
if sld is not None:
params["SLD"] = sld
if filter is not None:
params["FILTER"] = urllib.parse.quote(filter)
qs = "?" + "&".join(["%s=%s" % i for i in params.items()])
return self._result(self._execute_request(qs))
def is_point_table_still_exist(self):
conn = spatialite_connect(self.db_clone)
cur = conn.cursor()
sql = "select * from point"
point_table_exist = True
try:
cur.execute(sql)
except:
point_table_exist = False
conn.close()
return point_table_exist
def _result(self, data):
headers = {}
for line in data[0].decode('UTF-8').split("\n"):
if line != "":
header = line.split(":")
self.assertEqual(len(header), 2, line)
headers[str(header[0])] = str(header[1]).strip()
return data[1], headers
if __name__ == '__main__':
unittest.main()