问题描述
所以我知道MySQL注入,并且在将其放入我的数据库之前总是将所有的用户输入转义。但是我想知道,想像一个用户试图提交一个注入的查询,我逃避它。如果我稍后从数据库中获取此值,并在查询中使用该值,该怎么办?所以($ code> sql :: escape()包含我的转义函数)
$ userinput ='); DROP`table` - ;
mysql_query(INSERT INTO`table`
(`foo`,`bar`)
VALUES
('foobar','。sql :: escape($ userinput) 。 '));
//插入php / mysql以将`table`.`bar`输入$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ `,`bar`)
VALUES
('foobar','$ output。'));
MySQL会自动转义其输出或类似的东西,还是应该在第二个查询中转义?
这是一个测试用例,但这种情况在我的程序中以其他方式发生,我想知道这样的情况下安全性有多紧张。 >
编辑
我的转义功能
static function escape($ string){
if(get_magic_quotes_gpc())
$ string = stripslashes($ string);
返回mysql_real_escape_string($ string);
}
您还需要在第二个查询中转义。 MySQL不会在其输出中执行任何转义。
长回答:MySQL字符串转义不会修改正在插入的字符串,它只是确保它不在当前查询中不会有任何伤害。任何SQL注入尝试仍然保留在数据中。
So I know about MySQL injection and always escape all my user input before putting it in my database. However I was wondering, imagine a user tries to submit a query to inject, and I escape it. What if I then at a later moment take this value from the database, and use it in a query. Do I have to escape it again?
So: (sql::escape()
contains my escape function)
$userinput = "'); DROP `table` --";
mysql_query("INSERT INTO `table`
(`foo`,`bar`)
VALUES
('foobar','".sql::escape($userinput)."')");
// insert php/mysql to fetch `table`.`bar` into $output here
mysql_query("INSERT INTO `table2`
(`foo`,`bar`)
VALUES
('foobar','".$output."')");
Does MySQL automatically escape their output or something like that, or should I escape in the second query as well?
This is a testcase but this occurs in some other ways within my program and I'm wondering how tight the security has to be for cases like this.
EDIT
My escape function
static function escape($string){
if(get_magic_quotes_gpc())
$string = stripslashes($string);
return mysql_real_escape_string($string);
}
You need to escape in the second query as well. MySQL does not do any escaping on its output.
Long answer: MySQL string escaping does not modify the string that is being inserted, it just makes sure it doesn't do any harm in the current query. Any SQL injection attempt still remains in the data.
这篇关于从数据库转义用户输入需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!