是否有某种方法可以注入 SQL,即使' 角色被删除?

发布于 2024-07-06 08:52:26 字数 81 浏览 8 评论 0 原文

如果我从 SQL 查询中删除所有 ' 字符,是否还有其他方法对数据库进行 SQL 注入攻击?

如何做呢? 谁能给我举个例子吗?

If I remove all the ' characters from a SQL query, is there some other way to do a SQL injection attack on the database?

How can it be done? Can anyone give me examples?

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

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

发布评论

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

评论(14

半夏半凉 2024-07-13 08:52:27

我只能重复别人说过的话。 参数化 SQL 是正确的选择。 当然,编码起来有点痛苦——但是一旦你完成了一次,那么剪切和粘贴该代码并进行所需的修改并不困难。 我们有很多 .Net 应用程序,允许网站访问者指定整个范围的搜索条件,并且代码会动态构建 SQL Select 语句 - 但用户可能输入的所有内容都会进入参数。

I can only repeat what others have said. Parametrized SQL is the way to go. Sure, it is a bit of a pain in the butt coding it - but once you have done it once, then it isn't difficult to cut and paste that code, and making the modifications you need. We have a lot of .Net applications that allow web site visitors specify a whole range of search criteria, and the code builds the SQL Select statement on the fly - but everything that could have been entered by a user goes into a parameter.

浮世清欢 2024-07-13 08:52:27

当您需要数字参数时,您应该始终验证输入以确保它是数字。 除了帮助防止注入之外,验证步骤还将使应用程序更加用户友好。

如果您在预期 id = 1044 时收到 id = "hello",那么最好向用户返回有用的错误,而不是让数据库返回错误。

When you are expecting a numeric parameter, you should always be validating the input to make sure it's numeric. Beyond helping to protect against injection, the validation step will make the app more user friendly.

If you ever receive id = "hello" when you expected id = 1044, it's always better to return a useful error to the user instead of letting the database return an error.

执笔绘流年 2024-07-13 08:52:26

就在这里。 摘自 维基百科

“SELECT * FROM data WHERE id = " + a_variable + "; "

从这个声明中可以清楚地看出,作者希望 a_variable 是与“id”字段相关的数字。 但是,如果它实际上是一个字符串,那么最终用户可以根据自己的选择操作该语句,从而绕过转义字符的需要。 例如,将 a_variable 设置为

1;DROP TABLE users

将从数据库中删除(删除)“users”表,因为 SQL 将呈现如下:

SELECT * FROM DATA WHERE id =1;DROP TABLE users;

SQL 注入不是一种简单的攻击。 如果我是你,我会非常仔细地研究。

Yes, there is. An excerpt from Wikipedia

"SELECT * FROM data WHERE id = " + a_variable + ";"

It is clear from this statement that the author intended a_variable to be a number correlating to the "id" field. However, if it is in fact a string then the end user may manipulate the statement as they choose, thereby bypassing the need for escape characters. For example, setting a_variable to

1;DROP TABLE users

will drop (delete) the "users" table from the database, since the SQL would be rendered as follows:

SELECT * FROM DATA WHERE id=1;DROP TABLE users;

SQL injection is not a simple attack to fight. I would do very careful research if I were you.

守望孤独 2024-07-13 08:52:26

是的,取决于您使用的语句。 您最好通过使用存储过程或至少参数化查询来保护自己。

有关预防示例,请参阅维基百科

Yes, depending on the statement you are using. You are better off protecting yourself either by using Stored Procedures, or at least parameterised queries.

See Wikipedia for prevention samples.

丿*梦醉红颜 2024-07-13 08:52:26

我建议您将变量作为参数传递,而不是构建自己的 SQL。 否则总会有一种方法可以进行 SQL 注入,而这种方式是我们目前不知道的。

您创建的代码类似于:

' Not Tested
var sql = "SELECT * FROM data WHERE id = @id";
var cmd = new SqlCommand(sql, myConnection);
cmd.Parameters.AddWithValue("@id", request.getParameter("id"));

如果您的名字像我的一样,其中包含 '。 非常烦人的是,所有 '- 字符都被删除或标记为无效。

您可能还想查看有关 SQL 注入的 Stackoverflow 问题

I suggest you pass the variables as parameters, and not build your own SQL. Otherwise there will allways be a way to do a SQL injection, in manners that we currently are unaware off.

The code you create is then something like:

' Not Tested
var sql = "SELECT * FROM data WHERE id = @id";
var cmd = new SqlCommand(sql, myConnection);
cmd.Parameters.AddWithValue("@id", request.getParameter("id"));

If you have a name like mine with an ' in it. It is very annoying that all '-characters are removed or marked as invalid.

You also might want to look at this Stackoverflow question about SQL Injections.

无边思念无边月 2024-07-13 08:52:26

是的,这绝对是可能的。

如果您希望在下一个 SELECT 语句中使用整数,那么您可以输入类似的内容:

SELECT * FROM thingy WHERE attributeID=

  • 5 (好答案,没问题)
  • 5; 删除表用户; (糟糕、糟糕、糟糕...)

以下网站详细介绍了经典 SQL 注入技术: SQL 注入备忘单

使用参数化查询或存储过程也好不到哪儿去。 这些只是使用传递的参数预先进行的查询,它们也可以作为注入源。 此页面上也对此进行了描述:攻击 SQL 中的存储过程

现在,如果您抑制简单的引用,则只能阻止一组给定的攻击。 但不是全部。

一如既往,不要相信来自外部的数据。 在这 3 个级别上过滤它们:

  • 界面级别用于明显的内容(下拉选择列表比自由文本字段更好)
  • 逻辑级别用于与数据性质(整数、字符串、长度)、权限(这种类型的数据可以是该用户在此页面使用)...
  • 数据库访问级别(转义简单引用...)。

玩得开心,别忘了查看维基百科来获取答案。

Yes, it is definitely possible.

If you have a form where you expect an integer to make your next SELECT statement, then you can enter anything similar:

SELECT * FROM thingy WHERE attributeID=

  • 5 (good answer, no problem)
  • 5; DROP table users; (bad, bad, bad...)

The following website details further classical SQL injection technics: SQL Injection cheat sheet.

Using parametrized queries or stored procedures is not any better. These are just pre-made queries using the passed parameters, which can be source of injection just as well. It is also described on this page: Attacking Stored Procedures in SQL.

Now, if you supress the simple quote, you prevent only a given set of attack. But not all of them.

As always, do not trust data coming from the outside. Filter them at these 3 levels:

  • Interface level for obvious stuff (a drop down select list is better than a free text field)
  • Logical level for checks related to data nature (int, string, length), permissions (can this type of data be used by this user at this page)...
  • Database access level (escape simple quote...).

Have fun and don't forget to check Wikipedia for answers.

娇女薄笑 2024-07-13 08:52:26

参数化内联 SQL 或参数化存储过程是保护自己的最佳方法。 正如其他人指出的那样,简单地剥离/转义单引号字符是不够的。

您会注意到我专门谈论“参数化”存储过程。 如果您恢复将过程传递的参数连接在一起,那么仅仅使用存储过程也是不够的。 换句话说,将完全相同的易受攻击的 SQL 语句包装在存储过程中并不会使其更安全。 您需要在存储过程中使用参数,就像使用内联 SQL 一样。

Parameterized inline SQL or parameterized stored procedures is the best way to protect yourself. As others have pointed out, simply stripping/escaping the single quote character is not enough.

You will notice that I specifically talk about "parameterized" stored procedures. Simply using a stored procedure is not enough either if you revert to concatenating the procedure's passed parameters together. In other words, wrapping the exact same vulnerable SQL statement in a stored procedure does not make it any safer. You need to use parameters in your stored procedure just like you would with inline SQL.

屋顶上的小猫咪 2024-07-13 08:52:26

另外,即使您只是查找撇号,您也不想删除它。 你想逃避它。 您可以通过用两个撇号替换每个撇号来做到这一点。

但参数化查询/存储过程要好得多。

Also- even if you do just look for the apostrophe, you don't want to remove it. You want to escape it. You do that by replacing every apostrophe with two apostrophes.

But parameterized queries/stored procedures are so much better.

妳是的陽光 2024-07-13 08:52:26

由于这是一个相对较旧的问题,我不会费心写一个完整而全面的答案,因为该答案的大部分方面已被一个或另一个海报在这里提到。
然而,我确实发现有必要提出另一个这里没有人涉及的问题——SQL 走私。 在某些情况下,即使您尝试删除引号字符 ',也有可能将其“走私”到您的查询中。 事实上,即使您使用了正确的命令、参数、存储过程等,这也是可能的。

查看完整的研究论文 http://www.comsecglobal.com/FrameWork/Upload/SQL_Smuggling.pdf(披露,我是这方面的主要研究人员)或者只是谷歌“SQL Smuggling”。

Since this a relatively older question, I wont bother writing up a complete and comprehensive answer, since most aspects of that answer have been mentioned here by one poster or another.
I do find it necessary, however, to bring up another issue that was not touched on by anyone here - SQL Smuggling. In certain situations, it is possible to "smuggle" the quote character ' into your query even if you tried to remove it. In fact, this may be possible even if you used proper commands, parameters, Stored Procedures, etc.

Check out the full research paper at http://www.comsecglobal.com/FrameWork/Upload/SQL_Smuggling.pdf (disclosure, I was the primary researcher on this) or just google "SQL Smuggling".

你对谁都笑 2024-07-13 08:52:26

。 。 。 呃,大约 50000000 种其他方式

可能类似于 5; 删除表员工; --

生成的 sql 可能类似于:
从 number = 5 的地方选择 *; 删除表员工; -- 和 Sadfsf

-- 开始评论)

