98
Schizophrenic files Ange Albertini MetaRheinMainConstructionDays MRMCD 5-7 september 2014 HS Darmstadt www.mrmcd.net 2014/09/05

Schizophrenic files v2

Embed Size (px)

DESCRIPTION

slightly reworked version

Citation preview

Page 1: Schizophrenic files v2

Schizophrenicfiles

Ange Albertini

MetaRheinMainConstructionDaysMRMCD5-7 september 2014HS Darmstadtwww.mrmcd.net

2014/09/05

Page 2: Schizophrenic files v2

Gynvael ColdwindSecurity researcher,Google

Dragon Sector captainlikes hamburgershttp://gynvael.coldwind.pl/

This talk is a collaboration with:

Page 4: Schizophrenic files v2
Page 5: Schizophrenic files v2

1 file, 2 programs⇒ 2 different contents

No active detection of the program in the file

Page 6: Schizophrenic files v2

Fooling, not failing

Both programs will load the file correctly:No reported warning or error, no exploitation.

Page 7: Schizophrenic files v2

Abusing parsers for

● fun

● bypassing security○ same-origin policy○ evade detection○ exfiltration○ signing

■ Android Master Key

Page 8: Schizophrenic files v2

ZIP

Page 9: Schizophrenic files v2

excerpt from Gynvael's talk:"Dziesięć tysięcy pułapek: ZIP, RAR, etc."

