TryHackMe: Yara
What is Yara? All about Yara "The pattern matching swiss knife for malware researchers (and everyone else)" (Virustotal., 2020) With such a fitting quote, Yara can identify information based on both binary and textual patterns, such as hexadecimal and strings contained within a file. Rules are used to label these patterns. For example, Yara rules are frequently written to determine if a file is malicious or not, based upon the features - or patterns - it presents. Strings are a fundamental component of programming languages. Applications use strings to store data such as text. For example, the code snippet below prints "Hello World" in Python. The text "Hello World" would be stored as a string. print("Hello World!") We could write a Yara rule to search for "hello world" in every program on our operating system if we would like. Why does Malware use Strings? Malware, just like our "Hello World" application, uses strings to store textual data. Here are a few examples of the data that various malware types store within strings: Type Data Description Ransomware 12t9YDPgwueZ9NyMgw519p7AA8isjr6SMw Bitcoin Wallet for ransom payments Botnet 12.34.56.7 The IP address of the Command and Control (C&C) server Introduction to Yara Rules Using a Yara rule is simple. Every yara command requires two arguments to be valid, these are: The rule file we create. Name of file, directory, or process ID to use the rule for. yara myrule.yar somedirectory Expanding on Yara Rules Checking whether or not a file exists isn't all that helpful. After all, we can figure that out for ourselves...Using much better tools for the job. Yara has a few conditions, which I encourage you to read here at your own leisure. However, I'll detail a few below and explain their purpose. Keyword Desc Meta Strings Conditions Weight Meta This section of a Yara rule is reserved for descriptive information by the author of the rule. For example, you can use desc, short for description, to summarise what your rule checks for. Anything within this section does not influence the rule itself. Similar to commenting code, it is useful to summarise your rule. Strings Remember our discussion about strings in Task 2? Well, here we go. You can use strings to search for specific text or hexadecimal in files or programs. For example, say we wanted to search a directory for all files containing "Hello World!", we would create a rule such as below: rule helloworld_checker{ strings: $hello_world = "Hello World!" } We define the keyword Strings where the string that we want to search, i.e., "Hello World!" is stored within the variable $hello_world Of course, we need a condition here to make the rule valid. In this example, to make this string the condition, we need to use the variable's name. In this case,$hello_world: rule helloworld_checker{ strings: $hello_world = "Hello World!" condition: $hello_world } Essentially, if any file has the string "Hello World!" then the rule will match. However, this is literally saying that it will only match if "Hello World!" is found and will not match if "hello world" or "HELLO WORLD." To solve this, the condition any of them allows multiple strings to be searched for, like below: rule helloworld_checker{ strings: $hello_world = "Hello World!" $hello_world_lowercase = "hello world" $hello_world_uppercase = "HELLO WORLD" condition: any of them } Now, any file with the strings of: 1. Hello World! 2. hello world 3. HELLO WORLD Will now trigger the rule. Conditions We have already used the true and any of them condition. Much like regular programming, you can use operators such as: = (more than or equal to) != (not equal to) For example, the rule below would do the following: rule helloworld_checker{ strings: $hello_world = "Hello World!" condition: #hello_world

