15 Jan 2023

Using Pyarmor to Obfuscate your Python Application

Pyarmor is a python library that allows your code to be obfuscated. I will provide a general guide on how to use Pyarmor to obfuscate a Django application.

Integrating Pyarmor with your code.

  1. Install pyarmor with pip.
  2. Create a dist folder. The dist folder will be our main working directory.
  3. Copy Django application’s source folder to the dist folder.
  4. Go to the folder or code you wish to obfuscate in the src folder.
  5. Run the following command to obfuscate the code.

    pyarmor obfuscate --src="." -r --output=../../dist/apps/ __init__.py
    
  6. Open the output files and you will see that the python files are now obfuscated.

Integration with Cryptography

One of the things that you can also combine with Pyarmor is the usage of the Cryptography module which allows you to encrypt a license key in which you can integrate this into your codebase.

  1. Create an unecrypted license file which can contain e.g. expiry date.
  2. Use the cryptography module to encrypt the license file using a user defined key.
  3. Inside your application, integrate or store the user defined key used to encrypt the license file in a variable.
  4. Create function that will read the encrypted file and decrypt it using the user defined key.
  5. Obfuscate the python code using pyarmor to protect the user defined key.

Refer to the following repository for more details.


Tags: