即使变量被转义,SQL 注入也会发生
仅当我的查询如下例所示时,sql 注入才会起作用
SELECT * FROM login WHERE id = $my_id_va;
假设如果我的查询是
SELECT * FROM login WHERE id = $my_id_va ORDER BY id DESC
那么我将收到以下错误
#1064 - 您的 SQL 语法有错误;检查手册 与您的 MySQL 服务器版本相对应,以便使用正确的语法 第 1 行“order by id desc”附近
因此,这个 1 或 1=1; SHOW TABLES 不起作用,对吗?
我的网站连续多次被黑客攻击。
我想要一个快速答案:当我的查询如下所示时,他们可以使用哪些方式或哪些类型的查询来攻击我的网站?
SELECT * FROM login WHERE id = $my_id_va ORDER BY id DESC
执行 show table< 的方法有哪些/code> 在下面的查询中,
SELECT * FROM login WHERE id = $my_id_va ORDER BY id DESC
我还使用转义函数来处理查询字符串值,例如 mysql_real_escape_string($my_id_va) 。是的,显然这是针对单一相关的黑客攻击,但不确定。
添加了更多内容
SELECT EventActuallyCharged, EventDate FROM tblevent WHERE EventDate between '2011-07-21 or 1=1; SHOW TABLES --' and '2011-07-31' ORDER BY EventDate DESC
,但显示表格不起作用
The sql injection will work only when my query looks like below sample
SELECT * FROM login WHERE id = $my_id_va;
Assume if my query is
SELECT * FROM login WHERE id = $my_id_va ORDER BY id DESC
Than I will get following error
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'order by id desc' at line 1
So, this 1 or 1=1; SHOW TABLES
will not work, correct?
My site was hacked successively many times.
I want one quick answer: When my query looks like the following one, what ways or which types of query can they use to hack my site?
SELECT * FROM login WHERE id = $my_id_va ORDER BY id DESC
What are the ways to execute the show table
in the following query
SELECT * FROM login WHERE id = $my_id_va ORDER BY id DESC
I am also using escaping function to handle the query string values, like mysql_real_escape_string($my_id_va)
. Yes, obviously this for single related hack, but not sure.
Added some more
SELECT EventActuallyCharged, EventDate FROM tblevent WHERE EventDate between '2011-07-21 or 1=1; SHOW TABLES --' and '2011-07-31' ORDER BY EventDate DESC
but show table not worked
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
1. 第一个查询以某种方式安全
2. 第二个查询以某种方式安全
阅读有关
sprintf< 的文档/code>
如果你不明白。
然而,正如其他人所说,如果您将参数化查询与诸如 PDO 或 < MySQLi。
1. First query somehow secured
2. Second query somehow secured
Read the docs about
sprintf
if you don't understand it.However, as others have said, it would be very very secure if you would use parameterized queries with a class such as PDO or MySQLi.
如果您使用 PHP5,请使用参数化查询,使用 PDO。
If you are using PHP5, use parametarized query, use PDO.
Intcast
如果
id
是一个数字,你也可以对你的变量进行intcast。整数可以安全使用:mysql_real_escape_string
如果你想传递一个字符串,你可以而且应该使用
mysql_real_escape_string
,但是这个函数只转义字符串内的那些字符。您仍然需要在字符串周围添加引号,因此:.. 将导致查询:
select * from Table where id = hello
。这显然不是一个有效的查询,因为 hello 应该用引号引起来。将查询更改为:
.. 一切正常。您添加引号,
mysql_real_escape_string
会处理字符串内的特殊字符(如果有)。参数
另一种解决方案是使用参数化查询。这可以通过使用 MySQLi 或 PDO 来完成。优点是您只需告诉数据库应该在哪里插入变量,数据库会自行处理转义。
它还可能会增加性能优势,因为这些查询可以在没有参数的情况下进行缓存,从而更有效地利用查询缓存。不过,这在当前版本的 MySQL 中还不能真正起作用。
Int cast
If
id
is a number, you can int-cast your variable as well. Integers are safe to use:mysql_real_escape_string
If you want to pass a string, you can, and should, use
mysql_real_escape_string
, but this function escapes only those characters that are inside the string. You will still need to add quotes around the string, so:.. will result in the query:
select * from Table where id = hello
. This is obiously not a valid query, since hello should be in quotes.Change the query to:
.. and everything works fine. You add the quotes around, and
mysql_real_escape_string
takes care of special characters inside the string, if any.Parameters
Another solution is to use parameterized queries. This can by done using MySQLi or PDO. The advantage is that you only tell your database where a variable should be inserted, and the database takes care of the escaping yourself.
It also may add a performance benefit, because these queries could be cached without their parameters, make a more efficient use of the query cache. This doesn't really work yet in current versions of MySQL, though.
你是对的,
1 或 1=1; SHOW TABLES
将给出语法错误,但这将起作用:-- 注释掉查询的其余部分。
在您的情况下,该值是一个整数,因此您可以使用
intval
。You are right that
1 or 1=1; SHOW TABLES
will give a syntax error but this will work:The -- comments out the rest of the query.
In your case the value is an integer so instead of using
mysql_real_escape_string
you can useintval
.如果将
$my_id_va
设置为:1 或 1=1; SHOW TABLES --
--
将注释掉命令的其余部分,从而有效地终止它。我不确定 mysql_real_escape_string 会对查询产生什么影响。你应该做的是 参数化查询。
If you set
$my_id_va
to:1 or 1=1; SHOW TABLES --
The
--
will comment out the rest of the command, effectively terminating it.I'm not sure what effect
mysql_real_escape_string
will have on the query. What you should be doing is parameterized queries.