How to decompile linux python into source code

File is executable. / test executable

1, pyinstxtractor

use pyinstxtractor to report errors

Error: Unsupported pyinstaller version or not a pyinstaller archive

2, uncompyle6
change the file name suffix to .pyc or pyo, uses uncompyle6 to report an error

KeyError: b"\x7fELF"
The results of

3 and objdump
objdump-x are as follows. It is known that the execution environment is python3.6, without shell, and-d can generate asm code with a length of about 28w lines

.
test:     file format elf64-x86-64
test
architecture: i386:x86-64, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x0000000000404040

Program Header:
    PHDR off    0x0000000000000040 vaddr 0x0000000000400040 paddr 0x000000000040                                                                                                                                                            0040 align 2**3
         filesz 0x0000000000000268 memsz 0x0000000000000268 flags r-x
  INTERP off    0x00000000000002a8 vaddr 0x00000000004002a8 paddr 0x000000000040                                                                                                                                                            02a8 align 2**0
         filesz 0x000000000000001c memsz 0x000000000000001c flags r--
    LOAD off    0x0000000000000000 vaddr 0x0000000000400000 paddr 0x000000000040                                                                                                                                                            0000 align 2**21
         filesz 0x0000000000132990 memsz 0x0000000000132990 flags r-x
    LOAD off    0x0000000000132c90 vaddr 0x0000000000732c90 paddr 0x000000000073                                                                                                                                                            2c90 align 2**21
         filesz 0x0000000000016a30 memsz 0x000000000001b2a8 flags rw-
 DYNAMIC off    0x0000000000132ca0 vaddr 0x0000000000732ca0 paddr 0x000000000073                                                                                                                                                            2ca0 align 2**3
         filesz 0x0000000000000220 memsz 0x0000000000000220 flags rw-
    NOTE off    0x00000000000002c4 vaddr 0x00000000004002c4 paddr 0x000000000040                                                                                                                                                            02c4 align 2**2
         filesz 0x0000000000000020 memsz 0x0000000000000020 flags r--
    NOTE off    0x00000000000002e8 vaddr 0x00000000004002e8 paddr 0x000000000040                                                                                                                                                            02e8 align 2**3
         filesz 0x0000000000000030 memsz 0x0000000000000030 flags r--
    NOTE off    0x0000000000000318 vaddr 0x0000000000400318 paddr 0x000000000040                                                                                                                                                            0318 align 2**2
         filesz 0x0000000000000024 memsz 0x0000000000000024 flags r--
EH_FRAME off    0x00000000001265b0 vaddr 0x00000000005265b0 paddr 0x000000000052                                                                                                                                                            65b0 align 2**2
         filesz 0x000000000000155c memsz 0x000000000000155c flags r--
   STACK off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x000000000000                                                                                                                                                            0000 align 2**4
         filesz 0x0000000000000000 memsz 0x0000000000000000 flags rw-
   RELRO off    0x0000000000132c90 vaddr 0x0000000000732c90 paddr 0x000000000073                                                                                                                                                            2c90 align 2**0
         filesz 0x0000000000000370 memsz 0x0000000000000370 flags r--

Dynamic Section:
  NEEDED               libpython3.6m.so.1.0
  NEEDED               libpthread.so.0
  NEEDED               libdl.so.2
  NEEDED               libutil.so.1
  NEEDED               libm.so.6
  NEEDED               libc.so.6
  INIT                 0x00000000004033e0
  FINI                 0x000000000051f7c4
  INIT_ARRAY           0x0000000000732c90
  INIT_ARRAYSZ         0x0000000000000008
  FINI_ARRAY           0x0000000000732c98
  FINI_ARRAYSZ         0x0000000000000008
  GNU_HASH             0x0000000000400340
  STRTAB               0x00000000004015d0
  SYMTAB               0x0000000000400400
  STRSZ                0x0000000000000ca2
  SYMENT               0x0000000000000018
  DEBUG                0x0000000000000000
  PLTGOT               0x0000000000733000
  PLTRELSZ             0x0000000000000c00
  PLTREL               0x0000000000000007
  JMPREL               0x00000000004027e0
  RELA                 0x0000000000402420
  RELASZ               0x00000000000003c0
  RELAENT              0x0000000000000018
  VERNEED              0x00000000004023f0
  VERNEEDNUM           0x0000000000000001
  VERSYM               0x0000000000402272

