Hi

Is there a way to hide the python source code from being copied or hacked?

I am planning to write and distribute some python code to my team, but I would prefer not to reveal the code. Is it possible in Linux?

In windows I wud probably create an .exe file and distribute it, how can i do the same in Linux?

Regards

Antonio

Dani AI

Generated

Short, practical summary and safer options (adds to and ): there is no foolproof way to "hide" Python source once you ship runnable code. The usual quick tricks raise the work required to recover logic but do not stop a determined attacker. For real protection, treat the goal as "raise cost" and choose one of three principled approaches depending on the threat model: move sensitive logic into compiled native code, run the sensitive parts on a server you control, or accept local distribution and combine obfuscation with legal/licensing controls.

Concrete approaches and cautions:

  • Compile critical modules to native extensions (Cython or a native compiler). This increases effort to reverse-engineer and keeps core algorithms out of plain .py files, but compiled .so files are tied to Python version, ABI, and architecture and must be built per target platform.
  • Produce native executables (Nuitka or similar) or distributable bundles for convenience; these still include bytecode or symbols that can be extracted, so treat them as medium protection.
  • Put the secret logic on a server/API; clients call the API and never receive the code. This is the strongest technical protection.
    Recommended quick commands (examples only — test on your distro and Python version):
pip install cython
python -m cythonize -i module.pyx
pip install nuitka
python -m nuitka --standalone --follow-imports myscript.py

Operational tips: never hardcode secrets; use environment variables or a secrets manager. Build and test binaries for the exact Python minor version and CPU architecture you target. Add licensing, tamper checks, and code signing if legal deterrence is needed. For on Linux, compiled-extension or server-side execution are the most practical ways to keep important code out of shipped plaintext.

Recommended Answers

All 2 Replies

1) Convert the .py or .pyw files to a .pyc (python compiled file)
This is accomplished by importing the file; distribute the .pyc (Note that there are .pyc decompilers if they are wanting to know your code _that_ badly)
2) Obfuscate your code
This is accomplished by taking your code and making it as unreadable as possible. Take for example, the following code:

# For logging in to the system
class Secure():

    logbase = {'admin':'123456'}
    logged = False
    invalid = False

    def login(self, user, passw):
        self.logged = False
        self.invalid = False
        try:
            if self.logbase[user] == passw:
                self.logged = True
            else:
                self.logged = False
        except:
            self.logged = False
            self.invalid = True

    def __init__(self):
        while self.logged == False:
            user = raw_input('Username: ')
            passw = raw_input('Password: ')
            self.login(user, passw)
            if self.invalid == True:
                print "Invalid username"
            elif self.logged == False:
                print "Username and Password do not match"
            elif self.logged == True:
                print "Welcome to the system %s" %(user)

S = Secure()

Readable, right? Well, go through and rename every single variable to something else (_, __, ___, ____, etc.), and use import * as _. Also, try and fit as much as you can on one code; if possible, use the lambda function (I don't even know how to use it, sorry). So, our code now looks like this:

class _():
    _, __, ___ = {'admin':'123456'}, False, False
    def ____(_, __, ___):
        _.__ = False
        _.___ = False
        try:
            if _._[__] == ___: _.__ = True
            else: _.__ = False
        except:
            _.__, _.___ = False, True
    def __init__(_):
        while _.__ == False:
            __, ___ = raw_input('Username: '), raw_input('Password: ')
            _.____(__, ___)
            if _.___ == True: print "Invalid username"
            elif _.__ == False: print "Username and Password do not match"
            elif _.__ == True: print "Welcome to the system %s" %(__)
__ = _()

Now the code is highly unreadable, but still functional. Maybe this plus a fancy encryptor? xD That would work wonders. If you want a basic encryptor based on hexing, I can post some source code. Hope this helped :D

class _():
    _, __, ___ = {'admin':'123456'}, False, False
    def ____(_, __, ___):
        _.__ = False
        _.___ = False
        try:
            if _._[__] == ___: _.__ = True
            else: _.__ = False
        except:
            _.__, _.___ = False, True
    def __init__(_):
        while _.__ == False:
            __, ___ = raw_input('Username: '), raw_input('Password: ')
            _.____(__, ___)
            if _.___ == True: print "Invalid username"
            elif _.__ == False: print "Username and Password do not match"
            elif _.__ == True: print "Welcome to the system %s" %(__)
__ = _()

Duh!
I didn't thought of this.
a hacker heart will broke once he finds the code he used a lot of time and energy is that mess. He might even start thinking that his decompiler is lying to him ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.