A bit more of PE

69
a bit more of PE (since Hashdays) Ange Albertini 22 th June 2012

description

Hack in Paris 2012

Transcript of A bit more of PE

Page 1: A bit more of PE

a bit more of

PE(since Hashdays)

Ange Albertini 22th June 2012

Page 2: A bit more of PE

Author● reverse engineer

● since dos 3.21● ashamed by a malware● back to my studies

● shared on my site

Page 3: A bit more of PE

http://CORKAMI.com

Page 4: A bit more of PE

Fact → PoC

Page 5: A bit more of PE

made with love● Hand-made, from scratch

● patched generated compiled● tedious

– full control

● Pin-pointed● Crystal clear● Clean

Page 6: A bit more of PE

technical

Page 7: A bit more of PE

be nice to your friends● ads log-in pay-wall columns● BSD/CC BY licence

● reusable commercially● free sources, using free tools

● reviews, comments, suggestions● free binaries

● downloadable in one click● free documents

● including all the graphics

Page 8: A bit more of PE

free

Page 9: A bit more of PE

goals● advertisement

● for my own use● a good reference

● learn. remember. teach.● a meaningful test set

● failed all tools● clean

Page 10: A bit more of PE

PoCs → Wiki Pageenough

→ presentation

Page 11: A bit more of PE

a graphic is worth 1000 lines of doc

Page 12: A bit more of PE

useful

Page 13: A bit more of PE

technicalfree

useful

Ange ↔ Corkami

Page 14: A bit more of PE

Agenda

1.What's a PE?● yet another doc?

2.Static oddities3.Dynamic oddities

Page 15: A bit more of PE

Introduction

Page 16: A bit more of PE

Portable Executable

Common Object File Format

Page 17: A bit more of PE
Page 18: A bit more of PE
Page 19: A bit more of PE
Page 20: A bit more of PE
Page 21: A bit more of PE
Page 22: A bit more of PE
Page 23: A bit more of PE

PEuniversal windows binary

Page 24: A bit more of PE

pe101.corkami.compe101.corkami.com

Page 25: A bit more of PE

questions?

Page 26: A bit more of PE

FASTEN YOURSEATBELTS

Page 27: A bit more of PE

pe.corkami.compe.corkami.com

Page 28: A bit more of PE

incomplete

specsspecsVS.VS.

reality of the

OSOS

Page 29: A bit more of PE
Page 30: A bit more of PE
Page 31: A bit more of PE
Page 32: A bit more of PE
Page 33: A bit more of PE

FAILFAIL

Page 34: A bit more of PE

is there a perfect documentation?

Page 35: A bit more of PE
Page 36: A bit more of PE

Not at Microsoft, at least :)

Page 37: A bit more of PE

Other documentations?● mostly based on existing files● no PoCs anyway

● messy/limited/private

Corkami's is perfect?● no!

– just a hobby● explain everything

– highlight oddities

Page 38: A bit more of PE

just to make sure

standard PE:● Sections● EntryPoint● Imports

Page 39: A bit more of PE
Page 40: A bit more of PE

Static oddities

Page 41: A bit more of PE

most basic PE● 'DataFile PE'

● LoadlibraryEx with LOAD_LIBRARY_AS_DATAFILE● must be a PE● just a PE

● 'MZ' / e_lfanew / 'PE'. that's it● machine magic imagebase alignments subsystem● code!● non-null!● break parsers

– Corrupt values/truncated headers

Page 42: A bit more of PE
Page 43: A bit more of PE

back to 'classic' PEs

Page 44: A bit more of PE

DOS header● Good old 16b stub

● still in Windows 7 64b !● “This program cannot be run in DOS mode.” ?

Page 45: A bit more of PE

ImageBase● multiple of 0x10000● user-mode

● any address except system DLLs● 00000000 under XP

● kernel-mode● via relocation● relocated to 10000● CVE-2012-2273

Page 46: A bit more of PE

EntryPoint● null

● MZ => dec ebp/pop edx

Page 47: A bit more of PE

EntryPoint● virtual

● 00 C0 => add al, al

Page 48: A bit more of PE

EntryPoint● external

● in a DLL / allocated via TLS

Page 49: A bit more of PE

EntryPoint● ignored

● via TLS

Page 50: A bit more of PE

Subsystem● no trick :(

● last required element of the header● no specific requirements

● low alignments– unpack drivers in user-mode– multi-subsystem PE

Page 51: A bit more of PE

Sections

● 0-96/65536● oversized or not (up to 0x74xx0000)● sections in sections, duplicates, shuffled

Page 52: A bit more of PE
Page 53: A bit more of PE

Dynamic oddities

Page 54: A bit more of PE

loading process 1/2● Headers are parsed on disk● Data directories are parsed in memory

● after section mapping

Page 55: A bit more of PE

loading process 2/2● sections overlap header

● true Data directories are revealed

Page 56: A bit more of PE

TLS 1/2● list of callbacks, updated on the fly● executed at threat start/stop

● before EntryPoint● after ExitProcess

● can trigger unhandled exceptions

Page 57: A bit more of PE

TLS 2/2● points to import● tricky execution conditions● different loading order● 'anything but ESI'

Page 58: A bit more of PE

Relocations● rebase code if loaded at different address

● not required in x64● empty relocations still in x64b binaries

Page 59: A bit more of PE

faked relocations

Page 60: A bit more of PE

manual relocations

Page 61: A bit more of PE

Relocations encryption● applied anywhere

● encryption● on itself!

● MIPS supported on Intel OS+PE

Page 62: A bit more of PE

Relocations on ImageBase● affects the EntryPoint

Page 63: A bit more of PE

one last...

Page 64: A bit more of PE

Conclusion● PE is a mess

● different OSes, different parsers● no doc/tool is perfect

● still many unknowns

● simple http://pe101.corkami.com● advanced http://pe.corkami.com

● 160+ PoCs

Page 65: A bit more of PE

Acknowledgments

● Peter Ferrie● Bernhard Treutwein, Costin Ionescu, Deroko,

Ivanlef0u, Kris Kaspersky, Moritz Kroll, ReversingLabs, Walied Assar, ...

Questions?

Page 66: A bit more of PE

Thank YOU!

@ange4771@ange4771Ange Albertini @gmail.com

Page 67: A bit more of PE
Page 68: A bit more of PE
Page 69: A bit more of PE