What is Yara?
All about Yara
"The pattern matching swiss knife for malware researchers (and everyone else)" (Virustotal., 2020)
With such a fitting quote, Yara can identify information based on both binary and textual patterns, such as hexadecimal and strings contained within a file.
Rules are used to label these patterns. For example, Yara rules are frequently written to determine if a file is malicious or not, based upon the features - or patterns - it presents. Strings are a fundamental component of programming languages. Applications use strings to store data such as text.
For example, the code snippet below prints "Hello World" in Python. The text "Hello World" would be stored as a string.
print("Hello World!")
We could write a Yara rule to search for "hello world" in every program on our operating system if we would like.
Why does Malware use Strings?
Malware, just like our "Hello World" application, uses strings to store textual data. Here are a few examples of the data that various malware types store within strings:
Type | Data | Description |
---|---|---|
Ransomware | 12t9YDPgwueZ9NyMgw519p7AA8isjr6SMw | Bitcoin Wallet for ransom payments |
Botnet | 12.34.56.7 | The IP address of the Command and Control (C&C) server |
Introduction to Yara Rules
Using a Yara rule is simple. Every yara
command requires two arguments to be valid, these are:
- The rule file we create.
- Name of file, directory, or process ID to use the rule for.
yara myrule.yar somedirectory
Expanding on Yara Rules
Checking whether or not a file exists isn't all that helpful. After all, we can figure that out for ourselves...Using much better tools for the job.
Yara has a few conditions, which I encourage you to read here at your own leisure. However, I'll detail a few below and explain their purpose.
Keyword
- Desc
- Meta
- Strings
- Conditions
- Weight
Meta
This section of a Yara rule is reserved for descriptive information by the author of the rule. For example, you can use desc
, short for description, to summarise what your rule checks for. Anything within this section does not influence the rule itself. Similar to commenting code, it is useful to summarise your rule.
Strings
Remember our discussion about strings in Task 2? Well, here we go. You can use strings to search for specific text or hexadecimal in files or programs. For example, say we wanted to search a directory for all files containing "Hello World!", we would create a rule such as below:
rule helloworld_checker{
strings:
$hello_world = "Hello World!"
}
We define the keyword Strings
where the string that we want to search, i.e., "Hello World!" is stored within the variable $hello_world
Of course, we need a condition here to make the rule valid. In this example, to make this string the condition, we need to use the variable's name. In this case,$hello_world
:
rule helloworld_checker{
strings:
$hello_world = "Hello World!"
condition:
$hello_world
}
Essentially, if any file has the string "Hello World!" then the rule will match. However, this is literally saying that it will only match if "Hello World!" is found and will not match if "hello world" or "HELLO WORLD."
To solve this, the condition any of them
allows multiple strings to be searched for, like below:
rule helloworld_checker{
strings:
$hello_world = "Hello World!"
$hello_world_lowercase = "hello world"
$hello_world_uppercase = "HELLO WORLD"
condition:
any of them
}
Now, any file with the strings of:
1. Hello World!
2. hello world
3. HELLO WORLD
Will now trigger the rule.
Conditions
We have already used the true
and any of them
condition. Much like regular programming, you can use operators such as:
- <= (less than or equal to)
- >= (more than or equal to)
- != (not equal to)
For example, the rule below would do the following:
rule helloworld_checker{
strings:
$hello_world = "Hello World!"
condition:
#hello_world <= 10
}
The rule will now:
1. Look for the "Hello World!" string\
2. Only say the rule matches if there are less than or equal to ten occurrences of the "Hello World!" string
Combining keywords
Moreover, you can use keywords such as:
- and
- not
- or
To combine multiple conditions. Say if you wanted to check if a file has a string and is of a certain size (in this example, the sample file we are checking is less than<10 kb and has "Hello World!" you can use a rule like below:
rule helloworld_checker{
strings:
$hello_world = "Hello World!"
condition:
$hello_world and filesize < 10KB
}
The rule will only match if both conditions are true. To illustrate: below, the rule we created, in this case, did not match because although the file has "Hello World!", it has a file size larger than 10KB:
Yara failing to match the file mytextfile because it is larger than 10kb
cmnatic@thm:~$
However, the rule matched this time because the file has both "Hello World!" and a file size of less than 10KB.
Yara successfully matching the file mytextfile because it has "Hello World" and a file size of less than 10KB
cmnatic@thm:~$ yara myfirstrule.yar mytextfile.txt
helloworld_textfile_checker mytextfile.txt
Remembering that the text within the red box is the name of our rule, and the text within the green is the matched file.
Anatomy of a Yara Rule
Information security researcher "fr0gger_" has recently created a handy cheatsheet that breaks down and visualises the elements of a YARA rule
Yara Modules
Integrating With Other Libraries
Frameworks such as the Cuckoo Sandbox or Python's PE Module allow you to improve the technicality of your Yara rules ten-fold.
Cuckoo
Cuckoo Sandbox is an automated malware analysis environment. This module allows you to generate Yara rules based upon the behaviours discovered from Cuckoo Sandbox. As this environment executes malware, you can create rules on specific behaviours such as runtime strings and the like.
Python PE
Python's PE module allows you to create Yara rules from the various sections and elements of the Windows Portable Executable (PE) structure.
Explaining this structure is out of scope as it is covered in my malware introductory room. However, this structure is the standard formatting of all executables and DLL files on windows. Including the programming libraries that are used.
Examining a PE file's contents is an essential technique in malware analysis; this is because behaviours such as cryptography or worming can be largely identified without reverse engineering or execution of the sample.
Other tools and Yara
Yara Tools
Knowing how to create custom Yara rules is useful, but luckily you don't have to create many rules from scratch to begin using Yara to search for evil. There are plenty of GitHub resources and open-source tools (along with commercial products) that can be utilized to leverage Yara in hunt operations and/or incident response engagements.
LOKI (What, not who, is Loki?)
LOKI is a free open-source IOC (Indicator of Compromise) scanner created/written by Florian Roth.
Based on the GitHub page, detection is based on 4 methods:
- File Name IOC Check
- Yara Rule Check* (we are here)*
- Hash Check
- C2 Back Connect Check
There are additional checks that LOKI can be used for. For a full rundown, please reference the GitHub readme.
LOKI can be used on both Windows and Linux systems and can be downloaded here.
Please note that you are not expected to use this tool in this room.
Displaying Loki's help menu
cmnatic@thm:~/Loki$ python3 loki.py -h
usage: loki.py [-h] [-p path] [-s kilobyte] [-l log-file] [-r remote-loghost]
[-t remote-syslog-port] [-a alert-level] [-w warning-level]
[-n notice-level] [--allhds] [--alldrives] [--printall]
[--allreasons] [--noprocscan] [--nofilescan] [--vulnchecks]
[--nolevcheck] [--scriptanalysis] [--rootkit] [--noindicator]
[--dontwait] [--intense] [--csv] [--onlyrelevant] [--nolog]
[--update] [--debug] [--maxworkingset MAXWORKINGSET]
[--syslogtcp] [--logfolder log-folder] [--nopesieve]
[--pesieveshellc] [--python PYTHON] [--nolisten]
[--excludeprocess EXCLUDEPROCESS] [--force]
Loki - Simple IOC Scanner
optional arguments:
-h, --help show this help message and exit
THOR (superhero named programs for a superhero blue teamer)
THOR Lite is Florian's newest multi-platform IOC AND YARA scanner. There are precompiled versions for Windows, Linux, and macOS. A nice feature with THOR Lite is its scan throttling to limit exhausting CPU resources. For more information and/or to download the binary, start here. You need to subscribe to their mailing list to obtain a copy of the binary. Note that THOR is geared towards corporate customers. THOR Lite is the free version.
Please note that you are not expected to use this tool in this room.
Displaying Thor Lite's help menu
cmnatic@thm:~$ ./thor-lite-linux-64 -h
Thor Lite
APT Scanner
Version 10.7.3 (2022-07-27 07:33:47)
cc) Nextron Systems GmbH
Lite Version
> Scan Options
-t, --template string Process default scan parameters from this YAML file
-p, --path strings Scan a specific file path. Define multiple paths by specifying this option multiple times. Append ':NOWALK' to the path for non-recursive scanning (default: only the system drive) (default [])
--allhds (Windows Only) Scan all local hard drives (default: only the system drive)
--max_file_size uint Max. file size to check (larger files are ignored). Increasing this limit will also increase memory usage of THOR. (default 30MB)
> Scan Modes
--quick Activate a number of flags to speed up the scan at cost of some detection.
This is equivalent to: --noeventlog --nofirewall --noprofiles --nowebdirscan --nologscan --noevtx --nohotfixes --nomft --lookback 3 --lookback-modules filescan
FENRIR (naming convention still mythical themed)
This is the 3rd tool created by Neo23x0 (Florian Roth). You guessed it; the previous 2 are named above. The updated version was created to address the issue from its predecessors, where requirements must be met for them to function. Fenrir is a bash script; it will run on any system capable of running bash (nowadays even Windows).
Please note that you are not expected to use this tool in this room.
Running Fenrir
cmnatic@thm-yara:~/tools$ ./fenrir.sh
##############################################################
____ _
/ __/__ ___ ____(_)___
/ _// -_) _ \/ __/ / __/
/_/ \__/_//_/_/ /_/_/
v0.9.0-log4shell
Simple Bash IOC Checker
Florian Roth, Dec 2021
##############################################################
YAYA (Yet Another Yara Automaton)
YAYA was created by the EFF (Electronic Frontier Foundation) and released in September 2020. Based on their website, "YAYA is a new open-source tool to help researchers manage multiple YARA rule repositories. YAYA starts by importing a set of high-quality YARA rules and then lets researchers add their own rules, disable specific rulesets, and run scans of files."
Note: Currently, YAYA will only run on Linux systems.
Running YAYA
cmnatic@thm-yara:~/tools$ yaya
YAYA - Yet Another Yara Automaton
Usage:
yaya [-h]
-h print this help screen
Commands:
update - update rulesets
edit - ban or remove rulesets
add - add a custom ruleset, located at
scan - perform a yara scan on the directory at
Using LOKI and its Yara rule set
Last 2 flags Writeup
I did a recursive grep to get the file content the matches the string webshell_metaslsoft
.
From here, we can see that only 1 string is flagged.
Last flag
Creating Yara rules with yarGen
Valhalla
Per the website, "Valhalla boosts your detection capabilities with the power of thousands of hand-crafted high-quality YARA rules."
From the image above, we should denote that we can conduct searches based on a keyword, tag, ATT&CK technique, sha256, or rule name.
Note: For more information on ATT&CK, please visit the MITRE room.
Taking a look at the data provided to us, let's examine the rule in the screenshot below:
We are provided with the name of the rule, a brief description, a reference link for more information about the rule, along with the rule date.
Feel free to look at some rules to become familiar with the usefulness of Valhalla. The best way to learn the product is by just jumping right in.
Picking up from our scenario, at this point, you know that the 2 files are related. Even though Loki classified the files are suspicious, you know in your gut that they are malicious. Hence the reason you created a Yara rule using yarGen to detect it on other web servers. But let's further pretend that you are not code-savvy (FYI - not all security professionals know how to code/script or read it). You need to conduct further research regarding these files to receive approval to eradicate these files from the network.
Flags
Explanations
Do the same for file 2. What is the name of the first Yara rule to detect file 2?
Examine the information for file 2 from Virus Total (VT). The Yara Signature Match is from what scanner?
Enter the SHA256 hash of file 2 into Virus Total. Did every AV detect this as malicious? (Yay/Nay)
Besides .PHP, what other extension is recorded for this file?
Go to Details > Names
What JavaScript library is used by file 2?
Hint: b374k GitHub
Is this Yara rule in the default Yara file Loki uses to detect these type of hack tools? (Yay/Nay)