software attacks lorenzo dematté software attacks advanced buffer overflow: heap smashing

25
Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Upload: brooke-gallagher

Post on 16-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Software attacks

Advanced buffer overflow:heap smashing

Page 2: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Heap overflow

void func(void* arg, int len) {char* ptr = malloc(100);memcpy(ptr, arg, len); //buffer overflow if len>100...

}

Overflows on the heap can be exploited as well as stack overflows

However, this is a data-overwrite attack. How can we use it to inject an arc (redirect program control flow) and allow arbitrary code execution? We don’t have RETs on the heap!

=> This is possible due to arbitrary memory writing as we’ll see later

Page 3: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Heap overflow• One paper on secure coding suggested

to solve the problem of stack overflows moving the buffers to heap!

• This is not the solution: exploiting a heap overflow is harder, but feasible. See:– Heap Buffer Overrun in JPEG Processing (GDI+)

Allows Code Execution (MS04-028) (it’s sufficient to preview image in explorer – IE) gdiplus.dll

– Windows RPC DCOM Long Filename Heap Overflow Exploit (MS03-039) rpcss.exe

Page 4: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Heap overflow

• They are much more architecture dependent than stack overflows

• Heap is a more complex architecture – its layout is influenced by the OS and by the C-C++ runtime

• We are going to explore some possible exploits for Win32

• A lot of exploits for linux-gcc exist as well (w00w00 on heap)<- room for a

seminary!

Page 5: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap APIThe Virtual Memory Manager allocates and deallocates

with the granularity of one page (4k)

Process Address Space0x00000000

0x7fffffff

VirtualAlloc(lpAddress, 2, AllocationType, flProtect );

Only 2 bytesA whole page (4k) is allocated

Even the stack region grows and shrink in pages (done automatically by the OS)

Page 6: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap API

• We need a more flexible structure!! We want not to waste space and have allocations of few byteschar* buff = malloc(2 * sizeof(char));

and of MBsvoid* buff = malloc(1048576);

• This is done using the structure called Heap• API for the heap is provided by the C

runtime library (malloc, free, delete, new)• In Win32, Heap API is provided by the OS

too!

Page 7: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap API

Kernel32.dll GlobalAlloc LocalAlloc HeapAlloc

RtlAllocateHeap

malloc newmsvcrt.dll

ntdll.dll

GlobalFree LocalFree HeapFree

RtlFreeHeap

free deletemsvcrt.dll

ntdll.dll

Kernel32.dll

Page 8: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap structures• In order to handle different size, and cope

well with problems (fragmentation) the Heap has a complex structure

• It uses several algorithms too: – Allocation algorithm (different strategies for

different size)– Free algorithm– Coalesce (fusion of 2 adiacent free segments)

Page 9: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap structures

PEB

Default HeapDefault Heap

0x0010 Default Heap

0x0080 HeapsCount

0x0090 Heap List

0x70000

0x170000

This is the structure that holds Process properties. Its location is fixed at 7ffdf000 (or go via TEB -> PEB)mov eax, dword ptr fs:[18]

mov eax, dword ptr[eax+0x30]

So we can easily access to the Heap address. This will be useful later!

Page 10: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap structures

SegmentsSegments

Look aside Table

Segment Table

Free ListsTable

Free list usagebit map

Virtual Allocation list

typedef struct _LIST_ENTRY { struct _LIST_ENTRY *Flink; struct _LIST_ENTRY *Blink;} LIST_ENTRY;

Page 11: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap structures

Management Structures When a heap is first created there are two pointers that point to the first free block set in FreeList[0]. Assuming the heap base address is 0x00350000 then first available block can be found at 0x00350688.

0x00350178 (FreeList[0].Flink) = 0x00350688 (First Free Block)0x0035017C (FreeList[0].Blink) = 0x00350688 (First Free Block)

0x00350688 (First Free Block) = 0x00350178 (FreeList[0])0x0035068C (First Free Block+4) = 0x00350178 (FreeList[0])

The heap management structures reside in the heap!

Page 12: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap structures

When an allocation occurs these pointers are updated accordingly. As more allocations and frees occur these pointers are continually updated and in this fashion allocated blocks are tracked in a doubly linked list.

When a heap based buffer is overflowed the control information is overwritten

When the buffer (allocated block) is freed and it comes to updating the pointers in the FreeList array there’s going to be an access violation

Page 13: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap structures

Access violation / Memory overwriteObserve the following code:

mov dword ptr [ecx],eax

If we own both EAX and ECX we have an arbitrary DWORD overwrite. We can overwrite the data at any 32bit address with a 32bit value of our choosing.

In RtlHeapFree we have such a line of code!!

