从数据库转义用户输入需要?

编程入门 行业动态 更新时间:2024-10-22 14:36:40
本文介绍了从数据库转义用户输入需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我知道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不会在其输出中执行任何转义。

长回答: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); }

解决方案

Does MySQL automatically escape their output or something like that, or should I escape in the second query as well?

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.

更多推荐

从数据库转义用户输入需要?

本文发布于:2023-10-25 08:21:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526437.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!