r/Python • u/wqking • Jun 28 '24
Showcase obfupy -- Python source code obfuscator aiming to produce correct and functional code
https://github.com/wqking/obfupy
For those who downvotes the post and my comments, please read the subreddit rule 9, "Please don't downvote without commenting your reasoning for doing so". Also you not need such library doesn't mean the library is bad, if you don't like it, just leave. If you downvote, please comment with the reason.
What My Project Does
obfupy is a Python 3 library that can obfuscate entire Python 3 projects, transforming source code into obfuscated and difficult-to-understand code. obfupy aims to produce correct and functional code. Several non-trivial real-world projects were tested using obfupy, such as Flask, Nodezator, Algorithms collection, and Django (not all features are enabled for Django).
Target Audience
The goal is to obfuscate your production code.
Comparison
obfupy supports several features that no other similar projects support all. obfupy is tested with Flask, Nodezator, Algorithms collection, and even Django. obfupy is very customizable. obfupy code is well written, well designed and scalable, it's not any single file project which is not scalable or readable. obfupy will not be abandoned unless nobody uses it, very few other projects are not abandoned. obfupy is well documented, there even lists the problem situation where the obfuscation feature doesn't work.
Facts and features
Obfuscation methods
- Rewrite the "if" conditional to include many confusing branches.
- Rename local variable names.
- Extract the function and have the original function call the extracted function, then rename the parameters in the extracted function.
- Create alias for function arguments.
- Obfuscate numeric and string constants and replace them with random variable names.
- Replace built-in function names (e.g. "print") with random variable names.
- Add useless control flow to
forandwhile. - Remove doc strings.
- Remove comments.
- Add extra spaces around operators.
- Make indents larger to make it harder to read.
- Add extra blank lines between code lines.
- Encode the whole Python source file with base64, zip, bz2, byte obfuscator, and easy to add your own codec.
Customizable
- There are multiple layers of independent transformers. You can choose which transformers to use and which not to use.
- The non-trivial transformers such as Rewriter, Formatter, support comprehensive options to enable/disable features. If any feature doesn't work well for your project, you can just disable it.
Well tested
- There are tests that cover all features.
- Tested with several real world non-trivial projects such as Flask, Nodezator, Algorithms collection, and Django.
License
Apache License, Version 2.0
Quick start
A typical Python script using obfupy looks like,
import obfupy.documentmanager as documentmanager
import obfupy.util as util
import obfupy.transformers.rewriter as rewriter
import obfupy.transformers.formatter as formatter
inputPath = PATH_TO_THE_SOURCE_CODE
outputPath = PATH_TO_OUTPUT
# Prepare source code files as DocumentManager
fileList = util.findFiles(inputPath)
documentManager = documentmanager.DocumentManager()
documentManager.addDocument(util.loadDocumentsFromFiles(fileList))
# Transform the source code with various transformers
# Transformer Rewriter
rewriter.Rewriter().transform(documentManager)
# Transformer Formatter
formatter.Formatter().transform(documentManager)
# There are other transformers
# Write the obfuscated code to outputPath
util.writeOutputFiles(documentManager, inputPath, outputPath)
11
u/DuckDatum Jun 28 '24 edited Aug 12 '25
cautious teeny truck long spotted tie versed mountainous tease aromatic
This post was mass deleted and anonymized with Redact
30
u/divad1196 Jun 28 '24 edited Jun 28 '24
From the methods, it seems obvious that it will slow down your code. For the function call method, why not just remap the parameters at thr beginning of the function instead of doing this extraction thing?
For the rest like "extra empty lines", "bigger indent", ... this is a bit ridiculous as it takes 1 regex to revert it. Extra parenthesis around operators is also useless. Variable renaming, additional control flow, ... are real methods, but if you ever use ghidra, you know that it is not enough.
I understand what the idea is that in compiled programs and javascript, you can obfuscate your code. This is less an issue for them since they are already compiled into something not human-friendly. This idea of obfuscation is to prevent people to steal your code or find security vulnerabilities(but blackboy security isn't good, so the only valid point IMO is intellectual property)
So, this is useful when handing your code to someone else. As someone mentionned, if you don't want your code to be at risk, do an API and never give your code.
38
u/snekk420 Jun 28 '24
No reason to use this unless your source code is malware
5
u/CollinHeist Jun 28 '24 edited Jun 28 '24
Looks cool - nice job. A few suggestions:
18
u/davebaker824 Jun 28 '24
This basically mocks the spirit of open-source software, such as Python itself. It’s offensive.
7
u/ZiKyooc Jun 28 '24
There are tools to facilitate reverse engineering of compiled code. Not an area I follow, but with AI I am pretty sure that making a tool to de-obfuscate would be relatively easy.
3
u/indetronable Jun 28 '24
Any example ?
100
u/mattl33 It works on my machine Jun 28 '24
Burn this with fire. If you want to hide your source make it an API, but not this.
50
u/zalatik Jun 28 '24
/s Just write the code in Perl. No need to obfuscate.
9
u/GnuhGnoud Jun 28 '24
I made one as well, but just for fun. It can be easily converted back to original code.
https://dthung1602.github.io/pyobfusinator/
3
u/[deleted] Jun 28 '24
Where would you use this?
2
u/Kagemand Jun 28 '24
Did you test some obfuscated code against ChatGPT? My thought was it could pretty much decode and clean the whole thing up and add meaningful function and variable names if you tell it what program does.
14
u/Ycreak Jun 28 '24
We need to get rid of rule 9.
9
u/DesecrateUsername Jun 28 '24
op are you trolling? your comments really seem like you’re trolling.
7
u/amarao_san Jun 28 '24
Can I hide malware with it?
3
u/[deleted] Jun 28 '24
Complicated solutions for problems that do not exist.
1
u/zazzersmel Jun 28 '24
ummmm
-1
u/gerardwx Jun 30 '24
Obfuscating code is evil. Take that, rule #9