From ee17550620f19216385c9a11c94b2e0b71f15c00 Mon Sep 17 00:00:00 2001 From: Jondy Zhao Date: Tue, 15 Oct 2019 08:33:10 +0800 Subject: [PATCH] Refine plugin examples --- docs/advanced.rst | 1 + plugins/README.rst | 48 +++++++++++++++++--------------------- plugins/check_docker.py | 14 +++++------ plugins/check_multi_mac.py | 28 +++++++++++++--------- plugins/foo.py | 2 +- 5 files changed, 46 insertions(+), 47 deletions(-) diff --git a/docs/advanced.rst b/docs/advanced.rst index e1bc564c..7ea30dd3 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -225,6 +225,7 @@ after the first comment:: At the same time, the prefix of second comment will be stripped:: def main(): + # PyArmor Plugin: check_expired() check_expired() So the plugin takes effect. diff --git a/plugins/README.rst b/plugins/README.rst index 7570d894..b994b1fa 100644 --- a/plugins/README.rst +++ b/plugins/README.rst @@ -36,7 +36,12 @@ Get sha384 of `extra_hdinfo.so`:: Edit the file `check_multi_mac.py`, replace the value of `lib_hdinfo_checksum` got above. -Next edit the entry script `foo.py`, insert two comment lines:: +Then edit the entry script `foo.py `_ , insert two comment lines:: + + # {PyArmor Plugins} + # PyArmor Plugin: check_docker_id() + +It maybe like this, for example:: import logging import sys @@ -80,8 +85,8 @@ The last step is to generate the license file for the obfuscated script. 2. Generate the license file and copy it to dist path:: - pyarmor licenses 70:f1:a1:23:f0:94.08:00:27:51:d9:fe - cp licenses/70:f1:a1:23:f0:94.08:00:27:51:d9:fe/license.lic ./dist + pyarmor licenses -x 70:f1:a1:23:f0:94.08:00:27:51:d9:fe CODE-0001 + cp licenses/CODE-0001/license.lic ./dist Distributing the obfuscated scripts to target machine: @@ -95,15 +100,14 @@ First write the plugin `check_docker.py`:: from pytransform import _pytransform from ctypes import py_object, PYFUNCTYPE - - def get_license_code(): + + def get_license_data(): prototype = PYFUNCTYPE(py_object) dlfunc = prototype(('get_registration_code', _pytransform)) rcode = dlfunc().decode() - index = rcode.find('*CODE:') - return rcode[index+6:] - from pytransform import get_license_code - + index = rcode.find(';', rcode.find('*CODE:')) + return rcode[index+1:] + def check_docker_id(): cid = None with open("/proc/self/cgroup") as f: @@ -111,35 +115,25 @@ First write the plugin `check_docker.py`:: if line.split(':', 2)[1] == 'name=systemd': cid = line.strip().split('/')[-1] break - - if cid is None or cid != get_license_code(): + + if cid is None or cid != get_license_data(): raise RuntimeError('license not for this machine') - -Then edit the entry script `foo.py`, insert two comment lines:: - import logging - import sys +Then edit the entry script `foo.py `_ , insert two comment lines:: # {PyArmor Plugins} - - def main(): - # PyArmor Plugin: check_docker_id() - print("Hello World!") - - - if __name__ == '__main__': - main() + # PyArmor Plugin: check_docker_id() Now, obfuscate the script with this plugin:: pyarmor obfuscate --plugin check_docker foo.py - + If the plugin file isn’t in the current path, use absolute path instead:: pyarmor obfuscate --plugin /path/to/check_docker foo.py - + The last step is to generate the license file for the obfuscated script:: - pyarmor licenses f56b1824e453126ab5426708dbbed41d0232f6f2ab21de1c40da934b68a5d8a2 - cp licenses/f56b1824e453126ab5426708dbbed41d0232f6f2ab21de1c40da934b68a5d8a2/license.lic ./dist + pyarmor licenses -x f56b1824e453126ab5426708dbbed41d0232f6f2ab21de1c40da934b68a5d8a2 CODE-0002 + cp licenses/CODE-0002/license.lic ./dist diff --git a/plugins/check_docker.py b/plugins/check_docker.py index 07e1e2b8..c728c2e1 100755 --- a/plugins/check_docker.py +++ b/plugins/check_docker.py @@ -1,13 +1,11 @@ -from pytransform import _pytransform -from ctypes import py_object, PYFUNCTYPE - - -def get_license_code(): +def get_license_data(): + from pytransform import _pytransform + from ctypes import py_object, PYFUNCTYPE prototype = PYFUNCTYPE(py_object) dlfunc = prototype(('get_registration_code', _pytransform)) rcode = dlfunc().decode() - index = rcode.find('*CODE:') - return rcode[index+6:] + index = rcode.find(';', rcode.find('*CODE:')) + return rcode[index+1:] def check_docker_id(): @@ -18,5 +16,5 @@ def check_docker_id(): cid = line.strip().split('/')[-1] break - if cid is None or cid != get_license_code(): + if cid is None or cid != get_license_data(): raise RuntimeError('license not for this machine') diff --git a/plugins/check_multi_mac.py b/plugins/check_multi_mac.py index 2a757f3a..247c13f3 100644 --- a/plugins/check_multi_mac.py +++ b/plugins/check_multi_mac.py @@ -1,20 +1,25 @@ -from hashlib import sha384 -from ctypes import cdll, c_char -from pytransform import get_license_code +def get_license_data(): + from pytransform import _pytransform + from ctypes import py_object, PYFUNCTYPE + prototype = PYFUNCTYPE(py_object) + dlfunc = prototype(('get_registration_code', _pytransform)) + rcode = dlfunc().decode() + index = rcode.find(';', rcode.find('*CODE:')) + return rcode[index+1:] -lib_hdinfo_filename = "/usr/lib/extra_hdinfo.so" -lib_hdinfo_checksum = "7838938a424b273c1e9782a1f1aebe3b70421d83a73a091e2502a5206931f0a58c7dee1d4d2d0e313ae6b992f5fa865d" -expected_mac_addresses = get_license_code() - -def _check_lib_hdinfo(): +def _check_lib_hdinfo(lib_hdinfo_filename): + from hashlib import sha384 + lib_hdinfo_checksum = "7838938a424b273c1e9782a1f1aebe3b70421d83a73a091e2502a5206931f0a58c7dee1d4d2d0e313ae6b992f5fa865d" with open(lib_hdinfo_filename, 'rb') as f: checksum = sha384(f.read()).hexdigest() if not checksum == lib_hdinfo_checksum: raise RuntimeError('unexpected %s' % lib_hdinfo_filename) -def _check_multi_mac(): +def _check_multi_mac(lib_hdinfo_filename): + from ctypes import cdll, c_char + expected_mac_addresses = get_license_data() m = cdll.LoadLibrary(lib_hdinfo_filename) size = 1024 t_buf = c_char * size @@ -26,5 +31,6 @@ def _check_multi_mac(): def check_multi_mac(): - _check_lib_hdinfo() - _check_multi_mac() + lib_hdinfo_filename = "/usr/lib/extra_hdinfo.so" + _check_lib_hdinfo(lib_hdinfo_filename) + _check_multi_mac(lib_hdinfo_filename) diff --git a/plugins/foo.py b/plugins/foo.py index 32dd2468..61fb59c3 100644 --- a/plugins/foo.py +++ b/plugins/foo.py @@ -1,8 +1,8 @@ import logging -import sys # {PyArmor Plugins} + def main(): # PyArmor Plugin: check_multi_mac() logging.info("Hello World!")