我正试图提高我的应用程序的安全性。每当我从用户那里接收数据(无论是通过POST还是GET),这些数据应该是一个整数,我都会进行相应的验证。但经常出现数据是VARCHAR类型的情况,有时还带有HTML标签。
在这种情况下,我该如何保护我的数据库免受SQL注入攻击?
<cfqueryparam value="#form.textInput#" cfsqltype="cf_sql_varchar">
是否可以保护查询免受在VARCHAR值中发送恶意SQL语句的攻击?
我正试图提高我的应用程序的安全性。每当我从用户那里接收数据(无论是通过POST还是GET),这些数据应该是一个整数,我都会进行相应的验证。但经常出现数据是VARCHAR类型的情况,有时还带有HTML标签。
在这种情况下,我该如何保护我的数据库免受SQL注入攻击?
<cfqueryparam value="#form.textInput#" cfsqltype="cf_sql_varchar">
是否可以保护查询免受在VARCHAR值中发送恶意SQL语句的攻击?
简短的回答是肯定的。
cfqueryparam 将阻止某些 SQL 注入攻击。
还有其他攻击变量可以使用,因此请小心,但编写良好的 ColdFusion 可以非常安全。
如果您正在存储并稍后显示输入的 HTML,请注意跨站点脚本攻击,特别是 JavaScript 标签。
简单回答你的问题是“是的”。
我使用三种方法来阻止黑客攻击。
I use cfqueryparam in all my database queries. I will use cfparam at the top of the template/cfm files for url scope variables.
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.
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注入攻击。
如果同时使用这三种方法,服务器/站点应该非常安全。建议定期查看服务器日志,因为攻击会不断进化和改进。
maxlength
。例如,如果您知道一个文本字段只会有最多16个字符长的字符串,那么就没有必要允许更长的字符串进入,因为它们应该被标记为错误。 - Simon at My School Portal