-3

我正在尝试将Python脚本的内容编码为Linux. 我刚开始使用一个名为 test.py 的简单脚本 -

# !/app/logs/Python/bin/python3
# -*- coding: ascii -*-
print ("hi")

获得脚本后,我执行vim -x test.py两次并输入加密密钥。然后正常保存文件,然后使用执行脚本python test.py

我尝试了此处链接中提供的几乎所有示例,但最终还是出现以下错误-

SyntaxError: Non-ASCII character '\x97' in file test.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

我检查了使用的默认编码print sys.getdefaultencoding(),它是 acsii。

我在这里想念什么。请澄清。我知道这个问题是重复的,但没有一个解决方案有帮助。

4

1 回答 1

2

Python 知道如何执行明文 Python 源代码。如果您对源文件进行加密,则它不再包含有效的 Python 源并且无法直接执行。

这里有两种可能的方法。首先是只混淆你的来源。您应该知道,混淆不是安全的,用户可以通过一些工作来恢复一些 Python 源(不一定是原始源,因为注释和文档字符串可能已被剥离,变量名可能已被更改)。您可以阅读如何保护 Python 代码?和 google for python obfuscate找到一些可能的混淆 Python 源代码的方法及其权衡。

混淆源的好消息是任何人都可以使用它而无需任何密码。

第二种方法是加密源。如果您使用一个不错的工具,您可以假设在不知道密钥的情况下无法读取或执行文件。从这个意义上说,vim加密货币的声誉并不是最高的。以最简单的方式(例如使用您的示例vim -x),您必须解密文件才能执行它。不幸的是,好的加密模块没有在标准的 Python 安装中提供,必须从 pypi 下载。众所周知的加密模块包括pycryptocryptography

然后,您可以加密大部分代码,然后在运行时请求密钥、解密并执行它。仍然是一项严肃的工作,但可行。

或者,您可以用另一种语言 (C/C++) 构建一个解密器,用于解密文件的其余部分并将其输入 python 解释器,但从功能上讲,这只是上述方法的一种变体。


根据您的评论,我假设您想在运行时加密源代码并解密(使用密码)。原则是:

  • 构建一个 Python 脚本,该脚本将采用另一个任意 Python 脚本,使用安全加密模块对其进行编码,并在其前面添加一些解密代码。
  • 在运行时,前置代码将要求输入密码,解密加密代码执行它

构建器可以是(此代码使用加密模块):

import cryptography.fernet
import cryptography.hazmat.primitives.kdf.pbkdf2
import cryptography.hazmat.primitives.hashes
import cryptography.hazmat.backends
import base64
import os
import sys

# the encryption function
def encrypt_source(infile, outfile, passwd):
    with open(infile, 'rb') as fdin:     # read original file
        plain_data = fdin.read()
    salt, key = gen_key(passwd)          # derive a key from the password
    f = cryptography.fernet.Fernet(key)
    crypted_data = f.encrypt(plain_data) # encrypt the original code
    with open(outfile, "w") as fdout:    # prepend a decoding block
        fdout.write("""#! /usr/bin/env python

import cryptography.fernet
import cryptography.hazmat.primitives.kdf.pbkdf2
import cryptography.hazmat.primitives.hashes
import cryptography.hazmat.backends
import base64
import os

def gen_key(passwd, salt):             # key derivation
    kdf = cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC(
        algorithm = cryptography.hazmat.primitives.hashes.SHA256(),
        length = 32,
        salt = salt,
        iterations = 100000,
        backend = cryptography.hazmat.backends.default_backend()
    )
    return base64.urlsafe_b64encode(kdf.derive(passwd))

passwd = input("Password:")            # ask for the password
salt = base64.decodebytes({})
key = gen_key(passwd.encode(), salt)   # derive the key from the password and the original salt

crypted_source = base64.decodebytes(   # decode (base64) the crypted source
b'''{}'''
)
f = cryptography.fernet.Fernet(key)
plain_source = f.decrypt(crypted_source) # decrypt it
exec(plain_source)                     # and exec it
""".format(base64.encodebytes(salt),
           base64.encodebytes(crypted_data).decode()))

# derive a key from a password and a random salt
def gen_key(passwd, salt=None):        
    if salt is None: salt = os.urandom(16)
    kdf = cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC(
        algorithm = cryptography.hazmat.primitives.hashes.SHA256(),
        length = 32,
        salt = salt,
        iterations = 100000,
        backend = cryptography.hazmat.backends.default_backend()
    )
    return salt, base64.urlsafe_b64encode(kdf.derive(passwd))

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("Usage {} infile outfile".format(sys.argv[0]))
        sys.exit(1)
    passwd = input("Password:").encode()             # ask for a password
    encrypt_source(sys.argv[1], sys.argv[2], passwd) # and generates an encrypted Python script
于 2018-07-12T13:32:28.913 回答