MYSQL注入和md5加密
如果我用 md5 加密 MYSQL 注入,它还会执行吗?如果我在执行“mysql_real_escape_string”之前加密 MYSQL 注入,它是否能够使 mysql 注入无效?我应该在加密之前运行“mysql_real_escape_string”吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您已使用 MD5 对值进行哈希处理,则它不再包含任何可恶意注入的字符。 MySQL 不会解码 MD5 哈希并将其解释/执行为 SQL。事实上,MD5 哈希无法解码,因为它是一种单向算法。
If you have hashed a value with MD5, it no longer contains any maliciously injectable characters. MySQL will not decode the MD5 hash and interpret/execute it as SQL. In fact, the MD5 hash cannot be decoded, as it is a one-way algorithm.
MD5 哈希值是二进制数据,在 php 中以十六进制表示。这意味着,它们仅包含数字 0..9 和字母 A..F。
使用此字母不可能进行 SQL 注入,因此您是安全的。
MD5 hashed values are binary data which are represented hexadecimal in php. This means, they only contain the numbers 0..9 and the letters A..F.
SQL injections are not possible with this letters, so you are safe.
MD5 哈希值本身不会导致 SQL 注入,但如果您试图避免 SQL 注入,那么您的方法是引起 SQL 注入的一个非常好的方法。
您对一类不能导致注入的变量(MD5 密码)进行特殊处理,然后对另一类变量进行特殊处理(例如评估为整数的事物),然后您忘记哪些应该转义,哪些不应该转义,最终会得到一个应该转义的微小的无关紧要的变量,但没有转义,而且您的团队中没有人发现它......哎呀,您的代码中存在真正的漏洞。
你避免转义一个安全类,你避免转义第二个安全类,然后你错误地认为第三个类也是安全的——事实证明,MD5 采用二进制编码,有人设法找到一种方法来创建一个注入它...哎呀...您的代码中还有一个真正的漏洞。
您应该:
1) 始终使用参数化查询。这使得逃避变得不必要,并避免了忘记逃避某些事情的风险。使用
mysqli
和bind_param< /code>
:
2) 如果由于某种原因你不能,就逃避一切。即使没有必要。你不想忘记什么。
对抗 SQL 注入的规则是:不要试图了解什么是安全的、什么是不安全的,假设一切都是不安全的,并将一切都视为不安全。如果你将一个安全值视为不安全,程序仍然可以正常运行,如果你将一个不安全值视为安全,你的程序将包含一个巨大的安全漏洞,一个错误就足够了。
MD5 hashed values themselves can't cause an SQL injection, but if you're trying to avoid an SQL injection your approach is a very good way to cause one.
You special case one class of variables that can't cause an injection (MD5'd passwords), then you special case another (e.g. things evaluated to integers), then you forget which should be escaped and which not, you end up with one little tiny insignificant variable that should be escaped but isn't and nobody from your team spots it.... Oops, there's a real vulnerability in your code.
You avoid escaping one safe class, you avoid escaping a second safe class, and then you you incorrectly assume that a third class is also safe – it turns out, the MD5 was in a binary encoding and somebody managed to find a way to create an injection with it... Oops... You have one more real vulnerability in your code.
You should:
1) Always use parametrized queries. This makes escaping unnecessary and avoids the risk of forgetting to escape something. Use
mysqli
andbind_param
:2) If for some reason you can't, escape everything. Even if it is unnecessary. You don't want to forget something.
Rule to fight against SQL injections is: Don't try to learn what is safe and what isn't, assume everything is unsafe, and treat everything as unsafe. If you treat a safe value as unsafe, the program will still function correctly, if you treat an unsafe value as safe your program will contain a huge security hole, and one mistake is enough.
虽然一般来说 md5 哈希值不能用于注入攻击,但值得指出的是,原始 md5 哈希值可以被利用。原始 md5 哈希值是使用以下命令生成的哈希值
md5($userSubscribedValue, true)
- 它们很容易受到攻击,因为输出是一个可以包含非十六进制输出的字符串。虽然以原始形式生成哈希值并不常见,但请务必注意从技术上讲,哈希值可以在注入攻击中被利用。以下是如何完成此操作的示例:
http://cvk.posterous .com/sql-injection-with-raw-md5-hashes
不过,正如其他人指出的那样,十六进制 md5 输出不易受到注入攻击。
While in general md5 hashed values cannot be used in injection attacks, it's worth pointing out that raw md5 hashes values can be exploited. Raw md5 hashes are hashes generated with
md5($userSubmittedValue, true)
-- they're vulnerable because the output is a string that can contain non-hexadecimal output.. While it's not common to generate hashes in raw form, it's important to note that technically hashed values can be exploited in injection attacks.Here's an example of how it can be done:
http://cvk.posterous.com/sql-injection-with-raw-md5-hashes
As other have pointed out, though, hexadecimal md5 output is not vulnerable to injection attacks.
md5()
函数 返回一个字符串包含 ASCII 字符 0-9 和 af。 SQL 注入需要使用'
或"
等字符,因此考虑到该算法有效,由 md5() 生成的散列字符串永远不会导致 SQL 注入 因此,您可以放心地编写这样的内容:
但最好始终使用 mysql_real_escape_string()。
The
md5()
function returns a string containing the ASCII characters 0-9 and a-f. An SQL injection requires using characters like'
or"
, thus a hashed string that was generated by md5() won't cause an SQL injection EVER, considering that the algorithm works expectedly.Thus you can write something like this without worry:
But it's a good practice to always escape the data that you pass to your query with mysql_real_escape_string().