(http://gynvael.coldwind.pl/?id=523)

Page 10: Schizophrenic files v2

ZIP archives

Page 11: Schizophrenic files v2

ZIP structures are parsed from the end.

Page 12: Schizophrenic files v2

File names are actually duplicated.

Page 13: Schizophrenic files v2

Why this weird structure?

Page 14: Schizophrenic files v2

ZIP archives were commonly read & written on the fly over multiple floppies.

Page 15: Schizophrenic files v2

● creationa. create one LFH per file

Floppy full ⇒ start a new LFH on the next floppyb. when all files are finished, write CDs sequence

(1/file)c. when all CDs are written, write the EoCD

● extractiona. insert last floppy (contains the EoCD)b. insert the floppy with 1st CD

(often, the last floppy contains EoCD + all CDs)c. insert the corresponding LFH’s first floppy

insert next floppies if required

Minimize floppy swaps

Page 16: Schizophrenic files v2

ZIP was very useful,but now it’s awkward.

Newer archive formats are parsed top-down.

Page 17: Schizophrenic files v2

Position in the file

Page 18: Schizophrenic files v2

Prepended and appended data is tolerated...

Page 19: Schizophrenic files v2

...but not too much!(for obvious performance reason)

Page 20: Schizophrenic files v2

Duplicating the (relatively small) EoCD increases compatibility.

Page 21: Schizophrenic files v2

Scanning direction

Page 22: Schizophrenic files v2

If you concatenate 2 archives...

Page 23: Schizophrenic files v2

...if you parse bottom-up (standard), you find the 2nd one...

Page 24: Schizophrenic files v2

...but you will get the other archive if you parse top-down.

Page 25: Schizophrenic files v2

Superfluous headers

Page 26: Schizophrenic files v2

You could parse everything nicely...

Page 27: Schizophrenic files v2

...but in the end, only the Local File Headers matter.

Page 28: Schizophrenic files v2

1 file = 1 Local File Header

Page 29: Schizophrenic files v2

Since most ZIP archives start with a sequence of Local File Headers...

Page 30: Schizophrenic files v2

You can parse them top-down (until a break) and ignore the CD and EoCD.

Page 31: Schizophrenic files v2

Standard parsing:bottom-up + all headers

Page 32: Schizophrenic files v2

“Efficient” parsing:top-down + LFHs only

Not standard, but good enough in most cases.

Page 33: Schizophrenic files v2

Nowadays, most ZIPs area sequence of LFHs

from the start

Page 34: Schizophrenic files v2

ZIP Archive comment

Page 35: Schizophrenic files v2

The EoCD contains an optional comment field...

Page 36: Schizophrenic files v2

...that can contain a complete archive !

Page 37: Schizophrenic files v2

● Parsing direction:○ standard is bottom-up○ parsing LFHs from the start would work in most cases

● ZIP should be located near the end of the file○ or at least, its EoCD

● An archive comment can contain another complete archive

Recap

Page 38: Schizophrenic files v2

Let's test the parsers!abstract.zip

Page 39: Schizophrenic files v2

4 LFHs, 4 ways to parse this archive:

Page 40: Schizophrenic files v2

1/ you parse it bottom-up

Page 41: Schizophrenic files v2

2/ you parse it top-down

Page 42: Schizophrenic files v2

3/ look for LFHs from the start (until a break)

Page 43: Schizophrenic files v2

4/ scan for LFHs aggressively (you get all four)

Page 44: Schizophrenic files v2

Portable Document File

Page 46: Schizophrenic files v2
Page 47: Schizophrenic files v2

PDF Trick #1trailers

Page 48: Schizophrenic files v2

trailer ⇒ root object ⇒ complete document

Page 49: Schizophrenic files v2

a line comment - a correct trailer - a corrupted trailer

Page 50: Schizophrenic files v2

Each reader sees a different trailer.

Page 51: Schizophrenic files v2

PDF parsing

Each reader sees a completely different document3 co-existing documents, all parsed through

Viewers tolerance makes foreign elements ignored

Page 52: Schizophrenic files v2

Also available in PDF/A flavor(OK for Adobe Reader, but not for Preflight)

Page 53: Schizophrenic files v2

sometimes,it’s in the specs......but who knows all of them ?

(obscurity via over-specification)

Page 54: Schizophrenic files v2

Notice anything unusual?

Page 55: Schizophrenic files v2

This document contains layers.(an advanced feature)

Page 56: Schizophrenic files v2

What you see is not what you’ll get...

Page 57: Schizophrenic files v2

“Optional Content Configuration”● principles

○ define layered content via various /Forms○ enable/disable layers on viewing/printing

● no warning when printing

● “you can see the preview!”○ bypass preview by keeping page 1 unchanged○ just do a minor change in the file

PDF Layers 1/2

Page 58: Schizophrenic files v2

● it’s Adobe only○ what’s displayed varies with readers○ could be hidden via previous schizophrenic trick

● it was in the specs all along○ very rarely used○ can be abused with no warning

PDF Layers 2/2

Page 59: Schizophrenic files v2

BMP

Page 60: Schizophrenic files v2

BMP

A pointer to some information that usually comes next…What could go wrong...

Page 61: Schizophrenic files v2

BMP Trick #1: ignoring the data pointer

getting data right after the header

getting datavia the pointer

(standard)

Page 62: Schizophrenic files v2

Trick #2:Run-Length Encoding

Page 63: Schizophrenic files v2

BMP RLE trick

RLE structure (each box is 1 byte)

Length>0

Palette Index (color)

Length0

End of Line0

Length0

End of Bitmap1

Length0

Move Cursor2 X offset Y offset

Length0

RAW Length>2

Palette Index (color)

Palette Index (color)

...

Page 64: Schizophrenic files v2

BMP RLE trick

If you just skip pixels, what is their color?

Length0

End of Line0

Length0

End of Bitmap1

Length0

Move Cursor2 X offset Y offset

Page 65: Schizophrenic files v2

Option 1The missing data will be filled with background color.

(palette index 0)

Page 66: Schizophrenic files v2

Option 2The missing data will be black.

Page 67: Schizophrenic files v2

Option 3The missing data will be transparent.

Page 68: Schizophrenic files v2

PNG

Portable Network Graphics

Page 69: Schizophrenic files v2

Combined data + 2 palettes

Same data chunk combining 2 images via 2 palettes

cute PoC by @reversity

“There shall not be more than one PLTE chunk”

Page 70: Schizophrenic files v2

Different images depending on which PLTE chunk is used

Page 71: Schizophrenic files v2

Portable Executable

Page 72: Schizophrenic files v2

PE = complex + badly documented● fail or fool external tools ? too easy...● fooling Windows is much harder:

○ Windows’ loader usually closes holes⇒ older PEs just not working anymore

the PE Loader

Page 73: Schizophrenic files v2

PE Trick #1Data directory loading order

Page 74: Schizophrenic files v2

Pointing TLS’ AddressOfIndex to an Import descriptor

Page 75: Schizophrenic files v2

W7: TLS is loaded first ⇒ AoI’s address set to 0⇒ Imports descriptors’s sequence is truncated before loading

Page 76: Schizophrenic files v2

XP: Imports are loaded first - all descriptors are parsedTLS is then parsed - descriptors are not relevant anymore

Page 77: Schizophrenic files v2

PE Trick #2Relocations

Page 78: Schizophrenic files v2

Relocations:patching absolute addresses

to solve address space conflicts

Page 79: Schizophrenic files v2

W8VistaXP

Relocations types

Type 4HIGH_ADJ -- -- ✓Type 9

MIPS_JMPADDR16IA64_IMM64

MACHINE_SPEC_932 bit 64 bit ✗

Page 80: Schizophrenic files v2

Relocations on relocations

Type 4HIGH_ADJ -- -- ✓Type 9

MIPS_JMPADDR16IA64_IMM64

MACHINE_SPEC_932 bit 64 bit ✗

Type 10DIR64 ✓ ✓ ✓

as seen in

PoC||GTFO #1

Page 81: Schizophrenic files v2

Relocation-based PE Schizophren

Page 82: Schizophrenic files v2

Julian Bangert, Sergey Bratus -- ELF Eccentricitieshttps://www.youtube.com/watch?v=4LU6N6THh2U

Page 83: Schizophrenic files v2

GIF

Page 84: Schizophrenic files v2

GIF

A GIF is made of blocks.if no animation speed is defined,they should all be displayed at once.

Page 85: Schizophrenic files v2

GIF

If a frame speed is defined, then:first block = backgroundnext blocks = animation frames

Background(from block 1)

Frame 1(with block 2)

Frame 2(with block 3)

Page 86: Schizophrenic files v2

GIF

Frame 1 Frames 2-100011x1 px

Frame 10002

1 complete pic + 10.000 pixels + 1 complete pic

Page 87: Schizophrenic files v2

Forcing animation (even if no frame speed is defined)

Displaying all blocks at once (standard)

Page 88: Schizophrenic files v2

same-tool schizophrenia1 file + 1 tool = 2 behaviors

(in different sub-components)

Page 89: Schizophrenic files v2

Because it was too simple...

● WinRar: viewing ⇔ extracting○ opening/failing○ opening/’nothing’

● Adobe: viewing ⇔ printing○ well, it’s a feature

Page 90: Schizophrenic files v2

Failures & Ideas

Page 91: Schizophrenic files v2

Failures & Ideas

● screen ⇔ printer○ embedded color profiles?

● JPG○ IrfanView vs the world

● Video○ FLV: early data pointer, like BMP

PoC: video fails but plays sound

Page 92: Schizophrenic files v2

PNG

Various ancillary chunks (rendering level)● partially supported:

○ gamma○ transparency (for palettes)

● never supported?○ significant bits○ chromacities

● always supported?○ physical size

Page 93: Schizophrenic files v2

Conclusion

Page 94: Schizophrenic files v2

We tend to take our own shortcuts.

Page 95: Schizophrenic files v2

Conclusion

● such a mess○ specs are messy

■ unclear■ historical reasons

○ parsers don’t even respect them(particularly when there is an easy shortcut)

○ official tools “forced” to be tolerant■ They’re even trying to repair corrupted files (!)

● no CVE/blaming for parsing errors?○ no security bug if no crash or exploit :(

Page 96: Schizophrenic files v2

Schizophrenia symptoms

● different parsing (seeing different data)○ BMP: ignoring data pointer○ ZIP: different parsing algorithm & directions○ PE: different data directory loading order○ PDF: different trailer parsing

● different interpretation (same data)○ GIF: ignoring animation speed○ BMP RLE: using different default color○ PE: different relocations implementation○ PNG: using different palette○ PDF: conditional layers

Page 97: Schizophrenic files v2

ACK

@gynvael@reversity @travisgoodspeed @sergeybratus

qkumba @internot @pdfkungfoo

@j00ru ise ds vx, MulanderFelix Groebert, Salvation

Page 98: Schizophrenic files v2

@angealbertinicorkami.com

Damn, that's the second time those alien bastards shot up my ride!