Post

Mastering Yara: How To Effective And High Perfomance Rules

Introduction

In the rapidly evolving landscape of cybersecurity, the ability to accurately identify and classify malicious activity is paramount. YARA has established itself as the gold standard for pattern matching and rule-based detection, providing security analysts with a powerful framework to defend against sophisticated threats. While mastering the basic syntax is the foundation, the true strength of YARA lies in the development of sophisticated, performant, and resilient detection logic. This section focuses on these advanced aspects, exploring the core principles and best practices for writing effective YARA rules that not only maximize detection accuracy but also ensure optimal performance in demanding production environments.

Understanding malware analysis and YARA rule creation

Understanding malware analysis and YARA rule creation is essential for writing effective YARA rules. In this section, we’ll cover the basics of malware analysis and how to use YARA to create rules that can detect and classify malware.

Malware analysis is the process of examining a piece of software (usually malicious) to determine its behavior and functionality. Malware analysts use various techniques to analyze malware, including static analysis (examining the code without executing it), dynamic analysis (running the code in a controlled environment and monitoring its behavior), and hybrid analysis (a combination of static and dynamic analysis).

YARA is a tool that is commonly used in malware analysis to identify and classify malware. It uses rules that define patterns and characteristics of malware, and can be used to detect known malware or to identify new and unknown malware.

To create effective YARA rules, it’s important to understand the behavior and characteristics of malware. This can involve researching the latest malware trends and techniques, analyzing malware samples, and understanding the types of attacks that are most common. Once you have a good understanding of the malware landscape, you can begin to create YARA rules that can detect and classify malware.

YARA rules are based on patterns and characteristics of malware, such as strings that are commonly used in malware code, specific file names or paths, or unique features of a malware’s behavior. YARA also supports regular expressions, which can be used to define more complex patterns.

When creating YARA rules, it’s important to follow best practices for rule creation, such as using descriptive names for rules, avoiding overly complex rules, and testing and validating rules before deploying them in a production environment. YARA also supports modular rules, which can be used to break up large rules into smaller, more manageable components.

Why are automatic rule generators ineffective?

Automatic YARA rule generators can be useful in some situations, but they may not be as effective as manually crafted YARA rules. Here are some reasons why:

  1. Lack of Context: Automatic YARA rule generators work based on patterns or signatures found in a sample. However, they often lack the contextual information that is necessary to create effective YARA rules. For example, they may not take into account the specific environment, operating system, and software configurations, which can lead to false positives.
  2. Limited Scope: Automatic YARA rule generators are limited to the specific patterns and characteristics that are included in their algorithm. They may not be able to identify more complex or less common characteristics of malware.
  3. Higher False Positive Rate: Automatic YARA rule generators may generate rules that are too broad, leading to a high number of false positives. False positives can be problematic as they can waste security analysts’ time and resources, leading to a reduction in the effectiveness of the security program.
  4. Lack of Customization: Automatic YARA rule generators do not take into account the specific needs and context of the organization. Effective YARA rules must be customized to the specific environment in which they will be deployed, taking into account factors such as the types of applications and systems being used, the threat landscape, and the organization’s risk tolerance.
  5. Lack of Human Expertise: Automatic YARA rule generators are not able to replicate the intuition and experience of a human malware analyst. Experienced analysts can detect more subtle and complex characteristics of malware that an automatic YARA rule generator may not be able to identify.

Manual vs Generator

Let’s look at the difference between a manually written YARA rule and a generator-written YARA rule.

The image below shows a YARA rule created by the YarGen tool.

YarGen Rule

As you can see, the automatically generated YARA rule contains very complex and very specific strings. This causes the YARA rule to detect only one sample.

The image below shows a manually written YARA rule using the same sample.

Manual Rule

As you can see, the manually written YARA rule is much simpler and more likely to detect other samples in the same family.

Let’s take a look at the difference between these two YARA rules by using the unpac.me platform’s YARA hunt service.

In the image below, the YARA rule created with the YarGen tool is scanned.

Auto YARA Hunt

When the YARA rule written with the YarGen tool was scanned, only one sample was detected. The sample used to create the YARA rule on the detected sample.

In the image below, the manually written YARA rule is scanned.

Manual YARA Hunt

When the manual YARA rule is scanned, it is seen that it detects more than one sample. This confirms that the manually written rule is much more effective and more suitable for sample hunting.

Best practices for writing effective YARA rules

