网页上,通过某个用户输入数据,以获取其相关数据的行为,实质上是网页调用后台SQL语句(关系型数据库)的过程,难免会遇到这样的情况
理想的情况是这样的:
mysql> desc eg;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| name | char(10) | YES | | NULL | |
| scores | int(10) | YES | | NULL | |
+--------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> select * from eg where id=1;
+----+------+--------+
| id | name | scores |
+----+------+--------+
| 1 | a | 20 |
+----+------+--------+
1 row in set (0.00 sec)
但是,很可能出现这样的情况
mysql> select * from eg where id=1 or 'a' = 'a';
+----+------+--------+
| id | name | scores |
+----+------+--------+
| 1 | a | 20 |
| 2 | b | 30 |
| 3 | c | 50 |
| 4 | d | 70 |
+----+------+--------+
4 rows in set (0.00 sec)
上面这种情况就是网站注入。
我采用的办法是创建procedure来解决这个问题
mysql> create procedure so(a_id INT(10))
-> begin
-> select * from eg where id = a_id;
-> end //
再次执行查询语句
mysql> call sos(1 or 'a'='a');
+----+------+--------+
| id | name | scores |
+----+------+--------+
| 1 | a | 20 |
+----+------+--------+
1 row in set (0.00 sec)
就可以解决网站注入问题啦。
这是我自己想到的方法,可能会有很多纰漏,希望大家能够指正