Protect Python scripts against reverse engineering with Pyarmor
Python is easy to read and widely used. While this readability encourages collaboration, it increases the risk of unauthorized access and misuse. A competitor or malicious actor can copy your proprietary logic and algorithms without proper protection. This will negatively affect the integrity of the software and user trust.
Implement strong security measures, such as data obfuscation and license verification, fortifying your software against potential threats. Protecting Python scripts isn't just a matter of routine; it's an important strategy for ensuring the security of your innovations and maintaining user trust in the digital landscape.
What is Pyarmor?
Pyarmor is a command line library. It helps protect and obfuscate Python scripts and packages. It transforms the original Python code into a more obscure form while maintaining its functionality. Cloaking renames variables, functions, and classes to non-descriptive names. It also removes comments and refactors the code. This makes the code difficult to reverse, forge or copy.
Pyarmor can secure individual Python scripts and entire packages, and even add license validation to your code.
Install Pyarmor . library
Pyarmor is listed on the Python Package Index (PyPI). Use pip to install it by running the following command:
pip install pyarmor
You don't need to install Pyarmor in the same directory where your project is stored. You can install it anywhere on your computer and can secure any Python script from the desired directory.
However, if you want to run secured scripts without having to install Pyarmor on the target machine, you need to install it in the same directory where your project is stored. This is because secured scripts will contain references to the Pyarmor runtime. This is a required condition to run the scripts.
Secure each Python script
Securing each script with Pyarmor is pretty straightforward. Let's look at the script that adds the following two numbers:
def add_numbers(num1, num2): result = num1 + num2 print("The sum of {} and {} is: {}".format(num1, num2, result)) # Sử dụng num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) add_numbers(num1, num2)
Use the command line to navigate to the directory where you installed Pyarmor. Then run the following command to navigate to the directory where you installed Pyarmor. Replace main.py with your script name.
pyarmor gen --output dist main.py
After running this command, Pyarmor creates a new directory named dist . Inside it contains your security script.
Open the security script to see its contents.
The above screenshot shows the result after Pyarmor obfuscate and encode a simple additional script. Now you can't tell what the script does just by looking at it.
To run a secured script, open a terminal or command prompt and navigate to the location where the dist folder is located . Then use the following command to run the script:
python dist/main.py
Replace main.py with the name of the script. The script will run as normal without any disturbance. Thoroughly test to make sure all functions work as you expect.
Protect the entire Python package
Package can contain several modules or hundreds of modules depending on their purpose. Protecting each module is exhausting. Fortunately, Pyarmor is capable of securing the entire package.
Suppose you have a simple Python package named sample_package with the following structure:
sample_package/ |-- __init__.py |-- module1.py |-- module2.py
You can create as many modules as you want.
To encrypt and shuffle a package, open a terminal or command prompt, and then navigate to the directory where your package is located. Then run the command:
pyarmor gen -O dist -r -i sample_package
Replace sample_package with the name of the package. This command will encrypt and obfuscate your packages directory and save the protected output to the dist directory . Use the protected package as you would for any other Python package.
For example, using the sample package above, create a new script inside the dist directory :
from my_package import module1, module2 module1.say_hello() module2.do_something()
When running this code, the package will work as it should before securing it.
Control access to scripts
You may want to limit how long the user runs the script, such as during a trial period. Use the following command after shuffling the script:
pyarmor gen -O dist -e 30 main.py
Replace 30 with the number of days you want the script to work. You can also replace it with an exact date. After that date, the script will expire.
You can test this feature by setting a date in the past. That will cause running the script to throw an error. Use the following command to shuffle a script with an expiration date:
pyarmor gen -O dist -e 2022-01-01 main.py
Then run the following security script:
The error says the license key has expired, so the script cannot run.
Above are the things you need to know about scripting security in Python using Pyarmor. Hope the article is useful to you.
You should read it
- How to set up Python to program on WSL
- What is Python? Why choose Python?
- More than 100 Python exercises have solutions (sample code)
- 5 choose the best Python IDE for you
- Why should you learn Python programming language?
- Multiple choice quiz about Python - Part 3
- Bookmark 5 best Python programming learning websites
- Python data type: string, number, list, tuple, set and dictionary
May be interested
- More than 100 Python exercises have solutions (sample code)more than 100 python code examples are shared by guy zhiwehu on github, however, the solution of this series is written on the old python version. following tipsmake.com will be vietnameseized and edited to suit python 3.x to help you learn and practice python.
- Instructions for creating reverse textwrite letters in reverse, overturning words easily if you know the following tips. let's see how to write this reverse text, you can copy it on facebook so your friends will get tired of reading it.
- Bookmark 5 best Python programming learning websitesif you are a developer or you are studying and want to stick with this industry, learn python to add a highlight in your journey.
- For in Python loopin this article, we will learn more about for for loop in python as well as its variations, how to use for to repeat a string of elements in python such as list, string or other iterative objects.
- Manage files and folders in Pythonpython also provides a variety of methods to handle various directory-related operations. in this article, we will learn about managing files and directories in python, namely creating folders, renaming folders, listing folders and working with them.
- Download the reverse video creation application, Reverse Movie FX is free on the AppStorereverse movie fx is an application that supports reverse video creation, allowing users to get back-to-back, reverse-play, reverse-action videos, which look like an interesting magic trick.
- How to perform Reverse DNS Lookupwhat is reverse dns? dns is often used to resolve domain names into ip addresses. this action is performed every time you visit a website on the internet.
- Multiple choice quiz about Python - Part 3today's topic quantrimang wants to challenge you is about file and exception handling in python. let's try the following 15 questions!
- 5 choose the best Python IDE for youin order to learn well python, it is essential that you find yourself an appropriate ide to develop. quantrimang would like to introduce some of the best environments to help improve your productivity.
- What is Python? Why choose Python?python is a powerful, high-level, object-oriented programming language, created by guido van rossum. python is easy to learn and emerging as one of the best introductory programming languages for people who are first exposed to programming languages.