def __init__(self, data, do_checksum=False): offset = 0 self.level = bytes_to_int(data[offset:offset + 1]) offset += 1 self.name = bytes_to_int(data[offset:offset + 2]) offset += 2 self.type = bytes_to_int(data[offset:offset + 2]) offset += 2 att_length = bytes_to_int(data[offset:offset + 4]) offset += 4 self.data = data[offset:offset + att_length] offset += att_length att_checksum = bytes_to_int(data[offset:offset + 2]) offset += 2 self.length = offset if do_checksum: calc_checksum = checksum(self.data) if calc_checksum != att_checksum: logger.warn("Checksum: %s != %s" % (calc_checksum, att_checksum)) else: calc_checksum = att_checksum # whether the checksum is ok self.good_checksum = calc_checksum == att_checksum
def host(): email, A = yield [] password = USERS[email] salt = random_bytes(16) x = hash_to_int(salt + password) print("s: x:", x) v = mod(G, x) b = dh_secret(P) B = K * v + mod(G, b) print("{} trying to log in".format(email.decode())) u = hash_to_int(int_to_bytes(A) + int_to_bytes(B)) print("host: A:", A) print("host: B:", B) print("host: salt:", bytes_to_int(salt)) print("host: u", u) print("host: x:", x) user_mac, *_ = yield [salt, B] t0 = A * mod(v, u) S = mod(t0, b) print("host: S:", S) key = hashlib.sha256(int_to_bytes(S)).digest() print("host: key:", binascii.hexlify(key)) host_mac = hmac_sha256(key, salt) yield ["OK" if user_mac == host_mac else "NO"]
def user(): start = yield [] assert start is Start a = dh_secret(P) A = mod(G, a) print("user: sending email") salt, B = yield [b"*****@*****.**", A] u = hash_to_int(int_to_bytes(A) + int_to_bytes(B)) print("user: A:", A) print("user: B:", B) print("user: salt:", bytes_to_int(salt)) print("user: u", u) x = hash_to_int(salt + b"pass") print("user: x:", x) t1 = B - K * mod(G, x) assert t1 > 0 S = mod(t1, a + u * x) print("user: S:", S) key = hashlib.sha256(int_to_bytes(S)).digest() print("user: key:", binascii.hexlify(key)) response, *_ = yield [hmac_sha256(key, salt)] print("server says password was", response) assert response == "OK"
def get_analog_data(self): if self.filename is None: print("Cannot get data without setting filename") sys.exit(-1) # Read in the wave file CHUNK = 1 wf = wave.open(self.filename, 'rb') data = wf.readframes(CHUNK) analog_data = [0.0] * self.n_frames # Wave-file data is actually stored as ints in the range # [-2**15, 2**15). These are 16-bit ints stored across two # bytes. We'll shift and scale those values so that we get # floats in the range [0.0, 1.0) data_index = 0 while len(data) > 0: # Bytes -> ints new_data = bytes([data[0], data[1]]) x = util.bytes_to_int(data[0], data[1]) # Ints -> floats analog_data[data_index] = float(x + 2**15) / 2**16 assert (analog_data[data_index] >= 0 and analog_data[data_index] <= 1.0) # double-check data = wf.readframes(CHUNK) data_index += 1 return analog_data
def host(): email, A = yield [] password = USERS[email] salt = random_bytes(16) x = hash_to_int(salt + password) v = mod(G, x) b = dh_secret(P) B = mod(G, b) u = random_int_from_n_bytes(128 // 8) user_mac, *_ = yield [salt, B, u] S = mod(A * mod(v, u), b) key = hashlib.sha256(int_to_bytes(S)).digest() host_mac = hmac_sha256(key, salt) print("=" * 80) print("{} trying to log in".format(email.decode())) print("host: A:", A) print("host: B:", B) print("host: salt:", bytes_to_int(salt)) print("host: u", u) print("host: x:", x) print("host: S:", S) print("host: key:", binascii.hexlify(key)) yield ["OK" if user_mac == host_mac else "NO"]
def user(): start = yield [] assert start is actors.Start a = dh_secret(P) A = mod(G, a) salt, B, u = yield [b"*****@*****.**", A] x = hash_to_int(salt + b"pass") S = mod(B, a + u * x) key = hashlib.sha256(int_to_bytes(S)).digest() response, *_ = yield [hmac_sha256(key, salt)] print("=" * 80) print("user: A:", A) print("user: B:", B) print("user: salt:", bytes_to_int(salt)) print("user: u", u) print("user: x:", x) print("user: S:", S) print("user: key:", binascii.hexlify(key)) print("user: server says password was", response) assert response == "OK"
def unwrap_user_info(data): return util.bytes_to_int( data[defi.UserInfoOffsetUUID:defi.UserInfoOffsetUUID + defi.UserInfoLengthUUID] ), data[defi.UserInfoOffsetUsername:defi.UserInfoOffsetUsername + data[defi.UserInfoOffsetUsernameLength]].decode("utf-8"), data[ defi.UserInfoOffsetNickname:defi.UserInfoOffsetNickname + data[defi.UserInfoOffsetNicknameLength]].decode("utf-8")
def __init__(self, data, do_checksum = True): self.signature = bytes_to_int(data[0:4]) if self.signature != TNEF.TNEF_SIGNATURE: sys.exit("Wrong TNEF signature: 0x%2.8x" % self.signature) self.key = bytes_to_int(data[4:6]) self.objects = [] self.attachments = [] self.mapi_attrs = {} offset = 6 if not do_checksum: logger.info("Skipping checksum for performance") while (offset < len(data)): obj = TNEFObject(data[offset: offset+len(data)], do_checksum) offset += obj.length self.objects.append(obj) # handle attachments if obj.name == TNEF.ATTATTACHRENDDATA: attachment = TNEFAttachment() self.attachments.append(attachment) elif obj.level == TNEF.LVL_ATTACHMENT: attachment.add_attr(obj) # handle MAPI properties elif obj.name == TNEF.ATTMAPIPROPS: attrs = decode_mapi(obj.data) attr_names = [attr.name for attr in attrs] self.mapi_attrs.update(zip(attr_names, attrs)) # handle BODY property if TNEFMAPI_Attribute.MAPI_BODY in self.mapi_attrs: p = self.mapi_attrs[TNEFMAPI_Attribute.MAPI_BODY] self.body = p.data else: self.body = None # handle BODY_HTML property if TNEFMAPI_Attribute.MAPI_BODY_HTML in self.mapi_attrs: p = self.mapi_attrs[TNEFMAPI_Attribute.MAPI_BODY_HTML] self.htmlbody = p.data else: self.htmlbody = None else: logger.warn("Unknown TNEF Object: %s" % obj)
def __init__(self, data, do_checksum=True): self.signature = bytes_to_int(data[0:4]) if self.signature != TNEF.TNEF_SIGNATURE: sys.exit("Wrong TNEF signature: 0x%2.8x" % self.signature) self.key = bytes_to_int(data[4:6]) self.objects = [] self.attachments = [] self.mapi_attrs = {} offset = 6 if not do_checksum: logger.info("Skipping checksum for performance") while (offset < len(data)): obj = TNEFObject(data[offset:offset + len(data)], do_checksum) offset += obj.length self.objects.append(obj) # handle attachments if obj.name == TNEF.ATTATTACHRENDDATA: attachment = TNEFAttachment() self.attachments.append(attachment) elif obj.level == TNEF.LVL_ATTACHMENT: attachment.add_attr(obj) # handle MAPI properties elif obj.name == TNEF.ATTMAPIPROPS: attrs = decode_mapi(obj.data) attr_names = [attr.name for attr in attrs] self.mapi_attrs.update(zip(attr_names, attrs)) # handle BODY property if TNEFMAPI_Attribute.MAPI_BODY in self.mapi_attrs: p = self.mapi_attrs[TNEFMAPI_Attribute.MAPI_BODY] self.body = p.data else: self.body = None # handle BODY_HTML property if TNEFMAPI_Attribute.MAPI_BODY_HTML in self.mapi_attrs: p = self.mapi_attrs[TNEFMAPI_Attribute.MAPI_BODY_HTML] self.htmlbody = p.data else: self.htmlbody = None else: logger.warn("Unknown TNEF Object: %s" % obj)
def key_expansion(key: bytearray, word: List[bytearray], nk: int, nr: int): i = 0 while i < nk: word[i] = bytearray([key[4 * i], key[4 * i + 1], key[4 * i + 2], key[4 * i + 3]]) i = i + 1 i = nk while i < Nb * (nr + 1): temp = word[i - 1] if i % nk == 0: temp = bytes_to_int(subWord(rotWord(temp))) ^ \ Rcon[int(i / nk)] temp = int_to_bytes(temp) elif nk > 6 and i % nk == 4: temp = subWord(temp) word[i] = int_to_bytes(bytes_to_int(word[i - nk]) ^ bytes_to_int(temp)) i = i + 1
def __init__(self, data, do_checksum = True): self.signature = bytes_to_int(data[0:4]) if self.signature != TNEF.TNEF_SIGNATURE: raise Exception('Wrong TNEF signature: 0x%2.8x' % self.signature) self.key = bytes_to_int(data[4:6]) self.objects = [] self.attachments = [] self.mapiprops = [] offset = 6 if not do_checksum: logger.info("Skipping checksum for performance") while (offset < len(data)): obj = TNEFObject(data[offset: offset+len(data)], do_checksum) offset += obj.length self.objects.append(obj) # handle attachments if obj.name == TNEF.ATTATTACHRENDDATA: attachment = TNEFAttachment() self.attachments.append(attachment) elif obj.level == TNEF.LVL_ATTACHMENT: attachment.add_attr(obj) # handle MAPI properties elif obj.name == TNEF.ATTMAPIPROPS: self.mapiprops = decode_mapi(obj.data) # handle BODY property for p in self.mapiprops: if p.name == TNEFMAPI_Attribute.MAPI_BODY: self.body = p.data elif p.name == TNEFMAPI_Attribute.MAPI_BODY_HTML: self.htmlbody = p.data elif obj.name == TNEF.ATTSUBJECT: self.subject = obj.data elif obj.name == TNEF.ATTDATESENT: self.date_sent = obj.data elif obj.name == TNEF.ATTMESSAGEID: self.messageid = obj.data else: logger.warn("Unknown TNEF Object: %s" % obj)
def __init__(self, data, do_checksum=False): offset = 0 self.level = bytes_to_int(data[offset: offset+1]); offset += 1 self.name = bytes_to_int(data[offset:offset+2]); offset += 2 self.type = bytes_to_int(data[offset:offset+2]); offset += 2 att_length = bytes_to_int(data[offset:offset+4]); offset += 4 self.data = data[offset:offset+att_length]; offset += att_length att_checksum = bytes_to_int(data[offset:offset+2]); offset += 2 self.length = offset if do_checksum: calc_checksum = checksum(self.data) if calc_checksum != att_checksum: logger.warn("Checksum: %s != %s" % (calc_checksum, att_checksum)) else: calc_checksum = att_checksum # whether the checksum is ok self.good_checksum = calc_checksum == att_checksum
def __init__(self, data, do_checksum=True): self.signature = bytes_to_int(data[0:4]) if self.signature != TNEF.TNEF_SIGNATURE: raise TNEFSignatureError( "Wrong TNEF signature: 0x%2.8x" % self.signature, self.signature) self.key = bytes_to_int(data[4:6]) self.objects = [] self.attachments = [] self.mapiprops = [] offset = 6 if not do_checksum: logger.info("Skipping checksum for performance") while (offset < len(data)): obj = TNEFObject(data[offset:offset + len(data)], do_checksum) offset += obj.length self.objects.append(obj) # handle attachments if obj.name == TNEF.ATTATTACHRENDDATA: attachment = TNEFAttachment() self.attachments.append(attachment) elif obj.level == TNEF.LVL_ATTACHMENT: attachment.add_attr(obj) # handle MAPI properties elif obj.name == TNEF.ATTMAPIPROPS: self.mapiprops = decode_mapi(obj.data) # handle BODY property for p in self.mapiprops: if p.name == TNEFMAPI_Attribute.MAPI_BODY: self.body = p.data elif p.name == TNEFMAPI_Attribute.MAPI_BODY_HTML: self.htmlbody = p.data else: logger.warn("Unknown TNEF Object: %s" % obj)
def get_request_code(self): return util.bytes_to_int( self.data[self.OffsetRequestCode:self.OffsetRequestCode + self.LengthRequestCode])
def get_ack(self): return util.bytes_to_int(self.data[self.OffsetACK:self.OffsetACK + self.LengthACK])
def decode_mapi(data): "decode MAPI types" SZMAPI_UNSPECIFIED = 0x0000 # MAPI Unspecified SZMAPI_NULL = 0x0001 # MAPI null property SZMAPI_SHORT = 0x0002 # MAPI short (signed 16 bits) SZMAPI_INT = 0x0003 # MAPI integer (signed 32 bits) SZMAPI_FLOAT = 0x0004 # MAPI float (4 bytes) SZMAPI_DOUBLE = 0x0005 # MAPI double SZMAPI_CURRENCY = 0x0006 # MAPI currency (64 bits) SZMAPI_APPTIME = 0x0007 # MAPI application time SZMAPI_ERROR = 0x000A # MAPI error (32 bits) SZMAPI_BOOLEAN = 0x000B # MAPI boolean (16 bits) SZMAPI_OBJECT = 0x000D # MAPI embedded object SZMAPI_INT8BYTE = 0x0014 # MAPI 8 byte signed int SZMAPI_STRING = 0x001E # MAPI string SZMAPI_UNICODE_STRING = 0x001F # MAPI unicode-string (null terminated) # SZMAPI_PT_SYSTIME = 0x001e # MAPI time (after 2038/01/17 22:14:07 or before 1970/01/01 00:00:00) SZMAPI_SYSTIME = 0x0040 # MAPI time (64 bits) SZMAPI_CLSID = 0x0048 # MAPI OLE GUID SZMAPI_BINARY = 0x0102 # MAPI binary SZMAPI_BEATS_THE_HELL_OUTTA_ME = 0x0033 dataLen = len(data) attrs = [] offset = 0 num_properties = bytes_to_int(data[offset : offset + 4]) offset += 4 # logger.debug("%i MAPI properties" % num_properties) try: for i in range(num_properties): if offset >= dataLen: logger.warn("Skipping property '%i'" % i) continue attr_type = bytes_to_int(data[offset : offset + 2]) offset += 2 attr_name = bytes_to_int(data[offset : offset + 2]) offset += 2 # logger.debug("Attribute type: 0x%4.4x" % attr_type) # logger.debug("Attribute name: 0x%4.4x" % attr_name) guid = "" if attr_name >= 0x8000: guid = "%32.32x" % bytes_to_int(data[offset : offset + 16]) offset += 16 kind = bytes_to_int(data[offset : offset + 4]) offset += 4 # logger.debug("Kind: %8.8x" % kind) if kind == 0: # Skip the iid offset += 4 else: iidLen = bytes_to_int(data[offset : offset + 4]) offset += 4 q, r = divmod(strLen, 4) if r != 0: iidLen += 4 - r offset += iidLen attr_data = None if attr_type == SZMAPI_SHORT: attr_data = data[offset : offset + 2] offset += 2 elif attr_type in (SZMAPI_INT, SZMAPI_FLOAT, SZMAPI_ERROR, SZMAPI_BOOLEAN): attr_data = data[offset : offset + 4] offset += 4 elif attr_type in (SZMAPI_DOUBLE, SZMAPI_APPTIME, SZMAPI_CURRENCY, SZMAPI_INT8BYTE, SZMAPI_SYSTIME): attr_data = data[offset : offset + 8] offset += 8 elif attr_type == SZMAPI_CLSID: attr_data = data[offset : offset + 16] offset += 16 elif attr_type in (SZMAPI_STRING, SZMAPI_UNICODE_STRING, SZMAPI_OBJECT, SZMAPI_BINARY, SZMAPI_UNSPECIFIED): num_vals = bytes_to_int(data[offset : offset + 4]) offset += 4 # logger.debug("Number of values: %i" % num_vals) attr_data = [] for j in range(num_vals): length = bytes_to_int(data[offset : offset + 4]) offset += 4 q, r = divmod(length, 4) if r != 0: length += 4 - r # logger.debug("Length: %i" % length) attr_data.append(data[offset : offset + length]) offset += length else: logger.debug("## Unknown MAPI type 0x%4.4x" % attr_type) logger.debug("Attribute type: 0x%4.4x" % attr_type) logger.debug("Attribute name: 0x%4.4x" % attr_name) break # logger.debug("Adding MAPI attribute %i to list" % (i+1)) attr = TNEFMAPI_Attribute(attr_type, attr_name, attr_data, guid) attrs.append(attr) except Exception as e: # stack = traceback.format_exc() logger.debug("decode_mapi Exception %s" % e) # logger.debug(stack) return attrs
def unwrap_message(data): return (util.bytes_to_int( data[defi.MessageOffsetUUID:defi.MessageOffsetUUID + defi.MessageLengthUUID]), data[defi.MessageOffsetMessage:defi.MessageOffsetMessage + data[defi.MessageOffsetMessageLength]].decode("utf-8"))
def get_seq(self): return util.bytes_to_int(self.data[self.OffsetSEQ:self.OffsetSEQ + self.LengthSEQ])
def hash_to_int(data: bytes) -> int: xH = hashlib.sha256(data).digest() return bytes_to_int(xH)