Knowledge Center Monthly Newsletter - March 2025
Stay up to date with the latest from the Knowledge Center. See all new and updated Knowledge Center articles published in the last month and re:Post’s top contributors.
如何在 AWS WAF 中将特定的 URI 从对 HTTP 请求的 XSS 或 SQLi 检查中排除?
我在使用 AWS WAF 时,某些 HTTP 请求的 SQL 注入(SQLi)或跨站脚本(XSS)会出现误报。我想将特定的 URI 从对 HTTP 请求的 XSS 或 SQLi 检查中排除。
简短描述
在对 AWS 托管规则和自定义规则进行 XSS 和 SQLi 规则检查期间,有时会出现误报。为避免误报,应从 XSS 和 SQLi 检查中排除特定的 URI 路径。为此,可以使用嵌套语句编写包含例外的阻止规则,让 AWS WAF 根据所有其他规则评估请求。
解决方法
HTTP 或 HTTPS 请求示例
http://www.amazon.com/path/string1?xss=%3Cscript%3E%3Cscript%3E&sql=UNION%20ALL%20SELECT%201
在前面的请求中,URI 路径为 ** /path/ ** string1。问号(?)后面的字符串为查询字符串。在此示例中,查询字符串为 xss=%3Cscript%3E%3Cscript%3E&sql=UNION%20ALL%20SELECT%201。
在 XSS 或 SQLi 检查中允许特定 URI 的示例规则
注意: 以下示例规则配置仅供参考。针对位置约束、搜索字符串和文本转换等信息自定义这些规则。您可以使用类似的逻辑来允许诸如特定标头和查询参数之类的配置。
案例 1: 使用 AWS 托管规则
AWS 托管规则组 AWSManagedRulesCommonRuleSet 包含以下规则:
- CrossSiteScripting_COOKIE
- CrossSiteScripting_QUERYARGUMENTS
- CrossSiteScripting_BODY
- CrossSiteScripting_URIPATH
AWSManagedRulesCommonRuleSet 规则组有一个 BLOCK 操作,用于检查请求的对应部分是否有 XSS 攻击字符串。有关更多信息,请参阅核心规则集(CRS)托管规则组。
同样,规则组 AWSManagedRulesSQLiRuleSet 也有检查查询参数、正文、URI 路径和 SQLi 注入攻击模式的 Cookie 的规则。有关更多信息,请参阅用例特定规则组。
当请求符合这些规则时,AWS WAF 会生成相应的标签。然后,WEB ACL 中的自定义规则可以使用这些 AWS 托管规则标签有选择地从匹配的规则签名中排除特定请求。
要允许特定 URI,请完成以下步骤:
- 在计数模式下保留 AWSManagedRulesCommonRuleSet 规则组中的以下规则:
CrossSiteScripting_COOKIE
CrossSiteScripting_QUERYARGUMENTS
CrossSiteScripting_BODY
CrossSiteScripting_URIPATH - 创建带有阻止操作和 URI 路径例外的规则。将规则的优先级配置为低于 AWSManagedRulesCommonRuleSet。要在 AWS WAF 控制台中配置更低的优先级,将规则放在列表中更靠下的位置。要在 JSON 中配置更低的优先级,使用更大的优先级值。
规则使用以下逻辑: (XSS_URIPATH or XSS_Cookie or XSS_Body or XSS_QueryArguments) AND (NOT allowlisted URIString) = BLOCK
使用以下配置:
注意: 在此示例中,OrStatement 从 Web 请求的所有标签和部分中排除特定 URI:正文、标头、URI 路径和查询参数。此示例假设您在 Web 请求的所有部分中都遇到同一个 URI 的误报。但是,您可能只是在 Web 请求的一个部分中遇到,如在查询参数中。在这种情况下,确保安全的最佳做法是仅为 Web 请求的一个部分及其匹配的标签创建单独的规则。在这个规则中,不将特定 URI 路径从 Web 请求的所有部分中排除。{ "Name": "whitelist-xss", "Priority": 10, "Statement": { "AndStatement": { "Statements": [ { "OrStatement": { "Statements": [ { "LabelMatchStatement": { "Scope": "LABEL", "Key": "awswaf:managed:aws:core-rule-set:CrossSiteScripting_URIPath" } }, { "LabelMatchStatement": { "Scope": "LABEL", "Key": "awswaf:managed:aws:core-rule-set:CrossSiteScripting_Cookie" } }, { "LabelMatchStatement": { "Scope": "LABEL", "Key": "awswaf:managed:aws:core-rule-set:CrossSiteScripting_Body" } }, { "LabelMatchStatement": { "Scope": "LABEL", "Key": "awswaf:managed:aws:core-rule-set:CrossSiteScripting_QueryArguments" } } ] } }, { "NotStatement": { "Statement": { "ByteMatchStatement": { "SearchString": "/path/string1", "FieldToMatch": { "UriPath": {} }, "TextTransformations": [ { "Priority": 0, "Type": "NONE" } ], "PositionalConstraint": "CONTAINS" } } } } ] } }, "Action": { "Block": {} }, "VisibilityConfig": { "SampledRequestsEnabled": true, "CloudWatchMetricsEnabled": true, "MetricName": "whitelist-xss" } }
对于 AWSManagedRulesSQLiRuleSet,使用相同的步骤,但将标签替换为 AWSManagedRulesSQLiRuleSet 生成的标签。
如果您要在检查中排除多个 URI,则在 NotStatement 中使用 OrStatement。例如,要排除 /path/string1 和 /path/string2,使用以下 NotStatement:
{ "NotStatement": { "Statement": { "OrStatement": { "Statements": [ { "ByteMatchStatement": { "SearchString": "/path/string1", "FieldToMatch": { "UriPath": {} }, "TextTransformations": [ { "Priority": 0, "Type": "NONE" } ], "PositionalConstraint": "CONTAINS" } }, { "ByteMatchStatement": { "SearchString": "/path/string2", "FieldToMatch": { "UriPath": {} }, "TextTransformations": [ { "Priority": 0, "Type": "NONE" } ], PositionalConstraint": "CONTAINS" } } ] } } } }
案例 2: 使用自定义 XSS 和 SQLi 规则
规则使用以下逻辑:
(XSS_URIPATH or XSS_Cookie or XSS_Body or XSS_QueryArguments) AND (NOT allowlisted URIString) = BLOCK
使用以下规则配置检查请求中是否有 XSS 攻击字符串,也可以选择性地排除特定 URI_PATH:
{ "Name": "xss-URI", "Priority": 10, "Action": { "Block": {} }, "VisibilityConfig": { "SampledRequestsEnabled": true, "CloudWatchMetricsEnabled": true, "MetricName": "xss-URI" }, "Statement": { "AndStatement": { "Statements": [ { "OrStatement": { "Statements": [ { "XssMatchStatement": { "FieldToMatch": { "UriPath": {} }, "TextTransformations": [ { "Priority": 0, "Type": "NONE" } ] } }, { "XssMatchStatement": { "FieldToMatch": { "Cookies": { "MatchPattern": { "All": {} }, "MatchScope": "ALL", "OversizeHandling": "CONTINUE" } }, "TextTransformations": [ { "Priority": 0, "Type": "NONE" } ] } }, { "XssMatchStatement": { "FieldToMatch": { "Body": { "OversizeHandling": "CONTINUE" } }, "TextTransformations": [ { "Priority": 0, "Type": "NONE" } ] } }, { "XssMatchStatement": { "FieldToMatch": { "AllQueryArguments": {} }, "TextTransformations": [ { "Priority": 0, "Type": "NONE" } ] } } ] } }, { "NotStatement": { "Statement": { "ByteMatchStatement": { "FieldToMatch": { "UriPath": {} }, "PositionalConstraint": "CONTAINS", "SearchString": "/path/string1", "TextTransformations": [ { "Type": "NONE", "Priority": 0 } ] } } } } ] } } }
使用 SQLi 语句时,按照以下过程操作。
注意: 设置一个优先级更高、只允许 URI 的规则并不是最佳做法。这会阻止根据 Web ACL 中定义的所有其他规则对具有允许的 URI_PATH 的请求进行评估。
相关信息
相关内容
- AWS 官方已更新 3 年前
- AWS 官方已更新 10 个月前
- AWS 官方已更新 3 年前
- AWS 官方已更新 1 年前