Skip to content

Instantly share code, notes, and snippets.

@Demonslay335
Last active May 28, 2022 23:52
Show Gist options
  • Save Demonslay335/aa435774bbb93b505a243b388b7278fa to your computer and use it in GitHub Desktop.
Save Demonslay335/aa435774bbb93b505a243b388b7278fa to your computer and use it in GitHub Desktop.
Generate BlackMatter checksum
import sys, struct, base64, argparse
def gen_id_from_guid(guid: str) -> str:
checksum = checksum_string(guid + '\0')
b64 = base64.b64encode(checksum.to_bytes(8, 'little'))[0:9]
return b64.decode('utf-8').replace('+', 'x').replace('/', 'i').replace('=', 'z')
def gen_checksum_from_bytes(blob: str) -> str:
blob = bytearray.fromhex(blob)
return checksum(blob, len(blob))
def swap32(i):
return struct.unpack("<I", struct.pack(">I", i))[0]
max_bits = 32
ROR4 = lambda val, r_bits: \
((val & (2**max_bits-1)) >> r_bits%max_bits) | \
(val << (max_bits-(r_bits%max_bits)) & (2**max_bits-1))
def checksum_round(buffer, length, initial):
result_upper, result_lower = initial >> 16, initial & 0xFFFF
while length > 0:
v8 = 0xFA1
if (length < 0xFA1):
v8 = length
length -= v8
i = 0
while v8 > 0:
result_lower += buffer[i]
result_upper += result_lower
v8 -= 1
i += 1
result_upper = result_upper % 0x1000F
result_lower = result_lower % 0x1000F
return (result_upper << 16) + result_lower
def checksum(buffer, length):
v3 = checksum_round(buffer, length, 0xD6917A)
v4 = checksum_round(buffer, length, swap32(v3))
return swap32(checksum_round(buffer, length, swap32(v4)))
def checksum_string_round(string: str, length: int, initial: int) -> int:
i = 0
while i < length:
c = ord(string[i])
if c >= 0x41 and c <= 0x5A:
c |= 0x20
initial = (c + (ROR4(initial, 13)) & 0xFFFFFFFF)
i += 1
return initial
def checksum_string(string: str) -> int:
v3 = checksum_string_round(string, len(string), 0xFFFFFFFF)
v4 = checksum_string_round(string, len(string), v3)
v11_0 = checksum_string_round(string, len(string), v4)
v11_1 = swap32(v11_0)
return (v11_1 << 32) | v11_0
if __name__ == '__main__':
header = 'Generate BlackMatter ID or checksum'
parser = argparse.ArgumentParser(description=header)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-g', '--guid', help='Machine GUID')
group.add_argument('-b', '--blob', help='Blob bytes')
args = parser.parse_args()
if args.guid:
id = gen_id_from_guid(args.guid)
print('ID: ' + id)
print('Extension: .' + id)
elif args.blob:
print('Checksum: ' + hex(gen_checksum_from_bytes(args.blob)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment