sql 注入/浏览器劫持者预防 php
我有一个网站,无法使用 html_entities() 或 html_specialchars() 来处理用户输入数据。相反,我添加了一个自定义函数,它最终是一个函数,它使用数组 $forbidden 来清除输入字符串中所有不需要的字符。目前,由于 sql 注入/浏览器劫持,我将 '<'、'>'、"'" 作为不需要的字符。我的网站采用 utf-8 编码 - 我是否必须向该数组添加更多字符,即用其他字符集编码的字符“<”?
感谢您的帮助,
梅尼
I have a website where I can't use html_entities() or html_specialchars() to process user input data. Instead, I added a custom function, which in the end is a function, which uses an array $forbidden to clean the input string of all unwanted characters. At the moment I have '<', '>', "'" as unwanted characters because of sql-injection/browser hijacking. My site is encoded in utf-8 - do I have to add more characters to that array, i.e. the characters '<', encoded in other charsets?
Thanks for any help,
Maenny
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您也应该转义
"
。它比'
危害更大,因为您经常将 HTML 属性括在"
中。但是,为什么不简单地使用htmlspecialchars
来完成这项工作呢?此外:对 SQL 和 HTML 使用同一个转义函数并不好。 HTML 需要转义标签,而 SQL 不需要。因此,如果您使用
htmlspecialchars
进行 HTML 输出,使用PDO::quote
(或mysql_real_escape_string
或您正在使用的任何内容)进行 SQL 输出,那就最好了查询。但我知道(根据我自己的经验),转义 SQL 查询中的所有用户输入可能真的很烦人,有时我只是不转义部分,因为我认为它们是“安全的”。但我确信我的假设并不总是正确的。因此,最后我想确保我确实转义了 SQL 查询中使用的所有变量,因此编写了一个小类来轻松完成此操作: com/nikic/DB" rel="nofollow noreferrer">http://github.com/nikic/DB 也许您也想使用类似的东西。
You should escape
"
, too. It is much more harm than'
, because you often enclose HTML attributes in"
. But, why don't you simlpy usehtmlspecialchars
to do that job?Futhermore: It isn't good to use one escaping function for both SQL and HTML. HTML needs escaping of tags, whereas SQL does not. So it would be best, if you used
htmlspecialchars
for HTML output andPDO::quote
(ormysql_real_escape_string
or whatever you are using) for SQL queries.But I know (from my own experience) that escaping all user input in SQL queries may be really annoying and sometimes I simply don't escape parts, because I think they are "secure". But I am sure I'm not always right, about assuming that. So, in the end I wanted to ensure that I really escape all variables used in an SQL query and therefore have written a little class to do this easily: http://github.com/nikic/DB Maybe you want to use something similar, too.
将此代码放入您的标题页中。它可以防止PHP中的SQL注入攻击。
函数 clean_header($string)
{
$字符串=修剪($字符串);
// 来自 RFC 822:“字段主体可以由任何 ASCII 组成
// 字符,除了 CR 或 LF。”
if (strpos($string, “\n“) !== false) {
$string = substr($string, 0, strpos($string, “\n”));
}
if (strpos($string, “\r“) !== false) {
$string = substr($string, 0, strpos($string, “\r“));
返回
$字符串;
}
Put this code into your header page. It can prevent SQL injection attack in PHP.
function clean_header($string)
{
$string = trim($string);
// From RFC 822: “The field-body may be composed of any ASCII
// characters, except CR or LF.”
if (strpos($string, “\n“) !== false) {
$string = substr($string, 0, strpos($string, “\n“));
}
if (strpos($string, “\r“) !== false) {
$string = substr($string, 0, strpos($string, “\r“));
}
return $string;
}