Easy Python Decompiler ❲2K❳

# Most common commands pip install uncompyle6 uncompyle6 file.pyc > recovered.py uncompyle6 -o output/ *.pyc Remember: Decompilation helps recover YOUR lost code, not steal others' work.

for root, dirs, files in os.walk(input_dir): for file in files: if file.endswith('.pyc'): pyc_path = os.path.join(root, file) rel_path = os.path.relpath(root, input_dir) out_subdir = os.path.join(output_dir, rel_path) os.makedirs(out_subdir, exist_ok=True) py_name = file.replace('.pyc', '.py') output_path = os.path.join(out_subdir, py_name) try: with open(output_path, 'w') as out_file: uncompyle6.decompile_file(pyc_path, out_file) print(f"Success: pyc_path -> output_path") except Exception as e: print(f"Failed: pyc_path - str(e)") batch_decompile("./compiled_files", "./decompiled_source") Handling Different Python Versions # Python 2.7 bytecode uncompyle6 --py-version=2.7 script.pyc Python 3.6 bytecode uncompyle6 --py-version=3.6 script.pyc Python 3.8+ bytecode uncompyle6 --py-version=3.8 script.pyc Decompiling Packaged Executables # Step 1: Extract from .exe (PyInstaller) python pyinstxtractor.py myapp.exe Step 2: Decompile extracted .pyc files uncompyle6 extracted/*.pyc Step 3: Reconstruct package structure Common Issues & Solutions Issue 1: "Unknown magic number" Error # Solution: Identify Python version first file script.pyc # Output: Python 3.8 bytecode Use correct version flag uncompyle6 --py-version=3.8 script.pyc Issue 2: Corrupted or Incomplete Bytecode # Try alternative decompiler import marshal import dis Read raw bytecode with open('script.pyc', 'rb') as f: f.read(16) # Skip header code_obj = marshal.load(f) Display disassembly for analysis dis.dis(code_obj) Issue 3: Missing Variable Names Decompiled code loses original variable names:

# Run original bytecode orig_output = subprocess.check_output(['python', original_pyc]) Easy Python Decompiler

# Common patterns to fix: # 1. Restore meaningful variable names # 2. Add missing imports # 3. Fix indentation issues # 4. Reconstruct string literals # 5. Restore comments from context # Complete recovery process for lost source code 1. Locate all bytecode files find . -name "*.pyc" > pyc_files.txt 2. Decompile to temporary directory mkdir recovered_source uncompyle6 -o recovered_source $(cat pyc_files.txt) 3. Fix common decompilation artifacts sed -i 's/ doc /"""DOCSTRING"""/g' recovered_source/*.py 4. Validate syntax for file in recovered_source/*.py; do python -m py_compile "$file" done 5. Compare with runtime behavior python -c "import sys; sys.path.insert(0, 'recovered_source'); import your_module" Alternative Tools Comparison | Tool | Python Versions | Speed | Accuracy | Maintenance | |------|----------------|-------|----------|-------------| | uncompyle6 | 1.0-3.8 | Medium | High | Active | | decompyle3 | 3.7-3.9 | Medium | High | Active | | pycdc | 1.0-3.9 | Fast | Medium | Active | | pycdasm | 2.0-3.7 | Slow | Medium | Inactive | | unpyc3 | 3.2-3.6 | Slow | Low | Inactive | Security Considerations Detecting Malicious Code # Scan decompiled code for suspicious patterns suspicious_patterns = [ r'eval\s*\(', r'exec\s*\(', r'__import__\s*\(', r'base64\.b64decode', r'compile\s*\(', r'socket\.', r'subprocess\.' ] def scan_decompiled(filepath): with open(filepath, 'r') as f: content = f.read()

:

for pattern in suspicious_patterns: if re.search(pattern, content, re.IGNORECASE): print(f"Warning: Found pattern in filepath") Easy Python Decompiler and its command-line alternatives provide effective ways to recover source code from bytecode files. While decompilation isn't perfect, it often produces workable code that can be manually corrected. Always respect intellectual property rights and use these tools ethically.

# Run recovered source recov_output = subprocess.check_output(['python', recovered_py]) # Most common commands pip install uncompyle6 uncompyle6

if orig_output == recov_output: print("✓ Decompilation successful") else: print("✗ Decompilation may have errors") When decompilation produces imperfect code:

# Original: customer_price = calculate_discount(base_price) # Decompiled: var1 = func1(var2) : Use bytecode analysis with debug info if available. Best Practices 1. Preserve Original Files # Always backup before decompiling cp original.pyc original.pyc.backup uncompyle6 original.pyc > recovered.py 2. Verify Decompilation Quality # Test recovered code import subprocess def test_decompiled(original_pyc, recovered_py): """Compare outputs to verify correctness""" Add missing imports # 3