Created
April 2, 2016 08:38
-
-
Save adam900710/c96fb7e8f374c7a5bdab498b0dda3c1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from errno import EINVAL | |
from sys import argv | |
from sys import exit | |
from sys import stdout | |
from sys import stderr | |
import getopt | |
import fcntl, socket, struct | |
license_magic=b"\xb2\x26\xbc\x27\x4e\x22\x0f\x53" | |
def get_mac(ifname): | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', | |
bytes(ifname[:15], 'utf-8'))) | |
return info[18:24] | |
def license_early_check(path) : | |
fd = open(path, "rb") | |
fd.seek(0) | |
buf = fd.read(8) | |
fd.close() | |
if buf != license_magic : | |
stderr.write("wrong license magic, is it a valid license template?\n") | |
exit(1) | |
return 0 | |
def usage(argv) : | |
stdout.write("usage: %s -t <template_file> -i <interface> [-d YYYY/MM/DD]" | |
% argv[0]) | |
exit(1) | |
def generate_license_code(mac): | |
mac_res = [] | |
for i in range(0, 16): | |
if i < 6: | |
mac_res.append(mac[i] + i) | |
else: | |
mac_res.append(mac[i % 6] + i) | |
serial = bytearray(16) | |
for i in range(0, 8): | |
serial[i] = (mac_res[i] + mac_res[i + 8]) % 256 | |
license = bytearray(16) | |
rd = [162,15,239,202,57,14,45,164,147,232,120,90,117,15,239,232] | |
for i in range(0, 16): | |
license[i] = (serial[i] + rd[i]) % 256 | |
return license | |
def output_licese_file(license_code, template_path): | |
fd = open(template_path, 'rb') | |
fd.seek(0) | |
stdout.buffer.write(fd.read(0x40)) | |
stdout.buffer.write(bytes(license_code)) | |
fd.seek(0x50) | |
stdout.buffer.write(fd.read(0x50)) | |
if __name__ == "__main__" : | |
template_path = "" | |
interface_name = "" | |
try: | |
opts, args = getopt.getopt(argv[1:], "t:i:d:") | |
except getopt.GetoptError as err: | |
stdout.write("%s\n" % str(err)) | |
usage(argv) | |
exit(1) | |
for o, a in opts: | |
if o == "-t": | |
template_path = a | |
if o == "-i": | |
interface_name = a | |
if o == "-d": | |
stdout.write("specifying date is not yet supported\n") | |
exit(1) | |
if not template_path or not interface_name: | |
usage(argv) | |
exit(1) | |
license_early_check(template_path) | |
mac = bytes(get_mac(interface_name)) | |
license_code = generate_license_code(mac) | |
output_licese_file(license_code, template_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment