How to "hide" some .py files into .exe with cx_Freeze?

Viewed 1483

Env:

Windows 10
python 3.6.6
cx-Freeze 5.0.2

Git hub example

It contails .msi for installing

Example project structure:

/package_name
        /some_packet
            /__init.py
            /module_name.py  # for an example contains valiable in code "module_1"
    /main.py
    /setup.py
    /some_module.py  # for an example contains valiable "module_2"
    /some_other_module.py  # for an example contains valiable "module_3"

Example of setup.py(simplified)

import cx_Freeze

cx_Freeze.setup(
    name="example",
    options={
        "build_exe": {
            "packages": ["asyncio"],
            "include_files": ["static\some_static_file.png"]
        },
        "bdist_msi": {
            "upgrade_code": "{492de237-1853-4599-a707-c283d567699f}"
        }
    },
    executables=[cx_Freeze.Executable("main.py")]
)

Current behavior

For creating .msi install file -> run command python setup.py bdist_msi. It will generate .msi files for installing application.

After installing this application: directory(where application is installed) will contain:

  • main.exe
  • lib\some_packet directory
  • lib\some_packet\module_name.pyc file
  • other files

There are following statements:

1) From root directory(where application is installed) i start search(via grep -Rna command under Ubuntu guest system, it's just more convenient for me) and valiable module_1 could be found in directories(in lib\some_packet\module_name.pyc) and module_2/module_3 couldn't be found.
Details:

(v_decompile) any@any-pc:/mnt/hgfs/shar/example_installed$ grep -Rna "module_1" 
lib/some_packet/module_name.pyc:2:B�!]�@dZdS)module_1N)r�rr�PG:\heroes\installer-with-cx_Freeze\sources_of_project\some_packet\module_name.py<module>s 
(v_decompile) any@any-pc:/mnt/hgfs/shar/example_installed$ grep -Rna -a "module_2" 
(v_decompile) any@any-pc:/mnt/hgfs/shar/example_installed$ grep -Rna -a "module_3"

2) File lib\some_packet\module_name.pyc could be easily converted to original file(without comments) by e.g. python-uncompyle6.
Details:

(v_decompile) any@any-pc:/mnt/hgfs/shar/example_installed$ uncompyle6 lib/some_packet/module_name.pyc 
# uncompyle6 version 3.3.3 
# Python bytecode 3.6 (3379) 
# Decompiled from: Python 3.6.6 (default, Jul 20 2018, 15:39:05) 
# [GCC 4.8.4] 
# Embedded file name: G:\heroes\installer-with-cx_Freeze\sources_of_project\some_packet\module_name.py 
# Compiled at: 2019-07-07 11:28:50 
module_1 = 'module_1' 
# okay decompiling lib/some_packet/module_name.pyc

3) (solved with this question) In both points: file contains source path G:\heroes\installer-with-cx_Freeze\sources_of_project\some_packet\module_name.py It confuses me a bit. Application was installed from .msi and (as I understand) should not know about source directories (regarding path) which was used for creating last one.

Questions:

  1. Is there any way to recover some_module.py and some_other_module.py to original files from main.exe?(like it could be done with lib\some_packet\module_name.pyc)

  2. How to "hide" some other files in application into main.exe or somehow avoid converting .pyc to original files.(maybe some properties in cx_Freeze?)

Note:

It should be done with cx_Freeze.

PS: I don't want to create single .exe. I try to find convenient way for specifying which files should be stored in main.exe like it was done with some_module.py and some_other_module.py

PSS: At this moment I see only I way: put all files on main.py level :) But it will look weird for big project.

3 Answers

Quoting How to obfuscate Python source code:

These two methods [using pyobfuscate and distributing bytecode] are really just a deterrent, not a secure way of hiding the code.

If you want something a bit more robust, you should take a look at Nuitka, which compiles Python code to C++, so you can compile that and just distribute the executable. It seems to be broadly compatible with different libraries and different versions of Python.

See also Python Code Obfuscation

This video might help. It talks about cxfreeze and how you can use cxfreeze to make a excutable, and I know it works for 3.4+ because the video uses python 3.4, but really your method should be fine...

Related