コード例 #1
0
ファイル: utils.py プロジェクト: kalufinnle/pyarmor
def make_bootstrap_script(output, capsule=None, relative=None):
    filename = os.path.basename(output)
    co = compile('', filename, 'exec')
    flags = 0x18000000
    prokey = get_product_key(capsule)
    buf = pytransform.encrypt_code_object(prokey, co, flags)
    with open(output, 'w') as f:
        f.write(buf.decode())
    _make_entry(output, relative=relative)
コード例 #2
0
ファイル: utils.py プロジェクト: qwertypoluyt/pyarmor
def encrypt_script(pubkey,
                   filename,
                   destname,
                   wrap_mode=1,
                   obf_code=1,
                   obf_mod=1,
                   protection=0,
                   rpath=None):
    if sys.version_info[0] == 2:
        with open(filename, 'r') as f:
            lines = f.readlines()
    else:
        encoding = _guess_encoding(filename)
        with open(filename, 'r', encoding=encoding) as f:
            lines = f.readlines()

    if protection:
        n = 0
        for line in lines:
            if line.startswith('# No PyArmor Protection Code'):
                break
            elif (line.startswith('# {PyArmor Protection Code}')
                  or line.startswith("if __name__ == '__main__':")
                  or line.startswith('if __name__ == "__main__":')):
                logging.info('Patch this entry script with protection code')
                template = None if protection == 1 else protection
                lines[n:n] = make_protect_pytransform(template, rpath=rpath)
                break
            n += 1

    modname = _frozen_modname(filename, destname)
    co = compile(''.join(lines), modname, 'exec')

    flags = obf_code | obf_mod << 8 | wrap_mode << 16
    s = pytransform.encrypt_code_object(pubkey, co, flags)

    with open(destname, 'w') as f:
        f.write(s.decode())
コード例 #3
0
ファイル: utils.py プロジェクト: kalufinnle/pyarmor
def encrypt_script(pubkey,
                   filename,
                   destname,
                   wrap_mode=1,
                   obf_code=1,
                   obf_mod=1,
                   adv_mode=0,
                   rest_mode=1,
                   protection=0,
                   platforms=None,
                   plugins=None,
                   rpath=None):
    lines = _readlines(filename)
    if plugins:
        n = 0
        k = -1
        plist = []
        pnames = []
        markers = '# PyArmor Plugin: ', '# pyarmor_', '# @pyarmor_'
        for line in lines:
            if line.startswith('# {PyArmor Plugins}'):
                k = n + 1
            else:
                for marker in markers:
                    i = line.find(marker)
                    if i > -1:
                        name = line[i + len(marker):].strip().strip('@')
                        t = name.find('(')
                        name = (name if t == -1 else name[:t]).strip()
                        if _filter_call_marker(plugins, marker, name):
                            plist.append((n + 1, i, marker))
                            pnames.append(name)
            n += 1
        if k > -1:
            logging.info('Patch this script with plugins')
            lines[k:k] = _patch_plugins(plugins, pnames)
        for n, i, m in plist:
            c = '@' if m[2] == '@' else ''
            lines[n] = lines[n][:i] + c + lines[n][i + len(m):]

    if protection:
        n = 0
        for line in lines:
            if line.startswith('# No PyArmor Protection Code') or \
               line.startswith('# {No PyArmor Protection Code}'):
                break
            elif (line.startswith('# {PyArmor Protection Code}')
                  or line.startswith("if __name__ == '__main__':")
                  or line.startswith('if __name__ == "__main__":')):
                logging.info('Patch this entry script with protection code')
                template = os.path.join(PYARMOR_PATH, protect_code_template) \
                    if isinstance(protection, int) else protection
                logging.info('Use template: %s', template)
                targets = _build_platforms(platforms) if platforms else None
                lines[n:n] = [
                    _make_protect_pytransform(template=template,
                                              filenames=targets,
                                              rpath=rpath)
                ]
                break
            n += 1

    if sys.flags.debug and (protection or plugins):
        patched_script = filename + '.pyarmor-patched'
        logging.info('Write patched script for debugging: %s', patched_script)
        with open(patched_script, 'w') as f:
            f.write(''.join(lines))

    modname = _frozen_modname(filename, destname)
    co = compile(''.join(lines), modname, 'exec')

    flags = obf_code | obf_mod << 8 | wrap_mode << 16 | adv_mode << 24 \
        | (11 if rest_mode == 4 else 15 if rest_mode == 3 else
           7 if rest_mode == 2 else rest_mode) << 28
    s = pytransform.encrypt_code_object(pubkey, co, flags)

    with open(destname, 'w') as f:
        f.write(s.decode())
