Skip to content

Commit

Permalink
Refine plugin example with get_license_code
Browse files Browse the repository at this point in the history
  • Loading branch information
jondy committed Jun 16, 2019
1 parent 55067f1 commit 027cd76
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
51 changes: 28 additions & 23 deletions plugins/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,47 @@ There are 2 files in this plugin::

extra_hdinfo.c
check_multi_mac.py
First build dynamic library `extra_hdinfo.so` used to get all mac
addresses::

gcc -shared -o extra_hdinfo.so -fPIC extra_hdinfo.c

The dynamic library `extra_hdinfo.so` exports one function
`get_multi_mac` which could get all the mac addresses.

The script `check_multi_mac.py` will call `get_multi_mac` in the
dynamic library `extra_hdinfo.so` by `ctypes`, and then check whether
it's expected.

Run the following command to get all mac addresses in target machine::

gcc -DAPP -o hdinfo extra_hdinfo.c
./hdinfo

Edit the file `check_multi_mac.py`, replace the value of
`expected_mac_addresses` got above.
The script `check_multi_mac.py` will get the multiple mac address by
calling `get_multi_mac` in the dynamic library `extra_hdinfo.so`, then
compare the expected mac address saved in the `license.lic` of the
obfuscated scripts, check whether it's expected.

It will also check the file `extra_hdinfo.so` to be sure it's not
changed by someone else.

First build dynamic library `extra_hdinfo.so` used to get all mac
addresses::

gcc -shared -o extra_hdinfo.so -fPIC extra_hdinfo.c

Get sha384 of `extra_hdinfo.so`::

sha384sum extra_hdinfo.so

Edit the file `check_multi_mac.py`, replace the value of
`lib_hdinfo_checksum` got above.

Now edit the entry script `foo.py`, insert two comment lines::
Next edit the entry script `foo.py`, insert two comment lines::

import logging
import sys

# {PyArmor Plugins}

def main():
# PyArmor Plugin: check_multi_mac()
logging.info("Hello World!")


if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
main()
Finally, obfuscate the script with this plugin::

Now, obfuscate the script with this plugin::

pyarmor obfuscate --plugin check_multi_mac foo.py

Expand All @@ -75,6 +68,18 @@ If the plugin file isn’t in the current path, use absolute path instead::

pyarmor obfuscate --plugin /path/to/check_multi_mac foo.py

The last step is to generate the license file for the obfuscated script.

1. Run the following command to get all mac addresses in target machine::

gcc -DAPP -o hdinfo extra_hdinfo.c
./hdinfo

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

Distributing the obfuscated scripts to target machine:

* Copy all the files in the `dist` path to target machine
Expand Down
3 changes: 2 additions & 1 deletion plugins/check_multi_mac.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from hashlib import sha384
from ctypes import cdll, c_char
from pytransform import get_license_code

lib_hdinfo_filename = "/usr/lib/extra_hdinfo.so"
lib_hdinfo_checksum = "7838938a424b273c1e9782a1f1aebe3b70421d83a73a091e2502a5206931f0a58c7dee1d4d2d0e313ae6b992f5fa865d"
expected_mac_addresses = "00:16:3e:08:20:b4,70:f1:a1:23:f0:94"
expected_mac_addresses = get_license_code()


def _check_lib_hdinfo():
Expand Down
2 changes: 1 addition & 1 deletion plugins/extra_hdinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ query_interface_mac_address(int sock, char *buf, unsigned int size)
return -1;
}

snprintf(buf + j * 18, size, "%02x:%02x:%02x:%02x:%02x:%02x,",
snprintf(buf + j * 18, size, "%02x:%02x:%02x:%02x:%02x:%02x.",
(unsigned char)req[0].ifr_hwaddr.sa_data[0],
(unsigned char)req[0].ifr_hwaddr.sa_data[1],
(unsigned char)req[0].ifr_hwaddr.sa_data[2],
Expand Down

0 comments on commit 027cd76

Please sign in to comment.