PyArmorでPythonのコードを難読化する

Python

Pythonコードを配布する際、中身を知られたくない場合には、PyArmorを用いて難読化すると便利です。PyArmorは有料のシェアウエアとなっていますが、非商用利用の場合には、機能を制限した体験版を利用できます。

次のようにしてpipを用いてPyArmorをインストールすることができます。

pip install pyarmor

適当なコード(test.pyとする)を難読化してみます。

$ pyarmor gen test.py
INFO     Python 3.11.4
INFO     Pyarmor 8.3.11 (trial), 000000, non-profits
INFO     Platform linux.x86_64
INFO     search inputs ...
INFO     find script test.py
INFO     find 1 top resources
INFO     start to generate runtime files
INFO     target platforms {'linux.x86_64'}
INFO     write dist/pyarmor_runtime_000000/pyarmor_runtime.so
INFO     patch runtime file
INFO     generate runtime files OK
INFO     start to obfuscate scripts
INFO     process resource "test"
INFO     obfuscating file test.py
INFO     write dist/test.py
INFO     obfuscate scripts OK

dist ディレクトリ以下に難読化されたコード dist/test.py 生成されます。python dist/test.py として、元のコードと同様に実行することができます。dist/test.py の中身は次のように難読化されています。

# Pyarmor 8.3.11 (trial), 000000, non-profits, 2023-10-16T13:57:01.796102from pyarmor_runtime_000000 import __pyarmor__
__pyarmor__(__name__, __file__, b'PY000000\x00\x03\x0b\x00\xa7\r\r\n\x80\x00\x01\x00\x08\x00\x00\x00\x04\x00\x00\x00@\x00\x00\x00?\x05\x00\x00\x12\t\x04(略)')

また、dist/pyarmor_runtime_000000/ 以下に、コードを実行するのに必要なパッケージファイルが格納されます。このディレクトリ名を変更するには、有料ライセンスが必要です。

コードを配布する際には、distディレクトリ以下のファイル一式だけでOKです。配布先のマシンでPyArmorをインストールする必要はありません。

参考資料

広告
ゴールデンウェイジャパン(FXTF MT4)

本サイトの内容は、投資の勧誘を目的としたものではなく、本サイト内の情報に基づいて行った取引の損失について、本サイトは一切の責を負いかねます。当該情報の欠落・誤謬等につきましてもその責を負いかねますのでご了承ください。免責事項もご覧ください。

Python

コメント

タイトルとURLをコピーしました