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
68 lines (57 loc) · 2.6 KB
/
protect_code.pt
File metadata and controls
68 lines (57 loc) · 2.6 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
66
67
68
def protect_pytransform():
{relative}import pytransform{suffix} as pytransform
def assert_builtin(func):
type = ''.__class__.__class__
builtin_function = type(''.join)
if type(func) is not builtin_function:
raise RuntimeError('%s() is not a builtin' % func.__name__)
def check_obfuscated_script():
CO_SIZES = 55, 52, 49, 46, 42, 40, 38, 36
CO_NAMES = set(['pytransform{suffix}', 'pyarmor_runtime',
'__pyarmor{suffix}__', '__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():
def _check_co_key(co, v):
return (len(co.co_names), len(co.co_consts), len(co.co_code)) == v
for k, (v1, v2, v3) in {keylist}:
co = getattr(pytransform, k).{code}
if not _check_co_key(co, v1):
raise RuntimeError('unexpected pytransform.py')
if v2:
if not _check_co_key(co.co_consts[1], v2):
raise RuntimeError('unexpected pytransform.py')
if v3:
if not _check_co_key(co.{closure}[0].cell_contents.{code}, v3):
raise RuntimeError('unexpected pytransform.py')
def check_lib_pytransform():
platname = pytransform.sys.platform
if platname.startswith('darwin'):
return
libname = '_pytransform{suffix}.dylib' if platname.startswith('darwin') else \
'_pytransform{suffix}.dll' if platname.startswith('win') else \
'_pytransform{suffix}.dll' if platname.startswith('cygwin') else \
'_pytransform{suffix}.so'
if getattr(pytransform.sys, 'frozen', False):
filename = pytransform.os.path.join(pytransform.sys._MEIPASS, libname)
else:
filename = pytransform.os.path.join({rpath}, {spath}, libname)
with open(filename, 'rb') as f:
buf = bytearray(f.read())
value = sum(buf)
if getattr(pytransform.sys, 'frozen', False) and sys.platform == 'darwin':
if '{suffix}':
value += 886 - sum(b'{suffix}') + 299
else:
value += 1217
if value not in {checksum}:
raise RuntimeError('unexpected %s' % filename)
assert_builtin(sum)
assert_builtin(open)
assert_builtin(len)
check_obfuscated_script()
check_mod_pytransform()
check_lib_pytransform()
protect_pytransform()