在使用 AWS WAF 時,如何在 XSS 或 SQLi 檢查中排除 HTTP 請求的特定 URI?
上次更新日期:2022 年 7 月 21 日
我在特定 HTTP 請求上收到 SQL 隱碼攻擊 (SQLi) 或跨網站指令碼 (XSS) 的誤報。 在使用 AWS WAF 時,如何在 XSS 或 SQLi 檢查中排除 HTTP 請求的特定 URI?
簡短描述
在針對 AWS 受管規則和自訂規則進行 XSS 和 SQLi 規則檢查期間,有時會發生誤報。您可以在 XSS 和 SQLi 檢查中排除特定的 URI 路徑,以避免誤報。若要這麼做,請使用巢狀陳述式撰寫允許規則,以便根據所有其他規則評估請求。
解決方案
HTTP 或 HTTPS 請求例子
http://www.amazon.com/path/string1?abc=123&xyz=567
在上述的請求中,URI 路徑為 '/path/string1'。在 '?' 之後的任何字串稱為查詢字串,如上述例子中的 ' abc=123&xyz=567' 。在查詢字串中,查詢參數 'abc' 和 'xyz' 的值分別為 '123' 和 '567'。
在 XSS 或 SQLi 檢查中允許特定 URI 的規則例子
注意: 下列規則組態例子僅供參考。您必須自訂這些規則的 PositionalConstraint、SearchString 和 TextTransformations 等。您可以使用類似的邏輯允許特定的標頭和查詢參數等。
案例 1:使用 AWS 受管規則
AWS 受管規則群組 AWSManagedRulesCommonRuleSet 包含下列規則:
- CrossSiteScripting_COOKIE
- CrossSiteScripting_QUERYARGUMENTS
- CrossSiteScripting_BODY
- CrossSiteScripting_URIPATH
AWSManagedRulesCommonRuleSet 規則群組設有 BLOCK (封鎖) 動作,以檢查請求中對應部分的 XSS 攻擊字串 。如需詳細資訊,請參閱 核心規則集 (CRS) 受管規則群組。
同樣地,AWSManagedRulesSQLiRuleSet 規則群組設有多項規則,以檢查 SQLi 隱碼攻擊模式的查詢參數、內文和 Cookie。如需詳細資訊,請參閱 使用案例特定規則群組。
如果請求符合上述規則,AWS WAF 會產生對應的標籤。這些標籤會用於 Web ACL 稍後定義的規則中,以選擇性地排除特定請求 (在此範例中,根據 URI 排除)。
若要允許特定的 URI,請執行下列操作:
1. 將 AWSManagedRulesCommonRuleSet 規則群組中的下列規則設定為 Count (計數) 模式:
- CrossSiteScripting_COOKIE
- CrossSiteScripting_QUERYARGUMENTS
- CrossSiteScripting_BODY
- CrossSiteScripting_URIPATH
2. 建立允許規則,並設定優先順序低於 AWSManagedRulesCommonRuleSet。
規則的邏輯如下:
(XSS_URIPATH or XSS_Cookie or XSS_Body or XSS_QueryArguments) AND (NOT whitelisted URIString) = BLOCK
請使用以下組態:
{
"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": "URI_SearchString",
"FieldToMatch": {
"UriPath": {}
},
"TextTransformations": [
{
"Priority": 0,
"Type": "NONE"
}
],
"PositionalConstraint": "CONTAINS"
}
}
}
}
]
}
},
"Action": {
"Block": {}
},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "whitelist-xss"
}
}
重複上述步驟以設定 AWSManagedRulesSQLiRuleSet,將標籤替換成 AWSManagedRulesSQLiRuleSet 產生的標籤。
案例 2:使用自訂的 XSS 和 SQLi 規則
規則的邏輯如下:
(XSS_URIPATH or XSS_Cookie or XSS_Body or XSS_QueryArguments) AND (NOT whitelisted 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": "URI_SearchString",
"TextTransformations": [
{
"Type": "NONE",
"Priority": 0
}
]
}
}
}
}
]
}
}
}
使用 SQLi 陳述式時,請遵循上述程序。
注意: 建立優先順序較高且只允許特定 URI 的規則並不是最佳實務。如果採取這種做法,將無法根據 Web ACL 定義的所有其他規則來評估具有已允許 URI_PATH 的請求。