2

I know python modules/scripts can be run from within a zip archive, as outlined here: https://www.python.org/dev/peps/pep-0441/ and https://blogs.gnome.org/jamesh/2012/05/21/python-zip-files/

But my question is:

Is it possible to password protect this archive or .pyz file and run it with another small python script that will send the password and then run the __main__.py ?

Thanks!

FuzzkingCool
  • 309
  • 4
  • 14

2 Answers2

3

The code that manages the import is inside the zipimport module. Reading the archive is done in the function getdata It supposes that the file is unencrypted by decompressing it (see here).

So no, unfortunately it does not seem possible to use encrypted file directly from the command line. However you can imagine a wrapper that do this by using importlib (an example).

GHugo
  • 2,584
  • 13
  • 14
3

I needed something like this for myself, so I made it. You need the module found here: https://github.com/Dakkaron/ArchiveImporter

Then you can just use it like this:

python ArchiveImporter.py [zipfile] [-p=password] [args...]

Works for both Python2 and Python3.

The module can also be used from code:

# First import the ArchiveImporter module
import ArchiveImporter
# Then add the password encrypted file you want to import from using addZip(zippath, password)
ArchiveImporter.addZip("test.pyz", "password")
# Now import modules from the archive as usual
import testmod
Dakkaron
  • 5,930
  • 2
  • 36
  • 51