-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmprotect.py
202 lines (175 loc) · 8.02 KB
/
vmprotect.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# MIT License
#
# (c) 2020 pilao
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import base64
import hashlib
import secrets
import sys
from datetime import date
from io import BytesIO
import lxml.etree
class Generator:
def __init__(self, data: str):
root = lxml.etree.fromstring(base64.b64decode(data).decode('utf-8'))
if root is None or root.tag != 'vmp-lm-product':
raise ValueError('not a product information string')
product = root.get('product')
if product is None:
raise ValueError('information about product is missed')
self.__product_code = base64.b64decode(product)
if len(self.__product_code) != 8:
raise ValueError('product code has incorrect length')
algorithm = root.get('algorithm')
if algorithm is None:
raise ValueError('missed encryption algorithm')
if algorithm != 'RSA':
raise ValueError('unsupported encryption algorithm')
bits = root.get('bits')
if bits is None:
raise ValueError('missed bits for RSA algorithm')
self.__bits = int(bits)
exp = root.get('exp')
if exp is None:
raise ValueError('missed exp for RSA algorithm')
self.__exp = int.from_bytes(base64.b64decode(exp), 'big')
mod = root.get('mod')
if mod is None:
raise ValueError('missed mod for RSA algorithm')
self.__mod = int.from_bytes(base64.b64decode(mod), 'big')
@staticmethod
def __make_date(d: date) -> bytes:
return int((d.year << 16) | (d.month << 8) | d.day).to_bytes(4, sys.byteorder)
def __build_serial_number(self,
username: str = None,
email: str = None,
hardware_id: str = None,
exp_date: date = None,
running_time_limit: int = None,
user_data: bytes = None,
max_build_date: date = None) -> bytes:
with BytesIO() as s:
# 1 byte of data - version
s.write(b'\x01')
s.write(b'\x01')
if username is not None:
# 1 + N bytes - length + N bytes of customer's name (without ending \0).
s.write(b'\x02')
buffer = username.encode('utf-8')
count = len(buffer)
if count > 255:
raise ValueError(f'username is too long: {count} bytes in UTF-8, maximum is 255')
s.write(bytes([count]))
s.write(buffer)
if email is not None:
# 1 + N bytes - length + N bytes of customer's name (without ending \0).
s.write(b'\x03')
buffer = email.encode('utf-8')
count = len(buffer)
if count > 255:
raise ValueError(f'email is too long: {count} bytes in UTF-8, maximum is 255')
s.write(bytes([count]))
s.write(buffer)
if hardware_id is not None:
# 1 + N bytes - length + N bytes of hardware id (N % 4 == 0)
s.write(b'\x04')
buffer = base64.b64decode(hardware_id)
count = len(buffer)
if count == 0:
raise ValueError('hardware_id has zero length, use "None" instead')
if count > 255:
raise ValueError('hardware_id is too long')
if count % 4 != 0:
raise ValueError('hardware_id has invalid length')
s.write(bytes([count]))
s.write(buffer)
if exp_date is not None:
# 4 bytes - (year << 16) | (month << 8) | (day)
s.write(b'\x05')
s.write(self.__make_date(exp_date))
if running_time_limit is not None:
# 1 byte - number of minutes
s.write(b'\x06')
s.write(bytes([running_time_limit]))
# 8 bytes - used for decrypting some parts of exe-file
s.write(b'\x07')
s.write(self.__product_code)
if user_data is not None:
# 1 + N bytes - length + N bytes of user data
s.write(b'\x08')
count = len(user_data)
if count > 255:
raise ValueError('user_data cannot exceed 255 bytes')
if count == 0:
raise ValueError('user_data has zero length, use "None" instead')
s.write(bytes([count]))
s.write(user_data)
if max_build_date is not None:
# 4 bytes - (year << 16) | (month << 8) | (day)
s.write(b'\x09')
s.write(self.__make_date(max_build_date))
checksum = bytearray(hashlib.sha1(s.getbuffer()).digest()[:4])
checksum.reverse()
# 4 bytes - checksum: the first four bytes of sha-1 hash from the data before that chunk
s.write(b'\xff')
s.write(checksum)
return s.getvalue()
def __add_padding(self, bs: bytes) -> bytes:
min_padding = 8 + 3
max_padding = min_padding + 16
max_bytes = int(self.__bits / 8)
if len(bs) + min_padding > max_bytes:
raise ValueError('Serial number is too long for this algorithm')
max_padding_according_to_max_bytes = max_bytes - len(bs)
if max_padding_according_to_max_bytes < max_padding:
max_padding = max_padding_according_to_max_bytes
padding_bytes = min_padding
if max_padding > min_padding:
padding_bytes += secrets.randbelow(max_padding - min_padding)
with BytesIO() as s:
s.write(b'\x00')
s.write(b'\x02')
for _ in range(padding_bytes - 1):
s.write(bytes([secrets.choice(range(1, 0xff))]))
s.write(b'\x00')
s.write(bs)
rest = max_bytes - len(s.getbuffer())
s.write(secrets.token_bytes(rest))
return s.getvalue()
def __encrypt(self, b: bytes) -> bytes:
return pow(int.from_bytes(b, 'big'), self.__exp, self.__mod).to_bytes(len(b), 'big')
def generate(self,
username: str = None,
email: str = None,
hardware_id: str = None,
exp_date: date = None,
running_time_limit: int = None,
user_data: bytes = None,
max_build_date: date = None) -> str:
return base64.encodebytes(
self.__encrypt(
self.__add_padding(
self.__build_serial_number(username,
email,
hardware_id,
exp_date,
running_time_limit,
user_data,
max_build_date)))).decode('utf-8')