Este documento descreve as práticas recomendadas recomendadas das Operações de segurança do Google para escrever regras em YARA-L.
Filtrar valores zero
Os campos podem ser omitidos automaticamente nos eventos em que você executa as regras. Quando os campos são omitidos, eles usam os valores zero padrão.
Por exemplo, um valor de string omitido tem como padrão "".
Se você igualar dois campos omitidos, eles poderão ser padronizados com valores zero. Isso pode levar a correspondências não intencionais em que dois campos correspondem porque ambos têm valores zero. Para evitar esse comportamento, especifique explicitamente o valor zero.
Por exemplo, se você tiver uma regra que equipara dois eventos com base em dois campos, há uma chance de que ambos estejam vazios, causando uma correspondência:
$e1.field1 = $e2.field2
Se e1.field1 e e2.field2 forem omitidos nos dados, "" = "" será verdadeiro, causando uma correspondência.
As expressões de comparação a seguir garantem que você não receba uma correspondência porque e1.field1 e e2.field2 não incluem dados:
$e1.field1 = $e2.field2
$e1.field != ""
Valores zero e regras dependentes de enriquecimento
No exemplo a seguir, os endereços IP de cada evento da UDM são verificados em relação à lista de referência, consumindo muitos recursos:
events:
// For every UDM event, check if the target.ip is listed in
// the suspicious_ip_addresses reference list.
$e.target.ip in %suspicious_ip_addresses
Se a regra da YARA-L detectar apenas eventos do UDM de um determinado tipo, adicione um filtro de tipo de evento para otimizar a regra, reduzindo o número de eventos que ela precisa avaliar.
events:
// For every UDM event of type NETWORK_DNS, check if the target.ip is
// listed in the suspicious_ip_addresses reference list.
$e.metadata.event_type = "NETWORK_DNS"
$e.target.ip in %suspicious_ip_addresses
Adicione esses filtros ao início da seção de eventos. Também é recomendável colocar filtros de igualdade antes de regex ou outras comparações. Os filtros são aplicados na ordem em que aparecem na regra.
Para ver blogs da comunidade sobre como trabalhar com YARA-L, consulte:
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-08-21 UTC."],[[["\u003cp\u003eYARA-L rules in Google Security Operations should filter out zero values to avoid unintended matches caused by omitted fields defaulting to zero.\u003c/p\u003e\n"],["\u003cp\u003eWhen creating rules that depend on enriched data, it's crucial to implement null checks to handle potential zero or null values before the enrichment process is complete.\u003c/p\u003e\n"],["\u003cp\u003eAdding event type filters to the beginning of the events section in YARA-L rules optimizes performance by reducing the number of events that the rule needs to evaluate.\u003c/p\u003e\n"],["\u003cp\u003eEquality filters should be placed before regex or other comparisons in the events section, as filters are applied in the order they appear.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Security Operations has a large community library for YARA-L rules.\u003c/p\u003e\n"]]],[],null,["# YARA-L best practices\n=====================\n\nSupported in: \nGoogle secops [SIEM](/chronicle/docs/secops/google-secops-siem-toc)\n\nThis document describes Google Security Operations's recommended best practices for writing rules in YARA-L.\n\nFilter out zero values\n----------------------\n\nFields might be automatically omitted in the events you run your rules against. When fields are omitted, they default to their zero values.\n\nFor example, an omitted string value defaults to `\"\"`.\n\nIf you equate two fields that are both omitted, they might both default to their zero values. This might lead to unintended matches where two fields match because they both have zero values. You can avoid this behavior by explicitly specifying the zero value.\n\nFor example, if you have a rule that equates two events based on two fields, there is a chance that both of those fields are empty, causing a match: \n\n $e1.field1 = $e2.field2\n\nIf both `e1.field1` and `e2.field2` are omitted in the data, `\"\" = \"\"` is true, causing a match.\n\nThe following comparison expressions ensure that you don't get a match because `e1.field1` and `e2.field2` don't include any data: \n\n $e1.field1 = $e2.field2\n $e1.field != \"\"\n\n### Zero values and enrichment-dependent rules\n\nIf a rule depends on enriched data it hasn't been updated yet, the value might be null or zero.\nTherefore, it is good practice to filter out zero values (null checks) on enrichment-dependent rules. Learn [how Google SecOps enriches event and entity data](/chronicle/docs/event-processing/data-enrichment) and [how to use context-enriched data in rules](/chronicle/docs/detection/use-enriched-data-in-rules).\n| **Note:** Zero values are filtered out from match variables by default. For more information, see [zero value handling in the match section](/chronicle/docs/detection/yara-l-2-0-syntax#zero_value_handling_in_the_match_section).\n\nAdd an event type filter\n------------------------\n\nIn the following example, the IP addresses for each UDM event are checked against\nthe reference list, consuming a lot of resources: \n\n events:\n // For every UDM event, check if the target.ip is listed in\n // the suspicious_ip_addresses reference list.\n $e.target.ip in %suspicious_ip_addresses\n\nIf your YARA-L rule only detects on UDM events of a certain event type, adding an event type filter can help to optimize your rule by reducing the number of events the rule needs to evaluate. \n\n events:\n // For every UDM event of type NETWORK_DNS, check if the target.ip is\n // listed in the suspicious_ip_addresses reference list.\n $e.metadata.event_type = \"NETWORK_DNS\"\n $e.target.ip in %suspicious_ip_addresses\n\nAdd these filters to the beginning of the events section. You should also put equality filters before regex or other comparisons. Filters are applied in the order they appear in the rule.\n\nFor Community blogs on working with YARA-L, see:\n\n- [YARA-L basics](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-know-Google-SecOps-SIEM-YARA-L-basics/ta-p/635468)\n- [YARA-L rule variables](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-know-Google-SecOps-SIEM-YARA-L-rule-variables/ta-p/635481)\n- [YARA-L operators and modifiers](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-know-Google-SecOps-SIEM-YARA-L-operators-and/ta-p/635489)\n- [Building a single event rule using a regular expression](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-SIEM-Building-a-single-event-rule/ta-p/635498)\n- [Aggregating events in rules](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-SIEM-Aggregating-events-in-rules/ta-p/635507)\n- [Setting a threshold in conditions](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-SIEM-Setting-a-threshold-in/ta-p/635521)\n- [Rules editor navigation](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Chronicle-SIEM-Rules-Editor-Navigation/ta-p/659309)\n- [YARA-L Rule Options](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-SIEM-YARA-L-Rule-Options/ta-p/659313)\n- [Building a Single Event Rule - String Match](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Chronicle-SIEM-Building-a-Single-Event-Rule/ta-p/659317)\n- [Building a Multi Event Rule - Joining Events](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Chronicle-Building-a-Multi-Event-Rule-Joining/ta-p/665140)\n- [Building a Multi Event Rule - Ordering Events](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Building-a-Multi-Event-Rule/ta-p/677993)\n- [Building a Multi Event Rule - Multiple Joins and Counts in Conditions](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Chronicle-Building-a-Multi-Event-Rule-Multiple/ta-p/683984)\n- [Building a Multi Event Rule - Sliding Windows](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Building-a-Multi-Event-Rule/ta-p/693405)\n- [Introducing Outcomes in a Single Event Rule](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Chronicle-Introducing-Outcomes-in-a-Single-Event/ta-p/699600)\n- [Outcomes in a Multi Event Rule - Counts](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Outcomes-in-a-Multi-Event-Rule/ta-p/703096)\n- [Outcomes in Multi Event Rules - Arrays](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-SIEM-Outcomes-in-Multi-Event-Rules/ta-p/705657)\n- [Outcomes in a Multi Event Rule - Max, Min, Sum](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Outcomes-in-a-Multi-Event-Rule-Max/ta-p/709795)\n- [Outcomes - Risk Score, Conditional Logic and Mathematical Operators](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Chronicle-Outcomes-Risk-Score-Conditional-Logic/ta-p/714517)\n- [Functions - strings.concat](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Functions-strings-concat/ta-p/719344)\n- [Functions - strings.coalesce](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Functions-strings-coalesce/ta-p/726303)\n- [Functions - Network](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Functions-Network/ta-p/732001)\n- [Reference List](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Reference-List/ta-p/738238)\n- [CIDR Reference Lists](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-CIDR-Reference-Lists/ta-p/745365)\n- [Regex Reference Lists](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Regex-Reference-Lists/ta-p/750396)\n- [Strings Function - Upper or Lower Case](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Strings-Function-Upper-Lower-Case/ta-p/750408)\n- [Regular Expression Function - re.regex](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Regular-Expression-Function-re/ta-p/768660)\n- [Regular Expression Function - re.capture](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Regular-Expression-Function-re/ta-p/775278)\n- [String Function - strings.base64_decode](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-String-Function-strings-base64/ta-p/783968)\n- [Regular Expression Function - re.replace](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Regular-Expression-Function-re/ta-p/791965)\n- [Getting started with Statistical Search](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Getting-Started-with-Statistical/ta-p/799951)\n- [Statistical Search - More Than a Count](https://www.googlecloudcommunity.com/gc/Google-Security-Operations-Best/Getting-to-Know-Google-SecOps-Statistical-Search-More-Than-a/ta-p/803150)\n\n**Need more help?** [Get answers from Community members and Google SecOps professionals.](https://security.googlecloudcommunity.com/google-security-operations-2)"]]