Writing effective YARA rules requires following best practices for rule creation. In this section, we’ll cover some of the most important best practices to keep in mind when creating YARA rules.

  1. Use descriptive names: When creating YARA rules, use descriptive names that make it easy to understand what the rule does. Avoid using generic or overly complex names that may be difficult to interpret.
  2. Keep rules simple: YARA rules should be simple and focused on specific patterns or characteristics of malware. Avoid creating overly complex rules that may be difficult to read or understand.
  3. Use opcodes: When analyzing malware, only functions specific to that malware can be found. This function may be a decryption algorithm, if there is a 3rd party library it uses, it may be the function it uses, it may be the code block it uses to communicate with C2, etc. Using these functions in the YARA rule will expand the scope of the YARA rule and detect other samples of the same malware.
  4. Use regular expressions: Regular expressions can be used to define more complex patterns in YARA rules. Learn how to use regular expressions effectively to create more powerful and flexible rules.
  5. Test and validate rules: Before deploying YARA rules in a production environment, it’s important to test and validate them. Use test samples that are known to contain the malware you’re looking for, and make sure the rules are detecting the malware correctly.
  6. Avoid false positives: False positives can be a problem with YARA rules, so it’s important to avoid creating rules that are too broad and may generate false positives. Use specific patterns and characteristics of malware to create more targeted rules that minimize false positives.
  7. Optimize rules for performance: YARA rules can be resource-intensive, so it’s important to optimize them for performance. Use modular rules, break up large rules into smaller components, and use variables and functions to make rules more efficient.
  8. Stay up-to-date on malware trends: The malware landscape is constantly evolving, so it’s important to stay up-to-date on the latest malware trends and techniques. This can help you create more effective YARA rules that are better able to detect and prevent new and emerging malware threats.

While preparing the YARA rules, Brandefense Threat Research Team pays attention to these items, and tests the YARA rules on platforms such as unpac.me, Yaraify, hybrid analysis and makes them the most effective.

Rule testing and validation

Testing and validation are important steps in the YARA rule creation process, as they help ensure that rules are accurate, effective, and do not produce false positives. Here are some best practices for testing and validating YARA rules:

  1. Test on Known Samples: One of the best ways to test YARA rules is to use them on known samples of malware. This allows you to verify that the rules are correctly identifying the malware, and that they are not producing false positives. You can also use this opportunity to fine-tune your rules to make them more effective.
  2. Use Multiple Samples: To ensure that your rules are effective against a range of malware, it’s a good idea to test them on multiple samples. This helps identify any patterns or characteristics that may be unique to a particular sample, and allows you to create more general rules that can be applied across a range of malware.
  3. Verify False Positive Rate: It’s important to test YARA rules for false positives, as these can be time-consuming to investigate and can reduce the effectiveness of your security program. To do this, run your rules against a set of benign samples and ensure that they do not produce any alerts.
  4. Check for Rule Conflicts: If you have multiple YARA rules in your environment, it’s possible that they may conflict with each other and produce false positives. To prevent this, test your rules in combination with other rules to identify any conflicts.
  5. Validate Against Real-World Data: Once you have tested your YARA rules on known samples and benign files, it’s a good idea to validate them against real-world data. This allows you to see how your rules perform in the context of your environment and the current threat landscape.

Sample hunting with YARA

Although YARA rules are used to classify a file, it also allows sample hunting thanks to the services of some platforms.

Thanks to YARA rules, we can improve the sample pool and write more effective YARA rules.

unpac.me

unpac.me is a platform that unpacks packaged malware. It supports 32 bit files.

Runs the YARA rule given by the YaraHunt service on the samples on the platform and brings the matching files.

In the image below, the YaraHunt service is given. The YaraHunt service is used both to validate YARA rules and to enlarge the sample pool.

unpac.me platform

unpac.me result

unpac.me

YARAify

YARAify is a project from abuse.ch that allows anyone to scan suspicious files such as malware samples or process dumps against a large repository of YARA rules. With YARAhub, the platform also provides a structured way for sharing YARA rules with the community.

YARAhub is an initative of abuse.ch for sharing YARA rules with the community in a structured way. You can download a specific rule or all public available YARA rules that are classified as TLP:WHITE.

YARAhub

It scans the given YARA rule on the samples on the platform and returns the matching samples.

YARAify

Hybrid Analysis

Hybrid Analysis is a platform that can perform both malware analysis and query on malware on the platform.

Runs the YARA rule given with the Advanced Search service on the samples on the platform. Returns the matching files.

Hybrid Analysis

Hybrid Analysis

Config extraction with YARA

YARA is not just about file classification or sample hunting. For example, the configuration used by a malware can be extracted using the YARA rule. In the link given below, there is a blog post prepared by our teammate.

Config Extraction

Configuration Extraction with YARA

Conclusion

In conclusion, YARA is a powerful tool for malware analysis and detection, and its rules-based approach allows for the creation of highly specific and effective detection rules. However, creating effective YARA rules requires a deep understanding of malware characteristics and behavior, as well as knowledge of YARA syntax and rule optimization techniques. Best practices for YARA rule creation include testing and validation against known samples, multiple samples, benign files, and real-world data, as well as verifying false positive rates and checking for rule conflicts. By following these best practices, organizations can improve the efficiency of their security programs and better protect themselves against malware threats.

This post is licensed under CC BY 4.0 by the author.