. . . uh about 50000000 other ways

maybe somthing like 5; drop table employees; --

resulting sql may be something like:
select * from somewhere where number = 5; drop table employees; -- and sadfsf

(-- starts a comment)

千纸鹤 2024-07-13 08:52:26

是的,绝对的:根据您的 SQL 方言等,有很多方法可以实现不使用撇号的注入。

针对 SQL 注入攻击的唯一可靠防御是使用数据库接口提供的参数化 SQL 语句支持。

Yes, absolutely: depending on your SQL dialect and such, there are many ways to achieve injection that do not use the apostrophe.

The only reliable defense against SQL injection attacks is using the parameterized SQL statement support offered by your database interface.

云醉月微眠 2024-07-13 08:52:26

我不会尝试找出要过滤掉的字符,而是坚持使用参数化查询,并完全消除问题。

Rather that trying to figure out which characters to filter out, I'd stick to parametrized queries instead, and remove the problem entirely.

淡写薰衣草的香 2024-07-13 08:52:26

这取决于您如何组合查询,但本质上是的。

例如,在 Java 中,如果您要这样做(故意的令人震惊的示例):

 String query = "SELECT name_ from Customer WHERE ID = " + request.getParameter("id");

那么您很可能会遭受注入攻击。

Java 有一些有用的工具来防止这些问题,例如PreparedStatements(您可以在其中传递“SELECT name_ from Customer WHERE ID = ?”之类的字符串,并且 JDBC 层会在为您替换 ? 标记时处理转义),但也有一些其他语言对此没有太大帮助。

