Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running...

93
Inspecting and Manipulating binaries

Transcript of Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running...

Page 1: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Inspecting and Manipulating binaries

Page 2: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Introduction.

x86 architecture. Assembler.

Binary inspection.

General sample (crackme)

Binary manipulation.

Python to the rescue!

Malware analysis

What we (you) are going to do 2

Page 3: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Welcome to the city of death 3

Page 4: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Braaaaiiinnnssss 4

$ whoami

4

Page 5: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Binary Reverse Engineering Malware Programming The walking dead

5

$ whoami

Binary Reverse Engineering

5

Page 6: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Yeah, but what is reversing ???

6

Kind of a reconstruction

Code Binary

int main(int argc, char **argv) push ebp

{ mov ebp, esp

int gv = 0; // global mov dword_403394, 0

[…] […]

Page 7: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Requisites

ASM knowledge

Binary format (PE32, etc.)

OS Knowledge

Patience ;)

so, i want to do some reversing

7

Page 8: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

At the beginning, there was c0de

#include "stdafx.h"

#include <iostream> using namespace std;

int add(int x, int y)

{ return x + y; }

int main() {

int a = 0, b = 0; cout << "Enter a: ";

[…] return 0; }

8

Page 9: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

The compiler fucks it all up

9

Page 10: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

compiling is like...

… that scene of Apollo XIII

10

Page 11: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

It may hurt a little bit… 11

Page 12: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Bytes :: opcodes

12

Page 13: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Opcodes :: basic blocks

13

Page 14: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Basic blocks :: functions

14

Page 15: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Cpu registers

„General purpose“ registers:

eax, ebx, ecx, edx, esi, edi, ebp, esp

32 bits (64 bits in x86_64)

Some of them have special uses.

„The rootkit arsenal“ Bill Blunden

It may hurt a little bit… 15

Page 16: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Cpu registers

Not so „general purpose“ registers:

eax: Arithmetic and function return values

ecx: counter (loops, etc.)

esi, edi: src, dst in memcpy, strcpy, etc.

ebp, esp: stack operations :)

and more...

It may hurt a little bit… 16

Page 17: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Some common instructions

Read

mov eax, [ecx]

mov eax, [00401000]

Write

mov ebx, 0x20

mov [ebx+0x1C], ecx

It may hurt a little bit… 17

Page 18: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Some common instructions

push

push 0x100

push dword_1377

pop

pop ecx

pop 0x00c0ffee

It may hurt a little bit… 18

Page 19: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Some common instructions

inc, dec

mul, div Use your imagination :)

add, sub

add esp, 0x08

sub esp, 0x1c

It may hurt a little bit… 19

Page 20: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Some common instructions

lea dst, src

lea eax, [esi*2]

lea ecx, [esi+ecx]

shl, shr

shl eax, 2

It may hurt a little bit… 20

Page 21: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Some common instructions

jxx dst

jmp 0xbadc0de

jnz eax

ja 0xc0ffee

jl ebx

t

call 0x00412F1E

It may hurt a little bit… 21

Page 22: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Some common instructions

cmp dst, src

cmp eax, ebx

cmp ecx, 0xFF

test dst, src

test ecx, edx

It may hurt a little bit… 22

Page 23: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Important segments

.data: statically allocated *initialized*

int g = 1; char str[] = „yomamma“;

.bss: statically allocated *UNinitialized*

int var; char *ptr;

.rdata: read-only data (const c = 0)

It may hurt a little bit… 23

Page 24: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

It‘s all about the .(r)data

It may hurt a little bit… 24

Page 25: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Moar data

It may hurt a little bit… 25

Page 26: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Moar data

It may hurt a little bit… 26

Page 27: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Imports and all that stuff…

It may hurt a little bit… 27

Page 28: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

The problem with imports

Problem: O(n2) vs. O(n)

„Thunks and dynamic resolution makes the binary *portable* between Windows versions.

XP and Win7 don‘t have the same addresses for all

kernel32 functions

It may hurt a little bit… 28

Page 29: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

thunks Regular call

Opcode: 0xE8 + offset

Memcpy = thunk (nur jmp)

Not resolved yet

It may hurt a little bit… 29

Page 30: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

“direct“ import calls call ds:xxx

Opcodes: 0xFF15 + absolut address

Not resolved yet

It may hurt a little bit… 30

Page 31: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Page based (4 KB pages, arch dependent)

Pages (virtual mem.) -> page tables -> page frames

(physical mem.)

Pages have several attributes:

User / Supervisor (Kernel)

read-only, read-write, read-execute, etc.

Memory is not disk, dough! 31

