Skip to content
Navigation Menu
{{ message }}
forked from rapid7/hackazon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.php
More file actions
533 lines (476 loc) · 18.2 KB
/
Copy pathQuery.php
File metadata and controls
533 lines (476 loc) · 18.2 KB
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
<?php
namespace PHPixie\DB\PDOV;
use PHPixie\DB;
use VulnModule\DataType\ArrayObject;
use VulnModule\Vulnerability\XSS;
use VulnModule\VulnerableField;
/**
* PDO implementation of the database Query
* @property Connection $_db
* @package Database
*/
class Query extends DB\Query
{
/**
* Type of the database, e.g. mysql, pgsql etc.
* @var string
*/
protected $_db_type;
/**
* Character to use for quoting fields
* @var string
*/
protected $_quote;
/**
* Settings for the current query
* @var string
*/
protected $_settings;
/**
* Maps alias names to table names
* @var array
*/
protected $_aliases = [];
/**
* @var bool
*/
protected $isRoot = true;
/**
* @var null|Query
*/
protected $parent = null;
/**
* @var \SplObjectStorage
*/
protected $vulnFields;
/**
* Creates a new query object, checks which driver we are using and set the character used for quoting
*
* @param \PHPixie\DB $db Database connection
* @param string $type Query type. Available types: select, update, insert, delete, count
* @return \PHPixie\DB\PDOV\Query
@see \PHPixie\DB\Query::__construct()
*/
public function __construct($db, $type) {
parent::__construct($db, $type);
$this->_db_type = $this->_db->db_type;
$this->_quote = $this->_db_type == 'mysql' ? '`' : '"';
$this->vulnFields = new \SplObjectStorage();
$this->methods['limit'] = array('integer', 'NULL', 'string');
$this->methods['offset'] = array('integer', 'NULL', 'string');
}
/**
* Set/get settings for the execute query.
*
* @param string $val String to be enclosed in quotes
* @return string String surrounded with quotes
*/
public function settings($val = null) {
if ($val != null) {
$this->_settings = $val;
}
return $this->_settings;
}
/**
* Puts quotes around a string
*
* @param string $str String to be enclosed in quotes
* @return string String surrounded with quotes
*/
protected function quote($str) {
return $this->_quote . $str . $this->_quote;
}
/**
* If a string is passed escapes a field by enclosing it in specified quotes.
* If you pass an \PHPixie\DB\Expression object the value will be inserted into the query unescaped
*
* @param mixed $field Field to be escaped or an \PHPixie\DB\Expression object
* if the field must not be escaped
* @param bool $prepend_table
* @return string Escaped field representation
* @see \PHPixie\DB\Expression
*/
public function escape_field($field, $prepend_table = true) {
if (is_object($field) && $field instanceof DB\Expression) {
return $field->value;
}
$field = explode('.', $field);
if (count($field) == 1) {
if(!$prepend_table)
return $this->quote($field[0]);
array_unshift($field, $this->last_alias());
}
$str = $this->quote($field[0]) . '.';
if (trim($field[1]) == '*') {
return $str . '*';
}
return $str . $this->quote($field[1]);
}
/**
* Replaces the value with ? and appends it to the parameters array
* If you pass an \PHPixie\DB\Expression object the value will be inserted into the query unescaped
*
* @param mixed $val Value to be escaped or an \PHPixie\DB\Expression object
* if the value must not be escaped
* @param array &$params Reference to parameters array
* @return string Escaped value representation
*/
public function escape_value($val, &$params/*, $columnName = null*/) {
if ($val instanceof DB\Expression) {
return $val->value;
}
if ($val instanceof DB\Query) {
return $this->subquery($val, $params);
}
if ($val instanceof VulnerableField) {
$this->vulnFields->attach($val);
if (!($params['vuln_fields'] instanceof ArrayObject)) {
$params['vuln_fields'] = new ArrayObject(is_array($params['vuln_fields']) ? $params['vuln_fields'] : []);
}
if (!$params['vuln_fields']->contains($val)) {
$params['vuln_fields'][] = $val;
}
$filteredVal = $val->getFilteredValue();
if ($val->isVulnerableTo('SQL')) {
return "'" . $filteredVal . "'";
} else {
$params[] = $filteredVal;
return '?';
}
}
$params[] = $val;
return '?';
}
/**
* Gets the SQL for a subquery and appends its parameters to current ones
*
* @param Query $query Query builder for the subquery
* @param array &$params Reference to parameters array
* @return string Subquery SQL
*/
protected function subquery($query, &$params) {
$query->setIsRoot(false);
$query->setParent($this);
$query = $query->query();
$params = array_merge($params, $query[1]);
return "({$query[0]}) ";
}
/**
* Gets the SQL for a table to select from
*
* @param string|DB\Expression|DB\Query|array $table Table representation
* @param array &$params Reference to parameters array
* @throws \Exception
* @internal param $string &alias Alias for this table
* @return string Table SQL
*/
public function escape_table($table, &$params) {
$alias = null;
if (is_array($table)) {
$alias = $table[1];
$table = $table[0];
}
$this->getRootQuery()->addUsedAlias($alias);
if (is_string($table)) {
$table = $this->quote($table);
if ($alias != null)
$table.= " AS {$alias}";
return $table;
}
if ($alias == null) {
$alias = $this->last_alias();
$this->getRootQuery()->addUsedAlias($alias);
}
if ($table instanceof DB\Query)
return "{$this->subquery($table, $params)} AS {$alias}";
if ($table instanceof DB\Expression)
return "({$table->value}) AS {$alias}";
throw new \Exception("Parameter type " . get_class($table) . " cannot be used as a table");
}
/**
* Builds a query and fills the $params array with parameter values
*
* @throws \Exception
* @return array An array with a prepared query string and an array of parameters
*/
public function query() {
$query = '';
$params = array();
if (!$this->parent) {
$this->_aliases = [];
}
if ($this->_type == 'insert') {
$query .= "INSERT INTO {$this->escape_table($this->_table, $params)} ";
if (empty($this->_data) && $this->_db_type == 'pgsql') {
$query.= "DEFAULT VALUES ";
} else {
if (isset($this->_data[0]) && is_array($this->_data[0])) { // Batch insert
$first_row = true;
$columns_array = array();
foreach ($this->_data as $row) {
$columns = '';
$values = '';
$first = true;
if ($first_row) {
foreach ($row as $key => $val) {
if (!$first) {
$values .= ', ';
$columns .= ', ';
} else {
$first = false;
}
$columns .= $this->quote($key);
$columns_array[] = $key;
$values .= $this->escape_value($val, $params, $key);
}
$query .= "({$columns}) VALUES({$values})";
} else {
foreach ($columns_array as $col) {
if (!$first) {
$values .= ', ';
} else {
$first = false;
}
$values .= $this->escape_value($row[$col], $params, $col);
}
$query .= ", ({$values})";
}
$first_row = false;
}
} else {
$columns = '';
$values = '';
$first = true;
foreach ($this->_data as $key => $val) {
if (!$first) {
$values .= ', ';
$columns .= ', ';
} else {
$first = false;
}
$columns .= $this->quote($key);
$values .= $this->escape_value($val, $params, $key);
}
$query .= "({$columns}) VALUES({$values})";
}
}
} else {
if ($this->_type == 'select') {
$query .= "SELECT ";
if ($this->_fields == null) {
$query .= "* ";
} else {
$first = true;
foreach ($this->_fields as $f) {
if (!$first) {
$query .= ", ";
} else {
$first = false;
}
if (is_array($f)) {
$query .= "{$this->escape_field($f[0])} AS {$f[1]} ";
} else {
$query .= "{$this->escape_field($f)} ";
}
}
}
if (!empty($this->_table))
$query .= "FROM {$this->escape_table($this->_table, $params)} ";
}
if ($this->_type == 'count') {
$query .= "SELECT COUNT(*) as {$this->quote('count')} FROM {$this->escape_table($this->_table, $params)} ";
}
if ($this->_type == 'delete') {
if ($this->_db_type != 'sqlite') {
$query .= "DELETE {$this->last_alias()}.* FROM {$this->escape_table($this->_table, $params)} ";
} else {
if (!empty($this->_joins)) {
throw new \Exception("SQLite doesn't support deleting a table with JOIN in the query");
}
$query .= "DELETE FROM {$this->escape_table($this->_table, $params)} ";
}
}
if ($this->_type == 'update') {
$query .= "UPDATE {$this->escape_table($this->_table, $params)} ";
}
$joinedTables = [];
foreach ($this->_joins as $join) {
$table = $join[0];
$table = $this->escape_table($table, $params);
if (in_array($table, $joinedTables)) {
continue;
}
$query .= strtoupper($join[1]) . " JOIN {$table} ";
if (!empty($join[2]))
$query.="ON {$this->get_condition_query($join[2], $params, true, true)} ";
$joinedTables[] = $table;
}
if ($this->_type == 'update') {
$query.= "SET ";
$first = true;
foreach ($this->_data as $key => $val) {
if (!$first) {
$query.=", ";
} else {
$first = false;
}
$query .= "{$this->quote($key)} = {$this->escape_value($val, $params, $key)}";
}
$query .= " ";
}
if (!empty($this->_conditions)) {
$query .= "WHERE {$this->get_condition_query($this->_conditions, $params, true)} ";
}
if (($this->_type == 'select' || $this->_type == 'count') && $this->_group_by != null) {
$query .= "GROUP BY {$this->escape_field($this->_group_by)} ";
}
if (($this->_type == 'select' || $this->_type == 'count') && !empty($this->_having)) {
$query .= "HAVING {$this->get_condition_query($this->_having, $params, true)} ";
}
if ($this->_type == 'select' && !empty($this->_order_by)) {
$query .= "ORDER BY ";
$first = true;
foreach ($this->_order_by as $order) {
if (!$first) {
$query .= ',';
} else {
$first = false;
}
$query .= $this->escape_field($order[0]) . " ";
if (isset($order[1])) {
$dir = strtoupper($order[1]);
$query .= $dir . " ";
}
}
}
if (count($this->_union) > 0 && ($this->_type == 'select')) {
$query = "({$query}) ";
foreach ($this->_union as $union) {
$query .= $union[1] ? "UNION ALL " : "UNION ";
if ($union[0] instanceof DB\Query) {
$query .= $this->subquery($union[0], $params);
} elseif ($union[0] instanceof DB\Expression) {
$query .= "({$union[0]->value}) ";
} else {
throw new \Exception("You can only use query builder instances or \$pixie->db->expr() for unions");
}
}
}
if ($this->_type != 'count') {
if ($this->_limit != null) {
$query .= "LIMIT {$this->_limit} ";
}
if ($this->_offset != null) {
$query .= "OFFSET {$this->_offset} ";
}
}
}
return array($query, $params);
}
/**
* Recursively parses conditions array into a query string
*
* @param array $p Element of the cobditions array
* @param array &$params Reference to parameters array
* @param boolean $skip_first_operator Flag to skip the first logical operator in a query
* to prevent AND or OR to be at the beginning of the query
* @param boolean $value_is_field Flag if the the value in the logical operations should
* be treated as a field. E.g. for joins where the fields are
* compared between themselves and not with actual values
* @return string String representation of the conditions
* @throws \Exception If condition cannot be parsed
*/
public function get_condition_query($p, &$params, $skip_first_operator, $value_is_field = false) {
if (isset($p['field'])) {
if ($value_is_field) {
$param = $this->escape_field($p['value']);
} else {
$param = $this->escape_value($p['value'], $params, $p['field']);
}
return $this->escape_field($p['field']) . ' ' . $p['operator'] . ' ' . $param;
}
if (isset($p['logic'])) {
return ($skip_first_operator ? '' : strtoupper($p['logic']) . ' ')
. $this->get_condition_query($p['conditions'], $params, false, $value_is_field);
}
$conds = '';
$skip = $skip_first_operator || (count($p) > 1);
foreach ($p as $q) {
$conds .= $this->get_condition_query($q, $params, $skip, $value_is_field) . ' ';
$skip = false;
}
if (count($p) > 1 && !$skip_first_operator) {
return "( " . $conds . ")";
}
return $conds;
//throw new \Exception("Cannot parse condition:\n" . var_export($p, true));
}
/**
* @return \VulnModule\VulnInjection\Service
*/
public function getVulnService()
{
return $this->_db->pixie->getVulnService();
}
/**
* @param boolean $isRoot
*/
public function setIsRoot($isRoot)
{
$this->isRoot = !!$isRoot;
}
/**
* @return null|Query
*/
public function getRootQuery()
{
return $this->parent ? $this->parent->getRootQuery() : $this;
}
/**
* @return null
*/
public function getParent()
{
return $this->parent;
}
/**
* @param null $parent
*/
public function setParent($parent)
{
$this->parent = $parent;
}
/**
* @return array
*/
public function getUsedAliases()
{
return $this->_aliases;
}
public function addUsedAlias($alias)
{
if (!$alias) {
return;
}
if (!in_array($alias, $this->_aliases)) {
$this->_aliases[] = $alias;
}
}
public function where()
{
$params = func_get_args();
if (func_num_args() == 3) {
if (is_null($params[2])) {
if ($params[1] == '=') {
$params[1] = 'IS';
$params[2] = new DB\Expression('NULL');
} else if (in_array($params[1], ['<', '>', '<=', '>='])) {
$params[2] = 0;
}
} else if ($params[2] === 0) {
$params[2] = '0';
}
}
return call_user_func_array('parent::where', $params);
}
}
You can’t perform that action at this time.
