dashingsoft / pyarmor

A tool used to obfuscate python scripts, bind obfuscated scripts to fixed machine or expire obfuscated scripts.
http://pyarmor.dashingsoft.com
Other
3.45k stars 291 forks source link

[BUG] "unauthorized use of script" when packing with pyinstaller and setting assert-call/assert-import #1672

Closed antverdovsky closed 9 months ago

antverdovsky commented 9 months ago

I have a project with two files: a.py:

def p(x):
  print(f"Hello {x}")

main.py:

import a
a.p("world")

Following the documentation, I pack the project into an executable using pyinstaller:

pyinstaller -F main.py

If I use pyarmor with --assert-import:

pyarmor gen -O obfdist --pack dist/main a.py main.py --assert-import

INFO     Python 3.11.6
INFO     Pyarmor 8.4.6 (pro), 005520, atv
INFO     Platform darwin.aarch64

when I try to run the new dist/main, I get the following error:

./dist/main

Traceback (most recent call last):
  File "<frozen __main__>", line 3, in <module>
  File "<frozen main>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller/loader/pyimod02_importers.py", line 385, in exec_module
  File "<frozen a>", line 3, in <module>
RuntimeError: unauthorized use of script (1:1380)
[9505] Failed to execute script 'main' due to unhandled exception!

Using pyarmor with --assert-call, and running dist/main gives me the same error.

Is there some other step to enable assert-import and --assert-call when packing with pyinstaller?

jondy commented 9 months ago

Please search __assert_armored__ in the documentation, and use this builtin function manually.

irreg commented 9 months ago

I am experiencing the same problem. I get unauthorized use of script (1:1380) when I pack with --private.

It does not occur with pyarmor.cli.core 5.4.2, but with pyarmor.cli.core 5.4.3. Do you have any information on this?

antverdovsky commented 9 months ago

I also get a similar error when using --restrict, and I also tried restrict_module=2 (which seems to imply this is a duplicate of https://github.com/dashingsoft/pyarmor/issues/1274). My pyinstaller version is 5.13.2 if thats relevant.

Traceback (most recent call last):
  File "<frozen __main__>", line 3, in <module>
  File "<frozen protected_main>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller/loader/pyimod02_importers.py", line 385, in exec_module
  File "<frozen main>", line 3, in <module>
  File "<frozen main>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller/loader/pyimod02_importers.py", line 385, in exec_module
  File "<frozen mymodule>", line 3, in <module>
RuntimeError: unauthorized use of script (1:1380)
irreg commented 9 months ago

It says here that --restrict and --pack cannot be combined, so that pattern may not have been possible originally. But --private has no such statement, and there doesn't seem to be a workaround.

https://pyarmor.readthedocs.io/en/latest/how-to/obfuscation.html#protecting-system-packages

irreg commented 9 months ago

Can you work around the problem by downgrading to 5.4.2?

antverdovsky commented 9 months ago

I don't want to downgrade below 8 as there are some pyarmor deobfuscators out there for <8, but AFAIK version 8 has not been broken.

I also opened a new issue #1676 as I am getting the same issue with --private.

irreg commented 9 months ago

I meant the version of pyarmor.cli.core, which is a dependent library of pyarmor. pyarmor itself is 8.4.5.

jondy commented 9 months ago

@irreg please refer to #1676

pyarmor.cli.core v5.4.2 has a bug for --private, so even pack with --private it still works. core v5.4.3 fixes it.

One solution is described in issue #1676