1
0
mirror of https://github.com/ThunderEX/py-kms.git synced 2025-04-18 07:44:00 +03:00

fix uncorrect epid due to utf-16 support

This commit is contained in:
ThunderEX 2017-08-30 16:40:16 +08:00
parent f04e6f60df
commit 862b066ed4
No known key found for this signature in database
GPG Key ID: EE385D56D965A5C6
3 changed files with 98 additions and 5 deletions

View File

@ -7,6 +7,10 @@ import string
import sys
import uuid
import errno
try:
import codecs
except ImportError:
import upy.codecs as codecs
import filetimes, rpcBind, rpcRequest
from dcerpc import MSRPCHeader, MSRPCBindNak, MSRPCRespHeader
@ -180,8 +184,8 @@ def createKmsRequestBase():
requestDict['previousClientMachineId'] = b'\0' * 16 #requestDict['clientMachineId'] # I'm pretty sure this is supposed to be a null UUID.
requestDict['requiredClientCount'] = config['RequiredClientCount']
requestDict['requestTime'] = filetimes.timestamp2filetime(time.time())
requestDict['machineName'] = (config['machineName'] if (config['machineName'] is not None) else ''.join(random.choice(string.ascii_letters + string.digits) for i in range(random.randint(2,63)))).encode('utf-16le')
requestDict['mnPad'] = '\0'.encode('utf-16le') * (63 - len(requestDict['machineName'].decode('utf-16le')))
requestDict['machineName'] = codecs.encode(config['machineName'] or ''.join(random.choice(string.ascii_letters + string.digits) for i in range(random.randint(2,63))), 'utf_16_le')
requestDict['mnPad'] = codecs.encode('\0', 'utf_16_le') * (63 - codecs.encode(len(requestDict['machineName']), 'utf_16_le'))
# Debug Stuff
if config['debug']:

View File

@ -9,6 +9,10 @@ try:
import uuid
except ImportError:
import upy.uuid as uuid
try:
import codecs
except ImportError:
import upy.codecs as codecs
from structure import Structure
@ -240,9 +244,9 @@ class kmsBase:
response['versionMajor'] = kmsRequest['versionMajor']
if not self.config["epid"]:
response["kmsEpid"] = kmsPidGenerator.epidGenerator(kmsRequest['applicationId'].get(), kmsRequest['versionMajor'], self.config["lcid"]).encode('utf-16le')
response["kmsEpid"] = codecs.encode(kmsPidGenerator.epidGenerator(kmsRequest['applicationId'].get(), kmsRequest['versionMajor'], self.config["lcid"]), 'utf_16_le')
else:
response["kmsEpid"] = self.config["epid"].encode('utf-16le')
response["kmsEpid"] = codecs.encode(self.config["epid"], 'utf_16_le')
response['clientMachineId'] = kmsRequest['clientMachineId']
response['responseTime'] = kmsRequest['requestTime']
if self.config["CurrentClientCount"]:
@ -262,7 +266,7 @@ class kmsBase:
data = cur.fetchone()
#print "Data:", data
if data[6]:
response["kmsEpid"] = data[6].encode('utf-16le')
response["kmsEpid"] = codecs.encode(data[6], 'utf_16_le')
else:
cur.execute("UPDATE clients SET kmsEpid=? WHERE clientMachineId=?;", (str(response["kmsEpid"].decode('utf-16le')), str(kmsRequest['clientMachineId'].get())))

85
upy/codecs.py Normal file
View File

@ -0,0 +1,85 @@
__all__ = ['encode']
# from source of pypy:
# pypy / pypy / module / _codecs / interp_codecs.py
# pypy / rpython / rlib / runicode.py
import sys
BYTEORDER = sys.byteorder
BYTEORDER2 = BYTEORDER[0] + 'e' # either "le" or "be"
assert BYTEORDER2 in ('le', 'be')
def _storechar(result, CH, byteorder):
hi = ((CH) >> 8) & 0xff
lo = (CH) & 0xff
if byteorder == 'little':
result.append(lo)
result.append(hi)
else:
result.append(hi)
result.append(lo)
def encode_utf_16(s, errors,
errorhandler=None,
allow_surrogates=True,
byteorder='little',
public_encoding_name='utf16'):
if errorhandler is None:
pass
# errorhandler = default_unicode_error_encode
result = bytearray()
if byteorder == 'native':
_storechar(result, 0xFEFF, BYTEORDER)
byteorder = BYTEORDER
for pos, c in enumerate(s):
ch = ord(c)
if ch < 0xD800:
_storechar(result, ch, byteorder)
elif ch >= 0x10000:
_storechar(result, 0xD800 | ((ch-0x10000) >> 10), byteorder)
_storechar(result, 0xDC00 | ((ch-0x10000) & 0x3FF), byteorder)
elif ch >= 0xE000 or allow_surrogates:
_storechar(result, ch, byteorder)
else:
ru, rs, pos = errorhandler(errors, public_encoding_name,
'surrogates not allowed',
s, pos-1, pos)
if rs is not None:
# py3k only
if len(rs) % 2 != 0:
errorhandler('strict', public_encoding_name,
'surrogates not allowed',
s, pos-1, pos)
result.append(rs)
continue
for ch in ru:
if ord(ch) < 0xD800:
_storechar(result, ord(ch), byteorder)
else:
errorhandler('strict', public_encoding_name,
'surrogates not allowed',
s, pos-1, pos)
continue
return bytes(result)
def encode(obj, encoding='utf_8', errors='strict'):
if encoding == 'utf_8':
return obj.encode(encoding, errors)
elif encoding == 'utf_16':
return encode_utf_16(obj, [], None,
True, 'native',
'utf-16-' + BYTEORDER2)
elif encoding == 'utf_16_be':
return encode_utf_16(obj, [], None,
True, 'big',
'utf-16-be')
elif encoding == 'utf_16_le':
return encode_utf_16(obj, [], None,
True, 'little',
'utf-16-le')
else:
raise NotImplementedError('Encoding of {} not implemented'.format(encoding))