GNISREVER A POLYMORPHIC FILE-INFECTING RANSOMWARE … 1... · Malware Honeycomb virus POS bot...

Preview:

Citation preview

© Copyright Fortinet Inc. All rights reserved.

GNISREVER A POLYMORPHIC FILE-INFECTING RANSOMWARE November 10-11, 2016

Raul Alvarez

2

About Me

Senior Security

Researcher @ Fortinet

22 published articles in

Virus Bulletin

Regular contributor in our

company blog

Confidential

Malware Categories

4

trojan

script/

macro

Malware Honeycomb

virus POS

bot

botnet

worm ransom

ware

5

trojan

script/

macro

Virlock

virus POS

bot

botnet

worm ransom

ware

Agenda

7

Agenda

Confidential

Virlock as a common malware

• Reversing stages

• Metamorphic algorithm

Virlock as a file infector

• Detection

• Extracting the host file

Virlock as a polymorphic malware

• On-demand polymorphic algorithm

Virlock as a ransomware

• Visible signs

• Unlocking

Virlock

9

What Is A File Infector?

Confidential

Attaches the malware code into the host file.

Appending, prepending, and cavity type

Maintains persistency within the computer system

Infected file is hard to restore

10

What Is A Ransomware?

Confidential

Holds your computer for ransom

Encrypts files

Uses cryptocurrency, such as bitcoins, for payment

11

What Is Virlock?

Confidential

A ransomware

A file infector

Uses on-demand polymorphic algorithm

Uses metamorphic algorithm

Locks your screen

12

13

14

15

16

17

18

19 Confidential

Just kidding!

20

Debugging Tools

Confidential

ollydbg

http://www.ollydbg.de/

immunity debugger

http://www.immunityinc.com/p

roducts/debugger/

x64dbg

http://x64dbg.com/

Virlock As A Common Malware

22

Common Reversing

Confidential

encrypted/

packed

decryptor

MZ header

Decrypting/Unpacking

malware using a

debugger

decrypted/

unpacked

decryptor

MZ header

1

Static/Dynamic

Analysis

2

Reversing Stages

24

Reversing Stages

Confidential

.text 0xbb000

.rsrc 0x01200

A

.text

.rsrc 0x01200

B

metamorphic

algorithm 0x06C77

MZ header MZ header

.text

.rsrc 0x01200

C

MZ header

decoded bytes 0x0250

.text

.rsrc 0x01200

MZ header

D

metamorphic

algorithm 0x06C77

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

Virlock-infected file only

has 2 sections: .text and

.rscr

25

Reversing Stages

Confidential

.text 0xbb000

.rsrc 0x01200

A

.text

.rsrc 0x01200

B

metamorphic

algorithm 0x06C77

MZ header MZ header

.text

.rsrc 0x01200

C

MZ header

decoded bytes 0x0250

.text

.rsrc 0x01200

MZ header

D

metamorphic

algorithm 0x06C77

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

At the entry point, the

malware executes its

metamorphic algorithm.

26

Reversing Stages

Confidential

.text 0xbb000

.rsrc 0x01200

A

.text

.rsrc 0x01200

B

metamorphic

algorithm 0x06C77

MZ header MZ header

.text

.rsrc 0x01200

C

MZ header

decoded bytes 0x0250

.text

.rsrc 0x01200

MZ header

D

metamorphic

algorithm 0x06C77

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

The metamorphic

algorithm decodes

the initial decryptor.

27

Reversing Stages

Confidential

.text 0xbb000

.rsrc 0x01200

A

.text

.rsrc 0x01200

B

metamorphic

algorithm 0x06C77

MZ header MZ header

.text

.rsrc 0x01200

C

MZ header

decoded bytes 0x0250

.text

.rsrc 0x01200

MZ header

D

metamorphic

algorithm 0x06C77

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

The initial decryptor

produces the main

function.

28

Reversing Stages

Confidential

.text

.rsrc 0x01200

