YARA-L 中的單一和多重事件規則
本文顯示以 YARA-L 2.0 編寫的查詢。每個範例都會示範如何使用查詢規則語言建立事件關聯,藉此找出安全威脅、監控實體行為,以及運用商業邏輯強化偵測功能。
您可以將這些範例當做 YARA-L 2.0 的建構模塊,包括單一事件偵測、規則運算式比對和網路範圍篩選。這些範例依功能分類,可協助您從基本邏輯逐步進階到多事件關聯和複合偵測。
基礎語法和基本
本節範例說明如何有效建立 UDM 事件關聯,以及在規則語言中建構查詢。
| 主題 | 範例 |
|---|---|
| 單一事件查詢 | 初始使用者登入搜尋;5 分鐘登入偵測 |
| 查詢及調整 | 排除法程序偵測 |
| 網路範圍和邏輯 | 單一事件比對 (IP 範圍) |
| 查詢中的規則運算式 | 電子郵件篩選;主機名稱規則運算式;原始記錄檔搜尋 |
| 通用條件的重複欄位 | 可疑登入 IP 驗證 |
單一事件查詢
用途:基本偵測特定事件類型 (例如 USER_LOGIN),不需在時間範圍內建立關聯。
主要邏輯:僅使用事件和條件部分來識別單一事件。單一事件規則可以是:
- 不含
match區段的規則。 - 規則包含
match區段和condition區段,只檢查是否存在一個事件 (例如$e、#e > 0、#e >= 1、1 <= #e、0 < #e)。
範例:初始使用者登入搜尋
規則
以下規則範例會搜尋使用者登入 (USER_LOGIN) 事件,並傳回在 Google SecOps 帳戶儲存的企業資料儲存庫中,找到的第一個事件:
rule SingleEventRule {
meta:
author = "noone@altostrat.com"
events:
$e.metadata.event_type = "USER_LOGIN"
condition:
$e
}
搜尋
這個未匯總的搜尋範例會直接輸出個別事件。由於這項查詢不需要事件關聯,因此會省略事件變數,例如 $e1。
metadata.event_type = "USER_LOGIN"
資訊主頁
這項查詢邏輯著重於以原始狀態顯示特定、不相關的事件,因此不會使用資訊主頁圖表所需的 match 或 outcome 區段。
範例:五分鐘登入偵測
規則
以下範例顯示單一事件規則,使用 match 區段找出在 5 分鐘 (5m) 時間範圍內,至少發生一次登入事件的使用者。檢查是否存在使用者登入事件。
rule SingleEventRule {
meta:
author = "alice@example.com"
description = "windowed single event example rule"
events:
$e.metadata.event_type = "USER_LOGIN"
$e.principal.user.userid = $user
match:
$user over 5m
condition:
#e > 0
}
搜尋
這個統計搜尋範例會將活動匯總至五分鐘 (5m) 的滾動式時間區間,每個時間區間每位使用者輸出一列。由於查詢著重於每個時間範圍的數量,因此會省略 event 變數和 condition 區段,因為結果本來就包含一或多個事件。這個版本使用滾動式時間區間,而非跳躍式時間區間,確保結果在平台中正確顯示。
metadata.event_type = "USER_LOGIN"
principal.user.userid = $user
match:
$user by 5m
資訊主頁
以下範例包含 outcome 區段,可計算每位使用者的事件總數,有助於將資料繪製成一段時間內的統計值。查詢會使用滾動式時間區間而非跳躍式時間區間,確保資料點對應至不重疊的離散 bucket,並在資訊主頁中提供更清楚的趨勢圖表。
metadata.event_type = "USER_LOGIN"
principal.user.userid = $user
match:
$user by 5m
outcome:
$event_count = count(metadata.id)
查詢和調整
用途:偵測從非標準目錄啟動的 Windows svchost.exe。
主要邏輯:否定 (not) 搭配規則運算式比對。
範例:排除型程序偵測
規則
下列規則會檢查事件資料中的特定模式,並在找到模式時建立偵測。這項規則包含追蹤事件類型的變數 $e1 和 metadata.event_type UDM 欄位。規則會檢查規則運算式與 e1 的特定相符項目。事件 $e1 發生時,系統會建立偵測項目。規則中包含 not 條件,可排除特定非惡意路徑。您可以新增 not 條件,避免偽陽性。
rule suspicious_unusual_location_svchost_execution
{
meta:
author = "Google Cloud Security"
description = "Windows 'svchost' executed from an unusual location"
yara_version = "YL2.0"
rule_version = "1.0"
events:
$e1.metadata.event_type = "PROCESS_LAUNCH"
re.regex($e1.principal.process.command_line, `\bsvchost(\.exe)?\b`) nocase
not re.regex($e1.principal.process.command_line, `\\Windows\\System32\\`) nocase
condition:
$e1
}
搜尋
這個範例會執行未匯總的搜尋,輸出個別事件。由於這項搜尋不需要跨多個執行個體進行事件關聯,因此不需要 $e1 等事件變數。
metadata.event_type = "PROCESS_LAUNCH"
re.regex(principal.process.command_line, `\bsvchost(\.exe)?\b`) nocase
not re.regex(principal.process.command_line, `\\Windows\\System32\\`) nocase
資訊主頁
這個語法包含 match 和 outcome 區段,用於計算一段時間內的事件量。timestamp.get_timestamp() 函式會依日期將結果分組,以視覺化方式呈現趨勢。
metadata.event_type = "PROCESS_LAUNCH"
re.regex(principal.process.command_line, `\bsvchost(\.exe)?\b`) nocase
not re.regex(principal.process.command_line, `\\Windows\\System32\\`) nocase
$date = timestamp.get_timestamp(metadata.event_timestamp.seconds)
match:
$date
outcome:
$event_count = count(metadata.id)
網路範圍和邏輯
用途:根據特定 IP 子網路 (CIDR) 篩選活動,並比對多個可能的主機名稱。
主要邏輯:使用 net.ip_in_range_cidr() 函式比對子網路,並使用 OR 運算子檢查多個可能的主機名稱。
net.ip_in_range_cidr():這項函式會檢查指定的 IP 位址是否包含在指定的無類別跨網域路由 (CIDR) 子網路中。- 邏輯運算子
OR:用於結合多個條件。事件區段中的條件會隱含地與AND結合。OR運算子會檢查多個可能的主機名稱。
範例:單一事件比對 (IP 範圍)
規則
以下範例顯示單一事件規則,可搜尋兩個特定主機名稱與特定 IP 位址範圍之間的相符項目:
rule OrsAndNetworkRange {
meta:
author = "noone@altostrat.com"
events:
// Checks CIDR ranges.
net.ip_in_range_cidr($e.principal.ip, "203.0.113.0/24")
// Detection when the hostname field matches either value using or.
$e.principal.hostname = /pbateman/ or $e.principal.hostname = /sspade/
condition:
$e
}
搜尋
下列查詢範例會找出特定 IP 位址落在定義的 CIDR 範圍內,且主機名稱與特定使用者模式相符的事件:
net.ip_in_range_cidr(principal.ip, "203.0.113.0/24")
principal.hostname = /pbateman/ or principal.hostname = /sspade/
由於這是搜尋查詢,而非偵測規則,因此只要符合篩選條件,系統就會自動傳回整個事件。match 區段會依 principal.ip 和 principal.hostname 將資料分組。condition 區段並非必要,且由於未執行事件關聯,因此省略事件變數 ($e)。
資訊主頁
下列查詢範例會將不重複的 IP 和主機名稱配對分組,藉此匯總結果:
net.ip_in_range_cidr(principal.ip, "203.0.113.0/24")
principal.hostname = /pbateman/ or principal.hostname = /sspade/
match:
principal.ip, principal.hostname
查詢中的規則運算式
用途:搜尋彈性的字串模式 (例如電子郵件中的特定網域),同時忽略大小寫。最常用於搜尋和規則。
主要邏輯:使用 /regex/ nocase 進行基本比對,並使用 re.regex() 函式進行複雜的欄位分析。
範例:電子郵件篩選
規則
下列 YARA-L 2.0 規則運算式範例會搜尋從 altostrat.com 網域收到電子郵件的事件。由於 nocase 已新增至 $host 變數 regex 比較和 regex 函式,因此這些比較不區分大小寫。
rule RegexRuleExample {
meta:
author = "noone@altostrat.com"
events:
$e.principal.hostname = $host
$host = /.*HoSt.*/ nocase
re.regex($e.network.email.from, `.*altostrat\.com`) nocase
match:
$host over 10m
condition:
#e > 10
}
搜尋
在搜尋介面中,這項邏輯可用於高保真威脅搜尋和資料探索。分析師不必等待自動快訊,而是可以手動查詢 UDM,找出符合命名慣例的主機名稱,以及目標電子郵件網域。在將這些事件升級為永久偵測規則之前,這是驗證事件數量的主要方法。
principal.hostname = $host
$host = /.*HoSt.*/ nocase
re.regex(network.email.from, `.*altostrat\.com`) nocase
match:
$host over 10m
資訊主頁
下列邏輯會將 hostname 和 email 遙測資料匯總至 10 分鐘 (10m) 值區,藉此找出值得關注的模式。在資訊主頁中使用時,分析師可透過這項邏輯,以圖表呈現特定資產 (符合 host) 與 altostrat.com 網域的通訊頻率。這個檢視畫面對於監控內部資料移動趨勢,以及找出重要基礎架構中的主要通訊者至關重要。
principal.hostname = $host
$host = /.*HoSt.*/ nocase
re.regex(network.email.from, `.*altostrat\.com`) nocase
match:
$host over 10m
範例:主機名稱規則運算式
規則
以下範例會找出主體 hostname 識別為網路 (webserver) 或開發 (devserver) 伺服器的任何記錄活動。這項規則使用不區分大小寫的規則運算式,確保命名慣例的變化不會導致偵測遺漏。
rule WebServerOrDevServerActivity {
meta:
author = "Alex"
description = "Detects events where the principal hostname is 'webserver' or 'devserver', ignoring case."
severity = "Informational"
events:
$e.principal.hostname = /webserver|devserver/ nocase
condition:
$e
}
搜尋
在以下範例中,principal.hostname = /webserver|devserver/ nocase 會比對 "WebServer01"、"devserver-test"、"MyWebServers" 等主機名稱。這是尋找特定事件的常見用途。
// Use /regex/ followed by nocase for a case-insensitive match
principal.hostname = /webserver|devserver/ nocase
資訊主頁
雖然這個特定範例不會在資訊主頁中以圖表呈現,但這項規則會提供主動活動警報和持續偵測功能。與需要手動審查的資訊主頁不同,這項功能可確保這些伺服器上的每項活動都會自動標記,並記錄在偵測引擎中,以便立即搜尋。
範例:搜尋原始記錄
規則
手動搜尋適用於特定時間點的調查,偵測規則則可全天候持續監控遙測資料。您可以將成功的搜尋查詢轉換為 YARA-L 規則,自動執行警告程序。
規則的主要優點:
- 即時快訊:自動標記進入系統的比對結果。
- 保留設定:不必手動重新輸入搜尋字詞。
- 結果動作:直接饋送至偵測檢視畫面,供分析師分類及應變事件。
搜尋
資安分析師經常使用 regex,在 Google SecOps 中搜尋未剖析的原始記錄。這項動作可靈活比對模式,找出特定構件,即使構件未完全結構化或建立索引也沒問題。語法使用正斜線:
raw = /host/
這項查詢會傳回出現 "host" 字元序列的任何原始記錄行。相符的原始記錄內容範例可能包括 "hostname": "myhost123"。
資訊主頁
這個特定事件類型沒有專屬的資訊主頁變化版本。如要大規模呈現這些偵測結果,您可以:
- 在資訊主頁建立工具中,將
metadata.event_type對應至長條圖或圓餅圖。 - 追蹤這些事件在 7、30 或 90 天內的發生頻率,找出使用者行為異常。
具有通用條件的重複欄位
用途:稽核包含資料清單 (重複欄位) 的事件,確保沒有任何可信任的例外狀況,例如確認與登入相關聯的每個 IP 位址都不在已知的安全範圍內。
主要邏輯:使用 all 運算子,根據特定條件評估重複欄位中的每個元素,並示範如何將重複欄位指派給預留位置變數 (例如 $ip),為清單中的每個不重複值建立不同的偵測。
範例:可疑登入 IP 驗證
規則
下列規則會搜尋登入事件,其中所有來源 IP 位址在五分鐘內 (5m) 與已知安全的 IP 位址不符。
rule SuspiciousIPLogins {
meta:
author = "alice@example.com"
events:
$e.metadata.event_type = "USER_LOGIN"
// Detects if all source IP addresses in an event do not match "100.97.16.0"
// For example, if an event has source IP addresses
// ["100.97.16.1", "100.97.16.2", "100.97.16.3"],
// it will be detected since "100.97.16.1", "100.97.16.2",
// and "100.97.16.3" all do not match "100.97.16.0".
all $e.principal.ip != "100.97.16.0"
// Assigns placeholder variable $ip to the $e.principal.ip repeated field.
// There will be one detection per source IP address.
// For example, if an event has source IP addresses
// ["100.97.16.1", "100.97.16.2", "100.97.16.3"],
// there will be one detection per address.
$e.principal.ip = $ip
match:
$ip over 5m
condition:
$e
}
搜尋
metadata.event_type = "USER_LOGIN"
// Detects if all source IP addresses in an event do not match "100.97.16.0"
// For example, if an event has source IP addresses
// ["100.97.16.1", "100.97.16.2", "100.97.16.3"],
// it will be detected since "100.97.16.1", "100.97.16.2",
// and "100.97.16.3" all do not match "100.97.16.0".
all principal.ip != "100.97.16.0"
// Assigns placeholder variable $ip to the $e.principal.ip repeated field.
// There will be one detection per source IP address.
// For example, if an event has source IP addresses
// ["100.97.16.1", "100.97.16.2", "100.97.16.3"],
// there will be one detection per address.
principal.ip = $ip
match:
$ip over 5m
資訊主頁
metadata.event_type = "USER_LOGIN"
// Detects if all source IP addresses in an event do not match "100.97.16.0"
// For example, if an event has source IP addresses
// ["100.97.16.1", "100.97.16.2", "100.97.16.3"],
// it will be detected since "100.97.16.1", "100.97.16.2",
// and "100.97.16.3" all do not match "100.97.16.0".
all principal.ip != "100.97.16.0"
// Assigns placeholder variable $ip to the $e.principal.ip repeated field.
// There will be one detection per source IP address.
// For example, if an event has source IP addresses
// ["100.97.16.1", "100.97.16.2", "100.97.16.3"],
// there will be one detection per address.
principal.ip = $ip
match:
$ip over 5m
進階視窗
本節說明多級式模式,以及由其他規則活動觸發的偵測。
| 主題 | 範例 |
|---|---|
| 多重事件關聯 | 多城市登入偵測;快速建立及刪除使用者 |
| 查詢中的滑動式時間區間 | 偵測缺少的連續事件 |
| 多重事件查詢 | 高頻率登入偵測 |
| 計算結果的多事件查詢 | 暴力破解後成功登入;時間範圍內的主機比對 |
多事件關聯
本節提供範例,說明如何跨多個事件或時間範圍追蹤實體 (使用者或主機),以找出行為模式。
用途:偵測不可能的移動,也就是單一使用者在五 (5m) 分鐘內從兩個以上城市登入。
主要邏輯:使用 match 區段,依 $user 和 #city > 1 分組依據,找出不同的位置值。
範例:多城市登入偵測
規則
下列規則會搜尋在不到 5 (5m) 分鐘內,從兩個以上城市登入企業的使用者,其中 $user 是 match 變數、$udm 是事件變數,$city 和 $user 則是預留位置變數:
rule DifferentCityLogin {
meta:
events:
$udm.metadata.event_type = "USER_LOGIN"
$udm.principal.user.userid = $user
$udm.principal.location.city = $city
match:
$user over 5m
condition:
$udm and #city > 1
}
以下說明這項規則的運作方式:
- 依使用者名稱 (
$user) 將事件分組,並在找到相符項目時傳回 ($user)。 - 時間範圍為五分鐘 (
5m);只有相隔不到 5 分鐘 (5m) 的事件才會建立關聯。 - 搜尋事件類型為
USER_LOGIN的事件群組 ($udm)。 - 針對該事件群組,規則會將使用者 ID 稱為
$user,並將登入城市稱為$city。 如果 5 分鐘 (
5m) 時間範圍內,事件群組 ($udm) 中city值 (以#city表示) 的不重複數量大於1,則傳回相符項目。
搜尋
下列查詢範例會執行對等統計搜尋,找出不可能的旅遊模式。這項規則會將 5 分鐘 (5m) 內的 USER_LOGIN 事件依使用者分組,並篩選結果,只顯示單一身分偵測到多個不同城市的情況。
events:
metadata.event_type = "USER_LOGIN"
principal.user.userid = $user
principal.location.city = $city
match:
$user over 5m
condition:
#city > 1
資訊主頁
下列查詢範例會提供等效的資訊主頁圖表,用於追蹤潛在的帳戶盜用情形。這項規則會依使用者彙整 5 分鐘 (5m) 內的 USER_LOGIN 事件,並篩選出單一身分與多個不同城市 (#city) 相關聯的例項,讓您能繪製這些高風險地理位置異常事件隨時間變化的圖表。
events:
metadata.event_type = "USER_LOGIN"
principal.user.userid = $user
principal.location.city = $city
match:
$user over 5m
condition:
#city > 1
快速建立及刪除使用者
用途:找出在 4 小時內建立並刪除的免洗帳戶。
主要邏輯:在共用的 $user 變數上彙整兩種事件類型 (USER_CREATION 和 USER_DELETION),並比較兩者的時間戳記。
範例:快速建立及刪除使用者
規則
以下規則範例會搜尋在 4 小時內建立並刪除的使用者 (4h),其中 $create 和 $delete 是事件變數,$user 是 match 變數,且沒有預留位置變數:
rule UserCreationThenDeletion {
meta:
events:
$create.target.user.userid = $user
$create.metadata.event_type = "USER_CREATION"
$delete.target.user.userid = $user
$delete.metadata.event_type = "USER_DELETION"
$create.metadata.event_timestamp.seconds <=
$delete.metadata.event_timestamp.seconds
match:
$user over 4h
condition:
$create and $delete
}
搜尋
以下範例示範如何使用多事件統計資料搜尋,找出帳戶生命週期快速變更。這項查詢會輸出四小時內每位使用者的一列資料,並建立身分建立和刪除作業之間的關聯。
根據預設,搜尋會傳回包含指定事件的所有視窗,因此不需要 condition 區段。
$create.target.user.userid = $user
$create.metadata.event_type = "USER_CREATION"
$delete.target.user.userid = $user
$delete.metadata.event_type = "USER_DELETION"
$create.metadata.event_timestamp.seconds <=
$delete.metadata.event_timestamp.seconds
match:
$user over 4h
資訊主頁
以下範例說明如何透過多事件資訊主頁搜尋,繪製帳戶生命週期趨勢圖。使用滾動式時間區間 (by 4h) 後,結果會對應至不重疊的離散時間值區,非常適合用於圖表。
這個變化版本包含 outcome 區段,可計算每個時間範圍內不重複的建立事件數量。與先前的搜尋不同,這個版本不需要傳回特定事件變數,因為重點是匯總統計值,而非個別記錄行。
$create.target.user.userid = $user
$create.metadata.event_type = "USER_CREATION"
$delete.target.user.userid = $user
$delete.metadata.event_type = "USER_DELETION"
$create.metadata.event_timestamp.seconds <=
$delete.metadata.event_timestamp.seconds
match:
$user by 4h
outcome:
$event_count = count_distinct($create.metadata.id)
查詢中的滑動時間區間
用途:偵測潛在安全性問題,例如在特定時間範圍內,初始事件 (來自 firewall_1) 後未出現預期的後續事件 (來自 firewall_2)。
重要邏輯:
- 樞紐事件:規則會以
firewall_1的事件為中心,並指定為$e1。每次發生$e1事件時,該事件都會做為基準。 - 時間範圍:
match區段 ($host over 10m after $e1) 定義了 10 分鐘的時間範圍,在每個$e1事件後立即開始。這個視窗會隨著每個新的$e1事件滑動。 - 關聯:事件會依主機名稱 (
$host) 分組。 - 偵測條件 (
$e1和$e2):如果符合下列條件,就會觸發指定主機的偵測:firewall_1($e1) 的事件。AND:在該特定$e1事件發生後的 10 分鐘內,系統在同一主機上偵測到來自firewall_2($e2) 的NO事件。
範例:偵測缺少的依序事件
規則
以下範例說明主要觸發條件發生後,次要事件未發生的情況。這項規則會使用 !$e2 條件,在 10 分鐘的時間範圍內,標記缺少的遙測資料,特別是在某個位置看到防火牆記錄,但未出現在下一個預期的躍點,這表示可能存在觀測能力落差或流量下降。
rule MissingSequentialEvent {
meta:
author = "alice@example.com"
events:
$e1.metadata.product_name = "firewall_1"
$e1.principal.hostname = $host
$e2.metadata.product_name = "firewall_2"
$e2.principal.hostname = $host
match:
// $e1 is the pivot; the 10-minute window starts at the $e1 timestamp
$host over 10m after $e1
condition:
$e1 and !$e2
}
搜尋
以下範例示範如何使用依序搜尋,找出兩個來源之間的遙測資料落差。搜尋功能會以 $e1 為樞紐,尋找主要防火牆事件,且該事件在 10 分鐘內未在第二個防火牆上觸發對應事件。在調查期間,這是手動搜尋網路流量「黑洞」或記錄失敗的有效方法。
$e1.metadata.product_name = "firewall_1"
$e1.principal.hostname = $host
$e2.metadata.product_name = "firewall_2"
$e2.principal.hostname = $host
match:
// $e1 is the pivot; the 10-minute window starts at the $e1 timestamp
$host over 10m after $e1
condition:
$e1 and !$e2
資訊主頁
以下範例提供專為資訊主頁檢視畫面設計的可見度落差分析。只要匯總次要事件未接續主要事件發生的情況,就能以圖表呈現記錄管道在一段時間內的可靠性。繪製這些「遺漏」事件,有助於找出網路可見度中持續存在的死角,或特定主機名稱的設定問題。
$e1.metadata.product_name = "firewall_1"
$e1.principal.hostname = $host
$e2.metadata.product_name = "firewall_2"
$e2.principal.hostname = $host
match:
// $e1 is the pivot; the 10-minute window starts at the $e1 timestamp
$host over 10m after $e1
condition:
$e1 and !$e2
多事件查詢
用途:在特定時間範圍內,追蹤單一實體 (例如使用者或主機) 的多個事件,找出高頻率或暴力破解活動。
主要邏輯:使用
match區段依特定變數將事件分組,並使用condition區段檢查定義日期範圍內的計數是否達到門檻 (例如#e >= 10)。
一般多事件規則包含:
- 用於區分事件的事件變數。
match區段,指定事件分組的日期範圍。condition區段:指定應觸發偵測的條件,並檢查是否存在多個事件。
在 Google 搜尋中,多事件查詢是指查詢中包含多個事件。規則有兩種定義方式:
多個事件:例如
event1 = successful login, event2 = failed login。條件式觸發條件:條件會指定只有在多個事件符合條件時才觸發 (例如
event1 > 10)。這類規則也必須包含outcome區段。
範例:高頻率登入偵測
規則
以下規則會搜尋在 10 分鐘內登入至少 10 次的使用者:
rule MultiEventRule {
meta:
author = "noone@altostrat.com"
events:
$e.metadata.event_type = "USER_LOGIN"
$e.principal.user.userid = $user
match:
$user over 10m
condition:
#e >= 10
}
搜尋
以下範例使用多事件統計資料搜尋,找出高頻率登入活動。只要單一使用者在 10 分鐘 (10m) 內產生 10 次以上的登入事件,就會標記。
$e.metadata.event_type = "USER_LOGIN"
$e.principal.user.userid = $user
match:
$user by 10m
condition:
#e >= 10
資訊主頁
以下範例使用多事件搜尋,監控潛在的帳戶入侵。這項規則會將 10 分鐘滑動時間範圍內的登入嘗試建立關聯,找出特定使用者和主機多次登入失敗後成功登入的情況,讓您即時掌握高風險驗證模式。
$e.metadata.event_type = "USER_LOGIN"
$e.principal.user.userid = $user
match:
$user by 10m
condition:
#e >= 10
含有計算結果的多事件查詢
用途:套用條件式邏輯,根據資產嚴重性或網路流量設定
risk_score。主要邏輯:使用
outcome區段計算變數,並使用條件區段依這些變數篩選。
範例:暴力破解後成功登入
以下範例使用 outcome 區段計算 match 視窗內的事件。這項查詢會產生與標準多事件查詢相同的輸出內容,但會示範如何將計算變數納入偵測邏輯。
規則
rule PossibleBruteForceThenSuccessfulLogin {
meta:
author = "Alex"
description = "Detects multiple failed login attempts followed by a successful login for the same user and host within a 10-minute window."
severity = "High"
tactic = "Credential Access"
events:
// Define the first type of event: Failed Login
// We use $failed to represent any event matching these criteria.
$failed.metadata.event_type = "USER_LOGIN"
$failed.security_result.action = "FAIL"
// Extract common fields to correlate on
$failed.target.user.userid = $user
$failed.principal.hostname = $hostname
// Define the second type of event: Successful Login
// We use $success to represent any event matching these criteria.
$success.metadata.event_type = "USER_LOGIN"
$success.security_result.action = "ALLOW"
// Correlate using the same user and hostname placeholders
$success.target.user.userid = $user
$success.principal.hostname = $hostname
match:
// This section is key for multi-event rules. It groups events:
// - By the common placeholder variables: $user and $hostname.
// - Within a time window: by 10m.
// The rule will evaluate all events matching $failed or $success that share the same $user and $hostname within any given 10-minute period.
$user, $hostname by 10m
outcome:
// Calculate aggregate values from the events within the match window.
$failed_login_count = count($failed.metadata.id)
$successful_login_count = count($success.metadata.id)
condition:
// The conditions that must be met *within each matched group* ($user, $hostname over 10m).
// - #failed >= 5: There must be 5 or more events matching the $failed criteria.
// - #success >= 1: There must be at least 1 event matching the $success criteria.
#failed >= 5 and #success >= 1
}
搜尋
// Define the first type of event: Failed Login
// We use $failed to represent any event matching these criteria.
$failed.metadata.event_type = "USER_LOGIN"
$failed.security_result.action = "FAIL"
// Extract common fields to correlate on
$failed.target.user.userid = $user
$failed.principal.hostname = $hostname
// Define the second type of event: Successful Login
// We use $success to represent any event matching these criteria.
$success.metadata.event_type = "USER_LOGIN"
$success.security_result.action = "ALLOW"
// Correlate using the same user and hostname placeholders
$success.target.user.userid = $user
$success.principal.hostname = $hostname
match:
// This section is key for multi-event rules. It groups events:
// - By the common placeholder variables: $user and $hostname.
// - Within a sliding time window: over 10m.
// The rule will evaluate all events matching $failed or $success that share
// the same $user and $hostname within any given 10-minute period.
$user, $hostname over 10m
資訊主頁
// Define the first type of event: Failed Login
// We use $failed to represent any event matching these criteria.
$failed.metadata.event_type = "USER_LOGIN"
$failed.security_result.action = "FAIL"
// Extract common fields to correlate on
$failed.target.user.userid = $user
$failed.principal.hostname = $hostname
// Define the second type of event: Successful Login
// We use $success to represent any event matching these criteria.
$success.metadata.event_type = "USER_LOGIN"
$success.security_result.action = "ALLOW"
// Correlate using the same user and hostname placeholders
$success.target.user.userid = $user
$success.principal.hostname = $hostname
match:
// This section is key for multi-event rules. It groups events:
// - By the common placeholder variables: $user and $hostname.
// - Within a sliding time window: over 10m.
// The rule will evaluate all events matching $failed or $success that share
// the same $user and $hostname within any given 10-minute period.
$user, $hostname over 10m
範例:時間範圍主機比對
規則
以下規則會查看兩個事件,以取得 $hostname 的值。如果 $hostname 的值在 5 分鐘 (5m) 內相符,系統就會套用嚴重程度分數。在 match 區段中加入時間範圍後,規則就會檢查指定時間範圍內的資料。
rule OutcomeRuleMultiEvent {
meta:
author = "Google Cloud Security"
events:
$u.udm.principal.hostname = $hostname
$asset_context.graph.entity.hostname = $hostname
$severity = $asset_context.graph.entity.asset.vulnerabilities.severity
match:
$hostname over 5m
outcome:
$risk_score =
max(
100
+ if($hostname = "my-hostname", 100, 50)
+ if($severity = "HIGH", 10)
+ if($severity = "MEDIUM", 5)
+ if($severity = "LOW", 1)
)
$asset_id_list =
array(
if($u.principal.asset_id = "",
"Empty asset id",
$u.principal.asset_id
)
)
$asset_id_distinct_list = array_distinct($u.principal.asset_id)
$asset_id_count = count($u.principal.asset_id)
$asset_id_distinct_count = count_distinct($u.principal.asset_id)
condition:
$u and $asset_context and $risk_score > 50 and not arrays.contains($asset_id_list, "id_1234")
}
搜尋
// Define the first type of event: Failed Login
// We use $failed to represent any event matching these criteria.
$failed.metadata.event_type = "USER_LOGIN"
$failed.security_result.action = "FAIL"
// Extract common fields to correlate on
$failed.target.user.userid = $user
$failed.principal.hostname = $hostname
// Define the second type of event: Successful Login
// We use $success to represent any event matching these criteria.
$success.metadata.event_type = "USER_LOGIN"
$success.security_result.action = "ALLOW"
// Correlate using the same user and hostname placeholders
$success.target.user.userid = $user
$success.principal.hostname = $hostname
match:
// This section is key for multi-event rules. It groups events:
// - By the common placeholder variables: $user and $hostname.
// - Within a sliding time window: over 10m.
// The rule will evaluate all events matching $failed or $success that share
// the same $user and $hostname within any given 10-minute period.
$user, $hostname over 10m
outcome:
// Calculate aggregate values from the events within the match window.
$failed_login_count = count($failed.metadata.id)
$successful_login_count = count($success.metadata.id)
```
資訊主頁
// Define the first type of event: Failed Login
// We use $failed to represent any event matching these criteria.
$failed.metadata.event_type = "USER_LOGIN"
$failed.security_result.action = "FAIL"
// Extract common fields to correlate on
$failed.target.user.userid = $user
$failed.principal.hostname = $hostname
// Define the second type of event: Successful Login
// We use $success to represent any event matching these criteria.
$success.metadata.event_type = "USER_LOGIN"
$success.security_result.action = "ALLOW"
// Correlate using the same user and hostname placeholders
$success.target.user.userid = $user
$success.principal.hostname = $hostname
match:
// This section is key for multi-event rules. It groups events:
// - By the common placeholder variables: $user and $hostname.
// - Within a sliding time window: over 10m.
// The rule will evaluate all events matching $failed or $success that share
// the same $user and $hostname within any given 10-minute period.
$user, $hostname over 10m
outcome:
// Calculate aggregate values from the events within the match window.
$failed_login_count = count($failed.metadata.id)
$successful_login_count = count($success.metadata.id)
情境感知分析
您可以使用 YARA-L 規則,根據入侵指標 (IOC) 比對內容資料評估 UDM 事件資料。這類比對內容資料會以實體背景資訊的形式,儲存在 Google SecOps。
本節範例說明如何將事件欄位與 IOC 動態饋給 (例如 Google 安全瀏覽和自訂動態饋給) 彙整,建立情境感知分析。
| 主題 | 範例 |
|---|---|
| 比對檔案雜湊與入侵指標 | 可疑的程序啟動;安全瀏覽威脅清單 |
| 比對 IP 位址與入侵指標 | Tor 結束節點偵測;從 Tor 網路多次登入失敗 |
| 比對網域和入侵指標 | 普及度低的網域 |
| 網址與入侵指標比對 | 威脅動態饋給中的惡意網址比對 |
| 同時比對多個動態饋給 | Tor 結束節點和遠端存取工具偵測 |
檔案雜湊比對入侵指標
用途:偵測威脅情報中,標示為入侵指標 (IOC) 的檔案是否執行或存在。
主要邏輯:將 UDM 程序或檔案事件,與實體圖中 IOC 動態饋給的檔案實體 (雜湊) 彙整。
範例:可疑的程序啟動
規則
下列規則會根據一般 IOC 比對內容資料,評估 UDM 程序資料。這項規則會搜尋程序啟動事件,並將事件中的檔案雜湊與 IOC 實體圖中找到的檔案雜湊彙整。
rule ProcessLaunch {
meta:
events:
$ioc.graph.metadata.vendor_name = "ACME"
$ioc.graph.metadata.product_name = "IOCs"
$ioc.graph.metadata.entity_type = "FILE"
$ioc.graph.entity.file.sha256 = $hash
$process.metadata.event_type = "PROCESS_LAUNCH"
$process.principal.hostname = $hostname
(
not $process.target.process.file.sha256 = "" and
$process.target.process.file.sha256 = $hash
)
match:
$hash over 15m
condition:
$ioc and $process
}
範例:安全瀏覽威脅清單
規則
Google SecOps 會從與檔案雜湊相關的威脅清單擷取資料,並儲存為實體。下列規則會偵測 Google 安全瀏覽服務判定為惡意的檔案執行作業。
rule safe_browsing_file_execution {
meta:
author = "Google Security Operations"
description = "Example usage of Safe Browsing data, to detect execution of a file that's been deemed malicious"
severity = "LOW"
events:
// find a process launch event, match on hostname
$execution.metadata.event_type = "PROCESS_LAUNCH"
$execution.principal.hostname = $hostname
// join execution event with Safe Browsing graph
$sb.graph.entity.file.sha256 = $execution.target.process.file.sha256
// look for files deemed malicious
$sb.graph.metadata.entity_type = "FILE"
$sb.graph.metadata.threat_intel.severity = "SEVERITY_HIGH"
$sb.graph.metadata.product_name = "Google Safe Browsing"
$sb.graph.metadata.source_type = "GLOBAL_CONTEXT"
match:
$hostname over 1h
outcome:
$risk_score = max(if($sb.graph.metadata.threat_intel.severity = "SEVERITY_HIGH", 90, 0))
condition:
$execution and $sb
}
比對 IP 位址與入侵指標
用途:偵測與威脅動態消息中已知惡意 IP 位址的網路通訊。
主要邏輯:將 UDM 網路事件與實體圖表中 IOC 動態饋給的 IP 位址實體彙整。
範例:Tor 結束節點偵測
規則
找出源自或連至已知 Tor 結束節點的網路流量。這個範例會將 UDM 事件與 GCTI 動態饋給中的 Tor 結束節點動態饋給彙整,所有客戶都能使用。
rule TorExitNodeMatch {
meta:
author = "Google Security Operations"
description = "Detects traffic from known Tor exit nodes."
severity = "MEDIUM"
events:
// Event based on IP
$principal_ip = $e.principal.ip
// Filter Entity Graph to extract feed specific details
$gcti.graph.metadata.entity_type = "IP_ADDRESS"
$gcti.graph.metadata.feed = "Tor Exit Nodes"
$gcti.graph.metadata.product_name = "GCTI Feed"
// Join the IP data
$principal_ip = $gcti.graph.entity.ip
match:
$principal_ip over 30m
outcome:
$threat_feed = array_distinct($gcti.graph.metadata.feed)
condition:
$e and $gcti
}
範例:從 Tor 網路多次登入失敗
規則
偵測來自 Tor 網路的多個登入失敗嘗試。這個範例會將 login_failure 事件與 Tor 結束節點動態饋給建立關聯,並使用計數語法 (#) 套用頻率門檻。
rule TorMultiLoginFailure {
meta:
author = "Google Security Operations"
description = "Detects multiple failed logins from a Tor network."
severity = "HIGH"
events:
$failed_login_event.metadata.product_name = "login"
$failed_login_event.metadata.product_event_type = "login_failure"
$failed_login_event.metadata.vendor_name = "Google Workspace"
$sus_ip = $failed_login_event.principal.ip
$sus_target_account = $failed_login_event.target.user.email_addresses
$sus_target_account != ""
$gcti.graph.metadata.entity_type = "IP_ADDRESS"
$gcti.graph.metadata.feed = "Tor Exit Nodes"
$gcti.graph.metadata.product_name = "GCTI Feed"
// Join the IP data
$sus_ip = $gcti.graph.entity.ip
match:
$sus_ip over 1h
outcome:
$event_count = count_distinct($failed_login_event.metadata.id)
condition:
$failed_login_event and #failed_login_event >= 10 and $gcti
}
根據入侵指標比對網域
用途:偵測標記為入侵指標 (IOC) 的網域或普及度低的網域,查看是否有網路活動。
主要邏輯:使用網域名稱或主機名稱,將 UDM 網路事件與 IOC 動態饋給中的網域實體,或實體圖中的普及度資料彙整。
範例:普及度偏低的網域
規則
下列規則會比較普及度低的網域與已知 IOC,產生偵測警示。這項規則會將 DNS 查詢事件與普及度資料,以及 ET_PRO_IOC 動態饋給中的 IOC 實體彙整。
rule network_prevalence_uncommon_domain_ioc_match {
meta:
author = "Google Security Operations"
description = "Lookup Network DNS queries against Entity Graph for low prevalence domains with a matching IOC entry."
severity = "MEDIUM"
events:
$e.metadata.event_type = "NETWORK_DNS"
$e.network.dns.questions.name = $hostname
//only match FQDNs, such as: exclude chrome dns access tests and other internal hosts
$e.network.dns.questions.name = /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/
//prevalence entity graph lookup
$p.graph.metadata.entity_type = "DOMAIN_NAME"
$p.graph.entity.domain.prevalence.rolling_max > 0
$p.graph.entity.domain.prevalence.rolling_max <= 3
$p.graph.entity.domain.name = $hostname
//ioc entity graph lookup
$i.graph.metadata.vendor_name = "ET_PRO_IOC"
$i.graph.metadata.entity_type = "DOMAIN_NAME"
$i.graph.entity.hostname = $hostname
match:
$hostname over 10m
outcome:
$risk_score = max(
//increment risk score based upon rolling_max prevalence
if ( $p.graph.entity.domain.prevalence.rolling_max = 3, 50) +
if ( $p.graph.entity.domain.prevalence.rolling_max = 2, 70) +
if ( $p.graph.entity.domain.prevalence.rolling_max = 1, 90)
)
condition:
$e and $p and $i
}
比對網址與入侵指標
用途:偵測涉及威脅動態消息中惡意網址的網路活動。
主要邏輯:將含有網址資料的 UDM 網路事件,與實體圖中 IOC 動態饋給的網址實體彙整。
範例:威脅動態中的惡意網址比對
規則
下列規則會偵測網路事件,其中要求的網址與威脅情報動態饋給中的已知惡意網址相符。系統會將事件的 target.url 與實體圖表中的網址實體建立關聯。
rule ioc_url_threat_feed_match {
meta:
author = "Google Security Operations"
description = "Matches network events against known malicious URLs from threat intelligence feeds."
severity = "HIGH"
events:
// Filter for network traffic containing URL information
$network_event.metadata.event_type = "NETWORK_HTTP" or $network_event.metadata.event_type = "NETWORK_CONNECTION"
$url = $network_event.target.url
// Entity Graph Join: URL Indicators
$ioc.graph.metadata.entity_type = "URL"
$ioc.graph.metadata.product_name = "GCTI Feed"
$ioc.graph.metadata.feed = "Malicious URLs"
$ioc.graph.entity.url = $url
match:
$url over 5m
outcome:
$risk_score = 85
$threat_feed = array_distinct($ioc.graph.metadata.feed)
$principal_ip = array_distinct($network_event.principal.ip)
$principal_user = array_distinct($network_event.principal.user.userid)
condition:
$network_event and $ioc
}
同時比對多個動態饋給
用途:在單一事件查詢中,將多個屬性 (例如 IP 位址和檔案雜湊) 與不同的威脅動態饋給進行比對,藉此找出進階威脅之間的關聯。
主要邏輯:將單一 UDM 事件與多個實體圖表預留位置彙整,每個預留位置都會篩選特定 threat_feed_name (例如 Google Cloud 威脅情報動態饋給)。
範例:Tor 結束節點和遠端存取工具偵測
規則
下列規則會標記疑似與遠端監控和管理 (RMM) 相關,且源自或連至 Tor 結束節點的網路流量。這項規則會將 NETWORK_CONNECTION 事件與 IP 資訊提供 (Tor 結束節點) 和檔案雜湊資訊提供 (遠端存取工具) 彙整。
rule tor_and_remote_access_tool_match {
meta:
author = "Google Security Operations"
description = "Matches network connections involving both a Tor exit node and Remote Access Tool malware hashes."
severity = "CRITICAL"
events:
// Filter for network connection events, often recorded by EDR agents
$e.metadata.event_type = "NETWORK_CONNECTION"
$target_ip = $e.target.ip
$process_hash = $e.principal.process.file.sha256
// IP Feed Join: Tor Exit Nodes
$gcti_ip.graph.metadata.entity_type = "IP_ADDRESS"
$gcti_ip.graph.metadata.product_name = "GCTI Feed"
$gcti_ip.graph.metadata.feed = "Tor Exit Nodes"
$gcti_ip.graph.entity.ip = $target_ip
// Hash Feed Join: Remote Access Tools
$gcti_hash.graph.metadata.entity_type = "FILE"
$gcti_hash.graph.metadata.product_name = "GCTI Feed"
$gcti_hash.graph.metadata.feed = "Remote Access Tools"
$gcti_hash.graph.entity.file.sha256 = $process_hash
match:
$target_ip over 10m
outcome:
$risk_score = 95
$ip_feed = array_distinct($gcti_ip.graph.metadata.feed)
$hash_feed = array_distinct($gcti_hash.graph.metadata.feed)
condition:
$e and $gcti_ip and $gcti_hash
}
複合偵測項目
複合偵測項目會使用複合規則,強化威脅偵測功能。這些複合規則會使用其他規則的偵測結果做為輸入內容。這項功能可偵測個別規則可能無法偵測的複雜威脅。詳情請參閱「複合偵測總覽」。
| 主題 | 範例 |
|---|---|
| 高風險篩選 | 偵測管理員使用者 |
| 匯總和門檻 | 風險匯總 |
| 策略匯總 | MITRE 戰術匯總 |
| 依序複合偵測 | 暴力破解後成功登入 |
| 情境感知偵測 | 威脅情報充實 |
| 共現偵測 | 提權和資料竊取同時發生 |
高風險篩選
用途:篩選現有偵測結果,找出高風險屬性,例如涉及管理員帳戶的活動。
主要邏輯:對現有發現項目中的結果或中繼資料欄位執行作業。
高風險篩選複合偵測項目是最簡單的複合偵測項目,會對偵測結果中的欄位 (例如結果變數或規則中繼資料) 執行。這類標記可篩選出可能代表較高風險的條件,例如管理員使用者或正式環境。
範例:偵測管理員使用者
規則
下列複合規則會搜尋任何現有偵測項目,其中執行者為管理員使用者,並套用標準化風險評分。
rule composite_admin_detection {
meta:
rule_name = "Detection with Admin User"
author = "Google Cloud Security"
description = "Composite rule that looks for any detections where the actor is an admin user"
severity = "Medium"
events:
$rule_name = $d.detection.detection.rule_name
$principal_user = $d.detection.detection.variables["principal_users"]
$principal_user = /admin|root/ nocase
match:
$principal_user over 1h
outcome:
$risk_score = 75
$upstream_rules = array_distinct($rule_name)
condition:
$d
}
搜尋
下列統計搜尋會找出並匯總高權限帳戶的活動。這項查詢旨在找出所有不重複的規則名稱,這些規則觸發了涉及「admin」或「root」使用者的偵測。
在這項特定查詢中,時間範圍已移除,以便對所選時間範圍內的所有偵測結果執行單一統計分析。此外,由於這是針對現有偵測資料的未匯總搜尋,因此不需要事件區段、事件變數和條件區段。
$rule_name = detection.detection.rule_name
$principal_user = detection.detection.variables["principal_users"]
$principal_user = /admin|root/ nocase
match:
$principal_user
outcome:
$upstream_rules = array_distinct($rule_name)
資訊主頁
這項資訊主頁查詢可讓您以視覺化方式,查看哪些特定規則最常偵測到管理員相關活動。這項功能可讓您概略瞭解整個環境的偵測趨勢。
請注意,與先前的範例相比,match 變數和 outcome 匯總有所變更。這項查詢會依規則名稱將結果分組,並計算每項規則偵測到的管理員使用者數量。
$rule_name = detection.detection.rule_name
$principal_user = detection.detection.variables["principal_users"]
$principal_user = /admin|root/ nocase
match:
$rule_name
outcome:
$admin_detections = count($principal_user)
匯總和門檻
用途:找出產生大量警告或風險評分持續偏高的使用者或主機。
主要邏輯:使用 sum() 或 count_distinct() 分析匯總的偵測資料。
匯總複合偵測規則可讓您根據共用屬性 (例如主機名稱或使用者名稱) 將偵測結果分組,並分析匯總資料。以下是常見用途:
- 找出產生大量安全性警示或匯總風險的使用者。
- 匯總相關偵測結果,找出活動模式異常的主機。
範例:風險匯總
規則
這項規則會匯總單一使用者在 48 小時內的風險分數。這項功能會找出在多次偵測中,累積風險超過特定門檻的使用者。
在這個更新後的邏輯中,detection.detection.outcomes 會替換為地圖欄位變數,用於儲存 match 和 outcome 變數。此外,由於每個偵測結果都只包含一個比對變數值,且該值已擷取,因此系統會移除 $principal_users 結果變數。
rule composite_risk_aggregation {
meta:
rule_name = "Risk Aggregation Composite"
author = "Google Cloud Security"
description = "Composite detection that aggregates risk of a user over 48 hours"
severity = "High"
events:
$rule_name = $d.detection.detection.rule_name
$principal_user = $d.detection.detection.outcomes["principal_users"]
$risk = $d.detection.detection.risk_score
match:
$principal_user over 48h
outcome:
$risk_score = 90
$cumulative_risk = sum($risk)
$upstream_rules = array_distinct($rule_name)
condition:
$d and $cumulative_risk > 500
}
搜尋
這項統計搜尋會匯總偵測資料,計算使用者在 48 小時內的總風險。系統會為每個時間範圍內的主要使用者輸出一個資料列,提供多種偵測類型的帳戶風險概覽。
在這個變化版本中,事件變數並非必要。雖然規則引擎會自動篩除沒有主要使用者的偵測結果,但這項搜尋需要明確的篩選器 ($principal_user != ""),確保結果只包含已填入的資料。根據預設,只有在特定使用者有一或多項偵測結果時,查詢才會傳回結果。
$rule_name = detection.detection.rule_name
$principal_user = detection.detection.variables["principal_user"]
$principal_user != ""
$risk = detection.detection.risk_score
match:
$principal_user over 48h
outcome:
$risk_score = 90
$cumulative_risk = sum($risk)
$upstream_rules = array_distinct($rule_name)
condition:
$cumulative_risk > 500
資訊主頁
這個變化版本專為資訊主頁設計,可繪製一段時間內的使用者風險和偵測活動。這類圖表會將資料匯總至不連續的值區,非常適合用來呈現趨勢,例如觸發的不重複規則數量,或每位使用者的偵測總數。
在這項查詢中,視窗會從滑動 (躍進) 視窗切換為滾動式時間區間 (by 48h)。這樣一來,資料點就會對應至不重疊的時間區隔,時序圖的視覺化效果也會更清晰。與其他未匯總的搜尋一樣,事件變數並非必要,且 outcome 區段會展開,納入規則名稱和偵測 ID 的不重複計數。
$rule_name = detection.detection.rule_name
$principal_user = detection.detection.variables["principal_user"]
$principal_user != ""
$risk = detection.detection.risk_score
match:
$principal_user by 48h
outcome:
$cumulative_risk = sum($risk)
$rule_count = count_distinct($rule_name)
$detection_count = count_distinct(detection.id)
condition:
$cumulative_risk > 500
策略匯總
用途:找出活動觸發多項 MITRE ATT&CK 戰術偵測的使用者,這表示攻擊生命週期正在推進 (例如從「初始存取」轉移到「資料竊取」)。
主要邏輯:使用 count_distinct($tactic),只有在使用者於 48 小時內跨越特定不同策略門檻時,才會觸發。
範例:MITRE 戰術匯總
規則
rule composite_tactic_aggregation {
meta:
rule_name = "MITRE Tactic Aggregation Composite"
author = "Google Cloud Security"
description = "Composite detection that detects if a user has triggered detections over multiple mitre tactics."
severity = "Medium"
events:
$principal_user = $d.detection.detection.outcomes["principal_users"]
$tactic = $d.detection.detection.outcomes["mitre_tactic"]
$rule_name = $d.detection.detection.rule_name
match:
$principal_user over 48h
outcome:
$mitre_tactics_count = count_distinct($tactic)
$mitre_tactics = array_distinct($tactic)
$calculated_risk = 50 + (15 * $mitre_tactics_count)
$upstream_rules = array_distinct($rule_name)
condition:
$d and $mitre_tactics_count > 1 }
搜尋
以下範例示範的搜尋變體,專為需要關聯現有偵測項目並套用動態風險權重的資安開發人員設計。這項查詢邏輯會從 detection 資料來源擷取 MITRE ATT&CK 戰術和使用者資訊,依主體使用者將活動分組,並根據觀察到的戰術多樣性計算自訂風險評分。
detection.detection.outcomes.key = "principal_users"
detection.detection.outcomes.key = "mitre_tactic"
$principal_user = detection.detection.outcomes["principal_users"]
$tactic = detection.detection.outcomes["mitre_tactic"]
$rule_name = detection.detection.rule_name
match:
$principal_user
outcome:
$mitre_tactics_count = count_distinct($tactic)
$mitre_tactics = array_distinct($tactic)
$upstream_rules = array_distinct($rule_name)
$calculated_risk = 50 + (15 * $mitre_tactics_count)
$risk_score = if($calculated_risk > 100, 100, $calculated_risk)
資訊主頁
以下範例說明相同偵測分析邏輯的資訊主頁變化版本。在 Google SecOps 資訊主頁中使用這項查詢,開發人員就能連結不同規則的偵測結果,以圖表呈現高風險使用者。這項邏輯會擷取主要使用者和 MITRE 戰術、匯總發現項目,並套用風險評分上限,協助您直接在資訊主頁小工具中排定調查工作的優先順序。
detection.detection.outcomes.key = "principal_users"
detection.detection.outcomes.key = "mitre_tactic"
$principal_user = detection.detection.outcomes["principal_users"]
$tactic = detection.detection.outcomes["mitre_tactic"]
$rule_name = detection.detection.rule_name
match:
$principal_user
outcome:
$mitre_tactics_count = count_distinct($tactic)
$mitre_tactics = array_distinct($tactic)
$upstream_rules = array_distinct($rule_name)
$calculated_risk = 50 + (15 * $mitre_tactics_count)
$risk_score = if($calculated_risk > 100, 100, $calculated_risk)
```
依序複合偵測
用途:找出作業順序至關重要的重大攻擊模式,例如偵測到來自相同 IP 位址的一連串暴力破解嘗試警示後,才成功登入帳戶。
主要邏輯:將先前的偵測結果與後續的原始 UDM 事件建立關聯,方法是將兩者加入共同變數 (例如 $bruteforce_ip),並使用時間戳記比較,確保事件依正確順序發生。
依序複合偵測功能會找出相關事件的模式,其中偵測順序很重要,例如偵測到暴力破解登入嘗試,接著偵測到成功登入。這些模式可能涉及多個基本偵測項目,或基本偵測項目和事件的組合。
範例:暴力破解攻擊後成功登入
規則
下列複合規則會找出相關事件的模式,其中順序很重要。具體來說,這項規則會尋找 Google Workspace 暴力破解偵測事件,然後在 24 小時內,從相同來源 IP 成功登入的事件。
rule composite_bruteforce_login {
meta:
rule_name = "Bruteforce Login Composite"
author = "Google Cloud Security"
description = "Detects when an IP address associated with a Workspace brute force attempt successfully logs in"
severity = "High"
events:
$bruteforce_detection.detection.detection.rule_name = /Workspace Anomalous Failed Logins/
$bruteforce_ip = $bruteforce_detection.detection.detection.variables["principal_ips"]
$login_event.metadata.product_name = "login"
$login_event.metadata.product_event_type = "login_success"
$login_event.metadata.vendor_name = "Google Workspace"
$login_ip = $login_event.principal.ip
// Ensure the brute force detection and successful login occurred from the same IP
$login_ip = $bruteforce_ip
$target_account = $login_event.target.user.email_addresses
// Ensure the brute force detection occurred before the successful login
$bruteforce_detection.detection.detection_time.seconds < $login_event.metadata.event_timestamp.seconds
match:
$bruteforce_ip over 24h
outcome:
$risk_score = 90
$principal_users = array_distinct($target_account)
condition:
$bruteforce_detection and $login_event
}
搜尋
$bruteforce_detection.detection.detection.rule_name = /Workspace Anomalous Failed Logins/
$bruteforce_ip = $bruteforce_detection.detection.detection.variables["principal_ips"]
$login_event.metadata.product_name = "login"
$login_event.metadata.product_event_type = "login_success"
$login_event.metadata.vendor_name = "Google Workspace"
$login_ip = $login_event.principal.ip
// Ensure the brute force detection and successful login occurred from the same IP
$login_ip = $bruteforce_ip
$target_account = $login_event.target.user.email_addresses
// Ensure the brute force detection occurred before the successful login
$bruteforce_detection.detection.detection_time.seconds < $login_event.metadata.event_timestamp.seconds
match:
$bruteforce_ip over 24h
outcome:
$principal_users = array_distinct($target_account)
condition:
$bruteforce_detection and $login_event
資訊主頁
資訊主頁著重於以圖表呈現原始事件資料,而複合偵測邏輯則會將現有偵測警示與後續事件建立關聯。這項多層分析是針對偵測引擎進行最佳化,而非即時資訊主頁小工具。
情境感知偵測
用途:使用外部威脅情報強化現有偵測功能,確認警告是否涉及已知惡意實體,例如檢查安全偵測中標記的 IP 位址,是否也列在全域 TOR 結束節點威脅動態饋給中。
主要邏輯:使用複合規則,比對 IP 位址等共用屬性,將偵測結果與 GLOBAL_CONTEXT 圖表資料 (例如 Google Cloud 威脅情報 動態饋給) 彙整。
情境感知複合偵測功能會運用額外情境資訊 (例如威脅動態饋給中的 IP 位址),強化偵測結果。
範例:擴充威脅情報
規則
下列複合規則會自動將 TOR 情報動態饋給中的額外背景資訊,新增至現有偵測項目。這項規則會將先前偵測到的 IP 位址與 TOR 結束節點動態饋給建立關聯,提高發現項目的嚴重性和風險評分。
rule composite_tor_enrichment {
meta:
rule_name = "Detection with IP from TOR Feed"
author = "Google Cloud Security"
description = "Adds additional context from the TOR intel feed to detections"
severity = "High"
events:
$rule_name = $d.detection.detection.rule_name
$gcti.graph.metadata.entity_type = "IP_ADDRESS"
$gcti.graph.metadata.vendor_name = "Google Cloud Threat Intelligence"
$gcti.graph.metadata.source_type = "GLOBAL_CONTEXT"
$gcti.graph.metadata.product_name = "GCTI Feed"
$gcti.graph.metadata.feed = "Tor Exit Nodes"
$detection_ip = $d.detection.detection.variables["principal_ips"]
$detection_ip = $gcti.graph.entity.ip
match:
$detection_ip, $rule_name over 1h
outcome:
$risk_score = 80
condition:
$d and $gcti
}
搜尋
$rule_name = $d.detection.detection.rule_name
$gcti.graph.metadata.entity_type = "IP_ADDRESS"
$gcti.graph.metadata.vendor_name = "Google Cloud Threat Intelligence"
$gcti.graph.metadata.source_type = "GLOBAL_CONTEXT"
$gcti.graph.metadata.product_name = "GCTI Feed"
$gcti.graph.metadata.feed = "Tor Exit Nodes"
$detection_ip = $d.detection.detection.variables["principal_ips"]
$detection_ip = $gcti.graph.entity.ip
match:
$detection_ip, $rule_name over 1h
condition:
$d and $gcti
資訊主頁
共現偵測
用途:偵測特定時間範圍內,由相同實體觸發的相關策略組合,例如找出在 48 小時內同時觸發提權偵測和資料竊取偵測的使用者。
主要邏輯:使用匯總形式,在 match 區段內,透過共用實體變數 (例如 $pe_user) 彙整多個不同的偵測類型,藉此建立關聯。
共現複合偵測是一種匯總形式,可偵測相關事件的組合,例如使用者觸發的提權和資料竊取偵測組合。
範例:提權和資料竊取同時發生
規則
下列複合規則會搜尋與同一位使用者相關聯的特定偵測序列或組合 (提權後接著資料竊取),時間範圍為 48 小時。
rule composite_privesc_exfil_sequential {
meta:
rule_name = "Privilege Escalation and Exfiltration Composite"
author = "Google Cloud Security"
description = "Looks for a detection sequence of privilege escalation followed by exfiltration."
severity = "High"
events:
$privilege_escalation.detection.detection.rule_labels["tactic"] = "TA0004"
$exfiltration.detection.detection.rule_labels["tactic"] = "TA0010"
$privesc_user = $privilege_escalation.detection.detection.variables["principal_users"]
$exfil_user = $exfiltration.detection.detection.variables["principal_users"]
$privesc_user = $exfil_user
$privilege_escalation.detection.detection_time.seconds < $exfiltration.detection.detection_time.seconds
match:
$privesc_user over 48h
outcome:
$risk_score = 75
$privesc_rules = array_distinct($privilege_escalation.detection.detection.rule_name)
$exfil_rules = array_distinct($exfiltration.detection.detection.rule_name)
condition:
$privilege_escalation and $exfiltration
}
搜尋
$privilege_escalation.detection.detection.rule_labels["tactic"] = "TA0004"
$exfiltration.detection.detection.rule_labels["tactic"] = "TA0010"
$privesc_user = $privilege_escalation.detection.detection.variables["principal_users"]
$exfil_user = $exfiltration.detection.detection.variables["principal_users"]
$privesc_user = $exfil_user
$privilege_escalation.detection.detection_time.seconds < $exfiltration.detection.detection_time.seconds
match:
$privesc_user over 48h
outcome:
$privesc_rules = array_distinct($privilege_escalation.detection.detection.rule_name)
$exfil_rules = array_distinct($exfiltration.detection.detection.rule_name)
condition:
$privilege_escalation and $exfiltration
資訊主頁
結果和變數管理
本節提供計算風險和正規化資料的範例,供下游使用。
| 主題 | 範例 |
|---|---|
| 結果條件 | 依計算出的風險評分篩選 |
| 單一事件查詢 (含結果) | 時間點嚴重程度標記 |
| 網路型風險評分 | 網路型風險評分規則 |
| 重構多事件邏輯 (重構前) | 結果重構 (重構前) |
| 重構多事件邏輯 (重構後) | 結果重構 (重構後) |
| 函式與預留位置的對應關係 |
含有「outcome」專區的查詢
您可以在 YARA-L 2.0 規則中新增選用的 outcome 區段,擷取每項偵測結果的其他資訊。在 condition 區段中,您也可以指定結果變數的條件。您可以使用偵測規則的 outcome 區段,設定下游使用的變數。例如,您可以根據分析中的事件資料設定嚴重性分數。
如要瞭解詳情,請參考下列資源:
結果條件
用途:根據計算出的風險評分篩選偵測結果,減少雜訊,確保只有高可信度或高嚴重性的事件會觸發快訊。這項功能可用於隱藏不符合特定業務門檻的低風險活動。
主要邏輯:使用條件式數學運算 (例如根據檔案大小或時間增加風險),在 outcome 區段中定義變數,然後在 condition 區段中參照這些變數,以限制偵測。
範例:依計算出的風險評分篩選
規則
在 condition 區段中,您可以使用在 outcome 區段中定義的 outcome 變數。以下範例說明如何篩選風險評分,並使用結果條件減少偵測雜訊。
rule OutcomeConditionalRule {
meta:
author = "alice@example.com"
description = "Rule that uses outcome conditionals"
events:
$u.metadata.event_type = "FILE_COPY"
$u.principal.file.size = $file_size
$u.principal.hostname = $hostname
// 1 = Sunday, 7 = Saturday.
$dayofweek = timestamp.get_day_of_week($u.metadata.collected_timestamp.seconds)
outcome:
$risk_score =
if($file_size > 500*1024*1024, 2) + // Files 500MB are moderately risky
if($file_size > 1024*1024*1024, 3) + // Files over 1G get assigned extra risk
if($dayofweek=1 or $dayofweek=7, 4) + // Events from the weekend are suspicious
if($hostname = /highly-privileged/, 5) // Check for files from highly privileged devices
condition:
$u and $risk_score >= 10
}
搜尋
metadata.event_type = "FILE_COPY"
principal.file.size = $file_size
principal.hostname = $hostname
// 1 = Sunday, 7 = Saturday.
$dayofweek = timestamp.get_day_of_week(metadata.collected_timestamp.seconds)
outcome:
$risk_score =
if($file_size > 500*1024*1024, 2) + // Files 500MB are moderately risky
if($file_size > 1024*1024*1024, 3) + // Files over 1G get assigned extra risk
if($dayofweek=1 or $dayofweek=7, 4) + // Events from the weekend are suspicious
if($hostname = /highly-privileged/, 5) // Check for files from highly privileged devices
資訊主頁
這項查詢會新增 $hostname 結果變數,以視覺化方式呈現與各個風險評分相關聯的主機。
metadata.event_type = "FILE_COPY"
principal.file.size = $file_size
principal.hostname = $hostname
// 1 = Sunday, 7 = Saturday.
$dayofweek = timestamp.get_day_of_week(metadata.collected_timestamp.seconds)
outcome:
$host = $hostname
$risk_score =
if($file_size > 500*1024*1024, 2) + // Files 500MB are moderately risky
if($file_size > 1024*1024*1024, 3) + // Files over 1G get assigned extra risk
if($dayofweek=1 or $dayofweek=7, 4) + // Events from the weekend are suspicious
if($hostname = /highly-privileged/, 5) // Check for files from highly privileged devices
單一事件查詢 (含結果)
用途:根據使用者清單或檔案屬性指派嚴重性標記,不需時間範圍或事件關聯,即可立即提供背景資訊,豐富時間點偵測結果。
關鍵邏輯:在缺少 match 區段的規則中使用 outcome 區段。這樣一來,您就能針對符合條件的每個事件擷取中繼資料,並執行條件邏輯 (例如根據參考清單檢查使用者)。
範例:時間點嚴重性標記
規則
以下範例說明如何在單一事件規則中使用 outcome 區段,設定下游使用的變數,例如根據檔案複製事件中涉及的特定使用者和檔案大小,設定嚴重性分數。
rule OutcomeRuleSingleEvent {
meta:
author = "alice@example.com"
events:
$u.metadata.event_type = "FILE_COPY"
$u.principal.file.size = $file_size
$u.principal.hostname = $hostname
outcome:
$suspicious_host = $hostname
$admin_severity = if($u.principal.user.userid in %admin_users, "SEVERE", "MODERATE")
$severity_tag = if($file_size > 1024, $admin_severity, "LOW")
condition:
$u
}
搜尋
以下範例會找出檔案建立事件,並使用 outcome 區段,為每項結果動態指派嚴重性等級。與多重事件規則不同,這項未匯總搜尋不需要事件變數或 match 區段。而是個別處理每筆記錄,並根據檔案大小和使用者授權,使用自訂邏輯擴充,輸出 1 row per event。
metadata.event_type = "FILE_CREATION"
principal.file.size = $file_size
principal.hostname = $hostname
outcome:
$suspicious_host = $hostname
$admin_severity = if(principal.user.userid in %a1, "SEVERE", "MODERATE")
$severity_tag = if($file_size > 1024, $admin_severity, "LOW")
資訊主頁
這個範例的主要目的是標記及充實個別事件,因此不適用資訊主頁變化版本。雖然資訊主頁可以匯總這些事件 (例如計算每個嚴重性標記的事件總數),但這樣做會模糊未匯總搜尋旨在尋找的精細資料列層級詳細資料。
網路風險評分
用途:計算一組事件的累積網路流量,找出高風險的資料移轉。這樣一來,您就能找出資料門檻總量超過特定限制 (例如 1024 位元組) 的威脅,同時考量相關資產的安全漏洞嚴重性。
主要邏輯:使用 outcome 部分中的 sum() 匯總函式,合併 match 期間內所有事件的 sent_bytes 和 received_bytes。如果是規則,查詢會使用 if 陳述式,在總和超過定義的門檻時套用較高的風險分數。
範例:網路風險評分規則
規則
以下範例說明如何使用 outcome 區段,根據網路活動計算動態風險評分。規則會加總事件群組中傳輸的總位元組數,並為超過特定資料門檻 (1024 位元組) 的相符項目套用較高優先順序,同時考量相關資產的安全漏洞嚴重程度。
rule OutcomeRuleMultiEvent {
meta:
author = "alice@example.com"
events:
$u.udm.principal.hostname = $hostname
$asset_context.graph.entity.hostname = $hostname
$severity = $asset_context.graph.entity.asset.vulnerabilities.severity
match:
$hostname over 5m
outcome:
$total_network_bytes = sum($u.network.sent_bytes) + sum($u.network.received_bytes)
$risk_score = if($total_network_bytes > 1024, 100, 50) +
max(
if($severity = "HIGH", 10)
+ if($severity = "MEDIUM", 5)
+ if($severity = "LOW", 1)
)
$asset_id_list =
array(
if($u.principal.asset_id = "",
"Empty asset id",
$u.principal.asset_id
)
)
$asset_id_distinct_list = array_distinct($u.principal.asset_id)
$asset_id_count = count($u.principal.asset_id)
$asset_id_distinct_count = count_distinct($u.principal.asset_id)
condition:
$u and $asset_context and $risk_score > 50 and not arrays.contains($asset_id_list, "id_1234")
}
搜尋
以下範例示範如何使用搜尋變體,將 UDM 網路事件與實體內容圖 (ECG) 的資產背景資訊建立關聯。這項規則會使用 5 分鐘的 match 視窗,依主機名稱匯總網路流量,並根據資料量和安全漏洞嚴重性計算風險評分,然後套用條件式篩選器,從最終結果集中排除特定資產 ID。
$u.udm.principal.hostname = $hostname
$asset_context.graph.entity.hostname = $hostname
$severity = $asset_context.graph.entity.asset.vulnerabilities.severity
match:
$hostname over 5m
outcome:
$total_network_bytes = sum($u.network.sent_bytes) + sum($u.network.received_bytes)
$risk_score = if($total_network_bytes > 1024, 100, 50) +
max(
if($severity = "HIGH", 10)
+ if($severity = "MEDIUM", 5)
+ if($severity = "LOW", 1)
)
$asset_id_list =
array(
if($u.principal.asset_id = "",
"Empty asset id",
$u.principal.asset_id
)
)
$asset_id_distinct_list = array_distinct($u.principal.asset_id)
$asset_id_count = count($u.principal.asset_id)
$asset_id_distinct_count = count_distinct($u.principal.asset_id)
condition:
$u and $asset_context and $risk_score > 50 and not arrays.contains($asset_id_list, "id_1234")
資訊主頁
以下範例說明如何使用資產安全漏洞資料,強化即時網路遙測資料的資訊主頁。這項查詢會比對 5 分鐘滑動時間範圍內的主機名稱,讓開發人員建立資訊主頁小工具,以視覺化方式呈現資產風險等級。這項邏輯會根據網路處理量和資產上發現的最嚴重安全漏洞,動態調整風險評分,並優先顯示可能遭入侵的系統。
$u.udm.principal.hostname = $hostname
$asset_context.graph.entity.hostname = $hostname
$severity = $asset_context.graph.entity.asset.vulnerabilities.severity
match:
$hostname over 5m
outcome:
$total_network_bytes = sum($u.network.sent_bytes) + sum($u.network.received_bytes)
$risk_score = if($total_network_bytes > 1024, 100, 50) +
max(
if($severity = "HIGH", 10)
+ if($severity = "MEDIUM", 5)
+ if($severity = "LOW", 1)
)
$asset_id_list =
array(
if($u.principal.asset_id = "",
"Empty asset id",
$u.principal.asset_id
)
)
$asset_id_distinct_list = array_distinct($u.principal.asset_id)
$asset_id_count = count($u.principal.asset_id)
$asset_id_distinct_count = count_distinct($u.principal.asset_id)
condition:
$u and $asset_context and $risk_score > 50 and not arrays.contains($asset_id_list, "id_1234")
重構多事件outcome規則 (重構前)
用途:將多事件規則轉換為單一事件規則,提升系統效能並縮短處理延遲時間。如果規則原本只設計比對區段來啟用結果區段,但實際上不需要跨多個不同事件進行關聯,就適合使用這種方式。
主要邏輯:從 outcome 區段中移除 match 區段和任何匯總函式 (例如 max()、sum() 或 count())。這項轉換會將規則從依時間分組事件,改為在每個事件送達時個別評估。
match 區段) 和多重事件規則 (含有 match 區段的規則)。
outcome 區段可用於單一事件規則 (沒有 區段的規則) 和多事件規則。
如果您先前設計的規則是多事件規則,只是為了
使用結果區段,則可以選擇刪除
match 區段來重構這些規則,以提升效能。請注意,由於規則不再有套用分組的 match 區段,您可能會收到更多偵測結果。
範例:結果重構 (重構前)
規則
以下範例顯示僅使用一個事件變數的多事件結果規則。由於規則引擎使用 match 區段,因此必須先將 5 分鐘時間範圍內的事件分組,再計算結果,這比單一事件評估消耗更多資源。
rule OutcomeMultiEventPreRefactor {
meta:
author = "alice@example.com"
description = "Outcome refactor rule, before the refactor"
events:
$u.udm.principal.hostname = $hostname
match:
$hostname over 5m
outcome:
$risk_score = max(if($hostname = "my-hostname", 100, 50))
condition:
$u
}
搜尋
統計資料查詢對等項目
events:
$u.udm.principal.hostname = $hostname
match:
$hostname over 5m
outcome:
$risk_score = max(if($hostname = "my-hostname", 100, 50))
condition:
$u
資訊主頁
events:
$u.udm.principal.hostname = $hostname
match:
$hostname over 5m
outcome:
$risk_score = max(if($hostname = "my-hostname", 100, 50))
condition:
$u
重構多事件 outcome 規則 (重構後)
用途:完成查詢最佳化,提高處理速度。移除分組條件後,只要有單一相符事件,查詢就會立即觸發偵測,規則引擎的效率也大幅提升。
主要邏輯:刪除 match 區段,並從 outcome 變數指派中移除 aggregate 函式 (例如 max())。if 陳述式中的邏輯維持不變,但現在會套用至單一事件,而非群組。
您可以刪除 match 區段,重構查詢。注意:由於查詢現在是單一事件,因此您也必須移除 outcome 區段中的匯總。如要進一步瞭解匯總,請參閱「結果匯總」。
範例:Outcome refactor
規則
rule OutcomeSingleEventPostRefactor {
meta:
author = "alice@example.com"
description = "Outcome refactor rule, after the refactor"
events:
$u.udm.principal.hostname = $hostname
// We deleted the match section.
outcome:
// We removed the max() aggregate.
$risk_score = if($hostname = "my-hostname", 100, 50)
condition:
$u
}
搜尋
events:
$u.udm.principal.hostname = $hostname
outcome:
$risk_score = if($hostname = "my-hostname", 100, 50)
資訊主頁
events:
$u.udm.principal.hostname = $hostname
outcome:
$risk_score = if($hostname = "my-hostname", 100, 50)
函式與預留位置的對應關係
用途:正規化資料 (例如標準化電子郵件網域),確認比對部分的群組正確無誤。
主要邏輯:將 re.capture() 或 strings.concat() 的結果指派給預留位置變數。
範例:函式對預留位置變數指派
您可以將預留位置變數指派給函式呼叫的結果,並在規則的其他部分使用預留位置變數,例如 match 部分、outcome 部分或 condition 部分。
規則
rule FunctionToPlaceholderRule {
meta:
author = "alice@example.com"
description = "Rule that uses function to placeholder assignments"
events:
$u.metadata.event_type = "EMAIL_TRANSACTION"
// Use function-placeholder assignment to extract the
// address from an email.
// address@website.com -> address
$email_to_address_only = re.capture($u.network.email.to , "(.*)@")
// Use function-placeholder assignment to normalize an email:
// address@-> address@company.com
$email_from_normalized = strings.concat(
re.capture($u.network.email.from , "(.*)@"),
"@company.com"
)
// Use function-placeholder assignment to get the day of the week of the event.
// 1 = Sunday, 7 = Saturday.
$dayofweek = timestamp.get_day_of_week($u.metadata.event_timestamp.seconds)
match:
// Use placeholder (from function-placeholder assignment) in match section.
// Group by the normalized from email, and expose it in the detection.
$email_from_normalized over 5m
outcome:
// Use placeholder (from function-placeholder assignment) in outcome section.
// Assign more risk if the event happened on weekend.
$risk_score = max(
if($dayofweek = 1 or $dayofweek = 7, 10, 0)
)
condition:
// Use placeholder (from function-placeholder assignment) in condition section.
// Match if an email was sent to multiple addresses.
#email_to_address_only > 1
}
搜尋
metadata.event_type = "EMAIL_TRANSACTION"
// Use function-placeholder assignment to extract the
// address from an email.
// address@website.com -> address
$email_to_address_only = re.capture(network.email.from , "(.*)@")
// Use function-placeholder assignment to normalize an email:
// address@??? -> address@company.com
$email_from_normalized = strings.concat(
re.capture(network.email.to , "(.*)@"),
"@company.com"
)
// Use function-placeholder assignment to get the day of the week of the event.
// 1 = Sunday, 7 = Saturday.
$dayofweek = timestamp.get_day_of_week(metadata.event_timestamp.seconds)
match:
// Use placeholder (from function-placeholder assignment) in match section.
// Group by the normalized from email, and expose it in the detection.
$email_from_normalized over 5m
outcome:
// Use placeholder (from function-placeholder assignment) in outcome section.
// Assign more risk if the event happened on weekend.
$risk_score = max(
if($dayofweek = 1 or $dayofweek = 7, 10, 0)
)
condition:
// Use placeholder (from function-placeholder assignment) in condition section.
// Match if an email was sent to multiple addresses.
#email_to_address_only > 1
資訊主頁
以下範例說明經過最佳化調整、適合時間序列視覺化的資訊主頁變化版本。這項查詢使用一天的滾動式時間區間,而非分鐘層級的精細程度,因此能產生穩定、不重疊的資料點,非常適合用來繪製長期風險評分圖表。這項邏輯會正規化電子郵件實體,並對週末交易套用較高的風險權重,提供清楚的每日可疑電子郵件活動趨勢,以便長期監控。
metadata.event_type = "EMAIL_TRANSACTION"
// Use function-placeholder assignment to extract the
// address from an email.
// address@website.com -> address
$email_to_address_only = re.capture(network.email.from , "(.*)@")
// Use function-placeholder assignment to normalize an email:
// address@??? -> address@company.com
$email_from_normalized = strings.concat(
re.capture(network.email.to , "(.*)@"),
"@company.com"
)
// Use function-placeholder assignment to get the day of the week of the event.
// 1 = Sunday, 7 = Saturday.
$dayofweek = timestamp.get_day_of_week(metadata.event_timestamp.seconds)
match:
// Use placeholder (from function-placeholder assignment) in match section.
// Group by the normalized from email, and expose it in the detection.
$email_from_normalized over 5m
outcome:
// Use placeholder (from function-placeholder assignment) in outcome section.
// Assign more risk if the event happened on weekend.
$risk_score = max(
if($dayofweek = 1 or $dayofweek = 7, 10, 0)
)
condition:
// Use placeholder (from function-placeholder assignment) in condition section.
// Match if an email was sent to multiple addresses.
#email_to_address_only > 1
最佳化和篩選
如要有效最佳化規則,必須精準篩選資料,確保偵測引擎只處理有意義的資訊。排除「雜訊」或不完整的資料,可大幅提升規則成效,並確保產生的警示可做為行動依據。
| 主題 | 範例 |
|---|---|
| 排除零值 | 明確和隱含的零值排除 |
排除零值
用途:明確篩除空字串、空值或一般預留位置帳戶 (例如「訪客」),確保規則準確度並減少偽陽性,因為這些帳戶不會提供可做為行動依據的安全性資料。
主要邏輯:利用規則引擎的隱含篩選功能,篩除 match 區段中變數的零值,並對其他事件欄位使用明確的不等運算子 (!= ""),確保只有填入資料時才會觸發偵測。
規則引擎會隱含篩除 match 區段中所有預留位置的零值。使用 allow_zero_values 選項即可停用。不過,除非您明確指定這類條件,否則系統不會排除其他參照事件欄位的零值。詳情請參閱「比對區段中的零值」。
範例:明確和隱含零值排除
規則
rule ExcludeZeroValues {
meta:
author = "alice@example.com"
events:
$e1.metadata.event_type = "NETWORK_DNS"
$e1.principal.hostname = $hostname
// $e1.principal.user.userid may be empty string.
$e1.principal.user.userid != "Guest"
$e2.metadata.event_type = "NETWORK_HTTP"
$e2.principal.hostname = $hostname
// $e2.target.asset_id cannot be empty string as explicitly specified.
$e2.target.asset_id != ""
match:
// $hostname cannot be empty string. The rule behaves as if the
// predicate, `$hostname != ""` was added to the events section, because
// `$hostname` is used in the match section.
$hostname over 1h
condition:
$e1 and $e2
}
搜尋
您必須明確指出 hostname 不得為空字串,因為 match 區段中的預留位置沒有隱含的零值篩選器。
$e1.metadata.event_type = "NETWORK_DNS"
$e1.principal.hostname = $hostname
// $e1.principal.user.userid may be empty string.
$e1.principal.user.userid != "Guest"
$e2.metadata.event_type = "NETWORK_HTTP"
$e2.principal.hostname = $hostname
// $e2.target.asset_id and hostname cannot be empty string as explicitly specified.
$e2.target.asset_id != ""
$hostname != ""
match:
$hostname over 1h
資訊主頁
您必須明確指出 hostname 不得為空字串,因為 match 區段中的預留位置沒有隱含的零值篩選器。
$e1.metadata.event_type = "NETWORK_DNS"
$e1.principal.hostname = $hostname
// $e1.principal.user.userid may be empty string.
$e1.principal.user.userid != "Guest"
$e2.metadata.event_type = "NETWORK_HTTP"
$e2.principal.hostname = $hostname
// $e2.target.asset_id and hostname cannot be empty string as explicitly specified.
$e2.target.asset_id != ""
$hostname != ""
match:
$hostname over 1h
還有其他問題嗎?向社群成員和 Google SecOps 專業人員尋求解答。