Version References:
  required from libc.so.6:
    0x0d696914 0x00 03 GLIBC_2.4
    0x09691a75 0x00 02 GLIBC_2.2.5

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .interp       0000001c  00000000004002a8  00000000004002a8  000002a8  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  1 .note.ABI-tag 00000020  00000000004002c4  00000000004002c4  000002c4  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  2 .note.gnu.property 00000030  00000000004002e8  00000000004002e8  000002e8  2                                                                                                                                                            **3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 .note.gnu.build-id 00000024  0000000000400318  0000000000400318  00000318  2                                                                                                                                                            **2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .gnu.hash     000000bc  0000000000400340  0000000000400340  00000340  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  5 .dynsym       000011d0  0000000000400400  0000000000400400  00000400  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  6 .dynstr       00000ca2  00000000004015d0  00000000004015d0  000015d0  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  7 .gnu.version  0000017c  0000000000402272  0000000000402272  00002272  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  8 .gnu.version_r 00000030  00000000004023f0  00000000004023f0  000023f0  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  9 .rela.dyn     000003c0  0000000000402420  0000000000402420  00002420  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 10 .rela.plt     00000c00  00000000004027e0  00000000004027e0  000027e0  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 11 .init         00000017  00000000004033e0  00000000004033e0  000033e0  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 12 .plt          00000810  0000000000403400  0000000000403400  00003400  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 13 .text         0011bbb1  0000000000403c10  0000000000403c10  00003c10  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 14 .fini         00000009  000000000051f7c4  000000000051f7c4  0011f7c4  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 15 .rodata       00006dce  000000000051f7e0  000000000051f7e0  0011f7e0  2**5
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 16 .eh_frame_hdr 0000155c  00000000005265b0  00000000005265b0  001265b0  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 17 .eh_frame     0000ae80  0000000000527b10  0000000000527b10  00127b10  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 18 .init_array   00000008  0000000000732c90  0000000000732c90  00132c90  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 19 .fini_array   00000008  0000000000732c98  0000000000732c98  00132c98  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 20 .dynamic      00000220  0000000000732ca0  0000000000732ca0  00132ca0  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 21 .got          00000140  0000000000732ec0  0000000000732ec0  00132ec0  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 22 .got.plt      00000418  0000000000733000  0000000000733000  00133000  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 23 .data         000162a0  0000000000733420  0000000000733420  00133420  2**5
                  CONTENTS, ALLOC, LOAD, DATA
 24 .bss          00004878  00000000007496c0  00000000007496c0  001496c0  2**5
                  ALLOC
 25 .comment      00000058  0000000000000000  0000000000000000  001496c0  2**0
                  CONTENTS, READONLY
 26 .gnu.build.attributes 000007e8  0000000000000000  0000000000000000  00149718                                                                                                                                                              2**2
                  CONTENTS, READONLY
SYMBOL TABLE:
no symbols

Screenshot of assembly part

clipboard.png

question: how do I convert a test file to a pre-packaged py file?

Apr.23,2021

In theory, this cannot be restored to the original python source code, but you can first analyze it with a disassembly tool to get an approximate c source code, and then convert it to python source code.

as far as I know, there is no complete tool chain at present (August 22, 2018).
IDA Pro is preferred for disassembly tools.

Unlike pyinstaller-generated files, which are usually generated by cython + gcc,

relies on libpythonX.Ym.so library files, but does not require a python environment.
you can run ldd < file > to confirm.

attach a generated example

  1. write python source code, such as a.pyx

    print('hi')
  2. convert python source code to c source code to get A.C

    cython a.pyx --embed
  3. compile c file

    gcc -I /usr/include/pythonX.Ym -o a a.c -lpythonX.Ym
Menu