Page 32: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Picture: CC Hameed, (http://blogs.technet.com) 32

Page 33: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Picture: Bill Blunden (The Rootkit Arsenal) 33

Page 34: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Application starts. Process is generated. Process = running instance of an application.

Processes are separated from each other.

Every process gets its own virtual address space (32-bit: 4 GB)

A process is a container (own VM, Handles, Threads (min.1)) Thread = context execution of a process.

Multithreading.

Shares system resources (Code, Data, Handles)

Own stack, though.

Thread Local Storage.

Threads (within a process) can share memory.

Processes and Threads 34

Page 35: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Important data structures in every process

PEB: Process Environment Block (1 pro Process) Location of executable (ImageBase)

Information about DLLs

Information regarding the heap

TEB: Thread Environment Block (1 pro Thread) Location of the PEB

Location of the stack

Pointer to first SEH Chain entry

PEB vs. TEB 35

Page 36: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Process memory segmentation:

Code (.text): like the segment on disk

Data (.data): like the segment on disk

Stack: function arguments, local variables

Grows towards lower addresses

Defined through top (ESP) and bottom (EBP)

PUSH vs POP (dword)

Heap: managed by Allocator/Deallocator algorithms

Two new segments 36

Page 37: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

4GB

2GB Picture courtesy of CORELAN Copyright (c) Corelan GCV

37

Use

r la

nd

K

ern

el l

and

Page 38: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Open cmd.exe in ImmunityDebugger

Time to take a peek 38

Page 39: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Time to take a peek 39

Page 40: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Time to take a peek 40

Page 41: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

The stack . Push & pop.

It may hurt a little bit… 41

Page 42: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Function prologue

It may hurt a little bit… 42

Page 43: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

function calls

Different types:

cdecl (C progs, variable arg number)

Caller* is responsible for adjusting the stack.

How many args were there? Unknown

stdcall (windows API, fixed arg number)

Function self adjusts the stack before return.

It's clear in advance how many arguments

_funcName@nrBytes

It may hurt a little bit… 43

Page 44: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Argument passing

It may hurt a little bit… 44

Page 45: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Argument passing ( by ref )

It may hurt a little bit… 45

Page 46: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Argument passing ( by ref )

It may hurt a little bit… 46

Page 47: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Argument passing ( by value )

It may hurt a little bit… 47

Page 48: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Argument passing ( by value )

It may hurt a little bit… 48

Page 49: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Go home Compiler, you are drunk

#include <stdio.h> int main(void) { char x = 0xff; if(x == 0xff) puts("YES"); else puts("NO"); return 0; }

sign extensions from hell 49

Page 50: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Time to take a peek 50

Page 51: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

There is NOT such thing as static reversing ONLY

Combine static (IDA) and dynamic (debugger).

The trick is to optimize the information transfer

between these two.

Best of both worlds 51

Page 52: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Examples:

IDA debugging capabilities

Dynamic Binary Instrumentation (PIN).

Import results in IDA to see code coverage

Trace with .py

Differential debugging

Best of both worlds 52

Page 53: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

53

Page 54: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Not really scary… 54

Page 55: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Don't get distracted! 55

Page 56: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Look, it works! 56

Page 57: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Loooooong function 57

Page 58: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Don't really feel like doing this manually 58

Page 59: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

We will solve this soon… intelligently 59

F*ck this shit I'm outta here!

Page 60: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Take the control! 60

Page 61: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Not elegant but effective… 61

Page 62: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

I want more… finesse… 62

Page 63: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Python to the rescue!

Tzzzzzz. Tttzzzz… 63

Page 64: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Python to the rescue!

If it‘s in twitter it must be true 64

Page 66: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Example: utorrent readfile

Page 67: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Which ReadFile ?!?!

There are several references…

Manually inspecting all is a tedious job. 67

Page 68: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets
Page 69: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Reading from a File

CreateFile(...)

Returns handle

ReadFile(handle)

CloseHandle(handle)

Page 70: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Reading from a File

It's a long shot 70

Page 71: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

And... found!

It's a long shot 71

Page 72: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Binary manipulation

Binaries can be easily modified Patched (on disk, live) Functions intercepted (hooking, live)

Usage:

Inspection (ex. Tracing) Change execution flow Whatever you can imagine

Page 73: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

What you all have been waiting for… 73

Page 74: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Braaaaiiinnnsss… I mean, credeeeennttiiiaaalllssssss… 74

So many questions…

APIs used to send() and recv() data?

Sure? Think twice

Functions I don't see?

What happens with the data received?

How does the malware achieve persistency?

Page 75: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Braaaaiiinnnsss… I mean, credeeeennttiiiaaalllssssss… 75

Page 76: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Malware are deceiving bastards… 76

Are these the

ONLY APIs

used by this malware?

Page 77: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Braaaaiiinnnsss… I mean, CPU cycles… 77

Page 78: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

I like it here. I think I‘m gonna stick for a while… 78

How does the malware

achieve

persistency?

Page 79: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Braaaaiiinnnsss… I mean, CPU cycles… 79

Page 80: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Getting in the enemy's mind 80

Page 81: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Hands on: STRINGS

Juicy info in two minutes 81

Page 82: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Hands on: imports

Very interesting imports… 82

Page 83: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Hands on: resources

The .rsrc section is perfect for hiding data 83

Page 84: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

"Crypto" stuff

Takes less time and is less brain damaging 84

Page 85: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Hands on: sneaky bastards

I see what you did there… 85

Page 86: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

86

There are so many! What Do i do?!?!?

Page 87: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Who are u gonna call? Sneakbuster!

Takes less time and is less brain damaging 87

Page 88: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Hands on: WTFTLS

NO ME GUSTA 88

Page 89: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Hands on: WTFTLS

Call, call, call … 89

Page 90: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Hands on: running it

Thanks for the info… 90

Page 91: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Hands on: running it

Here's (another?) candy for you… 91

Page 92: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Last chance! ;) 92

Page 93: Inspecting and Manipulating binariesApplication starts.Process is generated. Process = running instance of an application. Processes are separated from each other. Every process gets

Twitter: @m0n0sapiens 93