MZ header

E

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

F

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

G

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

H

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

on-demand poly

on-demand poly

on-demand poly

on-demand poly

Host file

The main function calls

the malicious threads

and other sub-

functions. Each sub-

function is

decrypted/re-

encrypted by individual

on-demand

polymorphic algorithm.

29

Reversing Stages

Confidential

.text

.rsrc 0x01200

MZ header

E

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

F

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

G

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

H

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

on-demand poly

on-demand poly

on-demand poly

on-demand poly

Host file

When an on-demand

polymorphic algorithm

runs, it decrypts the

malicious code and

executes them. Then

re-encrypts itself and

the malicious code

with a different key.

30

Reversing Stages

Confidential

.text

.rsrc 0x01200

MZ header

E

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

F

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

G

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

H

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

on-demand poly

on-demand poly

on-demand poly

on-demand poly

Host file

After executing the

rest of the

malicious code, the

malware in

memory looks

totally different

from its original

binary content.

31

Reversing Stages

Confidential

.text

.rsrc 0x01200

MZ header

E

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

F

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

G

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

H

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

Host file

Finally, the host file

is decrypted,

dropped, and

executed.

Metamorphic Algorithm

33

Metamorphic Algorithm

Confidential

Basics:

Putting a value(0) in a register(EAX)

MOV EAX,0 EAX register gets 0 directly

XOR EAX,EAX XORing the same register by itself also

generates a zero value placed into a given

register

SUB EAX,EAX SUBtracting any register by itself also

generates the same result.

MOV EAX, 0x10

ADD EAX, 0x10

SUB EAX, 0x20

EAX also gets 0

34

Metamorphic Algorithm

Confidential

Detection Limitation

• Hard to find similar bytes

• Unknown length of bytes

• Unpredictable code

35

Reversing Stages

Confidential

.text 0xbb000

.rsrc 0x01200

A

.text

.rsrc 0x01200

B

metamorphic

algorithm 0x06C77

MZ header MZ header

.text

.rsrc 0x01200

C

MZ header

decoded bytes 0x0250

.text

.rsrc 0x01200

MZ header

D

metamorphic

algorithm 0x06C77

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

At the entry point, the

malware executes its

metamorphic algorithm.

36

Metamorphic Algorithm (sample 1)

Confidential

...

~ 28 kilobytes

Call to the decrypted

bytes at the start of the

.text section.

Entry Point

The size of the

metamorphic code varies

per infected file.

Approximately 28kb of

code constitutes the

metamorphic algorithm

that generates the rest of

the malicious code,

including the polymorphic

algorithm.

37

Metamorphic Algorithm (sample 1)

Confidential

irrelevant bytes

decrypted bytes

first DWORD

second DWORD

third DWORD

38

Metamorphic Algorithm (sample 2)

Confidential

irrelevant bytes

first DWORD

second DWORD

third DWORD

decrypted bytes

39

Metamorphic Algorithm (sample 1)

Confidential

irrelevant bytes

decrypted bytes

first DWORD

second DWORD

third DWORD

40

Metamorphic Algorithm (sample 2)

Confidential

irrelevant bytes

first DWORD

second DWORD

third DWORD

decrypted bytes

41

Metamorphic Algorithm (comparison)

Confidential

Sample 1

Sample 2

first DWORD

MOV [EDI],ECX

MOV [EAX],ESI

42

Metamorphic Algorithm (comparison)

Confidential

Sample 2

second DWORD

Sample 1

MOV [EAX],EBX

MOV [EDI],EBX

43

Metamorphic Algorithm (comparison)

Confidential

Sample 2

third DWORD

Sample 1

MOV [EDX],ESI

MOV [EBX],ECX

44

Metamorphic Algorithm (detection)

Confidential

Sample 2 Sample 1

MOV EAX, --------

NOP

JMP 0040108B

Demo – Metamorphic Algorithm

Virlock As A File Infector

47

Cleaning: How To Clean An Infected File