コード例 #4
0
def encrypt_script(pubkey,
                   filename,
                   destname,
                   wrap_mode=1,
                   obf_code=1,
                   obf_mod=1,
                   protection=0,
                   plugins=None,
                   rpath=None):
    if sys.version_info[0] == 2:
        with open(filename, 'r') as f:
            lines = f.readlines()
    else:
        encoding = _guess_encoding(filename)
        with open(filename, 'r', encoding=encoding) as f:
            lines = f.readlines()
        if encoding == 'utf-8' and lines:
            i = lines[0].find('#')
            if i > 0:
                lines[0] = lines[0][i:]

    if plugins:
        n = 0
        k = -1
        plist = []
        marker = '# PyArmor Plugin:'
        for line in lines:
            if line.startswith('# {PyArmor Plugins}'):
                k = n + 1
            elif (line.startswith("if __name__ == '__main__':")
                  or line.startswith('if __name__ == "__main__":')):
                if k == -1:
                    k = n
            else:
                i = line.find(marker)
                if i > -1:
                    plist.append((n + 1, i))
            n += 1
        if k > -1:
            logging.info('Patch this entry script with plugins')
            lines[k:k] = patch_plugins(plugins)
        for n, i in plist:
            lines[n] = lines[n][:i] + lines[n][i + len(marker):].lstrip()

    if protection:
        n = 0
        for line in lines:
            if line.startswith('# No PyArmor Protection Code') or \
               line.startswith('# {No PyArmor Protection Code}'):
                break
            elif (line.startswith('# {PyArmor Protection Code}')
                  or line.startswith("if __name__ == '__main__':")
                  or line.startswith('if __name__ == "__main__":')):
                logging.info('Patch this entry script with protection code')
                template, target = None, None
                if not isinstance(protection, int):
                    if protection.find(',') == -1:
                        target = _get_platform_library(protection)
                    else:
                        template, platname = protection.split(',')
                        target = _get_platform_library(platname)
                if template:
                    logging.info('Use template: %s', template)
                if target:
                    logging.info('Target dynamic library: %s', target)
                lines[n:n] = [
                    make_protect_pytransform(template=template,
                                             filename=target,
                                             rpath=rpath)
                ]
                break
            n += 1

    if sys.flags.debug and (protection or plugins):
        with open(filename + '.pyarmor-patched', 'w') as f:
            f.write(''.join(lines))

    modname = _frozen_modname(filename, destname)
    co = compile(''.join(lines), modname, 'exec')

    flags = obf_code | obf_mod << 8 | wrap_mode << 16
    s = pytransform.encrypt_code_object(pubkey, co, flags)

    with open(destname, 'w') as f:
        f.write(s.decode())
