Skip to content

Commit 1a9dbcb

Browse files
committed
Refine protect_pytransform function
1 parent 2518fa7 commit 1a9dbcb

2 files changed

Lines changed: 36 additions & 48 deletions

File tree

docs/security.rst

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,25 @@ need to inserted into the script. Here is sample code::
133133
code = '__code__' if sys.version_info[0] == 3 else 'func_code'
134134
closure = '__closure__' if sys.version_info[0] == 3 else 'func_closure'
135135

136-
co = getattr(pytransform.dllmethod, code).co_consts[1]
137-
if not (set(co.co_names) < set(CO_PYTRANSFORM_NAMES)):
138-
sys.exit(1)
136+
colist = [getattr(pytransform.dllmethod, code).co_consts[1]]
139137

140138
for name in ('dllmethod', 'init_pytransform', 'init_runtime', '_load_library', 'pyarmor_init', 'pyarmor_runtime'):
141-
co = getattr(getattr(pytransform, name), code)
142-
if not (set(co.co_names) < set(CO_PYTRANSFORM_NAMES)):
143-
sys.exit(1)
139+
colist.append(getattr(getattr(pytransform, name), code))
140+
141+
for name in ('init_pytransform', 'init_runtime'):
142+
colist.append(getattr(getattr(getattr(pytransform, name),
143+
closure)[0].cell_contents, code))
144144

145-
for item in ('init_pytransform', 'init_runtime'):
146-
co_closures = getattr(getattr(pytransform, item[0]), closure)
147-
co = getattr(co_closures[0].cell_contents, code)
148-
if not (set(co.co_names) < set(CO_PYTRANSFORM_NAMES)):
145+
for co in colist:
146+
if not (set(co.co_names) < set(CO_PYTRANSFORM_NAMES)):
149147
sys.exit(1)
150148

151149
def protect_pytransform():
152150
try:
153151
# Be sure obfuscated script is not changed
154152
check_obfuscated_script()
155153

156-
# Be sure 'pytransform.py' is not changed
154+
# Be sure '_pytransform._name' in 'pytransform.py' is not changed
157155
check_mod_pytransform()
158156

159157
# Be sure '_pytransform.so' is not changed

src/examples/helloworld/foo.py

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,6 @@
2020
'darwin64': '23da1bbc5b7768657d8a76a1a403a8c8',
2121
}
2222

23-
CO_SELF_SIZES = 46, 36
24-
CO_SELF_NAMES = ('pytransform', 'pyarmor_runtime', '__pyarmor__',
25-
'__name__', '__file__')
26-
CO_PYTRANSFORM_NAMES = ('Exception', 'LoadLibrary', 'None', 'PYFUNCTYPE',
27-
'PytransformError', '__file__', '_debug_mode',
28-
'_get_error_msg', '_handle', '_load_library',
29-
'_pytransform', 'abspath', 'basename', 'byteorder',
30-
'c_char_p', 'c_int', 'c_void_p', 'calcsize', 'cdll',
31-
'dirname', 'encode', 'exists', 'exit',
32-
'format_platname', 'init_pytransform', 'init_runtime',
33-
'int', 'isinstance', 'join', 'lower', 'normpath', 'os',
34-
'path', 'platform', 'print', 'pyarmor_init',
35-
'pythonapi', 'restype', 'set_option', 'str', 'struct',
36-
'sys', 'system', 'version_info')
37-
3823
def check_lib_pytransform(filename):
3924
print("Check md5sum for %s..." % filename)
4025
expected = MD5SUM_LIB_PYTRANSFORM[pytransform.format_platname()]
@@ -45,39 +30,44 @@ def check_lib_pytransform(filename):
4530
print("Check OK.")
4631

