Python obfuscation | Great technique to hide python code | Perfect use of pyarmor

  1. What is python obfuscation?
  2. Library to obfuscate python code
  3. CLI commands to obfuscate python code.
  4. Advantages and disadvantages of code obfuscation.
  5. 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.

  1. 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)
  2. 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)
  3. 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.

  1. 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
  2. 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
  3. 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
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:

  1. Source code can be distributed securely in an unsafe network.
  2. Give some level of surety that the intellectual property is protected against security threats.
  3. Make it difficult for source code reverse engineer.
  4. It provides an extra layer of security.
  5. You can set an expiry date of source code.
  6. It Protects against hackers and untrustworthy organizations people.
  7. It makes it hard to copy-paste the original codebase.

4.2. Disadvantage:

  1. This will impact the code performance.
  2. Obfuscation only slows down the process of reverse engineering, it does not prevent it.
  3. 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:

  1. Original code: Below is the original code and how it looks before obfuscating
    original python code below obfuscation
  2. Go to the script directory and execute the list command
  3. Run the below pyarmor command to obfuscate the script
    pyarmor obfuscate remove_duplicate_values.py
    pyarmor command to obfuscate the script
  4. It will create a new dist directory and put the obfuscated script into the dist folder.
    dist directory for obfuscated scripts dist directory for obfuscated code
  5. Let us see the obfuscated script
    obfuscated script
  6. Now let us run this obfuscated python script from CLI
    executing the obfuscated python script from cli
  7. The above obfuscated code is running perfectly. Thank you for your reading and support.

Below are my previous articles on python

17 thoughts on “Python obfuscation | Great technique to hide python code | Perfect use of pyarmor”

  1. graliontorile

    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 🙂

  2. vorbelutr ioperbir

    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 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *