01 BitsOfBinary
Search

Days 58–75 · 27 min read

File Classification by Offset & Structure

Identifying and inspecting PE, ELF, Mach-O, ISO, and OneNote files using fixed-offset integer reads versus string scanning, including an embedded OneNote payload case study.

On this page (22)

Day 58 - Accessing data at specified offsets

A feature of YARA I’ve used a few times so far in these posts are the operations to do comparisons of (unsigned) integer values at specific offsets in files. You can see the documentation for this feature here: https://yara.readthedocs.io/en/stable/writingrules.html#accessing-data-at-a-given-position

As highlighted in the docs, there are several types of functions you can use to access data at certain offsets: YARA integer offset functions summary

In short, you can either access signed or unsigned integer values, which are either of size:

  • 8-bits (i.e. 1 byte)
  • 16-bits (i.e. 2 bytes)
  • 32-bits (i.e. 4 bytes)

For each variation of these, you can also choose the endianess (that is, the order of the bytes). By default, little endian is used (i.e. the least signficant byte is evaluated first), but you can add be into the function to change it to big endian. Personally, I prefer using big endian where possible, as that is as if bytes are in the order that they appear in the file.

Why is this useful?

Whereas YARA is primarily designed for string matching, the fact you can access data at certain offsets means you can be very specific in how you write your rules. In particular, it makes it possible to be specific in the types of files you want to run your rules over, based on checking file headers. We’ll spend a few posts discussing this.

Day 59 - File classification: PE files

If you write a lot of YARA rules for Windows malware, you will get very used to writing this condition.

As can be seen in the docs, the following rule can be used to identify portable executable (PE) files, which is the executable file format for Windows:

rule IsPE
{
condition:
// MZ signature at offset 0 and ...
uint16(0) == 0x5A4D and
// ... PE signature at offset stored in MZ header at 0x3C
uint32(uint32(0x3C)) == 0x00004550
}

This rule is very useful in how descriptive it is. The comments explain the reasoning behind the checks.

The first check is looking for the bytes MZ, which is the start of the DOS stub (an artefact of MS-DOS). If you read the PE documentation that I’ve linked to, you’ll see that it specifies that at offset 0x3c is a pointer to where the PE header begins, which should start with the bytes PE\x00\x00. As such, this is why the second check uses two uint32 calls; one to get the pointer, and the other to get the data pointed to.

If you look closely at the rule, you’ll realise that both the MZ and PE strings are reversed. This is because, as highlighted in the previous post, the default for these functions is little endian. So you could equivalently write the rule as:

rule IsPE
{
condition:
// MZ signature at offset 0 and ...
uint16be(0) == 0x4D5A and
// ... PE signature at offset stored in MZ header at 0x3C
uint32be(uint32(0x3C)) == 0x50450000
}

Note: I can’t change the inner uint32 comparison as that pointer will be little endian; but I can change the comparisons.

Why would you choose one over the other? It’s really a matter of preference. The only technical limitation is that the big endian versions were only added in YARA v3.2.0, which was released in November 2014. Ideally, you should be using the latest version, but I believe some tools out there still rely on older versions of YARA.

Day 60 - File classification: PE files - Alternative approaches

It can be worth building in some redundancy into the ways you can classify files, in case obfuscation is applied to throw off standard signatures!

For the PE file format, an alternative way of signaturing this file may be to look at the MS-DOS stub: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#ms-dos-stub-image-only

As described in the documentation:

The MS-DOS stub is a valid application that runs under MS-DOS. It is placed at the front of the EXE image. The linker places a default stub here, which prints out the message “This program cannot be run in DOS mode” when the image is run in MS-DOS. The user can specify a different stub by using the /STUB linker option.

So we have two options. Either looking for the string This program cannot be run in DOS mode, or having a signature for the DOS stub itself. Given we’re focusing on offsets at the moment, we’ll do the latter.

Take for example calc.exe, the DOS stub begins at offset 0x40, and is made up of the following bytes:

0E 1F BA 0E 00 B4 09 CD 21 B8 01 4C CD 21

The following blog post has an annotated screenshot of this code: https://osandamalith.com/2020/07/19/exploring-the-ms-dos-stub/