Page 14: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Heap Structures

this

this

this

The instructions to remove an entry from a double linked list are:

prev_chunk->FLink = next_chunknext_chunk->BLink = prev_chunk

Where next_chunk is this->FLink and prev_chunk is this->BLink

Page 15: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Heap smashing

If we assume that prev_chunk->FLink is loaded in ECX (and since prev_chunk->FLink == &Prev_chunk + 0 in ECX is prev_chunk )

And that next_chunk is in EAX…

If we build a fake header with prev_chunk and next_chunk of our choiche we have (from the previous code)

mov dword ptr [ecx],eax

EAX (Address A) written in the address pointed by ECX (Address B)

Arbitrary memory overwrite will happen on free of our faked chunk!

Page 16: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Heap smashing

ntdll!RtlpCoalesceFreeBlocks+0x2fb:

784ac8d4 call ntdll!RtlpUpdateIndexRemoveBlock (784624a3)

784ac8d9 mov ecx,[edi+0xc]

784ac8dc mov eax,[edi+0x8]

784ac8df cmp eax,ecx

784ac8e1 mov [ecx],eax ds:0023:f1f1f1f1=????????

784ac8e3 mov [eax+0x4],ecx

In fact, two heap algorithms (Free of very large (I.e. virtually allocated) blocks and coalesce) adjust the list in this way!!

Page 17: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap structures

Previous chunk size

Self SizeSegment

IndexFlags

Unusedbytes

Tag index(Debug)

0 1 2 3 4 5 6 7 8

01 – Busy02 – Extra present04 – Fill pattern08 – Virtual Alloc10 – Last entry20 – FFU140 – FFU280 – Don’t coalesce

Page 18: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap structuresFree chunk structure – 16 Bytes

Previous chunk size

Self SizeSegment

IndexFlags

Unusedbytes

Tag index(Debug)

0 1 2 3 4 5 6 7 8

Next chunk Previous chunk

Page 19: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Heap smashing

• Exploit: fake a freed buffer• Utilize coalescing algorithms of the heap• Arbitrary overwrite happens when either the

overflowed buffer gets freed (usually guaranteed) or when the buffer AFTER the faked buffer gets freed

Previous chunk Size

<40 0x40

Address A Address B

40 – FFU2

This is overwritten on the old control structure (overflow)

Our buffer

Page 20: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

The Heap structuresVirtually Allocated chunk structure – 32 Bytes

0 1 2 3 4 5 6 7 8

Previous chunk size

Self SizeSegment

IndexFlags

Unusedbytes

Tag index(Debug)

Next chunk Previous chunk

Commit size Reserve size

Page 21: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Heap smashing

• Exploit: fake a virtual allocated block• Arbitrary memory overwrite will happen when the

buffer we faked is freed (the one next to the overflowed buffer)

< 0x40 | 9

Address A Address B

01 – Busy08 – Virtual Alloc

This is written at the end of our buffer

This is overwritten on the old control structure (overflow)

Page 22: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Heap smashing: coalesce algo

Buffer already freedBuffer removed from free list

This buffer is just freed

Buffer placed back in free list

A B C

B.BLink.FLink (A.FLink) = B.Flink (C)B.FLink.Blink (C.BLink) = B.Blink (A)

*(B.Blink) = B.Flink=> *AddressB = AddressA

Arbitrary memory overwrite!

Page 23: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Heap smashing: repairing the heap

Many of the Windows API calls use the default process heap. After the overflow the heap is corrupt, so there will be surely an access violation. We then repair the heap following Litchfield’s method: we reset the heap making it “appear” as if it is a fresh new heap.

Page 24: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Heap smashing: repairing the heap

• Get a pointer to the Thread Information Block at fs:[18]• Get a pointer to the Process Environment Block from the

TEB.• Get a pointer to the default process heap from the PEB• We now have a pointer to the heap. Read the

TotalFreeSize dword of the heap structure (at offset 0x28)• Write this to our heap control structure. • In the heap control structure, also set the flags to 0x14

(first segment) and and the next 2 bytes to 0• At heap_base+0x178 we have FreeLists[0]. Set

FreeLists[0].Flink and write edx into FreeLists[0].Blink to this

• Finally set the pointers at the end of our block to point to FreeLists[0]

Page 25: Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing

Software attacks

Lorenzo Dematté

Arbitrary memory write

• So, as in the data pointer overwrite, we have an arbitrary DWORD memory overwrite

• This can be used to overwrite particular function pointers (Exception handlers VEH + SEH, atexit(), stack cookie exception handler, Lock and Win32k function pointers in PEB)

• These arguments will be place for a seminar by one of you! =)