forked from dashingsoft/pyarmor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotect_code.pt
More file actions
65 lines (56 loc) · 2.69 KB
/
protect_code.pt
File metadata and controls
65 lines (56 loc) · 2.69 KB
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
57
58
59
60
61
62
63
64
65
def protect_pytransform():
try:
import pytransform
except ImportError:
from . import pytransform
def check_obfuscated_script():
CO_SIZES = 49, 46, 38, 36
CO_NAMES = set(['pytransform', 'pyarmor_runtime', '__pyarmor__',
'__name__', '__file__'])
co = pytransform.sys._getframe(3).f_code
if not ((set(co.co_names) <= CO_NAMES)
and (len(co.co_code) in CO_SIZES)):
raise RuntimeError('unexpected obfuscated script')
def check_mod_pytransform():
CO_NAMES = set(['Exception', 'LoadLibrary', 'None', 'PYFUNCTYPE',
'PytransformError', '__file__', '_debug_mode',
'_get_error_msg', '_handle', '_load_library',
'_pytransform', 'abspath', 'basename', 'byteorder',
'c_char_p', 'c_int', 'c_void_p', 'calcsize', 'cdll',
'dirname', 'decode', 'encode', 'exists', 'exit',
'format_platname', 'get_error_msg', 'init_pytransform',
'init_runtime', 'int', 'isinstance', 'join', 'lower',
'normpath', 'os', 'path', 'platform', 'print',
'pyarmor_init', 'pythonapi', 'restype', 'set_option',
'str', 'struct', 'sys', 'system', 'version_info'])
colist = []
for name in ('dllmethod', 'init_pytransform', 'init_runtime',
'_load_library', 'pyarmor_init', 'pyarmor_runtime'):
colist.append(getattr(pytransform, name).{code})
for name in ('init_pytransform', 'init_runtime'):
colist.append(getattr(pytransform, name).{closure}[0].cell_contents.{code})
colist.append(pytransform.dllmethod.{code}.co_consts[1])
for co in colist:
if not (set(co.co_names) < CO_NAMES):
raise RuntimeError('unexpected pytransform.py')
def check_lib_pytransform():
if getattr(pytransform.sys, 'frozen', False):
filename = pytransform.os.path.join(pytransform.sys._MEIPASS, {filename})
else:
filename = pytransform.os.path.join({rpath}, {filename})
size = {size}
n = size >> 2
with open(filename, 'rb') as f:
buf = f.read(size)
fmt = 'I' * n
checksum = sum(pytransform.struct.unpack(fmt, buf)) & 0xFFFFFFFF
if not checksum == {checksum}:
raise RuntimeError('unexpected %s' % filename)
try:
check_obfuscated_script()
check_mod_pytransform()
check_lib_pytransform()
except Exception as e:
print('Protection Fault: %s' % e)
pytransform.sys.exit(1)
protect_pytransform()