Obfuscation in general is to make anything unclear or disturb the normal in purpose to make it difficult/impossible to understand. We use this in programming for the following reasons:
- Hide the working logic of the code
- Protect the Intellectual Property
- Prevent an attacker from performing reverse engineering a proprietary software program
Encrypting parts or all of the code, stripping out potentially revealing metadata, replacing class and variable names with meaningless labels, and adding unused or meaningless code to an application script are some of the methods for performing obfuscation in coding.
The level of obfuscation is determined by the below factors:
- The extent to which transformed code resists automated deobfuscation attempts
- The degree to which transformed code differs from the original is another measure of how effective it is
- The cost-efficient obfuscation method will be more useful when it scales for larger applications
- The more layers the obfuscator adds, the more complex the program will be, making the obfuscation more successful
The obfuscation is included normally in static programming languages like C, C++, Java, and Scala since the compiler produces an intermediate machine code/object file/jar file which is difficult to decode by humans or other programs.
When it comes to Python language, it uses an interpreter to run the source code, so there are no intermediate files generated, so there is a purpose of doing obfuscation.
There are a few methods of obfuscation in Python, but the most effective one is done using the Pyarmor library.
How to obfuscate Python code using the Pyarmor
Pyarmor module is not included in the standard Python library, so we have to install it using the pip command in Linux/Mac OS.
python3 -m pip install pyarmor
(or)
pip3 install pyarmor
This package installs pyarmor command locally, using which we will obfuscate the source code using the below command
pyarmor obfuscate –restrict=0 <python code>
In our case, we have to obfuscate the below source code for demonstrating purposes. The code has one function definition – inference which takes a string and performs the date comparison operation. If the year is less than 2022, it returns True else False. Based on the returning value we will print appropriate messages.
cat evaluate.py
When we run the program, we get the below results
python evaluate.py
If we use the above pyarmor command to obfuscate our code, we get the below results and a new dist folder will be created in the present directory
pyarmor-7 obfuscate --restrict=0 evaluate.py
Inside the dist directory, there is a copy of original code in encoded format like below:
cat evaluate.py
If we run this code, we will get a similar output as before
python evaluate.py
How to obfuscate an entire project using pyarmor:
Here we have a sample project containing python files containing classes which do simple arithmetic operations. Below is the file structure of a project
- python_classes
- addition.py
- multiply.py
- subtraction.py
- subclasses
- division.py
- remainder.py
- implement.py
implement.py is the file, in which we call the classes present in this project. Below is the screenshot of one of the arithmetic file
cat python_classes/addition.py
And below is the screenshot of implement.py and result of execution of this file
python python_classes/implement.py
cat python_classes/implement.py
If we need to obfuscate this project, we should use an extra flag in the pyarmor called “-r”, which archives all the files in the given directory in recursive fashion like below:
pyarmor-7 o -r --restrict 0 python_classes/implement.py
In this case, we should mention the entry script for the project, i.e., implement.py to make it executable by the python command like below screenshot
cat dist/implement.py
Note: If we want to obfuscate all the files in a directory, that should be executable then we should run “pyarmor obfuscate –restrict 0 <folder name>/*”