如何将可疑的 SQL 注入攻击安全地存储在数据库中?

发布于 2024-10-31 14:00:04 字数 329 浏览 2 评论 0原文

我该如何将潜在的 SQL 注入攻击存储在数据库中?

首先假设我检测到潜在的攻击,并将有问题的攻击字符串放在变量中,并希望将其添加到包含可疑事件日志的表中。

我需要做什么才能安全地将这些字符串插入数据库以免产生错误?

我有一种感觉,这将是类似于 htmlspecialchars 和 mysql_real_escape_string 的东西......但我想把它扔在那里,看看其他人是否有任何想法!

一种想法是将攻击存储为编码的 base64 值,但这似乎有点 hackish...

仅供参考,我正在用 PHP 编写应用程序:)

任何回复将不胜感激!

How would I go about storing potential SQL injection attacks in a database?

Assume first that I have detected a potential attack, and have the offending attack string in a variable and would like to add it to a table containing a log of suspicious events.

What would I need to do to safely insert these strings into a database so that no errors would be produced?

I have a feeling that it will be something along the lines of htmlspecialchars and mysql_real_escape_string... but I wanted to throw it out there to see if anybody else had any ideas!

One thought was to store the attack as an encoded base64 value, but that seems a bit hackish...

FYI, I am writing the application in PHP :)

Any responses would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

╭⌒浅淡时光〆 2024-11-07 14:00:04

始终使用参数化查询。如果您使用参数,则不需要依赖转义字符串,并且您的查询将始终完全按照您的意图进行。

例如:

$statement = $db->prepare('INSERT INTO table_name (field_name1, field_name2) VALUES (:value, :value2)');
$statement->execute(array(':value' => $value, ':value2' => $value2));

请参阅此处的 PDO 准备文档:

http://www.php.net/手册/en/pdo.prepare.php

Always use parameterized queries. If you are using parameters, you don't need to rely on escaping strings and your query will always do exactly what you intend.

e.g.:

$statement = $db->prepare('INSERT INTO table_name (field_name1, field_name2) VALUES (:value, :value2)');
$statement->execute(array(':value' => $value, ':value2' => $value2));

See documentation for PDO prepare here:

http://www.php.net/manual/en/pdo.prepare.php

心头的小情儿 2024-11-07 14:00:04

使用mysqli准备好的语句来存储查询,这是避免sql注入最安全的方法。如果您打算通过 Web 界面显示它们并担心 XSS/CSRF 攻击,请在显示它们之前使用 htmlspecialchars()

Use mysqli prepared statements to store the queries, it's the safest method to avoid sql injection. If you're going to display them via a web interface and concerned about XSS/CSRF attacks, use htmlspecialchars() before displaying them.

空名 2024-11-07 14:00:04

与存储任何其他数据的方式相同。
无论你如何称呼它,存储 SQL 注入攻击并没有什么特别的。

The same way you are storing any other data.
There is nothing special in storing SQL injection attacks, whatever you call it.

贪了杯 2024-11-07 14:00:04

就像 Steve Mayne 所说...请使用 php PDO 连接和准备好的语句。现在是保险箱。不要再使用 mysql_connect() 和子函数,因为它已经过时了,并且您无法充分受益于新的 mysql / sql / 等 .. 函数。

Like Steve Mayne said ... please use php PDO connection with prepared statements. It's the safes right now . Don't user mysql_connect() and subfunctions anymore because it's old and you cannot fully benefit of new mysql / sql / etc .. funcitons .

朕就是辣么酷 2024-11-07 14:00:04

我要做的就是通过简单的加密来运行它。
然后当你想显示可疑的sql时,你只需解密它即可。
这应该确保可疑的 sql 语句不会在您的数据库上执行。

All I would do is run it though a simple encryption.
Then when you want to show the suspected sql, you would just decrypt it.
This should insure the suspected sql statement does not get executed on your db.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文