01 BitsOfBinary
Search

Days 31–34 · 4 min read

Hex Values in Text Strings

Combining hex escapes inside YARA text strings to unlock modifiers like wide, xor, and base64 that hexadecimal byte strings cannot use.

On this page (4)

Day 31 - YARA Tip - Hex values in text strings - Part 1

Did you know that you can put specific hex values in YARA text strings?

You can write YARA rules specifically for hexadecimal strings, as follows:

rule Hex_String_Test {
strings:
$ = {AB CD EF 00}
condition:
any of them
}

You can write an equivalent for this rule using text strings by prepending each byte with \x, as follows:

rule Hex_Chars_in_Text_String_Test {
strings:
$ = "\xAB\xCD\xEF\x00"
condition:
any of them
}

This rule is equivalent to the one above! It can be useful if you want to have a mixture of ASCII/non-ASCII characters in a text string (and some other benefits that I’ll go into tomorrow).

Thank you to David Cannings (@edeca) for teaching me this (among many other YARA tips/tricks over the years!).

Day 32 - YARA Tip - Hex values in text strings - Part 2

A benefit of writing hex values in text strings is that it allows you to apply modifiers to these strings. For example, if you wanted to look for all one-byte XOR values of {AB CD EF 00}, you would have to compute those values yourself and write a rule with all 255 versions of them:

rule One_Byte_XOR_Hex_Strings {
meta:
description = "Detects all one-byte XOR values of {AB CD EF 00}"
strings:
$key_01 = {aa cc ee 01}
$key_02 = {a9 cf ed 02}
// ... truncated for size ...
$key_fe = {55 33 11 fe}
$key_ff = {54 32 10 ff}
condition:
any of them
}

Instead of going to all this effort yourself, and having a rule that has 255 lines of strings, you can use the xor modifier like this:

rule One_Byte_XOR_Hex_Strings {
meta:
description = "Detects all one-byte XOR values of {AB CD EF 00}"
strings:
$ = "\xAB\xCD\xEF\x00" xor(0x01-0xff)
condition:
any of them
}

Both of these rules are equivalent in what they achieve/the efficiency in which they achieve it (the xor modifier will still have to generate and check for each variation as specified), but the second one is much more succinct and requires a lot less effort on your part.

Day 33 - YARA Tip - Hex values in text strings - Part 3

To really drive home the point about the use of hex values in text strings, here’s a final example using the Base64 modifier in YARA:

rule Reflective_Loader_Shellcode_Base64_Encoded {
meta:
description = "Detects Base64 encoded reflective loader shellcode stub, seen for example in Meterpreter samples"
hash = "ed48d56a47982c3c9b39ee8859e0b764454ab9ac6e7a7866cdef5c310521be19"
hash = "76d54a57bf9521f6558b588acd0326249248f91b27ebc25fd94ebe92dc497809"
hash = "1db32411a88725b259a7f079bdebd5602f11130f71ec35bec9d18134adbd4352"
strings:
// pop r10
// push r10
// push rbp
// mov rbp, rsp
// sub rsp, 20h
// and rsp, 0FFFFFFFFFFFFFFF0h
// call $+5
// pop rbx
$ = "\x4D\x5A\x41\x52\x55\x48\x89\xE5\x48\x83\xEC\x20\x48\x83\xE4\xF0\xE8\x00\x00\x00\x00\x5B" base64 base64wide
condition:
any of them
}

Here, I have taken the shellcode stub seen in Reflective loader samples, such as Meterpreter payloads. You can obviously just search for this hex string to find the PE/shellcode payloads; however given those may possibly only ever end up in memory, it might be better to hunt for the loaders as well.

So using the fact that we can use hex values in text strings, I’ve taken that shellcode stub, converted it to a text string, and applied the base64 and base64wide modifiers to find samples which have this shellcode Base64 encoded in them, which will catch PowerShell loaders for example!

This is one of those examples where you can take the string of one rule (i.e. finding Reflective Loader samples) and easily repurpose it for another one. You could also repeat this for one-byte XOR values too if you wanted!

Day 34 - Text strings vs. Hexadecimal strings

I’ve just spent the last few posts suggesting how effective it can be converting hexadecimal strings into text strings. However, as is usually the case, there are tradeoffs to using this approach.

You may have noticed already that the strings I used in the previous rules are “fixed”; there are no wildcards or variable chunks. Yet, these are the most useful features of using hexadecimal strings, as it allows you to write rules for code where you apply wildcards to mask off specific bytes to write more generic rules!

Wildcards and variable chunks are not possible in YARA text strings; instead you would need to start using regular expressions, but this also then loses the ability to apply certain modifiers: YARA string modifier summary

In summary, there’s usually going to be some tradeoff for the type of string you pick (text, hex, or regex) in YARA, but the more familiar you are with these tradeoffs, the better decisions you can make on which one to use!