4732
def check_obfuscated_script():
33+
CO_SIZES = 46, 36
34+
CO_NAMES = set(['pytransform', 'pyarmor_runtime', '__pyarmor__',
35+
'__name__', '__file__'])
4836
print('Check obfuscated script %s ...' % __file__)
4937
co = sys._getframe(3).f_code
50-
if not (set(co.co_names) <= set(CO_SELF_NAMES)
51-
and len(f_code.co_code) in CO_SELF_SIZES):
38+
if not ((set(co.co_names) <= CO_NAMES)
39+
and (len(f_code.co_code) in CO_SIZES)):
5240
print('Obfuscated script has been changed by others')
5341
sys.exit(1)
5442
print('Check OK.')
5543

5644
def check_mod_pytransform():
57-
code = '__code__' if sys.version_info[0] == 3 else 'func_code'
45+
CO_NAMES = set(['Exception', 'LoadLibrary', 'None', 'PYFUNCTYPE',
46+
'PytransformError', '__file__', '_debug_mode',
47+
'_get_error_msg', '_handle', '_load_library',
48+
'_pytransform', 'abspath', 'basename', 'byteorder',
49+
'c_char_p', 'c_int', 'c_void_p', 'calcsize', 'cdll',
50+
'dirname', 'encode', 'exists', 'exit',
51+
'format_platname', 'init_pytransform', 'init_runtime',
52+
'int', 'isinstance', 'join', 'lower', 'normpath', 'os',
53+
'path', 'platform', 'print', 'pyarmor_init',
54+
'pythonapi', 'restype', 'set_option', 'str', 'struct',
55+
'sys', 'system', 'version_info'])
56+
code = '__code__' if sys.version_info[0] == 3 else 'func_code'
5857
closure = '__closure__' if sys.version_info[0] == 3 else 'func_closure'
5958

60-
print('Check decorator dllmethod ...')
61-
co = getattr(pytransform.dllmethod, code).co_consts[1]
62-
if not (set(co.co_names) < set(CO_PYTRANSFORM_NAMES)):
63-
print('Check failed')
64-
sys.exit(1)
65-
print('Check OK.')
66-
59+
colist = [getattr(pytransform.dllmethod, code).co_consts[1]]
6760
for name in ('dllmethod', 'init_pytransform', 'init_runtime',
6861
'_load_library', 'pyarmor_init', 'pyarmor_runtime'):
69-
print('Check function %s ...' % name)
70-
co = getattr(getattr(pytransform, name), code)
71-
if not (set(co.co_names) < set(CO_PYTRANSFORM_NAMES)):
72-
print('Check failed')
73-
sys.exit(1)
74-
print('Check OK.')
62+
colist.append(getattr(getattr(pytransform, name), code))
7563

7664
for name in ('init_pytransform', 'init_runtime'):
77-
print('Check wrapped function %s ...' % name)
78-
co_closures = getattr(getattr(pytransform, name), closure)
79-
co = getattr(co_closures[0].cell_contents, code)
80-
if not (set(co.co_names) < set(CO_PYTRANSFORM_NAMES)):
65+
colist.append(getattr(getattr(getattr(pytransform, name),
66+
closure)[0].cell_contents, code))
67+
68+
for co in colist:
69+
print('Check %s ...' % co.co_name)
70+
if not (set(co.co_names) < CO_NAMES):
8171
print('Check failed')
8272
sys.exit(1)
8373
print('Check OK.')
@@ -87,7 +77,7 @@ def protect_pytransform():
8777
# Be sure obfuscated script is not changed
8878
check_obfuscated_script()
8979

90-
# Be sure 'pytransform.py' is not changed
80+
# Be sure '_pytransform._name' in 'pytransform.py' is not changed
9181
check_mod_pytransform()
9282

9383
# Be sure '_pytransform.so' is not changed
@@ -157,5 +147,5 @@ def main():
157147
protect_pytransform()
158148
check_expired_date_by_ntp()
159149
show_left_days_of_license()
160-
150+
161151
main()

0 commit comments

Comments
 (0)