1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| import re import sys import datetime import subprocess from Crypto.Cipher import AES from binascii import a2b_hex from binascii import b2a_hex
class LicenseEncode: def __init__(self, mac, license_path, expired_date=None): self.mac = mac self.license_path = license_path self.date = (datetime.datetime.now() + datetime.timedelta(days=30)).strftime( '%Y%m%d') if expired_date is None else expired_date
def encrypt(self, content): while len(content) % 16: content += ' ' content = content.encode('utf-8') aes = AES.new(b'2021052020210520', AES.MODE_CBC, b'2021052020210520') encrypted_content = aes.encrypt(content) return (b2a_hex(encrypted_content))
def gen_license_file(self): with open(self.license_path, 'w') as LF: LF.write('MAC : %s\n' % (self.mac))
LF.write('Date : %s\n' % (self.date))
sign = self.encrypt('%s#%s' % (self.mac, self.date)) print('Sign : ' + str(sign.decode('utf-8')) + '\n') LF.write('Sign : ' + str(sign.decode('utf-8')) + '\n')
class LicenseDecode: def __init__(self, license_path): self.license_path = license_path
def license_check(self): license_dic = self.parse_license_file() sign = self.decrypt(license_dic['Sign']) sign_list = sign.split('#') mac = sign_list[0].strip() date = sign_list[1].strip() if (mac != license_dic['MAC']) or (date != license_dic['Date']): print('*Error*: License file is modified!') sys.exit(1) if len(sign_list) == 2: macs = self.get_mac() current_date = datetime.datetime.now().strftime('%Y%m%d') if sign_list[0] not in macs: print('*Error*: Invalid host!') sys.exit(1)
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(self): license_dic = {}
with open(self.license_path, '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(self, content): aes = AES.new(b'2021052020210520', AES.MODE_CBC, b'2021052020210520') decrypted_content = aes.decrypt(a2b_hex(content.encode('utf-8'))) return (decrypted_content.decode('utf-8'))
def get_mac(self): SP = subprocess.Popen('/sbin/ifconfig', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = SP.communicate() macs = [] 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) macs.append(mac) return macs
if __name__ == '__main__': mac = '00:28:f8:fa:25:bc'
LicenseEncode(mac, './License.dat').gen_license_file()
|