So if we take the first few bytes of this stub, we could create the following rule:

rule IsPE {
condition:
uint32be(0x40) == 0x0E1FBA0E
}

So this is a potential alternative approach to finding PE files! What I would caution though is that the documentation above states that The user can specify a different stub by using the /STUB linker option, meaning that these bytes won’t be as consistent. However, it may be interesting to look for alternative DOS stubs in PE files to see what is being used!

Day 61 - File classification: PE files - Sufficient checks

Most of the time if I’m writing a rule to classify PE files, I will abbreviate my condition to:

rule IsPE
{
condition:
uint16(0) == 0x5A4D
}

That is, I’m only checking for the MZ string. Is that enough? Is that sufficient to determine whether a file is a valid PE?

The short answer is no. By doing this, I’m avoiding checking the PE\x00\x00 string. Neither am I checking for the This program cannot be run in DOS mode string which would be printed out by the DOS stub. I’m certainly not checking for whether the rest of the PE header is valid, or if the sections fit the specific provided, etc.

However, the check uint16(0) == 0x5A4D is “good enough”. If a file on a machine starts with the characters MZ, chances are it is a PE file (there’s always the PE module you can use if you want a more thorough check). However, if it isn’t a valid PE, this is potentially already a good chance to start writing rules for anomalous files…

Consider the following for instance:

