def test_encrypt_gcm(self, monkeypatch): def mockurandom(length): return b"\xff" * length monkeypatch.setattr(os, 'urandom', mockurandom) connection = Connection(uuid.uuid4(), "server", 445) connection.dialect = Dialects.SMB_3_1_1 connection.cipher_id = Ciphers.get_cipher(Ciphers.AES_128_GCM) session = Session(connection, "user", "pass") session.session_id = 1 session.encryption_key = b"\xff" * 16 expected = SMB2TransformHeader() expected['signature'] = b"\x39\xd8\x32\x34\xd7\x53\xd0\x8e" \ b"\xc0\xfc\xbe\x33\x01\x5f\x19\xbd" expected['nonce'] = b"\xff" * 12 + b"\x00" * 4 expected['original_message_size'] = 4 expected['flags'] = 1 expected['session_id'] = 1 expected['data'] = b"\xda\x26\x57\x33" actual = connection._encrypt(b"\x01\x02\x03\x04", session) assert isinstance(actual, SMB2TransformHeader) assert actual.pack() == expected.pack()
def test_encrypt_ccm(self, monkeypatch): def mockurandom(length): return b"\xff" * length monkeypatch.setattr(os, 'urandom', mockurandom) connection = Connection(uuid.uuid4(), "server", 445) connection.dialect = Dialects.SMB_3_1_1 connection.cipher_id = Ciphers.get_cipher(Ciphers.AES_128_CCM) session = Session(connection, "user", "pass") session.session_id = 1 session.encryption_key = b"\xff" * 16 expected = SMB2TransformHeader() expected['signature'] = b"\xc8\x73\x0c\x9b\xa7\xe5\x9f\x1c" \ b"\xfd\x37\x51\xa1\x95\xf2\xb3\xac" expected['nonce'] = b"\xff" * 11 + b"\x00" * 5 expected['original_message_size'] = 4 expected['flags'] = 1 expected['session_id'] = 1 expected['data'] = b"\x21\x91\xe3\x0e" actual = connection._encrypt(b"\x01\x02\x03\x04", session) assert isinstance(actual, SMB2TransformHeader) assert actual.pack() == expected.pack()
def test_create_message(self): message = SMB2TransformHeader() message['nonce'] = b"\xff" * 16 message['original_message_size'] = 4 message['session_id'] = 1 message['data'] = b"\x01\x02\x03\x04" expected = b"\xfd\x53\x4d\x42" \ b"\x00\x00\x00\x00\x00\x00\x00\x00" \ b"\x00\x00\x00\x00\x00\x00\x00\x00" \ b"\xff\xff\xff\xff\xff\xff\xff\xff" \ b"\xff\xff\xff\xff\xff\xff\xff\xff" \ b"\x04\x00\x00\x00" \ b"\x00\x00" \ b"\x01\x00" \ b"\x01\x00\x00\x00\x00\x00\x00\x00" \ b"\x01\x02\x03\x04" actual = message.pack() assert len(message) == 56 assert actual == expected
def test_parse_message(self): actual = SMB2TransformHeader() data = b"\xfd\x53\x4d\x42" \ b"\x00\x00\x00\x00\x00\x00\x00\x00" \ b"\x00\x00\x00\x00\x00\x00\x00\x00" \ b"\xff\xff\xff\xff\xff\xff\xff\xff" \ b"\xff\xff\xff\xff\xff\xff\xff\xff" \ b"\x04\x00\x00\x00" \ b"\x00\x00" \ b"\x01\x00" \ b"\x01\x00\x00\x00\x00\x00\x00\x00" \ b"\x01\x02\x03\x04" actual.unpack(data) assert len(actual) == 56 assert actual['protocol_id'].get_value() == b"\xfd\x53\x4d\x42" assert actual['signature'].get_value() == b"\x00" * 16 assert actual['nonce'].get_value() == b"\xff" * 16 assert actual['original_message_size'].get_value() == 4 assert actual['reserved'].get_value() == 0 assert actual['flags'].get_value() == 1 assert actual['session_id'].get_value() == 1 assert actual['data'].get_value() == b"\x01\x02\x03\x04"