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

Support HackTricks

Bug bounty tip: sign up for Intigriti, a premium bug bounty platform created by hackers, for hackers! Join us at https://go.intigriti.com/hacktricks today, and start earning bounties up to $100,000!

From Compiled Binary to .pyc

एक ELF संकलित बाइनरी से आप .pyc प्राप्त कर सकते हैं:

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 a python exe binary compiled आप .pyc प्राप्त कर सकते हैं:

python pyinstxtractor.py executable.exe

From .pyc to python code

For the .pyc data ("compiled" python) you should start trying to extract the original python code:

uncompyle6 binary.pyc  > decompiled.py

सुनिश्चित करें कि बाइनरी में एक्सटेंशन ".pyc" है (यदि नहीं, तो uncompyle6 काम नहीं करेगा)

uncompyle6 को निष्पादित करते समय आपको निम्नलिखित त्रुटियाँ मिल सकती हैं:

त्रुटि: अज्ञात जादुई संख्या 227

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

इसको ठीक करने के लिए आपको जनित फ़ाइल के प्रारंभ में सही जादुई संख्या जोड़ने की आवश्यकता है।

जादुई संख्याएँ पायथन संस्करण के साथ भिन्न होती हैं, पायथन 3.8 की जादुई संख्या प्राप्त करने के लिए आपको पायथन 3.8 टर्मिनल खोलने और निष्पादित करने की आवश्यकता होगी:

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

The magic number in this case for python3.8 is 0x550d0d0a, then, to fix this error you will need to add at the beginning of the .pyc file the following bytes: 0x0d550a0d000000000000000000000000

एक बार जब आप उस जादुई हेडर को जोड़ देंगे, तो त्रुटि ठीक हो जानी चाहिए।

This is how a correctly added .pyc python3.8 magic header will look like:

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

Error: Decompiling generic errors

अन्य त्रुटियाँ जैसे: class 'AssertionError'>; co_code should be one of the types (<class 'str'>, <class 'bytes'>, <class 'list'>, <class 'tuple'>); is type <class 'NoneType'> प्रकट हो सकती हैं।

इसका मतलब शायद यह है कि आपने सही तरीके से जादुई संख्या नहीं जोड़ी है या आपने सही जादुई संख्या का उपयोग नहीं किया है, इसलिए सुनिश्चित करें कि आप सही संख्या का उपयोग करें (या एक नई संख्या आजमाएँ)।

पिछली त्रुटि दस्तावेज़ की जाँच करें।

Automatic Tool

The python-exe-unpacker tool कई सामुदायिक उपलब्ध उपकरणों का संयोजन है जो शोधकर्ताओं को Python में लिखे गए निष्पादन योग्य फ़ाइलों को अनपैक और डिकंपाइल करने में सहायता करने के लिए डिज़ाइन किया गया है, विशेष रूप से उन फ़ाइलों के लिए जो py2exe और pyinstaller के साथ बनाई गई हैं। इसमें YARA नियम शामिल हैं जो यह पहचानने में मदद करते हैं कि क्या कोई निष्पादन योग्य फ़ाइल Python-आधारित है और निर्माण उपकरण की पुष्टि करते हैं।

ImportError: फ़ाइल का नाम: 'unpacked/malware_3.exe/pycache/archive.cpython-35.pyc' मौजूद नहीं है

एक सामान्य समस्या में एक अधूरा Python बाइटकोड फ़ाइल शामिल है जो unpy2exe या pyinstxtractor के साथ अनपैकिंग प्रक्रिया के परिणामस्वरूप उत्पन्न होती है, जो फिर uncompyle6 द्वारा एक गायब Python बाइटकोड संस्करण संख्या के कारण पहचानी नहीं जाती। इसे संबोधित करने के लिए, एक prepend विकल्प जोड़ा गया है, जो आवश्यक Python बाइटकोड संस्करण संख्या को जोड़ता है, जिससे डिकंपाइलिंग प्रक्रिया को सुगम बनाता है।

समस्या का उदाहरण:

# 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.

Analyzing python assembly

यदि आप पिछले चरणों का पालन करते हुए python "मूल" कोड निकालने में असमर्थ रहे, तो आप assembly निकालने की कोशिश कर सकते हैं (लेकिन यह बहुत वर्णनात्मक नहीं है, इसलिए फिर से मूल कोड निकालने की कोशिश करें)। यहाँ मैंने .pyc बाइनरी को disassemble करने के लिए एक बहुत सरल कोड पाया (कोड प्रवाह को समझने में शुभकामनाएँ)। यदि .pyc python2 से है, तो 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 to Executable

To start, we’re going to show you how payloads can be compiled in py2exe and PyInstaller.

To create a payload using py2exe:

  1. py2exe पैकेज को http://www.py2exe.org/ से इंस्टॉल करें।

  2. Payload के लिए (इस मामले में, हम इसे hello.py नाम देंगे), Figure 1 में दिए गए स्क्रिप्ट का उपयोग करें। “bundle_files” विकल्प जिसका मान 1 है, सब कुछ एक exe में बंडल करेगा जिसमें Python interpreter भी शामिल है।

  3. एक बार स्क्रिप्ट तैयार हो जाने के बाद, हम कमांड “python setup.py py2exe” जारी करेंगे। यह executable बनाएगा, ठीक Figure 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

To create a payload using PyInstaller:

  1. PyInstaller को pip का उपयोग करके स्थापित करें (pip install pyinstaller)।

  2. इसके बाद, हम कमांड “pyinstaller –onefile hello.py” जारी करेंगे (याद रखें कि ‘hello.py’ हमारा payload है)। यह सब कुछ एक निष्पादन योग्य में संकुचित कर देगा।

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.

संदर्भ

बग बाउंटी टिप: साइन अप करें Intigriti के लिए, एक प्रीमियम बग बाउंटी प्लेटफार्म जो हैकर्स द्वारा, हैकर्स के लिए बनाया गया है! आज ही https://go.intigriti.com/hacktricks पर हमारे साथ जुड़ें, और $100,000 तक की बाउंटी कमाना शुरू करें!

HackTricks का समर्थन करें

Last updated