-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpyc.py
executable file
·59 lines (46 loc) · 1.58 KB
/
checkpyc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
import os
import sys
import time
import struct
import marshal
import binascii
import dis
import platform
def view_pyc_file(path):
"""Read and display a content of the Python`s bytecode in a pyc-file."""
with open(path, 'rb') as file:
magic = file.read(4)
bit_field = None
timestamp = None
hashstr = None
size = None
if sys.version_info.major == 3 and sys.version_info.minor >= 7:
bit_field = int.from_bytes(file.read(4), byteorder=sys.byteorder)
if 1 & bit_field == 1:
hashstr = file.read(8)
else:
timestamp = file.read(4)
size = file.read(4)
size = struct.unpack('I', size)[0]
elif sys.version_info.major == 3 and sys.version_info.minor >= 3:
timestamp = file.read(4)
size = file.read(4)
size = struct.unpack('I', size)[0]
else:
timestamp = file.read(4)
code = marshal.load(file)
magic = binascii.hexlify(magic).decode('utf-8')
timestamp = time.asctime(time.localtime(struct.unpack('I', timestamp)[0]))
#dis.disassemble(code)
print('-' * 80)
print(
'Python version: {}\nMagic code: {}\nTimestamp: {}\nSize: {}\nHash: {}\nBitfield: {}'
.format(platform.python_version(), magic, timestamp, size, hashstr, bit_field)
)
if __name__ == '__main__':
usage = 'Usage: checkpyc.py file.pyc'
if len(sys.argv) <= 1 or not os.path.isfile(sys.argv[1]):
print(usage)
sys.exit()
view_pyc_file(sys.argv[1])