Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

INTRODUCTION What is this talk about? • Custom PE loaders are typical in malware nowadays • How common are custom formats? • We set out to answer this very question • Much like searching for new forms of life • We bring you some of our findings in this talk • But before so…

Slide 3

Slide 3 text

ABOUT US Meet the special agents Mark Lechtik Aleksandra Doniec @_marklech_ Check Point Research @hasherezade Malwarebytes

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

HIDDEN BEE

Slide 6

Slide 6 text

• Distributed by Underminer EK • Chinese miner • High and low-level elements: including a bootkit • We will focus on the high-level part: loaders in custom format HIDDEN BEE Profile

Slide 7

Slide 7 text

23 JULY 2018 MALWAREBYTES SLACK CHANNEL HIDDEN BEE Story

Slide 8

Slide 8 text

• bin/i386/core.sdb bin/i386/core.sdb 52he3kf2g2rr6l5s1as2u0198k.wasm glfw.wasm HIDDEN BEE Story

Slide 9

Slide 9 text

? This is not how the WASM file looks like... ? HIDDEN BEE Story

Slide 10

Slide 10 text

? UFO… UNIDENTIFIED FILE OBJECT FOUND HIDDEN BEE Story

Slide 11

Slide 11 text

• We don’t have: the initial loader • We have: 2 files with a consistent format • Let’s try to analyze the headers and guess what each field means • Let’s write our own loader HIDDEN BEE Story

Slide 12

Slide 12 text

typedef struct { WORD func_count; char name; } t_dll_name; ntdll.dll HIDDEN BEE Imports 0x13 0x18

Slide 13

Slide 13 text

0x0d827481 0x60 HIDDEN BEE IAT

Slide 14

Slide 14 text

DWORD checksum(char *func_name) { DWORD result = 0x1505; while ( *func_name ) result = *func_name++ + 33 * result; return result; } 0x60 0x0d827481 ntdll.dll HIDDEN BEE IAT 0x13

Slide 15

Slide 15 text

DWORD checksum(char *func_name) { DWORD result = 0x1505; while ( *func_name ) result = *func_name++ + 33 * result; return result; } 0x0d827481 778b81e0 0x0d827481 = checksum("memchr") GetProcAddress(mod, "memchr") 778b81e0 HMODULE mod = LoadLibraryA("ntdll"); HIDDEN BEE IAT 0x60 ntdll.dll 0x13

Slide 16

Slide 16 text

0x60 0x0d827481 0x13 HIDDEN BEE IAT ntdll.dll 0x13

Slide 17

Slide 17 text

0x2a62 HIDDEN BEE Entry Point

Slide 18

Slide 18 text

0x509c HIDDEN BEE File Size . . .

Slide 19

Slide 19 text

offset (0x4D78) size (0x0324) 0x0324 0x0284 HIDDEN BEE Relocations 0x0284: field to be relocated

Slide 20

Slide 20 text

https://github.com/hasherezade/bee_parser HIDDEN BEE Profile

Slide 21

Slide 21 text

OCEAN LOTUS

Slide 22

Slide 22 text

• Known as a Vietnamese APT32 group • Distributed mainly by phishing and waterholing attacks • Payloads of various types, often composed of multiple files OCEAN LOTUS Profile

Slide 23

Slide 23 text

49a2505d54c83a65bb4d 716a27438ed8f065c709 OCEAN LOTUS Sample *sample provided by @MinhTrietPT

Slide 24

Slide 24 text

The Alien Files What is this mysterious „blob”? Both files seem to be in the same format... But what are they really? SPORDER.cab It doesn’t look like a CAB format! SPORDER.blob OCEAN LOTUS

Slide 25

Slide 25 text

SPORDER.cab SPORDER.blob XOR key? OCEAN LOTUS The Alien Files

Slide 26

Slide 26 text

SPORDER.cab SPORDER.blob OCEAN LOTUS The Alien Files

Slide 27

Slide 27 text

SPORDER.cab We can find artefacts indicating that this format was converted from a PE file OCEAN LOTUS The Alien Files

Slide 28

Slide 28 text

SPORDER.cab OCEAN LOTUS The Alien Files

Slide 29

Slide 29 text

SPORDER.blob Sections: • not in the same order as typical PE • virtual format same as raw • custom headers - stripped in the virtual format OCEAN LOTUS The Alien Files

Slide 30

Slide 30 text

Raw format Header 1 The executable part (shuffled sections, converted from a PE file) Header 2 Padding The executable part (shuffled sections, converted from a PE file) Loaded imports Virtual format OCEAN LOTUS The Alien Files

Slide 31

Slide 31 text

SPORDER.dll sporder.exe SPORDER.cab SPORDER.blob hp6000.dll OCEAN LOTUS The Alien Files The loader All executables are used as a loading chain

Slide 32

Slide 32 text

We are lucky: we have the loader Let’s just look inside and see how it works! hp6000.dll One loading function for both: confirmed consistent format OCEAN LOTUS Approach

