Decompile compiled python binaries (exe, elf) - Retreive from .pyc

Impara l'hacking AWS da zero a eroe con htARTE (Esperto Red Team AWS di HackTricks)!

Altri modi per supportare HackTricks:

Suggerimento per il bug bounty: iscriviti a Intigriti, una piattaforma premium per i bug bounty creata da hacker, per hacker! Unisciti a noi su https://go.intigriti.com/hacktricks oggi e inizia a guadagnare taglie fino a $100,000!

Da Binario Compilato a .pyc

Da un binario compilato ELF puoi ottenere il .pyc con:

pyi-archive_viewer <binary>
# The list of python modules will be given here:
[(0, 230, 311, 1, 'm', 'struct'),
(230, 1061, 1792, 1, 'm', 'pyimod01_os_path'),
(1291, 4071, 8907, 1, 'm', 'pyimod02_archive'),
(5362, 5609, 13152, 1, 'm', 'pyimod03_importers'),
(10971, 1473, 3468, 1, 'm', 'pyimod04_ctypes'),
(12444, 816, 1372, 1, 's', 'pyiboot01_bootstrap'),
(13260, 696, 1053, 1, 's', 'pyi_rth_pkgutil'),
(13956, 1134, 2075, 1, 's', 'pyi_rth_multiprocessing'),
(15090, 445, 672, 1, 's', 'pyi_rth_inspect'),
(15535, 2514, 4421, 1, 's', 'binary_name'),
...

? X binary_name
to filename? /tmp/binary.pyc

In un eseguibile binario python puoi ottenere il file .pyc eseguendo:

python pyinstxtractor.py executable.exe

Da .pyc a codice Python

Per i dati .pyc ("compilati" python) dovresti iniziare provando a estrarre il codice Python originale:

uncompyle6 binary.pyc  > decompiled.py

Assicurati che il file binario abbia l'estensione ".pyc" (altrimenti uncompyle6 non funzionerà)

Durante l'esecuzione di uncompyle6 potresti incontrare i seguenti errori:

Errore: Numero magico sconosciuto 227

/kali/.local/bin/uncompyle6 /tmp/binary.pyc
Unknown magic number 227 in /tmp/binary.pyc

Per risolvere questo problema è necessario aggiungere il numero magico corretto all'inizio del file generato.

I numeri magici variano con la versione di Python, per ottenere il numero magico di Python 3.8 sarà necessario aprire un terminale Python 3.8 ed eseguire:

>> import imp
>> imp.get_magic().hex()
'550d0d0a'

Il numero magico in questo caso per python3.8 è 0x550d0d0a, quindi, per correggere questo errore dovrai aggiungere all'inizio del file .pyc i seguenti byte: 0x0d550a0d000000000000000000000000

Una volta che avrai aggiunto quell'intestazione magica, l'errore dovrebbe essere corretto.

Ecco come apparirà correttamente aggiunta l'intestazione magica .pyc python3.8:

hexdump 'binary.pyc' | head
0000000 0d55 0a0d 0000 0000 0000 0000 0000 0000
0000010 00e3 0000 0000 0000 0000 0000 0000 0000
0000020 0700 0000 4000 0000 7300 0132 0000 0064
0000030 0164 006c 005a 0064 0164 016c 015a 0064

Errore: Errori generici nella decompilazione

Altri errori come: class 'AssertionError'>; co_code should be one of the types (<class 'str'>, <class 'bytes'>, <class 'list'>, <class 'tuple'>); is type <class 'NoneType'> potrebbero apparire.

Questo probabilmente significa che non hai aggiunto correttamente il numero magico o che non hai utilizzato il numero magico corretto, quindi assicurati di utilizzare quello corretto (o prova con uno nuovo).

Controlla la documentazione sull'errore precedente.

Strumento Automatico

Il strumento python-exe-unpacker funge da combinazione di diversi strumenti disponibili nella comunità progettati per assistere i ricercatori nel disimballaggio e nella decompilazione di eseguibili scritti in Python, in particolare quelli creati con py2exe e pyinstaller. Include regole YARA per identificare se un eseguibile è basato su Python e conferma lo strumento di creazione.

ImportError: Nome file: 'unpacked/malware_3.exe/pycache/archive.cpython-35.pyc' non esiste

Un problema comune riscontrato coinvolge un file bytecode Python incompleto risultante dal processo di disimballaggio con unpy2exe o pyinstxtractor, che quindi non viene riconosciuto da uncompyle6 a causa dell'assenza del numero di versione del bytecode Python. Per affrontare questo problema, è stata aggiunta un'opzione di prepend, che aggiunge il numero di versione del bytecode Python necessario, facilitando il processo di decompilazione.

Esempio del problema:

# Error when attempting to decompile without the prepend option
test@test: uncompyle6 unpacked/malware_3.exe/archive.py
Traceback (most recent call last):
...
ImportError: File name: 'unpacked/malware_3.exe/__pycache__/archive.cpython-35.pyc' doesn't exist
# Successful decompilation after using the prepend option
test@test:python python_exe_unpack.py -p unpacked/malware_3.exe/archive
[*] On Python 2.7
[+] Magic bytes are already appended.

# Successfully decompiled file
[+] Successfully decompiled.

Analisi dell'assembly di Python

Se non sei riuscito a estrarre il codice "originale" di Python seguendo i passaggi precedenti, puoi provare a estrarre l'assembly (ma non è molto descrittivo, quindi prova a estrarre di nuovo il codice originale). In questo link ho trovato un codice molto semplice per disassemblare il binario .pyc (buona fortuna a capire il flusso del codice). Se il .pyc è di Python2, utilizza Python2:

>>> import dis
>>> import marshal
>>> import struct
>>> import imp
>>>
>>> with open('hello.pyc', 'r') as f:  # Read the binary file
...     magic = f.read(4)
...     timestamp = f.read(4)
...     code = f.read()
...
>>>
>>> # Unpack the structured content and un-marshal the code
>>> magic = struct.unpack('<H', magic[:2])
>>> timestamp = struct.unpack('<I', timestamp)
>>> code = marshal.loads(code)
>>> magic, timestamp, code
((62211,), (1425911959,), <code object <module> at 0x7fd54f90d5b0, file "hello.py", line 1>)
>>>
>>> # Verify if the magic number corresponds with the current python version
>>> struct.unpack('<H', imp.get_magic()[:2]) == magic
True
>>>
>>> # Disassemble the code object
>>> dis.disassemble(code)
1           0 LOAD_CONST               0 (<code object hello_world at 0x7f31b7240eb0, file "hello.py", line 1>)
3 MAKE_FUNCTION            0
6 STORE_NAME               0 (hello_world)
9 LOAD_CONST               1 (None)
12 RETURN_VALUE
>>>
>>> # Also disassemble that const being loaded (our function)
>>> dis.disassemble(code.co_consts[0])
2           0 LOAD_CONST               1 ('Hello  {0}')
3 LOAD_ATTR                0 (format)
6 LOAD_FAST                0 (name)
9 CALL_FUNCTION            1
12 PRINT_ITEM
13 PRINT_NEWLINE
14 LOAD_CONST               0 (None)
17 RETURN_VALUE

Python a Eseguibile

Per iniziare, ti mostreremo come i payload possono essere compilati in py2exe e PyInstaller.

Per creare un payload usando py2exe:

  1. Installa il pacchetto py2exe da http://www.py2exe.org/

  2. Per il payload (in questo caso, lo chiameremo hello.py), utilizza uno script come quello nella Figura 1. L'opzione "bundle_files" con il valore 1 includerà tutto, compreso l'interprete Python, in un unico exe.

  3. Una volta che lo script è pronto, emetteremo il comando "python setup.py py2exe". Questo creerà l'eseguibile, proprio come nella Figura 2.

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
options = {'py2exe': {'bundle_files': 1}},
#windows = [{'script': "hello.py"}],
console = [{'script': "hello.py"}],
zipfile = None,
)
C:\Users\test\Desktop\test>python setup.py py2exe
running py2exe
*** searching for required modules ***
*** parsing results ***
*** finding dlls needed ***
*** create binaries ***
*** byte compile python files ***
*** copy extensions ***
*** copy dlls ***
copying C:\Python27\lib\site-packages\py2exe\run.exe -> C:\Users\test\Desktop\test\dist\hello.exe
Adding python27.dll as resource to C:\Users\test\Desktop\test\dist\hello.exe

