ColdFusion中防止SQL注入攻击的方法

6

我正试图提高我的应用程序的安全性。每当我从用户那里接收数据(无论是通过POST还是GET),这些数据应该是一个整数,我都会进行相应的验证。但经常出现数据是VARCHAR类型的情况,有时还带有HTML标签。

在这种情况下,我该如何保护我的数据库免受SQL注入攻击?

<cfqueryparam value="#form.textInput#" cfsqltype="cf_sql_varchar">是否可以保护查询免受在VARCHAR值中发送恶意SQL语句的攻击?

2个回答

6

简短的回答是肯定的。

cfqueryparam 将阻止某些 SQL 注入攻击。

还有其他攻击变量可以使用,因此请小心,但编写良好的 ColdFusion 可以非常安全。

如果您正在存储并稍后显示输入的 HTML,请注意跨站点脚本攻击,特别是 JavaScript 标签。


3
cfqueryparam 只有在数据库代码的性质下,才无法阻止SQL注入攻击,而与攻击的性质无关。例如,如果您调用某个需要varchar参数并将其动态运行为SQL的数据库过程,则无论查询参数化程度如何,都无法帮助您。但是,在使用纯SQL(无数据库过程调用等)时,CfQueryParam 总是可以防止SQL注入攻击。 - dom_watson
1
不要忘记在适当的情况下,对于可变长度文本字段也使用 maxlength。例如,如果您知道一个文本字段只会有最多16个字符长的字符串,那么就没有必要允许更长的字符串进入,因为它们应该被标记为错误。 - Simon at My School Portal

5

简单回答你的问题是“是的”。

我使用三种方法来阻止黑客攻击。

  1. I use cfqueryparam in all my database queries. I will use cfparam at the top of the template/cfm files for url scope variables.

  2. I have used Portcullis or variants of it. You can get it from http://portcullis.riaforge.org/. Portcullis will also defend against some cross site scripting attacks.

  3. I use Windows IIS 7.5 (Windows Server 2008 R2). I use the URL Rewrite feature to block the bulk of URL based attacks. You can do similar things with Apache and the rewrite that it supports. Here are my IIS URL Rewrite rules:

    <?xml version="1.0" encoding="UTF-8"?>
    <appcmd>
        <CONFIG CONFIG.SECTION="system.webServer/rewrite/globalRules" path="MACHINE/WEBROOT/APPHOST" overrideMode="Inherit" locked="false">
            <system.webServer-rewrite-globalRules>
                <rule name="SQL Injection - EXEC - SCRIPT_NAME" stopProcessing="true">
                    <match url="^.*EXEC\s*[\(|%28].*$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SQL Injection - EXEC - QS" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{QUERY_STRING}" pattern="^.*EXEC\s*[\(|%28].*$" />
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SQL Injection - CAST - SCRIPT_NAME" stopProcessing="true">
                    <match url="^.*CAST\s*[\(|%28].*$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SQL Injection - CAST - QS" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{QUERY_STRING}" pattern="^.*CAST\s*[\(|%28].*$" />
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SQL Injection - DECLARE - SCRIPT_NAME" stopProcessing="true">
                    <match url="^.*DECLARE.*$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SQL Injection - DECLARE - QS" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{QUERY_STRING}" pattern="^.*DECLARE.*$" />
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SQL Injection - NVARCHAR - SCRIPT_NAME" stopProcessing="true">
                    <match url="^.*CHAR\s*[\(|%28].*$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SQL Injection - NVARCHAR - QS" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{QUERY_STRING}" pattern="^.*CHAR\s*[\(|%28].*$" />
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SQL Injection - sp_password - SCRIPT_NAME" stopProcessing="true">
                    <match url="^.*sp_password.*$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SQL Injection - sp_password - QS" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{QUERY_STRING}" pattern="^.*sp_password.*$" />
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SQL Injection - xp - SCRIPT_NAME" stopProcessing="true">
                    <match url="^.*%20xp_.*$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SQL Injection - xp - QS" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{QUERY_STRING}" pattern="^.*%20xp_.*$" />
                    </conditions>
                    <serverVariables>
                    </serverVariables>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
            </system.webServer-rewrite-globalRules>
        </CONFIG>
    </appcmd>
    

这些规则将被添加到IIS的C:\Windows\System32\inetsrv\config\applicationHost.config文件中。然而,我强烈建议您不要直接编辑此文件,一旦出现错误,IIS将无法加载。相反地,请将上述规则复制并保存为“iis-global-rewrite.xml”。然后运行以下批处理文件以将规则添加到您的IIS服务器:

C:\Windows\System32\inetsrv\appcmd.exe set config -in < iis-global-rewrite.xml

IIS重写规则适用于IIS 7.0(Windows Server 2008),但我没有测试过。

如果您无法访问服务器,则可以使用web.config文件将这些规则应用于单个站点。

为什么要使用三种不同的保护方法?因为它们都不能涵盖所有方面。IIS重写规则只能保护免受基于URL的攻击。黑客也可以使用表单提交攻击来达到相同的效果。我更喜欢使用IIS规则作为第一道防线,因为它适用于服务器上的所有站点,包括PHP、ASP等。Portcullis是ColdFusion的良好第二防线,因为它可以捕获基于表单的攻击和一些跨站点脚本攻击。最后的防线是cfqueryparam/cfparam代码,可以保护免受基于URL/表单的SQL注入攻击。

如果同时使用这三种方法,服务器/站点应该非常安全。建议定期查看服务器日志,因为攻击会不断进化和改进。


1
哇,那真是太完美了。我实际上正在使用IIS上的CF,所以我一定会研究一些更高级的重写规则来保护我的Web应用程序。谢谢! - Eleeist
IIS URL Rewrite和Apache mod_rewrite是非常有用的防御和SEO工具。http://blogs.iis.net/ruslany/archive/2009/04/08/10-url-rewriting-tips-and-tricks.aspx提供了一些IIS URL Rewrite的有用示例。 - Scott Jibben

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接