How To Write Malware and Learn How To Fight It!
How To Write Malware and Learn How To Fight It!
04 Phrack Author
http://www.phrack.org/papers/dotnet_instrumentation.html
05 Passionate F# developer
https://github.com/sponsors/enkomio
whoami.exe
■ We are not talking about amateur malware (skiddies writing a .NET RAT and posting it on
HackForums)
■ Professional cyber-criminal are very well organized:
○ They have a dedicated GIT repository
○ A testing botnet
○ A customer support platform (typically in form of Jabber chat)
○ A crypto service to evade AVs
○ They use a bulletproof hosting provider for their botnet
○ VPN service to hide his/her real IP
○ A distribution network (SPAM)
○ A mule network (monetization)
How to write a malware and make money
● Ransomware
● ATM Malware
● PoS Scraper
● Banking Trojan
● Credentials Stealer
Reversing AES
Pretty easy if S-Box is not obfuscated, just use FindCrypt(2) IDA plugin to identify the code that
use the S-Box
Reversing RSA
■ Be able to code
■ Knowledge about OS
■ Knowledge about computer architecture
■ Be able to read machine code
Reversing like a PRO
00406936 | 64:A1 30000000 | mov eax,dword ptr fs:[30] Move to EAX the value of FS[30]
0040693C | 8B40 0C | mov eax,dword ptr ds:[eax+C] Move to EAX the value at address EAX+C
0040693F | 8B40 0C | mov eax,dword ptr ds:[eax+C] Move to EAX the value at address EAX+C
00406942 | 8B00 | mov eax,dword ptr ds:[eax] Move to EAX the value at address EAX
00406944 | 8B00 | mov eax,dword ptr ds:[eax] Move to EAX the value at address EAX
00406946 | 8B40 18 | mov eax,dword ptr ds:[eax+18] Move to EAX the value at address EAX + 18
return
00406949 | C3 | ret
00406936 | 64:A1 30000000 | mov eax,dword ptr fs:[30] Move to EAX the PEB address from TEB
0040693C | 8B40 0C | mov eax,dword ptr ds:[eax+C] Move to EAX the Ldr address
0040693F | 8B40 0C | mov eax,dword ptr ds:[eax+C] Move to EAX the InLoadOrderModuleList address
00406942 | 8B00 | mov eax,dword ptr ds:[eax] Move to EAX the FLink from LIST_ENTRY
00406944 | 8B00 | mov eax,dword ptr ds:[eax] Move to EAX the FLink from LIST_ENTRY
00406946 | 8B40 18 | mov eax,dword ptr ds:[eax+18] Move to EAX the DllBase of the library
Return the DllBase
00406949 | C3 | ret
Program name
This function resolves the base address of Kernel32. If you think that it’s done in order ntdll.dll
to walk the EAT (Export Address Table) and to resolve the desider function address…
... kernel32.dll
you are right! (more soon...)
One more Reversing exercise
2
3
4
5
6
Any idea?
Decompiler FTW!
© Rolf Rolles: Automation Techniques in C++ Reverse Engineering
// remove the push of the this argument and add a jump in order to avoid the call
Breaking .NET decompilers
I did this test some time ago, the decompilers may have fixed this problem in the meantime
Anti-analysis - IDA Hex-Rays decompiler
Anti-analysis - IDA Hex-Rays decompiler
Anti-analysis - IDA Hex-Rays decompiler
???
Anti-analysis - IDA Hex-Rays decompiler
■ One of the most difficult task in Reverse Engineering is to understand how the underline
computer architecture works (instruction set, calling convention, memory layout,
compiler characteristics, used Libs, …)
■ We are very used to INTEL arch on Windows OS, but what about a new unknown
architecture? This is the basic concept of VM base protection
■ A personal experiment, Sacara: https://github.com/enkomio/sacara
VM based obfuscation
+ Encrypted Opcode
VS xor eax, ebx
+ Anti-tampering
+ ...
Reverse Engineering != Reading Assembly
// setup handlers
sandbox.BeforeEmulation.Add(fun proc ->
if 0x401061 = proc.ProgramCounter.As<Int32>() then
snapshotManager.TakeSnaphot()
)
* Source: https://www.proofpoint.com/us/threat-insight/post/new-kpot-v20-stealer-brings-zero-persistence-and-memory-features-silently-steal
KPOT function resolution algorithm
1. Walk TEB->PEB->Ldr to get the base address for Kernel and ntdll. Resolve LoadLibraryA by
walking Kernel32 EAT. Use LoadLibraryA to load the desired DLLs
2. Store the DLL base address and other info in a structure composed by the following items:
<base address, number of functions to lookup, function array>
3. Parse PE and walk EAT. For each exported function compute the
MurmurHash hash and search for this value in the above array. If found store the pointer.
Goal: We want to know which are the
functions that are resolved by the malware
■ Sample SHA-256 :
67f8302a2fd28d15f62d6d20d748bfe350334e5353cbdef112bd1f8231b5599d
■ By knowing which are the used functions we can have a better picture of the malware
functionalities. Let’s emulate the previous steps in Sojobo.
Goal: We want to know which are the
functions that are resolved by the malware
At Step 1 we have the biggest problem. We need to have a valid PEB structure to correctly
emulate the execution. The Ldr field is one of the most difficult to represent since it contains a
linked list via LIST_ENTRY structure.
At lower level it is easy to manage LIST_ENTRY, but how to represent it at a high level language
like F#? Possible solution:
■ LIST_ENTRY can point to any kind of data, it is a nice use case for using inheritance!
○ We can’t do this if we consider LIST_ENTRY like a struct. Struct cannot be inherited by definition.
■ Then consider LIST_ENTRY as a class
○ We can’t do this, since it is treated like a structure (it occupy 8 bytes in x86, since it has 2 pointers).
If we define it like a class we will have a pointer during serialization (4 bytes and not 8).
■ Treat it as a struct and consider the pointed object like a generic Object class
○ Goodbye deserialization → Impossible to know during deserialization which Object type
we have to create
■ ...
Goal: We want to know which are the
functions that are resolved by the malware
Twitter: s4tan
GitHub: https://github.com/sponsors/enkomio
Contact: [email protected]