forked from Ygnecz/MTLic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseLic.py
107 lines (79 loc) · 2.24 KB
/
ParseLic.py
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
101
102
103
104
105
106
107
#!/usr/bin/python3
import sys
import struct
import binascii
import array
from ecc import AffineCurvePoint, getcurvebyname, FieldElement
from MTTools import *
if len(sys.argv) != 2:
print(sys.argv[0]+" <license file>")
exit()
with open(sys.argv[1], "r") as licFile:
lic=licFile.read().split('\n')
if lic[0] != '-----BEGIN MIKROTIK SOFTWARE KEY------------' or \
lic[3] != '-----END MIKROTIK SOFTWARE KEY--------------':
print('Not a Mikrotik license file')
exit()
lic = lic[1]+lic[2]
# MTBase64 decode license
lic = MTBse64Decode(lic)
print("-- MTBase64 decoded")
printBytes(lic)
print()
# MT_Transform license value
licVal = MT_Transform(lic[:16])
print("-- Transformed license")
printBytes(licVal)
print()
# Software ID
SWID = int.from_bytes(licVal[:6], 'little')
print("-- Software ID")
print(hex(SWID))
print(MT_SWSNToSWID(SWID))
print()
# License level
print("-- License level")
print(licVal[7])
print()
# Signature verification
hash = MT_Hash(licVal)
print("-- License hash")
printBytes(hash)
print()
for i in range(16):
hash[8+i] = hash[8+i] ^ lic[16+i]
hash[31] = (hash[31] & 0x7F) | 0x40;
hash[ 0] = hash[ 0] & 0xF8;
print("-- Modified license hash")
printBytes(hash)
print()
sig = lic[32:64]
print("-- License signature (from License)")
printBytes(sig)
print()
curve = getcurvebyname("curve25519")
pub="8E1067E4305FCDC0CFBF95C10F96E5DFE8C49AEF486BD1A4E2E96C27F01E3E32"
pub=binascii.b2a_hex(binascii.a2b_hex(pub)[::-1])
pub=int(pub, 16)
hash = int.from_bytes(hash, 'little')
sig = int.from_bytes(sig, 'little')
# Py of public key to Px
pub=AffineCurvePoint(pub, int(FieldElement(pub**3+int(curve.a)*pub**2+pub, curve.p).sqrt()[0]), curve)
Y=int((pub*sig + curve.G*hash).x)
Y=Y.to_bytes(32, byteorder='little')
print("-- Elliptic curve computation result")
print(" Y = signature * PubKey + hash * G")
printBytes(Y)
print()
Yhash = MT_Hash(Y)
print("-- MT_Hash of elliptic curve result")
printBytes(Yhash)
print()
print("-- Compare computation result with License")
printBytes(Yhash[:16])
printBytes(lic[16:32])
print("-- Compare result")
if Yhash[:16] == lic[16:32]:
print('OK - License valid')
else:
print('Failed')