YARA-L 2.0 總覽

支援的國家/地區:

YARA-L 2.0 是 Google Security Operations 的專用語言,可處理企業記錄資料,讓安全專業人員探索資料、調查威脅及建構偵測規則。

本文將說明 YARA-L 及其語法,並展示如何使用 YARA-L 撰寫各種內容,從基本篩選查詢到尋找複雜模式的規則皆可。在 YARA-L 查詢中使用區段,支援匯總函式、條件邏輯,並透過聯結、模式比對等方式新增內容。

YARA-L 2.0 語法總覽

如要建立 YARA-L 規則或查詢,建議您先熟悉並瞭解指定變數宣告、定義和用法的結構和語法。

規則結構

YARA-L 規則包含下列部分,且必須按照下列順序指定:

訂單 區段 規則 搜尋/資訊主頁 說明
1 meta 必填 不適用 說明規則,可包含作者、嚴重程度、說明和優先順序等值。請參閱中繼資料區段語法
2 事件 必填 必填 定義如何篩選及聯結事件。請參閱事件部分語法
3 相符項目 選用 選用 指定匯總結果時要依據哪些欄位分組。請參閱「比對區段語法」。

注意:如果排除 match 區段,規則可以比對單一事件。
4 結果 選用 選用 定義規則執行或觸發時要輸出的資料。請參閱「結果」部分的語法
5 條件 必填 選用 包含判斷是否觸發規則的邏輯。請參閱「條件部分語法」。
6 選項 選用 選用 允許啟用或停用特定規則行為。請參閱選項區段語法

以下範例說明規則的一般結構:

rule <rule name>
{
    meta:
    // Stores arbitrary key-value pairs of the rule details, such as who wrote
    // it, what it detects on, version control, etc.

  events:
    // Defines which events to filter and the relationship between events.

  match:
    // Values to return when matches are found.

  outcome:
    // Define the output of each rule and security alert.

  condition:
    // Condition to check events and the variables used to find matches.

  options:
    // Options to turn on or off while executing this rule. The `options` syntax is only valid for rules.
}

建立規則時,Google SecOps 會對 YARA-L 語法執行型別檢查,並顯示錯誤,協助您修正規則,確保規則正常運作。以下範例顯示使用無效語法時出現的錯誤:

// $e.target.port is of type integer which cannot be compared to a string.
$e.target.port = "80"

// "LOGIN" is not a valid event_type enum value.
$e.metadata.event_type = "LOGIN"

這個規則範例會找出使用者在 10 分鐘內連續登入失敗 5 次的情況。

rule failed_logins
{
  meta:
   author = "Security Team"
   description = "Detects multiple failed user logins within 10-minute windows."
   severity = "HIGH"

  events:
   $e.metadata.event_type = "USER_LOGIN"
   $e.security_result.action = "FAIL"
   $user = $e.target.user.userid

  match:
   $user over 10m

  outcome:
   $failed_login_count = count($e.metadata.id)
   $first_fail_time = min($e.metadata.event_timestamp.seconds)

  condition:
    #e >= 5
}

規則定義如下:

  • meta」部分定義了規則作者 (安全性團隊)、說明 (在 10 分鐘內偵測到多筆使用者登入失敗記錄),以及嚴重程度 (高)。

偵測 10 分鐘內多次登入失敗的使用者

  • events 區段定義必須追蹤的事件:使用者登入、使用者登入失敗 (事件變數),以及與 user 比對變數 (預留位置變數) 的連結。

  • outcome 部分定義要對事件和預留位置變數執行的計算:計算登入失敗次數和第一次失敗的時間。

  • match 部分會定義用來將事件分組的變數 ($user),以及這些事件必須發生在的時間範圍 (10m),才會視為相符。

  • condition 區段指定只傳回登入失敗次數超過五次的使用者。

後續步驟

其他資訊

還有其他問題嗎?向社群成員和 Google SecOps 專業人員尋求答案。