1、生成license授权文件
#!/usr/bin/env python3
from Crypto.Cipher import AES
from binascii import b2a_hex
def encrypt(content):
# content length must be a multiple of 16.
while len(content) % 16:
content += ' '
content = content.encode('utf-8')
# Encrypt content.
aes = AES.new(b'2024062120240621', AES.MODE_CBC, b'2024062120240621')
encrypted_content = aes.encrypt(content)
return(b2a_hex(encrypted_content))
def gen_license_file():
license_file = './license.dat'
with open(license_file, 'w') as LF:
LF.write('MAC : 03:04:ac:11:00:02\n')
LF.write('Date : 20240621\n')
sign = encrypt('03:04:ac:11:00:02#20240621')
LF.write('Sign : ' + str(sign.decode('utf-8')) + '\n')
if __name__ == '__main__':
gen_license_file()
2、鉴权
#!/usr/bin/env python3
import os
import re
import sys
import datetime
import subprocess
from Crypto.Cipher import AES
from binascii import a2b_hex
## License check
def license_check():
license_dic = parse_license_file()
sign = decrypt(license_dic['Sign'])
sign_list = sign.split('#')
mac = sign_list[0].strip()
date = sign_list[1].strip()
# Check license file is modified or not.
if (mac != license_dic['MAC']) or (date != license_dic['Date']):
print('*Error*: License file is modified!')
sys.exit(1)
# Check MAC and effective date invalid or not.
if len(sign_list) == 2:
mac = get_mac()
current_date = datetime.datetime.now().strftime('%Y%m%d')
# Must run this script under specified MAC.
if sign_list[0] != mac:
print('*Error*: Invalid host!')
sys.exit(1)
# Current time must be before effective date.
if sign_list[1] < current_date:
print('*Error*: License is expired!')
sys.exit(1)
else:
print('*Error*: Wrong Sign setting on license file.')
sys.exit(1)
def parse_license_file():
license_dic = {}
license_file = './license.dat'
with open(license_file, 'r') as LF:
for line in LF.readlines():
if re.match('^\s*(\S+)\s*:\s*(\S+)\s*$', line):
my_match = re.match('^\s*(\S+)\s*:\s*(\S+)\s*$', line)
license_dic[my_match.group(1)] = my_match.group(2)
return(license_dic)
def decrypt(content):
aes = AES.new(b'2024062120240621', AES.MODE_CBC, b'2024062120240621')
decrypted_content = aes.decrypt(a2b_hex(content.encode('utf-8')))
return(decrypted_content.decode('utf-8'))
def get_mac():
mac = ''
SP = subprocess.Popen('/sbin/ifconfig', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = SP.communicate()
for line in str(stdout, 'utf-8').split('\n'):
if re.match('^\s*ether\s+(\S+)\s+.*$', line):
my_match = re.match('^\s*ether\s+(\S+)\s+.*$', line)
mac = my_match.group(1)
break
return(mac)
# Main function.
def test():
print('666666666666666!')
# 鉴权
license_check()
# 鉴权不通过,这句就不会输出
test()
3、创建setup.py文件,创建server.py文件,将核心代码和鉴权写入其中,然后使用cpython加密文件
import os
from distutils.core import setup
from Cython.Build import cythonize
py_files = ['server.py',]
setup(ext_modules = cythonize(py_files),)
4、执行如下命令,生成的文件,把server.xxxx.so文件改成server.so
# python3 setup.py build_ext --inplace
5、创建个空壳run.py引入server.so,因为server.py中用了flask,所以下面可以直接启动服务器,至此就授权加密就完成了
#!/usr/bin/env python3
import server
# 启动服务器(默认端口8000,可以通过环境变量FLASK_RUN_PORT修改)
if __name__ == '__main__':
server.app.run(host='0.0.0.0', debug=True, port=8000) # 监听所有接口