Skip to content

Commit b7cad26

Browse files
committed
Refine protection code
1 parent 666691e commit b7cad26

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/protect_code.pt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def protect_pytransform():
33
import pytransform
44

55
def check_obfuscated_script():
6-
CO_SIZES = 46, 36
6+
CO_SIZES = 49, 46, 38, 36
77
CO_NAMES = set(['pytransform', 'pyarmor_runtime', '__pyarmor__',
88
'__name__', '__file__'])
99
co = pytransform.sys._getframe(3).f_code
@@ -38,19 +38,20 @@ def protect_pytransform():
3838
if not (set(co.co_names) < CO_NAMES):
3939
raise RuntimeError('Unexpected pytransform.py')
4040

41-
def check_lib_pytransform(filename):
42-
size = 0x{size:X}
41+
def check_lib_pytransform():
42+
filename = pytransform.os.path.join({rpath}, {filename})
43+
size = {size}
4344
n = size >> 2
4445
with open(filename, 'rb') as f:
4546
buf = f.read(size)
4647
fmt = 'I' * n
4748
checksum = sum(pytransform.struct.unpack(fmt, buf)) & 0xFFFFFFFF
48-
if not checksum == 0x{checksum:X}:
49+
if not checksum == {checksum}:
4950
raise RuntimeError("Unexpected %s" % filename)
5051
try:
5152
check_obfuscated_script()
5253
check_mod_pytransform()
53-
check_lib_pytransform(pytransform._pytransform._name)
54+
check_lib_pytransform()
5455
except Exception as e:
5556
print("Protection Fault: %s" % e)
5657
pytransform.sys.exit(1)

src/utils.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,24 +218,26 @@ def get_registration_code():
218218
code = None
219219
return code
220220

221-
def make_protect_pytransform(filename=None, path=None):
221+
def make_protect_pytransform(template=None, filename=None, rpath=None):
222222
if filename is None:
223223
filename = pytransform._pytransform._name
224-
if path is None:
225-
path = PYARMOR_PATH
224+
if template is None:
225+
template = os.path.join(PYARMOR_PATH, protect_code_template)
226226
size = os.path.getsize(filename) & 0xFFFFFFF0
227227
n = size >> 2
228228
with open(filename, 'rb') as f:
229229
buf = f.read(size)
230230
fmt = 'I' * n
231-
checksum = sum(pytransform.struct.unpack(fmt, buf)) & 0xFFFFFFFF
231+
cosum = sum(pytransform.struct.unpack(fmt, buf)) & 0xFFFFFFFF
232232

233-
with open(os.path.join(path, protect_code_template)) as f:
233+
with open(template) as f:
234234
buf = f.read()
235235

236-
code= '__code__' if sys.version_info[0] == 3 else 'func_code'
236+
code = '__code__' if sys.version_info[0] == 3 else 'func_code'
237237
closure = '__closure__' if sys.version_info[0] == 3 else 'func_closure'
238-
return buf.format(size=size, checksum=checksum, code=code, closure=closure)
238+
rpath = 'os.path.dirname(__file__)' if rpath is None else repr(rpath)
239+
return buf.format(code=code, closure=closure, size=size, checksum=cosum,
240+
rpath=rpath, filename=repr(os.path.basename(filename)))
239241

240242
def _frozen_modname(filename, filename2):
241243
names = os.path.normpath(filename).split(os.sep)
@@ -266,7 +268,7 @@ def _guess_encoding(filename):
266268
return m and m.group(1)
267269

268270
def encrypt_script(pubkey, filename, destname, wrap_mode=1, obf_code=1,
269-
obf_mod=1, protection=0):
271+
obf_mod=1, protection=0, rpath=None):
270272
if sys.version_info[0] == 2:
271273
with open(filename, 'r') as f:
272274
lines = f.readlines()
@@ -278,12 +280,14 @@ def encrypt_script(pubkey, filename, destname, wrap_mode=1, obf_code=1,
278280
if protection:
279281
n = 0
280282
for line in lines:
281-
if line.startswith('def protect_pytransform():'):
283+
if line.startswith('# No PyArmor Protection Code'):
282284
break
283-
elif (line.startswith("if __name__ == '__main__':")
285+
elif (line.startswith('# {PyArmor Protection Code}')
286+
or line.startswith("if __name__ == '__main__':")
284287
or line.startswith('if __name__ == "__main__":')):
285288
logging.info('Patch this entry script with protection code')
286-
lines[n:n] = make_protect_pytransform()
289+
template = None if protection == 1 else protection
290+
lines[n:n] = make_protect_pytransform(template, rpath=rpath)
287291
break
288292
n += 1
289293

0 commit comments

Comments
 (0)