Slide 33

Slide 33 text

Header 1: before the executable part val + 0x2000 size of executable area XOR OCEAN LOTUS Format Details Allocation of executable area: Key[1] Key[0]

Slide 34

Slide 34 text

Header 2: after the executable part val * 8 + 0x400 space for loading imports OCEAN LOTUS Format Details Allocation of imports area:

Slide 35

Slide 35 text

Relocations? Imports? Exports? Entry Point? All together? Header 2: after the executable part OCEAN LOTUS Format Details What do we have here?

Slide 36

Slide 36 text

Header 2: records OCEAN LOTUS Format Details Type 1: Relocation

Slide 37

Slide 37 text

Relocation Field (0x3cab5) Header 2: records typedef struct { DWORD reloc_field; } reloc_t; Type (1) OCEAN LOTUS Format Details Type 1: Relocation

Slide 38

Slide 38 text

Header 2: records Type 2: Entry Point/Export OCEAN LOTUS Format Details

Slide 39

Slide 39 text

Header 2: records typedef struct { DWORD count; DWORD func_rva; DWORD name_rva; } entry_point_t; OCEAN LOTUS Format Details Type 2: Entry Point/Export Count (1) Function RVA (0x4900) Name RVA (0x10ad9f) IsValidLinkInfo Type (2)

Slide 40

Slide 40 text

Header 2: records Type 3: Import OCEAN LOTUS Format Details

Slide 41

Slide 41 text

Header 2: records typedef struct { DWORD type; DWORD dll_rva; DWORD func_rva; DWORD iat_rva; } import_t; OCEAN LOTUS Format Details Type 3: Import Type (3) IAT RVA (0x1000) Import By Name (2) DLL RVA (0x10a8c8) Function RVA (0x10a8aa) RegOpenKeyExW ADAVAPI32.DLL

Slide 42

Slide 42 text

OCEAN LOTUS Format Details Header 2: records Type 3: Import

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

GOZI PE Like never seen before

Slide 45

Slide 45 text

GOZI PE Like never seen before Overview • A popular banking Trojan • Also known as: ISFB, Ursnif • Source code has been leaked and reused across multiple variants • We are going to discuss Gozi version 3 • Paper by Maciek Kotowicz’s (@maciekkotowicz): https://lokalhost.pl/txt/isfbv3.pdf

Slide 46

Slide 46 text

GOZI Unpacking atypical payload Added Header PE-sieve unpacks malware by dumping the payload from memory. But unpacking Gozi v3 turned a bit problematic...

Slide 47

Slide 47 text

• In the dumped PE, we are still missing: • The Entry Point • Imports • Exports • Relocations • Let’s see how this PE looks in the memory, before it is mapped to the virtual format... GOZI

Slide 48

Slide 48 text

GOZI PE Like never seen before Load Sequence EXE EXE 1st stage loader 2nd stage loader ?

Slide 49

Slide 49 text

GOZI PE Like never seen before Modified Format Modified DOS Header Modified Section Table Original Sections Original NT Headers Original Section Table Modified Data Directories Modified PE Header Modified Headers Original PE Fields

Slide 50

Slide 50 text

GOZI PE Like never seen before Modified Format Modified DOS Header Modified Section Table Original Sections Original NT Headers Original Section Table Modified Data Directories Modified PE Header

Slide 51

Slide 51 text

GOZI PE Like never seen before Magic Checksum NT Header Offset Image Size Modified Format Magic NT Header Offset “PX” = Portable Xecutable? Modified PE Size Modified DOS Header Modified Section Table Original Sections Original NT Headers Original Section Table Modified Data Directories Modified PE Header Nt Header Size Example: Pointer to the original PE header Original NT Headers 0x958

Slide 52

Slide 52 text

GOZI PE Like never seen before Modified DOS Header Modified Format Modified Section Table Original Sections Original NT Headers Original Section Table Import Directory Header Export Directory Header Import Address Table Header Security Directory Header Unknown Directory Header Modified Data Directories Relocation Directory Header Modified PE Header Entry Point Machine ID Sections Count Original Format

Slide 53

Slide 53 text

GOZI PE Like never seen before Modified DOS Header Modified Format Modified Section Table Original Sections Original NT Headers Original Section Table Import Directory Header Export Directory Header Import Address Table Header Security Directory Header Unknown Directory Header Modified Data Directories Relocation Directory Header Modified PE Header Entry Point Machine ID Sections Count Original Format Modified Directory Headers • PX format contains only subset of the PE format directory headers • Order of appearance of directory headers is different

Slide 54

Slide 54 text

GOZI PE Like never seen before Original RVA Current RVA Modified DOS Header Modified Format Modified Section Table Original Sections Original NT Headers Original Section Table Size Import Directory Header Export Directory Header Import Address Table Header Security Directory Header Unknown Directory Header Modified Data Directories Relocation Directory Header Modified PE Header Original RVA Size Current RVA Entry Point Machine ID Sections Count Entry Point Machine ID Sections Count Example: RVA of directory in original PE (0xBD10) Size of directory (0x41) RVA in PX format (0x3B0) IMAGE_FILE_MACHINE_I386