Confidential

Basics:

• Determine the kind of virus

• Determine how to extract and restore the host

file

48

Different Kinds Of File Infectors

Confidential

Basics:

• Appending

• Prepending

• Cavity

• Overwriting

• Companion

49

Different Kinds Of File Infectors

Confidential

appending prepending cavity

overwriting companion

virlock

50

Cleaning: Extracting The Host File From Virlock

Confidential

Details:

• Host file is encrypted and embedded within the

malware

• DecryptionKey can be found within the malware

• DecryptionKey is encrypted using a simple XOR

• Uses a simple decryption algorithm to extract the

host file

51

Reversing Stages

Confidential

.text

.rsrc 0x01200

MZ header

E

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

F

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

G

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

H

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

Host file

Finally, the host file

is decrypted,

dropped, and

executed.

52

Cleaning: Extracting The Host File From Virlock

Confidential

EBX = initial key

XORing EBX with dword in [ESI]

generates the DecryptionKey

ECX = EBX = DecryptionKey

EBX = the next DWORD

ESI = location of the encrypted DecryptionKey

Decrypts the

HOST file

53

Cleaning: Extracting The Host File From Virlock

Confidential

Decrypts the

HOST file

DecryptionKey

Original Host

Filename Encrypted Host File

Decrypted Host File

Virlock As A Polymorphic Malware

55

Reversing Stages

Confidential

.text

.rsrc 0x01200

MZ header

E

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

F

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main function

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

G

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

on-demand poly

on-demand poly

on-demand poly

on-demand poly

.text

.rsrc 0x01200

MZ header

H

metamorphic

algorithm 0x06C77

decoded bytes 0x0250

main functions

on-demand poly

on-demand poly

on-demand poly

on-demand poly

Host file

When an on-demand

polymorphic algorithm

runs, it decrypts the

malicious code and

executes them. Then

re-encrypts itself and

the malicious code

with a different key.

Dynamic Routine Execution Map

57

Dynamic Routine Execution Map

Confidential

58

Dynamic Routine Execution Map

Confidential

59

Dynamic Routine Execution Map

Confidential

60

Dynamic Routine Execution Map

Confidential

61

Dynamic Routine Execution Map

Confidential

62

Dynamic Routine Execution Map

Confidential

On-Demand Polymorphic Algorithm

64

On-Demand Polymorphic Algorithm

Confidential

Implementation

• Uses Decryptor to decrypt a block of code using

an old key

• Executes the newly decrypted code

• Uses RDTSC (Read Time-Stamp Counter) to

generate a new dword value

• Uses NewKeyGenerator to generate new key

• Uses Encryptor to encrypt the same block of

code using the new key

newly decrypted

code

RDTSC

65

Decryptor

Confidential

Features:

• Uses garbage code

• Keygen function for redundancy check

• Uses XOR to generate the key

• Uses XOR to decrypt a block of code

66

Decryptor

Confidential

garbage code

# of bytes to

generate the key

actual key

generator;

EAX starts with

0xFFFFFFFF

starting location

of key-bytes

XOR decryptor keygen function

generates the

same EAX value

(key)

67

On-Demand Polymorphic Algorithm

Confidential

Implementation

• Uses Decryptor to decrypt a block of code using

an old key

• Executes the newly decrypted code

• Uses RDTSC (Read Time-Stamp Counter) to

generate a new dword value

• Uses NewKeyGenerator to generate new key

• Uses Encryptor to encrypt the same block of

code using the new key

newly decrypted

code

RDTSC

68

On-Demand Polymorphic Algorithm

Confidential

Implementation

• Uses Decryptor to decrypt a block of code using

an old key

• Executes the newly decrypted code

• Uses RDTSC (Read Time-Stamp Counter) to

generate a new dword value

• Uses NewKeyGenerator to generate new key

• Uses Encryptor to encrypt the same block of

code using the new key

newly decrypted

code

RDTSC

69

