Skip to content

Latest commit

 

History

History
191 lines (122 loc) · 5.63 KB

pytransform.rst

File metadata and controls

191 lines (122 loc) · 5.63 KB

Runtime Module pytransform

If you have realized that the obfuscated scripts are black box for end users, you can do more in your own Python scripts.In these cases, :mod:`pytransform` would be useful.

The :mod:`pytransform` module is distributed with obfuscated scripts, and must be imported before running any obfuscated scripts. It also can be used in your python scripts.

Contents

.. exception:: PytransformError

   It's DEPRECATED.

   This is raised when any pytransform api failed. The argument to the
   exception is a string indicating the cause of the error.

   It's not available in super mode.

.. function:: get_expired_days()

   Return how many days left for time limitation license.

   >0: valid in these days

   -1: never expired

Note

If the obfuscated script has been expired, it will raise exception and quit directly. All the code in the obfuscated script will not run, so this function will never return 0.

.. function:: get_license_info()

   Get license information of obfuscated scripts.

   It returns a dict with keys:

   * ISSUER: The issuer id
   * EXPIRED: Expired date
   * IFMAC: mac address bind to this license
   * HARDDISK: serial number of harddisk bind to this license
   * IPV4: ipv4 address bind to this license
   * DATA: extra data stored in this licese, used by extending license type
   * CODE: registration code of this license

   The value `None` means no this key in the license.

   The key `ISSUER` is introduced from v6.2.5. It will be `trial` if the
   `license.lic` is generated by trial pyarmor. For purchased pyarmor, it will
   be the purchased key like `pyarmor-vax-NNNNNN`.  Note that if the
   `license.lic` is generated by pyarmor before v6.0.1, it will be None.

   Raise :exc:`Exception` if license is invalid, for example, it has
   been expired.

.. function:: get_license_code()

   Return a string in non-super mode or bytes object in super mode, which is
   last argument as generating the licenses for obfucated scripts.

   Raise :exc:`Exception` if license is invalid.

.. function:: get_user_data()

   Return a string in non-super mode or bytes object in super mode, which is
   specified by ``-x`` as generating the licenses for obfucated scripts.

   Return None if no specify ``-x``.

   Raise :exc:`Exception` if license is invalid.

.. function:: get_hd_info(hdtype, name=None)

   Get hardware information by *hdtype*, *hdtype* could one of

   *HT_HARDDISK* return the serial number of first harddisk

   *HT_IFMAC* return mac address of first network card

   *HT_IPV4* return ipv4 address of first network card

   *HT_DOMAIN* return domain name of target machine

   Raise :exc:`Exception` if something is wrong.

   In Linux, `name` is used to get named network card or named harddisk. For
   example::

     get_hd_info(HT_IFMAC, name="eth2")
     get_hd_info(HT_HARDDISK, name="/dev/vda2")

   In Windows, `name` is used to get all network cards and harddisks. For
   example::

     get_hd_info(HT_IFMAC, name="*")
     get_hd_info(HT_HARDDISK, name="*")

     get_hd_info(HT_HARDDISK, name="/0")    # First disk
     get_hd_info(HT_HARDDISK, name="/1")    # Second disk

   .. note:: Changed in v6.5.3

       * Add new keyword parameter `name`
       * Remove keyword parameter `size`

.. attribute:: HT_HARDDISK, HT_IFMAC, HT_IPV4, HT_DOMAIN

   Constant for `hdtype` when calling :func:`get_hd_info`

.. function:: assert_armored(*args)

   A **decorator** function used to check each module/function/method list in
   the args is obfuscated.

   It could check module, function or method, any other type, `Class` for
   example, doesn't support. If the function is decoratored by builtin
   decorator, for example ``@staticmethod``, it will be taken as no obfuscation.

   Raise :exc:`RuntimeError` if anyone is not obfuscated.

   For example::

     import foo

     from pytransform import assert_armored
     @assert_armored(foo, foo.connect, foo.connect2)
     def start_server():
         foo.connect('root', 'root password')

   .. note:: Since v6.6.2, checking module is supported, but only for super mode

.. function:: check_armored(*args)

   Return True if all the functions/methods/modules in the args are obfuscated.

   Return False if any of them is not obfuscated.

   It could check module, function or method, any other type, `Class` for
   example, doesn't support. If the function is decoratored by any builtin
   decorator, for example, ``@staticmethod``, it will taken as not obfuscated
   and return `False`.

   For example::

     import foo

     from pytransform import check_armored
     if not check_armored(foo, foo.connect, foo.connect2):
         print('My script is hacked')

   .. note:: New in v6.6.2 only for super mode

Examples

Copy those example code to any script, for example foo.py, obfuscate it, then run the obfuscated script.

Show left days of license

from pytransform import get_license_info, get_expired_days
try:
    code = get_license_info()['CODE']
    left_days = get_expired_days()
    if left_days == -1:
        print('This license for %s is never expired' % code)
    else:
        print('This license for %s will be expired in %d days' % (code, left_days))
except Exception as e:
    print(e)

More usage refer to :ref:`Using Plugin to Extend License Type` and :ref:`Using plugin to improve security`

Note

Though pytransform.py is not obfuscated when running the obfuscated script, it's also protected by PyArmor. If it's changed, the obfuscated script will raise protection exception.

Refer to :ref:`special handling of entry script`