rule `Heuristic_MZ_Bytes_At_File_Start_Without_DOS_String {
strings:
$dos_string = "This program cannot be run in DOS mode"
condition:
uint16(0) == 0x5A4D and not $dos_string

As described, this rule will fire if there is no DOS string in a file that starts with MZ. While I can imagine this will yield a lot of FPs, it is one of those rules that could be a useful threat hunting edge case, in case threat actors are trying to hide data in weird ways!

Day 62 - File classification: ELF files

What other file types can we write conditions for? Let’s consider some other executable file formats, starting with Executable and Linkable Format (ELF).

This file format is used in Linux, and like PE files it has some “magic bytes” at the start of the binary (as seen in this example from Wikipedia): ELF file header structure example

As the specification lays out, ELF files will start with the byte 0x7f followed by the characters ELF. We can write a YARA rule for this as follows:

rule IsELF {
condition:
uint32be(0) == 0x7f454c46
}

Note that I’m using the big endian version here, as it means I can copy the bytes straight into the rule in order. You can do a little endian equivalent using uint32(0) == 0x464c457f. Again, up to you what you prefer.

Day 63 - File classification: ELF files - 32 bit or 64 bit?

Let’s say we wanted to write a YARA condition to find 64-bit ELF binaries (without using the ELF module). Let’s look at the specification again: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format

The byte at offset 0x4 is either set to 1 for 32-bit binaries, and 2 for 64-bit binaries. Now we can’t write this rule with just one check; there is no uint40 operator unfortunately. However, we can combine multiple checks together to make this work:

rule IsELF_64bit {
condition:
// Check for ELF header
uint32be(0) == 0x7f454c46 and
// Byte at offset 0x4 is 1 for 32-bit, 2 for 64-bit
uint8(4) == 2
}

Couple of notes for this rule:

  • It doesn’t really matter if I use uint8 or uint8be for the 32-bit/64-bit check, as the endianess doesn’t matter when we’re dealing with one byte;
  • You can either use decimal or hexadecimal representations of numbers in YARA; here I’m using the fact that 0x4 is equivalent to decimal 4 in the rule (equally I could use that 0x7f454c46 is equivalent to 2135247942, but that makes the rule a lot less clear about what it is checking); and,
  • You can use int or uint as long as you’re careful. Here, if I make both checks int, it will still work. But you’ll need to be careful if you’re doing checks against large values (especially if you don’t know what they are ahead of time), as you might have to factor in that the int versions can also be negative numbers. I tend to use uint for ease of knowing the number will always be positive.

Day 64 - File classification: ELF files - Number of sections case study - Part 1

As a case study for ELF files, let’s consider a slightly more complicated case. Again, we’ll reference the specification: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format

If I want to write a rule without using the ELF module to find ELF binaries with only one section (which could be an anomaly if combined with other heuristics), then I need to check a specific offset in the file. However, this offset changes depending on whether the binary is 32-bit or 64-bit; the offets at 0x30 and 0x3c respectively.

Fortunately, the rule in our previous post guides us on how to check for 32/64-bit binaries. But we run into another problem in that YARA doesn’t have if/elseif/else statements. So how do we make sure we use the right offset?

By using or statements, we can make branching statements based on the fact that if one statement in total evaluates to false, it will move on to the next one. As such, we can create an implicit if/elseif statement using the following:

rule Heuristic_ELF_with_One_Section {
condition:
uint32be(0) == 0x7f454c46 and
(
// 32-bit
(
uint8(4) == 1 and
uint16(0x30) == 1
)
or
// 64-bit
(
uint8(4) == 2 and
uint16(0x3c) == 1
)
)
}

I.e., if we see it is 32-bit, we check offset 0x30; if either of the statements in that block are false, the or statement will then check the second block, which will do a 64-bit check (i.e. checking offset 0x3c).

I’ll talk in the next post about ways of simplifying this condition a bit.

Day 65 - File classification: ELF files - Number of sections case study - Part 2

If you trust the ELF file format enough, we could convert the if/elseif equivalent statements from the rule in the previous post to an if/else equivalent statement, relying on the fact that if it isn’t 32-bit, it must be 64-bit:

rule Heuristic_ELF_with_One_Section {
condition:
uint32be(0) == 0x7f454c46 and
(
// 32-bit check
(
uint8(4) == 1 and
uint16(0x30) == 1
)
or
// Assume 64-bit if 32-bit check fails
uint16(0x3c) == 1
)
}

This is a condition with less checks involved, but is maybe a little less clear than the previous rule! Adding some comments can clear things up a bit, and I think in general we should strive to make our rules as clear as possible (so maybe in your use case, the rule in the previous post is better as it is more clear on how it works).

That said, I’ll show in the next post how to reduce this condition even further…

Day 66 - File classification: ELF files - Number of sections case study - Part 3

If you want to get more creative, and decide you want the number of sections computation done in one line, you could do the following:

rule Heuristic_ELF_with_One_Section {
condition:
uint32be(0) == 0x7f454c46 and
uint16(0x30 + ((uint8(4) - 1) * 0xc)) == 1
}

I.e., if the binary is 32-bit, the expression (uint8(4) - 1) * 0xc will collapse to zero, meaning the uint16 check will point at the 0x30 offset. If it is 64-bit, it will become 0xc, which will then be added to the 0x30 offset (i.e. 0x3c). To be honest I think this is unnecessary, and maybe even less efficient (i.e. the previous post’s rule will do up to three equals comparisons for the sections check, and while this rule only does one equals comparison it adds three mathematical operations), but I wanted to show this is possible! There’s probably an even more efficient way of doing it using logical & operations; let me know if you have a better approach!

I hope that by highlighting that despite YARA not having the traditional conditional statements that are expected of programming languages, you can still use the conditions available to create the logical statements you’re after.

Or just use a module…

Overall, these conditions can start to get quite complicated, so if you’ve got a module available to help, it might just be easier to use that. Here’s the same rule but in its module form:

import "elf"
rule Heuristic_ELF_with_One_Section {
condition:
elf.number_of_sections == 1
}

Day 67 - File classification: Mach-O files

Another executable file format is Mach-O, which is used in macOS. The magic bytes at the start of a Mach-O binary can vary, as highlighted in this blog: https://redmaple.tech/blogs/macho-files/

In particular, something I saw when looking up these values is that 0xCAFEBABE (used for Mach-O FAT binaries) is also used for Java .class files, meaning we might misclassify some files if just relying on this value! However, Apple has a hacky approach of getting around this based on Java versions: https://opensource.apple.com/source/file/file-80.40.2/file/magic/Magdir/cafebabe.auto.html

In short, the the 16-bit value at offset 0x4 would tell us the Java version, the lowest value of which is apparently 0x30. For Mach-0, the 32-bit value at offset 0x4 identifies the architecture, which according to the linked docs (which were written in 2018 so there may be later versions) only goes up to version 18. So, we could rely on the following to avoid trying to get false positives:

rule IsMacho {
condition:
uint32(0) == 0xfeedface or // the mach magic number
uint32(0) == 0xcefaedfe or // NXSwapInt(MH_MAGIC)
uint32(0) == 0xfeedfacf or // the 64-bit mach magic number
uint32(0) == 0xcffaedfe or // NXSwapInt(MH_MAGIC_64)
(
uint32(0) == 0xcafebabe and // Mach-O FAT binaries
uint16(4) < 0x30 // Avoid Java classes
)
}

I definitely want to learn more about macOS; I can highly recommend checking about Daniel Stinson’s (@shellcromancer) posts for #100DaysofYARA, as he has covered so far several different macOS malware families/heuristic rules: https://github.com/shellcromancer/DaysOfYARA-2023/tree/main/shellcromancer

Day 68 - File classification: ISO files

We’ve looked at some of the most common executable file formats so far, but there are so many more types of files out there that we may want to classify with YARA! One of the best resources I’ve seen to list a large variety of file signatures is the following: https://www.garykessler.net/library/file_sigs.html

For example, for optical disk images (ISO), which have been increasingly used by threat actors in recent times, has the following description from the above linked resource:

43 44 30 30 31 CD001
ISO ISO-9660 CD Disc Image
This signature usually occurs at byte offset 32769 (0x8001), 34817 (0x8801), or 36865 (0x9001).

This gives us the exact information we need to write a condition to target ISO files. We can either do this using the uint functions:

rule IsISO {
condition:
(uint32be(0x8001) == 0x43443030 and uint8(0x8005) == 0x31) or
(uint32be(0x8801) == 0x43443030 and uint8(0x8805) == 0x31) or
(uint32be(0x9001) == 0x43443030 and uint8(0x9005) == 0x31)
}

Or using strings:

rule IsISO {
strings:
$iso_sig = "CD001"
condition:
$iso_sig at 0x8001 or
$iso_sig at 0x8801 or
$iso_sig at 0x9001
}

Day 69 - File classification: Strings vs. offsets - Part 1

In the previous psot, I showed two different ways to classify ISO files; one using uint checks, and another using strings. Why would you pick one over the other?

One way I see people write rules to check if a file is a PE is as follows:

rule IsPE {
strings:
$mz = "MZ"
// equally you could use $mz = {4d 5a}
condition:
$mz at 0
}

As a rule, this is fine. The atom {4d 5a} is not going to be flagged by YARA as “slowing down your scanning”. And it follows the same logic that the uint16(0) == 0x5A4D check. In fact, when testing this in the current version of YARA, the simple condition of just $mz at 0 means that YARA will stop trying to find other instances of MZ once it has found the first one!

EDIT: As Wesley Shields pointed out to me, there are some inaccuracies in the following section. I believe some of my testing was inaccurate due to a typo in one of my rules, and so I would not rely on the following description that has been struck through (I want to leave it in for reference).

The following post will do some further tests to try and make some more sense of what is going on!

A potential performance issue appears when you start to introduce other strings/parts of the condition. For instance, the following rule:

rule IsPE {
strings:
$mz = {4d 5a}
// equally you could use $mz = {4d 5a}
$foobar = "foobar"
condition:
$mz at 0 and any of them
}

If you run this rule, and print out the matched strings, you will see that it will try to search for every instance of MZ unlike the other rule, even though we only care about whether it appears at offset 0. This is not as good performance-wise; if you’re scanning compressed/encrypted data, the chances of the bytes {4d 5a} appearing in many places increases! This means YARA will have to scan for the atom {4d 5a} everywhere, and record each location of its appearence (unless we specify YARA to run in fast mode with the -f flag that is).

Alternatively, the check uint16(0) == 0x5A4D doesn’t rely on strings, meaning we don’t have to search for MZ everywhere to evaluate the condition. These bytes will either be at the start of the file where we have specified, or they won’t, meaning the check is usually quicker.

Day 70 - File classification: Strings vs. offsets - Part 2

In the previous post I attempted to discuss some of the potential tradeoffs of using strings vs. offsets. However, there were some inaccuracies in my approach (thank you to Wesley Shields for pointing this out). I still need to look at the source code of YARA more in detail to see how the YARA searching algorithm changes depending on how large the file, but for now let’s do some further tests to see what potential differences there could be between strings and offsets.

Let’s consider the worst case scenario - large files of random data where the bytes {4d 5a} may occur often. To simulate this, I generated some random files of varying size:

import os
with open("1kb.bin", "wb") as outfile:
outfile.write(os.urandom(1024))
with open("200kb.bin", "wb") as outfile:
outfile.write(os.urandom(1024 * 200))
with open("500kb.bin", "wb") as outfile:
outfile.write(os.urandom(1024 * 500))
with open("1mb.bin", "wb") as outfile:
outfile.write(os.urandom(1024 * 1024))
with open("200mb.bin", "wb") as outfile:
outfile.write(os.urandom(1024 * 1024 * 200))
with open("500mb.bin", "wb") as outfile:
outfile.write(os.urandom(1024 * 1024 * 500))
with open("1gb.bin", "wb") as outfile:
outfile.write(os.urandom(1024 * 1024 * 1024))
with open("10gb.bin", "wb") as outfile:
outfile.write(os.urandom(1024 * 1024 * 1024 * 10))

And ran the following rules through Panopticon:

rule Just_Str_MZ_At_0 {
strings:
$mz = "MZ"
condition:
$mz at 0
}
rule Str_MZ_At_0_And_Extra_String {
strings:
$mz = "MZ"
$foobar = "foobar"
condition:
$mz at 0 and $foobar
}
rule Str_MZ_Anywhere {
strings:
$mz = "MZ"
condition:
any of them
}
rule Just_Offset_MZ_At_0 {
condition:
uint16(0) == 0x5A4D
}
rule Offset_MZ_At_0_And_Extra_String {
strings:
$foobar = "foobar"
condition:
uint16(0) == 0x5A4D and $foobar
}

In general, I wanted to test different cases of checking if a file is a PE based on offset checks vs. strings. Unfortunately, I seem to be getting inconsistent results in terms of the performance. These are the results I got on my system when running them against the random files:

[INFO ] Rule "Just_Str_MZ_At_0" is fast enough, not measuring any further due to fast mode, diff 7.2349 % below
alerting level: 18.9697 %
[INFO ] Rule "Str_MZ_At_0_And_Extra_String" is fast enough, not measuring any further due to fast mode, diff -17.0396
% below alerting level: 18.9697 %
[INFO ] Rule "Str_MZ_Anywhere" is fast enough, not measuring any further due to fast mode, diff -16.6793 % below
alerting level: 18.9697 %
[INFO ] Rule "Just_Offset_MZ_At_0" is fast enough, not measuring any further due to fast mode, diff -48.4826 % below
alerting level: 18.9697 %
[INFO ] Rule "Offset_MZ_At_0_And_Extra_String" is fast enough, not measuring any further due to fast mode, diff
-50.0343 % below alerting level: 18.9697 %

And these are results from a second run of the same rules (in which I changed the order of the rules a bit):

[INFO ] Rule "Just_Offset_MZ_At_0" is fast enough, not measuring any further due to fast mode, diff -51.9545 % below
alerting level: 3.0000 %
[INFO ] Rule "Offset_MZ_At_0_And_Extra_String" is fast enough, not measuring any further due to fast mode, diff
-50.7250 % below alerting level: 3.0000 %
[INFO ] Rule "Just_Str_MZ_At_0" is fast enough, not measuring any further due to fast mode, diff -25.8056 % below
alerting level: 3.0000 %
[INFO ] Rule "Str_MZ_At_0_And_Extra_String" is fast enough, not measuring any further due to fast mode, diff -48.6145
% below alerting level: 3.0000 %
[INFO ] Rule "Str_MZ_Anywhere" is fast enough, not measuring any further due to fast mode, diff -49.0124 % below
alerting level: 3.0000 %

What can be seen in both sets of results is that all of the rules I’ve run here are fast, but that that the offset check rules are consistently fast, while the string checks are inconsistent (sometimes just as fast as the offset rules, sometimes not).

While I would like to do more testing, I’m not sure that this is necessarily the right approach at this stage!

Some caveats: bear in mind that:

  • These rules are being run against large random files, which is going to be the worst case scenario
  • Are doing very basic checks, whereas as other rules will have more strings/checks alongside them
  • When run on their own vs. a larger set of rules the performance will likely change

In conclusion

I have a bias against $mz at 0 rules, due to my belief that they did not perform as well. While there may be some edge cases where there are differences in performance, it does not appear that this is the case in general!

In short, there’s unlikely to be a practical difference in whether you use strings or offsets to check for specific conditions. It is your preference for how you want to write these rules.

Day 71 - File classification: OneNote files

If you follow the latest threat vectors being used by threat actors in the wild, you may have seen that there has been an increase in OneNote files used in phishing campaigns: https://www.proofpoint.com/uk/blog/threat-insight/onenote-documents-increasingly-used-to-deliver-malware

With the changes made by Microsoft to by default disable macros in documents downloaded from the internet, it can be expected that threat actors will continue to experiment with different file formats! Using OneNote in particular seemed to be quite effective at the start, as not all security tooling was necessarily prepared to handle the file format.

So what can we do with YARA? As with many cases I’ve highlighted so far, the first thing we want to do is classify files as OneNote files. According to this document, they have the following signature at the start of a file:

E4 52 5C 7B 8C D8 A7 4D
AE B1 53 78 D0 29 96 D3

While we could write a condition for the whole header, it should be sufficient to look at the first 4 bytes:

rule IsOneNote {
condition:
uint32be(0) == 0xE4525C7B
}

Day 72 - File classification: OneNote Case Study - Embedded Files - Part 1

So now that we can classify OneNote files, what kind of heuristics can we perform? Let’s use the official file specification as a reference: https://interoperability.blob.core.windows.net/files/MS-ONE/%5bMS-ONE%5d.pdf

The general approach threat actors have been taking so far is to embed files within OneNote documents (such as PowerShell, HTA, PE, etc.), which when double clicked on within the notebook will be executed. If you go to the section 2.1.12 Properties of the file specification, you will see that many of the structures of OneNote have PropertyID values associated with them. In particular, I could see the following:

EmbeddedFileContainer 0x20001D9B

Even though we don’t currently have a YARA module to parse OneNote files, using this information we can write the following heuristic rule:

rule Heuristic_OneNote_Notebook_with_Embedded_File {
meta:
description = "Detects OneNote notebooks with an embedded file"
reference = "https://interoperability.blob.core.windows.net/files/MS-ONE/%5bMS-ONE%5d.pdf"
strings:
$embedded_file_container_property_id = {9B 1D 00 20}
condition:
uint32be(0) == 0xE4525C7B and any of them
}

While this rule will almost certainly pick up a legitimate OneNote files (embedding files is a feature of OneNote after all that will have some legitimate use), it is a starting point to apply more heuristics to search for suspicious OneNote files.

Day 73 - File classification: OneNote Case Study - Embedded Files - Part 2

So how can we add heuristics to the rule in the previous post? From what I’ve seen, for it to be possible to execute files embedded within OneNote files, they need a valid file extension.

In the OneNote file specification, the following PropertyID is listed:

EmbeddedFileName 0x1C001D9C

In particular, a jcidEmbeddedFileNode structure in a OneNote file may contain both a EmbeddedFileContainer and EmbeddedFileName structure. So a further heuristic could be to look for embedded files which we know have filenames listed:

rule Heuristic_OneNote_Notebook_with_Embedded_File_with_Filename {
meta:
reference = "https://interoperability.blob.core.windows.net/files/MS-ONE/%5bMS-ONE%5d.pdf"
strings:
$embedded_file_container = {9B 1D 00 20}
$embedded_file_name = {9C 1D 00 1C}
condition:
uint32be(0) == 0xE4525C7B and
$embedded_file_container and
for any i in (1 .. #embedded_file_container) : (
$embedded_file_name in (@embedded_file_container[i] .. @embedded_file_container[i] + 0x30)
)
}

This rule extends the previous post’s rule by adding a for loop which iterates over each instance of $embedded_file_container, and looks to see if a $embedded_file_name string lies within the first 0x30 bytes of an occurance of any of them.

Day 74 - File classification: OneNote Case Study - Embedded Files - Part 3

To turn these heuristics into useful rules, let’s consider what we want to look for in the filenames of embedded files.

We want to look for any occurances of “executable” files (that is, files that will be run in some format when double clicked). There are some obvious choices, such as .exe files, but there are also scripts such as .bat, or shortcut .lnk files.

As it turns out, there are quite a lot of valid executable extensions! I’ve not seen a complete list yet (please let me know if you have one), but here are some references that contain some good lists:

By picking out some of the most relevant extensions, we can complete our heuristic rule as follows:

rule Heuristic_OneNote_Notebook_with_Embedded_Executable_File {
meta:
description = "Detects OneNote notebooks with suspicious embedded executable files"
reference = "https://interoperability.blob.core.windows.net/files/MS-ONE/%5bMS-ONE%5d.pdf"
strings:
$embedded_file_container = {9B 1D 00 20}
$embedded_file_name = {9C 1D 00 1C}
$ext1 = ".ade" ascii wide nocase
$ext2 = ".adp" ascii wide nocase
$ext3 = ".ai" ascii wide nocase
$ext4 = ".bat" ascii wide nocase
$ext5 = ".chm" ascii wide nocase
$ext6 = ".cmd" ascii wide nocase
$ext7 = ".com" ascii wide nocase
$ext8 = ".cpl" ascii wide nocase
$ext9 = ".dll" ascii wide nocase
$ext10 = ".exe" ascii wide nocase
$ext11 = ".hlp" ascii wide nocase
$ext12 = ".hta" ascii wide nocase
$ext13 = ".inf" ascii wide nocase
$ext14 = ".ins" ascii wide nocase
$ext15 = ".isp" ascii wide nocase
$ext16 = ".jar" ascii wide nocase
$ext17 = ".js" ascii wide nocase
$ext18 = ".jse" ascii wide nocase
$ext19 = ".lib" ascii wide nocase
$ext20 = ".lnk" ascii wide nocase
$ext21 = ".mde" ascii wide nocase
$ext22 = ".msc" ascii wide nocase
$ext23 = ".msi" ascii wide nocase
$ext24 = ".msp" ascii wide nocase
$ext25 = ".mst" ascii wide nocase
$ext26 = ".nsh" ascii wide nocase
$ext27 = ".pif" ascii wide nocase
$ext28 = ".ps" ascii wide nocase
$ext29 = ".ps1" ascii wide nocase
$ext30 = ".reg" ascii wide nocase
$ext31 = ".scr" ascii wide nocase
$ext32 = ".sct" ascii wide nocase
$ext33 = ".shb" ascii wide nocase
$ext34 = ".shs" ascii wide nocase
$ext35 = ".sys" ascii wide nocase
$ext36 = ".vb" ascii wide nocase
$ext37 = ".vbe" ascii wide nocase
$ext38 = ".vbs" ascii wide nocase
$ext39 = ".vxd" ascii wide nocase
$ext40 = ".wsc" ascii wide nocase
$ext41 = ".wsf" ascii wide nocase
$ext42 = ".wsh" ascii wide nocase
condition:
uint32be(0) == 0xE4525C7B and
$embedded_file_container and
for any i in (1 .. #embedded_file_container) : (
$embedded_file_name in (@embedded_file_container[i] .. @embedded_file_container[i] + 0x30) and
any of ($ext*) in (@embedded_file_container[i] .. @embedded_file_container[i] + 0x100)
)
}

Extending the previous post’s rule futher, we are now also looking for embedded filenames with a list of known extensions.

From the samples I was looking at, the filenames were UTF-16, meaning that we have to apply the wide modifier. I include the ascii modifier just in case as well. I also apply nocase, as threat actors could change the casing in order to evade detection.

From my tests, this appears to pick up a lot of malicious OneNote files! I will add it to the 100 Days of YARA GitHub repo, so please feel free to use it: https://github.com/100DaysofYARA/2023/tree/main/bitsofbinary

Day 75 - File classification: OneNote Case Study - Embedded Files - Part 4

Let’s do a critique the rule from the previous post. Reviewing your own rules is always a good opportunity to learn how to improve.

Redundant string check?

The $embedded_file_container and line in the rule is actually redundant, as the line for any i in (1 .. #embedded_file_container) : ( won’t evaluate if there are no occurances of $embedded_file_container! So we could cut that line.

Unnecessary embedded filename check?

Is the $embedded_file_name in ... check really needed? On one hand, we may want to check if there is actually a filename to check at all; but on the other, it could be worthwhile just doing the file extensions check anyway. I think we could safely cut this line and the rule would function the same.

Is the range check for file extensions good enough?

The any of ($ext*) in ... line checks the first 0x100 bytes after an occurance of $embedded_file_container. Given that we’re doing some pretty rough parsing of a OneNote file, is this good enough? Or should we build in more slack? If so, what range should we check instead?

Is the for loop necessary at all?

Arguably, do we even need the for loop? Could we not just check for OneNote files that have those file extensions in them via strings section? I think there is a good case to be made for doing either; the rule in its current format I think is less FP-prone (i.e. it is less likely to hit on samples that have text containing those file extensions), but if my condition isn’t loose enough then perhaps I’m missing malicious samples?

Too many file modifiers?

Each string has ascii wide nocase applied to it. This will create quite a lot of atoms to check for, as each casing variation of both the ascii and wide string variants will need to be computed. However, given that these strings are relatively short, I think that the performance could be a lot worse, and I’d personally be happy including this in my ruleset.

Alternative rule

Based on the above, a variation of the rule from the previous post could look like this:

rule Heuristic_OneNote_Notebook_with_Embedded_Executable_File {
meta:
description = "Detects OneNote notebooks with suspicious embedded executable files"
reference = "https://interoperability.blob.core.windows.net/files/MS-ONE/%5bMS-ONE%5d.pdf"
strings:
$embedded_file_container = {9B 1D 00 20}
$ext1 = ".ade" ascii wide nocase
$ext2 = ".adp" ascii wide nocase
$ext3 = ".ai" ascii wide nocase
$ext4 = ".bat" ascii wide nocase
$ext5 = ".chm" ascii wide nocase
$ext6 = ".cmd" ascii wide nocase
$ext7 = ".com" ascii wide nocase
$ext8 = ".cpl" ascii wide nocase
$ext9 = ".dll" ascii wide nocase
$ext10 = ".exe" ascii wide nocase
$ext11 = ".hlp" ascii wide nocase
$ext12 = ".hta" ascii wide nocase
$ext13 = ".inf" ascii wide nocase
$ext14 = ".ins" ascii wide nocase
$ext15 = ".isp" ascii wide nocase
$ext16 = ".jar" ascii wide nocase
$ext17 = ".js" ascii wide nocase
$ext18 = ".jse" ascii wide nocase
$ext19 = ".lib" ascii wide nocase
$ext20 = ".lnk" ascii wide nocase
$ext21 = ".mde" ascii wide nocase
$ext22 = ".msc" ascii wide nocase
$ext23 = ".msi" ascii wide nocase
$ext24 = ".msp" ascii wide nocase
$ext25 = ".mst" ascii wide nocase
$ext26 = ".nsh" ascii wide nocase
$ext27 = ".pif" ascii wide nocase
$ext28 = ".ps" ascii wide nocase
$ext29 = ".ps1" ascii wide nocase
$ext30 = ".reg" ascii wide nocase
$ext31 = ".scr" ascii wide nocase
$ext32 = ".sct" ascii wide nocase
$ext33 = ".shb" ascii wide nocase
$ext34 = ".shs" ascii wide nocase
$ext35 = ".sys" ascii wide nocase
$ext36 = ".vb" ascii wide nocase
$ext37 = ".vbe" ascii wide nocase
$ext38 = ".vbs" ascii wide nocase
$ext39 = ".vxd" ascii wide nocase
$ext40 = ".wsc" ascii wide nocase
$ext41 = ".wsf" ascii wide nocase
$ext42 = ".wsh" ascii wide nocase
condition:
uint32be(0) == 0xE4525C7B and
for any i in (1 .. #embedded_file_container) : (
any of ($ext*) in (@embedded_file_container[i] .. @embedded_file_container[i] + 0x200)
)
}

I.e. we lose the $embedded_file_name checks, and increase the range check; but we still keep the for loop looking over $embedded_file_container instances.

Hopefully this self critique shows that while there are usually “objective” ways of improving rules, there are also many “subjective” opinions that can be applied! How would you change this rule, for example?