On-Demand Polymorphic Algorithm

Confidential

Implementation

• Uses Decryptor to decrypt a block of code using

an old key

• Executes the newly decrypted code

• Uses RDTSC (Read Time-Stamp Counter) to

generate a new dword value

• Uses NewKeyGenerator to generate new key

• Uses Encryptor to encrypt the same block of

code using the new key

newly decrypted

code

RDTSC

70

NewKeyGenerator

Confidential

Implementation:

• RDTSC generates a new dword value

• Saves it in different memory locations

• The memory locations are within the memory

range that contains key bytes

• Generates new key by XORing the key bytes

• Saves the new key to the original location of the

old key used in the Decryptor

71

NewKeyGenerator

Confidential

location of the

bytes needed to

generate the

new key

starting location

is one byte

before the first

DWORD value

from RDTSC

EAX = DWORD value

NEW KEY

72

NewKeyGenerator

Confidential

NEW KEY

old key

new key

location of the NEW KEY

73

On-Demand Polymorphic Algorithm

Confidential

Implementation

• Uses Decryptor to decrypt a block of code using

an old key

• Executes the newly decrypted code

• Uses RDTSC (Read Time-Stamp Counter) to

generate a new dword value

• Uses NewKeyGenerator to generate new key

• Uses Encryptor to encrypt the same block of

code using the new key

newly decrypted

code

RDTSC

74

On-Demand Polymorphic Algorithm

Confidential

Implementation

• Uses Decryptor to decrypt a block of code using

an old key

• Executes the newly decrypted code

• Uses RDTSC (Read Time-Stamp Counter) to

generate a new dword value

• Uses NewKeyGenerator to generate new key

• Uses Encryptor to encrypt the same block of

code using the new key

newly decrypted

code

RDTSC

75

Encryptor

Confidential

Features:

• Uses the same algorithm as the Decryptor

• Uses the new key to encrypt the same block of

code

76

Sample On-demand Polymorphic Values

Confidential

1C B0 19 99

F4 C7 7E 64

E2 40 7B 9A

F4 00 7B F1

E8 B0 62 02

00 C7 05 FF

16 40 00 01

00 00 00 6A

75 EB 89 E2

9D 9C EE 1F

8B 1B EB E1

9D 5B EB 8A

encrypted with OLD KEY

decrypted code

encrypted with NEW KEY

77

Detection

Confidential

1C B0 19 99

F4 C7 7E 64

E2 40 7B 9A

F4 00 7B F1

E8 B0 62 02

00 C7 05 FF

16 40 00 01

00 00 00 6A

75 EB 89 E2

9D 9C EE 1F

8B 1B EB E1

9D 5B EB 8A

encrypted with OLD KEY

decrypted code

encrypted with NEW KEY

78

Detection

Confidential

location of the

bytes needed to

generate the

new key

starting location

is one byte

before the first

DWORD value

from RDTSC

EAX = DWORD value

NEW KEY

79

Detection

Confidential

location of the

bytes needed to

generate the

new key

starting location

is one byte

before the first

DWORD value

from RDTSC

EAX = DWORD value

NEW KEY

80

Dynamic Routine Execution Map

Confidential

Virlock As A Ransomware

Visible Signs of Infection

83

84

85

86

87

88

89

Eradication

91

Eradication

Confidential

• Manually create solution to clean the infected

files

• Download reliable standalone solution to remove

the malware from the system

• Always make sure that your antivirus and

security apps are updated

92

What if your system is already locked?

Confidential

Steps:

• Reboot the system on safe mode

• Remove the malware entry on the startup

registry

• Then go back to eradication stage

Demo – Unlocking Virlock

94

Wrap Up

Confidential

• For reversing:

Set a breakpoint at the end of metamorphic

algorithm

Copy the decrypted code from memory

• For detection:

Get patterns from the decrypted code

• For cleaning:

Remove the entries from the registry keys

Extract the host file

Mulțumesc!

Recommended