Per creare un payload utilizzando PyInstaller:

  1. Installa PyInstaller utilizzando pip (pip install pyinstaller).

  2. Successivamente, emetteremo il comando "pyinstaller --onefile hello.py" (un promemoria che 'hello.py' è il nostro payload). Questo raggrupperà tutto in un eseguibile.

C:\Users\test\Desktop\test>pyinstaller --onefile hello.py
108 INFO: PyInstaller: 3.3.1
108 INFO: Python: 2.7.14
108 INFO: Platform: Windows-10-10.0.16299
………………………………
5967 INFO: checking EXE
5967 INFO: Building EXE because out00-EXE.toc is non existent
5982 INFO: Building EXE from out00-EXE.toc
5982 INFO: Appending archive to EXE C:\Users\test\Desktop\test\dist\hello.exe
6325 INFO: Building EXE from out00-EXE.toc completed successfully.

Riferimenti

Suggerimento per la caccia al bug: iscriviti a Intigriti, una piattaforma premium per la caccia ai bug creata da hacker, per hacker! Unisciti a noi su https://go.intigriti.com/hacktricks oggi e inizia a guadagnare taglie fino a $100,000!

Impara l'hacking di AWS da zero a eroe con htARTE (HackTricks AWS Red Team Expert)!

Altri modi per supportare HackTricks:

Last updated