It depends on how you put together the query, but in essence yes.

For example, in Java if you were to do this (deliberately egregious example):

 String query = "SELECT name_ from Customer WHERE ID = " + request.getParameter("id");

then there's a good chance you are opening yourself up to an injection attack.

Java has some useful tools to protect against these, such as PreparedStatements (where you pass in a string like "SELECT name_ from Customer WHERE ID = ?" and the JDBC layer handles escapes while replacing the ? tokens for you), but some other languages are not so helpful for this.

冷血 2024-07-13 08:52:26

问题是撇号可能是真正的输入,当你在代码中使用内联 SQL 时,你必须通过将它们加倍来转义它们。 您正在寻找的是一个正则表达式模式,例如:

\;.*--\

用于提前结束真实语句的分号,一些注入的 SQL,后跟双连字符以注释掉原始真实语句中的尾部 SQL。 攻击中的连字符可以省略。

因此答案是:不,仅仅删除撇号并不能保证您免受 SQL 注入的影响。

Thing is apostrophe's maybe genuine input and you have to escape them by doubling them up when you are using inline SQL in your code. What you are looking for is a regex pattern like:

\;.*--\

A semi colon used to prematurely end the genuine statement, some injected SQL followed by a double hyphen to comment out the trailing SQL from the original genuine statement. The hyphens may be omitted in the attack.

Therefore the answer is: No, simply removing apostrophes does not gaurantee you safety from SQL Injection.

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