- What is python obfuscation?
- Library to obfuscate python code
- CLI commands to obfuscate python code.
- Advantages and disadvantages of code obfuscation.
- Example: pyarmor practical examples to obfuscate the python script.
1. What is python obfuscation?
In simple words, obfuscation is the process of hiding the source code from a human being. Since the source code may contain trade secrets, business information, and basic credentials so the process of hiding this information from humans is known as obfuscation. The major benefit of using obfuscation is that we can secure the source code from attackers in prod.
2. Library to obfuscate python code
Below are the 3 main libraries used to obfuscate the python source code.
- pyarmor:
About Pyarmor is a python library that is used to obfuscate python scripts. Using pyarmor we can set the expiry date for the python scripts. install pip install pyarmor Url https://pyarmor.dashingsoft.com/ PyPI Releases https://pypi.org/project/pyarmor/ Is Open Source NO Source Code Open Source free to use at small scale, License required for enterprise Latest Version pyarmor 7.6.1 (till Nov 22) - python-minifier:
About Python-minifier is used to transform the source code into its compact version, it can also be used to obfuscate the python source code. install pip install python-minifier Url https://python-minifier.com/ PyPI Releases https://pypi.org/project/python-minifier/ Is Open Source YES (MIT licence) Source Code https://github.com/dflook/python-minifier/tree/main/src/python_minifier Latest Version Python-minifier 2.7.0 (till Nov 22) - pyminifier:
About Pyminifier is used to transform the source code into its compressed version, it can also be used to obfuscate the python source code. install pip install pyminifier Url http://liftoff.github.io/pyminifier/ PyPI Releases https://pypi.org/project/pyminifier/ Is Open Source YES Source Code https://github.com/liftoff/pyminifier/tree/master/pyminifier Latest Version pyminifier 2.1
Jump to the top of the page↵
3. CLI commands to obfuscate python code.
- pyarmor: Pyarmor is a command line tool, Below are some important commands of pyarmor.
- command to obfuscate the python script (mypthonscript.py)
pyarmor obfuscate mypythonscript.py - command to create the license key which will be used to attach the python script
pyarmor licenses –expired 2022-12-31 r001 - command to obfuscate the python script and attach the license key with expiry date
pyarmor obfuscate –with-license licenses/r001/license.lic mypythonscript.py
- command to obfuscate the python script (mypthonscript.py)
- python-minifier: This is also a command line tool, but we can also import this as a library to another python script.
- command to minify the python script (mypthonscript.py)
pyminify mypythonscript.py
- command to minify the python script (mypthonscript.py)
- pyminifier: This is also a command line tool, but we can also import this as a library to another python script.
- command to obfuscate all function/method names, variables, and classes of the python script (mypthonscript.py)
pyminify –obfuscate mypythonscript.py - command to obfuscate only classes of the python script
pyminify –obfuscate-classes mypythonscript.py - command to obfuscate function and method names only.
pyminify –obfuscate-functions mypythonscript.py
- command to obfuscate all function/method names, variables, and classes of the python script (mypthonscript.py)
Jump to the top of the page↵
4. Advantages and disadvantages of code obfuscation.
Below are the advantages of the code obfuscation
4.1. Advantages:
- Source code can be distributed securely in an unsafe network.
- Give some level of surety that the intellectual property is protected against security threats.
- Make it difficult for source code reverse engineer.
- It provides an extra layer of security.
- You can set an expiry date of source code.
- It Protects against hackers and untrustworthy organizations people.
- It makes it hard to copy-paste the original codebase.
4.2. Disadvantage:
- This will impact the code performance.
- Obfuscation only slows down the process of reverse engineering, it does not prevent it.
- Obfuscation can be used by attackers to hide malicious code.
Jump to the top of the page↵
5. Example: pyarmor practical examples to obfuscate the python script:
In this example, I am going to who how to obfuscate the python script. I will follow the below steps to obfuscate the code and I will show you how to execute the obfuscated python code.
Steps:
- Original code: Below is the original code and how it looks before obfuscating
- Go to the script directory and execute the list command
- Run the below pyarmor command to obfuscate the script
pyarmor obfuscate remove_duplicate_values.py
- It will create a new dist directory and put the obfuscated script into the dist folder.
- Let us see the obfuscated script
- Now let us run this obfuscated python script from CLI
- The above obfuscated code is running perfectly. Thank you for your reading and support.
Below are my previous articles on python
- Datapane python reporting library for interactive reports and beautiful charts
- Jinja a template based python library for beautiful and interactive reports
- Python reporting libraries | Python reporting tools to generate interactive and beautiful reports
- pyspark dataframe | python spark dataframe with examples
- A to Z of Python exception handling | Python try catch with example
- File Handling Python With Examples | Python Input-Output Awesome Tips
- Modules in python tactics that can boost your python programming skills
- Python array | numpy for python | How to create numpy or np array in python
- Python Lambda Function With Examples | How To Create A New Anonymous function
- Python Useful Built-in Functions With Examples | How to create a new function
- python iteration | while loop | for loop with examples
- Python Data Types With Examples
- Python – installation indentation variable comments
- Python Programming Introduction
Nice explanation with example.
Thank you Jhalak.
obfuscation?
Cheats for Left 4 Dead 2, MyBB
ford fiesta custom, obfuscating car accessory.
big gig, nice.
Thank you.
good source code hiding method.
Thank you.
check here drivers, bucher herunterladen
i will continue reading these articles, thank you for sharing.
Thank you.
उदाहरण के साथ अच्छी व्याख्या।
Thank you.
Real clear website and thanks for python code hiding technique, thanks for this post.
I have not checked in here for some time because I thought it was getting boring, but the last several posts are great quality so I guess I’ll add you back to my daily bloglist. You deserve it my friend 🙂
I think you have remarked some very interesting details, thankyou for the post.
I have not checked in here for some time because I thought it was getting boring, but the last few posts are great quality so I guess I’ll add you back to my everyday bloglist. You deserve it my friend 🙂