コード例 #5
0
ファイル: utils.py プロジェクト: 75509151/pyarmor
def encrypt_script(pubkey,
                   filename,
                   destname,
                   wrap_mode=1,
                   obf_code=1,
                   obf_mod=1,
                   adv_mode=0,
                   rest_mode=1,
                   protection=0,
                   plugins=None,
                   rpath=None):
    lines = _readlines(filename)
    if plugins:
        n = 0
        k = -1
        plist = []
        pnames = []
        markers = '# PyArmor Plugin:', '# pyarmor_', '# @pyarmor_'
        for line in lines:
            if line.startswith('# {PyArmor Plugins}'):
                k = n + 1
            else:
                for marker in markers:
                    i = line.find(marker)
                    if i > -1:
                        plist.append((n + 1, i, marker))
                        name = line[i + len(marker):].strip().strip('@')
                        t = name.find('(')
                        pnames.append((name if t == -1 else name[:t]).strip())
            n += 1
        if k > -1:
            logging.info('Patch this script with plugins')
            lines[k:k] = _patch_plugins(plugins, pnames)
        for n, i, m in plist:
            c = '@' if m[2] == '@' else ''
            lines[n] = lines[n][:i] + c + lines[n][i + len(m):].lstrip()

    if protection:
        n = 0
        for line in lines:
            if line.startswith('# No PyArmor Protection Code') or \
               line.startswith('# {No PyArmor Protection Code}'):
                break
            elif (line.startswith('# {PyArmor Protection Code}')
                  or line.startswith("if __name__ == '__main__':")
                  or line.startswith('if __name__ == "__main__":')):
                logging.info('Patch this entry script with protection code')
                template, target = None, None
                if not isinstance(protection, int):
                    if protection.find(',') == -1:
                        target = _get_platform_library(protection)
                    else:
                        template, platname = protection.split(',')
                        target = _get_platform_library(platname)
                if template:
                    logging.info('Use template: %s', template)
                if target:
                    logging.info('Target dynamic library: %s', target)
                lines[n:n] = [
                    make_protect_pytransform(template=template,
                                             filename=target,
                                             rpath=rpath)
                ]
                break
            n += 1

    if sys.flags.debug and (protection or plugins):
        with open(filename + '.pyarmor-patched', 'w') as f:
            f.write(''.join(lines))

    modname = _frozen_modname(filename, destname)
    co = compile(''.join(lines), modname, 'exec')

    flags = obf_code | obf_mod << 8 | wrap_mode << 16 | adv_mode << 24 \
        | (11 if rest_mode == 4 else 15 if rest_mode == 3 else
           7 if rest_mode == 2 else rest_mode) << 28
    s = pytransform.encrypt_code_object(pubkey, co, flags)

    with open(destname, 'w') as f:
        f.write(s.decode())
コード例 #6
0
def encrypt_script(pubkey,
                   filename,
                   destname,
                   wrap_mode=1,
                   obf_code=1,
                   obf_mod=1,
                   adv_mode=0,
                   rest_mode=1,
                   protection=0,
                   platforms=None,
                   plugins=None,
                   rpath=None,
                   suffix=''):
    lines = _readlines(filename)
    if plugins:
        n = 0
        k = -1
        plist = []
        stub_marker = '# {PyArmor Plugins}'
        inline_marker = '# PyArmor Plugin: '
        call_markers = '# pyarmor_', '# @pyarmor_'
        for line in lines:
            if line.startswith(stub_marker):
                k = n + 1
            else:
                i = line.find(inline_marker)
                if i > -1:
                    plist.append((n if k == -1 else n + 1, i, inline_marker))
                else:
                    for marker in call_markers:
                        i = line.find(marker)
                        if i == -1:
                            continue
                        name = line[i + len(marker):line.find('(')].strip()
                        if _filter_call_marker(plugins, name):
                            plist.append((n if k == -1 else n + 1, i, marker))
            n += 1
        if k > -1:
            logging.info('Patch this script with plugins')
            lines[k:k] = _patch_plugins(plugins)
        for n, i, m in plist:
            c = '@' if m[2] == '@' else ''
            lines[n] = lines[n][:i] + c + lines[n][i + len(m):]

    if protection:
        n = 0
        for line in lines:
            if line.startswith('# No PyArmor Protection Code') or \
               line.startswith('# {No PyArmor Protection Code}'):
                break
            elif (line.startswith('# {PyArmor Protection Code}')
                  or line.startswith("if __name__ == '__main__':")
                  or line.startswith('if __name__ == "__main__":')):
                logging.info('Patch this entry script with protection code')
                if os.path.exists(protection):
                    logging.info('Use template: %s', protection)
                    with open(protection) as f:
                        lines[n:n] = [f.read()]
                else:
                    lines[n:n] = [protection]
                break
            n += 1

    if sys.flags.debug and (protection or plugins):
        patched_script = filename + '.pyarmor-patched'
        logging.info('Write patched script for debugging: %s', patched_script)
        with open(patched_script, 'w') as f:
            f.write(''.join(lines))

    modname = _frozen_modname(filename, destname)
    co = compile(''.join(lines), modname, 'exec')

    flags = obf_code | obf_mod << 8 | wrap_mode << 16 | adv_mode << 24 \
        | (11 if rest_mode == 4 else 15 if rest_mode == 3 else
           7 if rest_mode == 2 else rest_mode) << 28
    s = pytransform.encrypt_code_object(pubkey, co, flags, suffix=suffix)

    with open(destname, 'w') as f:
        f.write(s.decode())