Slide 55

Slide 55 text

GOZI PE Like never seen before • Attempt to revive the PE format with a new “flavor” • To recover the original PE, we need to dump the PX format, and use the converter https://github.com/hasherezade/funky_malware_parsers/ tree/master/isfb_parser EXE PX

Slide 56

Slide 56 text

BACKSWAP BMP with a twist Overview • Banking malware revealed by ESET at May 2018 • Known to target banks in Poland, Spain and Czech Republic • Derivative of the Tinba banking malware • Known to use innovative techniques to avoid detection • Comes in an executable that diverts execution to a weird blob

Slide 57

Slide 57 text

BACKSWAP BMP with a twist Bitmap Header Pixel Data File Header BMP format outline

Slide 58

Slide 58 text

BACKSWAP BMP with a twist File Header signature size padding data start ‘B’‘M’ 00 00 00 00 76 00 00 00 FF004000

Slide 59

Slide 59 text

BACKSWAP BMP with a twist File Header signature size padding data start ‘B’ 00 00 00 00 76 00 00 00 FF004000 ‘M’ It is possible to modify the size field so that it will represent a jump instruction!

Slide 60

Slide 60 text

DE 54 E9 42 4D BACKSWAP BMP with a twist File Header signature size padding data start INC EDX DEC EBP 00 00 00 00 76 00 00 00 00 00 JMP offset It is possible to modify the size field so that it will represent a jump instruction!

Slide 61

Slide 61 text

BACKSWAP BMP with a twist Loader Code section Custom import table resolve decode Function Address Hash Function Address Hash Function Address Hash Function Address Hash Function Address Hash Bitmap Header Pixel Data File Header jump Malware format outline

Slide 62

Slide 62 text

BACKSWAP BMP with a twist Loader Code section Custom import table Bitmap Header Pixel Data File Header When we try to visualize the BMP, we realize it’s a polyglot polyglot binary represented by several formats, depending on how you look at it @angealbertini

Slide 63

Slide 63 text

BACKSWAP BMP with a twist Loader Code section Custom import table Bitmap Header Pixel Data File Header Malware Payload

Slide 64

Slide 64 text

BACKSWAP BMP with a twist Loader Code section Custom import table Bitmap Header Pixel Data File Header Malware Payload Eric Cartmen

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

ALIENS AMONG US Binary Format you Never Heard of • Typical format in AIX • IBM proprietary Unix OS • AIX is Deployed on various platforms, usually as a server • Example: Payment Switch Servers • Transaction processing and routing decisions Auxiliary Header File Header .text .bss Section Raw Data Section Headers .text relocations .loader relocations Relocation data … XCOFF (Extended Common Object File Format) .data .loader Line Number Info Symbol Table String Table

Slide 67

Slide 67 text

ALIENS AMONG US Operation Fast Cash Payment Switch Server Payment Switch Application Injected XCOFF Implant XCOFF Malware Hooks (send, recv) Install Compare Request Response applist.dat Encrypted list of attacker controlled Primary Account Numbers Fraudulent Cash Withdrawal Attempt Courtesy of Lazarus Money Mule Credit Card Very nice analysis of the XCOFF malware by Frank Boldewin (@r3c0nst)!

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

FINAL NOTES Funky formats are a spectrum

Slide 70

Slide 70 text

FINAL NOTES Funky formats are a spectrum

Slide 71

Slide 71 text

FINAL NOTES Custom formats make static detection problematic....

Slide 72

Slide 72 text

FINAL NOTES What does it take to make a custom format? What do we need to make code run? • Imports • Relocation If you can make a custom PE loader, you can take a step forward and make a custom format loader

Slide 73

Slide 73 text

THANK YOU Questions? Mark Lechtik Aleksandra Doniec @_marklech_ @hasherezade

Slide 74

Slide 74 text

APPENDIX • Malware analysis of Hidden Bee’s custom format, by Aleksandra (@hasherezade): • https://blog.malwarebytes.com/threat-analysis/2018/08/reversing-malware-in-a- custom-format-hidden-bee-elements/ • Paper by Maciek Kotowicz’s (@maciekkotowicz) on Gozi custom format: https://lokalhost.pl/txt/isfbv3.pdf • Backswap publication by Itay Cohen (@megabeets_): • https://research.checkpoint.com/the-evolution-of-backswap/ • Analysis of Lazarus XCOFF malware (Operation Fast Cash) by Frank Boldewin: • https://github.com/fboldewin/FastCashMalwareDissected/blob/master/Operation %20Fast%20Cash%20- %20Hidden%20Cobra%E2%80%98s%20AIX%20PowerPC%20malware%20dissected. pdf • Github repository with tools for analyzing the discussed formats: https://github.com/hasherezade/funky_malware_parsers