Matthew Profile picture
Nov 14, 2022 22 tweets 11 min read Read on X
🐲 Ghidra Tips 🐲- Malware Encryption and Hashing functions often produce byte sequences that are great for #Yara rules.

Using #Ghidra and a Text Editor - You can quickly develop Yara rules to detect common malware families.
(Demonstrated with #Qakbot)

[1/20]
#Malware #RE
[2/20]
Hashing and encryption functions make good targets for #detection as they are reasonably unique to each malware family and often contain lengthy and specific byte sequences due to the mathematical operations involved.

These characteristics make for good Yara rules 😁
[3/20] The biggest challenge is locating the functions responsible for hashing and encryption. I'll leave that for another thread, but for now...

You can typically recognize hashing/encryption through the use of bitwise operators inside a loop. (xor ^ and shift >> etc).
[4/20] For example, here's a string hashing function utilised by recent #qakbot samples.

Note the heavy usage of mathematical operators. Like xor (^), right shift (>>) and bitwise "AND" (&).

These will typically produce a unique sequence of bytecodes.
[5/20] The disassembly and bytecodes for those instructions can be used for a Yara rule.

To grab the bytecodes, Highlight the decompiled code (right), this will automatically highlight the disassembly and bytecodes (left)....
[6/20] Highlighting the entire function should be avoided, as it is only the mathematical operators that will be consistent enough between samples.

For example, by including the do/while loop, then the Jump instructions (JZ/JC etc) would also be included in the disassembly....
[7/20] ... Cont'd

Jumps (JZ/JC/JNZ) == inconsistent Byte Values == not good for a Yara rule.

If a jump is accidentally included, it can be manually unselected in the #Ghidra disassembly window.

The final result should look like this.
[8/20] At this point, it's useful to obtain multiple samples of the same malware. In order to check that the remaining selected bytes are the same between samples.

With #qakbot, this value (red) does change between samples. It's important to account for this in the final rule.
[9/20] The bytecodes are easily obtained using #Ghidra.

Highlight-> Right-Click -> Copy Special -> Byte String.

This copies the highlighted code in a format that can be used by #Yara.
[10/20] The bytes can then be pasted directly into a #Yara template.

I'll keep the rule as minimal as possible to demonstrate the concept.

(IRL - Filters would be added to improve performance)
[11/20] Running the rule from there, it successfully finds the original sample. But other related samples (3 others) in the same folder remain undetected.
[11/20] This is due to the issue mentioned, where bytes unrelated to mathematical operations can differ between similar samples.

Using #Ghidra to compare two samples from the same Qakbot campaign, there are minor differences that are enough to break the original Yara rule Hashing instructions between two Qakbot samples.
[12/20] To correct this, wildcards can be added to the bytes that differ between samples.

An example of this can be seen below. The new Yara rule is *mostly* the same, but with a few wildcards (??) added where the bytes differed.
[13/20] With the new changes saved, the rule can be re-run and multiple samples are now detected. Adding wildcards can be necessary to improve the yara rule.
[14/20] Running the rule against running processes is able to identify where #Qakbot has successfully injected itself.

Qakbot likes to inject into OneDriveSetup.exe, so this is likely a True Positive.

(Very useful when combined with #DFIR tooling)
[15/20] Qakbot was used for this example, but the concept works well across other malware families.

#IcedID is a good example where the unique encryption
can be used for detection.
[16/20] Now for a few notable and important caveats....

{1}: This technique is generally only effective against unpacked payloads or in situations where the malware is already executing in memory.

Detecting packed files on disk will typically require a different approach.
[17/20]

{2} - Technically the same approach can be applied to the bytecodes of unpacking routines used by loaders. But that tends to be more complex and a topic for another day.
[18/20]

{3}: The final rule has been kept simple to demonstrate the concept.

Although technically accurate for the use case, it lacks filters to perform quickly and without consuming large amounts of CPU. This would likely need to be adjusted in a production environment.
[19/20]

{4} - Malware authors can avoid this type of detection by updating encryption/hashing logic with each sample (or by introducing randomised junk instructions between the "real" code).
[20/20]

{5} - In-memory masking (like Foliage) will defend against this type of detection very well.

I'm yet to see this implemented by the major malware families, but it has been implemented (very effectively) by #HavocC2 and #BruteRatel.
[21] The base rule can be found on my Github here.

github.com/embee-research…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Matthew

Matthew Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @embee_research

Mar 24
Malware Deobfuscation With DnSpy and CyberChef 👨‍🍳

Let's look at some beginner tips for identifying encrypted data with DnSpy.

We'll then utilise CyberChef to recreate the decryption and obtain the address of the C2 server.

[1/12] 🧵

#malware #cyberchef
Image
Image
[2/] Finding encrypted strings

Config values are often initialised (and decrypted) early in execution. Hence the Entry Point is a great place to begin looking.

(In Dnspy, we can select a loaded file and use "Go To Entry Point")

Image
Image
Image
3/ Identifying Config Values

After locating the entry point, we can begin looking for signs of encrypted or obfuscated values.

The first method "Run" contains suspicious references to “”, as well as Arguments.IP and Arguments.Key StringDecrypt.Read
Image
Read 13 tweets
Jan 2
🔥Lumma Stealer - Manually Unpacking and Extracting C2's 🔥

Let's analyse a Lumma malware sample and manually unpack it with Dnspy and x32dbg.

We'll then leverage Ghidra and x32dbg to locate and decrypt four C2 addresses.

[1/24] 🖊️

#Malwareanalysis #Ghidra


Image
Image
Image
Image
1/ The initial sample can be found on Malware Bazaar

You can download it here if you'd like to follow along

bazaar.abuse.ch/sample/0ee580f…
2/ Checking What Were Up Against

Prior to manual analysis, we can submit the hash on @unpacme. This gives us an idea of how many "layers" of packing are present.

The initial sample is .NET based, but the payload is not. (This means we'll need both Dnspy and #Ghidra)
Image
Image
Read 25 tweets
Dec 30, 2023
How to Increase Your Engagement on Cyber-related Twitter Content ✏️

Tips and tricks learnt from writing about #malware on Twitter for 18 months😄

[1/9]
1/ Optimal Post Structure

Statement -> Explanation -> Conclusion

I've found this general flow is the easiest to create and for readers to follow. It follows the 1-2-1 structure by @Nicolascole77

If you don't yet have a post style/structure, this is a great place to start. Image
2/ Aim for Reposts Over Followers

The highest engagement posts are those that get reshared by large accounts.

You don't need a lot of followers for high engagement, as long as you can get reposted by someone who does. Image
Read 10 tweets
Dec 21, 2023
🔬Defeating Obfuscated .HTA Scripts to Obtain Cobalt Strike Shellcode 🔬

Let's look at Cyberchef, Manual Deobfuscation, Multi-stage script analysis and finally emulation to obtain a decoded C2.

[1/17]


Image
Image
Image
Image
2/ The analysis begins with obfuscated .hta script obtained from Malware Bazaar.

You can obtain the file here if you want to follow along.

bazaar.abuse.ch/sample/2c683d1…
3/ Initial Script Review.

The initial script contains multiple obfuscated strings, a partial powershell command, and a large blob of base64 text.
Image
Image
Read 17 tweets
Dec 20, 2023
🥷Defeating Obfuscated Malware 🥷

Today we take a look at a heavily obfuscated visual basic script containing Shellcode.

We'll use Regex, #Cyberchef and a Text Editor to deobfuscate #malware.

[1/18]


Image
Image
Image
Image
[2/] Initial #Malware Analysis

The initial script contains a mix of obfuscated and readable code.

The readable code contains references to Excel and Wscript. Image
[3/] Ignoring Plaintext

The plaintext code (in this case) suggests "how" the code is executed and not "what" is executed.

For this reason we can usually skip this component and focus on the obfuscated code.
Read 18 tweets
Dec 6, 2023
🐉Manual Shellcode Analysis Using Ghidra and x32dbg 🐉

17 tips for getting started with manual shellcode analysis (no relying on emulation to do the hard work 💪).

#Malware #Ghidra


Image
Image
Image
Image
[2/] Obtaining the Sample

If you want to follow along, you can obtain the sample from #Malware Bazaar using the link below.

bazaar.abuse.ch/sample/26f9955…
[3/] Verifying With Emulation

Prior to analysing with Ghidra or "advanced" tooling, it's a good idea to use an emulator (Like SpeakEasy) for verifying the code functionality.

Now you'll know what to look for in #Ghidra, x32dbg or other tooling. Image
Read 18 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(