- authenticate and prepare victim handovers from attack data - decrypt sealed NAS exports with stdlib XChaCha20-Poly1305
168 lines
5.9 KiB
Python
168 lines
5.9 KiB
Python
from __future__ import annotations
|
|
|
|
import struct
|
|
import time
|
|
import unittest
|
|
|
|
from towercore.sploits import towercore_handover as exploit
|
|
|
|
|
|
class FrameTests(unittest.TestCase):
|
|
def test_builds_handover_frame_with_expected_header_and_selector(self) -> None:
|
|
request = exploit.build_frame(
|
|
0x40,
|
|
[
|
|
(0x0B, struct.pack("<II", 0x161B74BE, 0)),
|
|
(0x0C, struct.pack("<I", 1001)),
|
|
],
|
|
request_id=5,
|
|
session_token=b"\0" * 6,
|
|
)
|
|
|
|
self.assertEqual(
|
|
request.hex(),
|
|
"4c5401400000050000000000000014000b000800be741b16000000000c000400e9030000",
|
|
)
|
|
|
|
def test_parser_preserves_repeated_tlvs_and_rejects_truncation(self) -> None:
|
|
request = exploit.build_frame(
|
|
0x23,
|
|
[(0x0B, b"first"), (0x0B, b"last")],
|
|
request_id=7,
|
|
session_token=b"\0" * 6,
|
|
)
|
|
parsed = exploit.parse_frame(request)
|
|
|
|
self.assertEqual([value for tag, value in parsed.fields if tag == 0x0B], [b"first", b"last"])
|
|
with self.assertRaises(exploit.ProtocolError):
|
|
exploit.parse_frame(request[:-1])
|
|
|
|
def test_receive_handles_partial_tcp_reads(self) -> None:
|
|
class FragmentedSocket:
|
|
def __init__(self, data: bytes) -> None:
|
|
self.data = data
|
|
self.offset = 0
|
|
|
|
def settimeout(self, timeout: float) -> None:
|
|
pass
|
|
|
|
def recv(self, size: int) -> bytes:
|
|
count = min(size, 3)
|
|
result = self.data[self.offset : self.offset + count]
|
|
self.offset += len(result)
|
|
return result
|
|
|
|
request = exploit.build_frame(
|
|
0x06,
|
|
[],
|
|
request_id=12,
|
|
session_token=b"\x01\x02\x03\x04\x05\x06",
|
|
)
|
|
parsed = exploit.receive_frame(FragmentedSocket(request), time.monotonic() + 2.0)
|
|
|
|
self.assertEqual(parsed.opcode, 0x06)
|
|
self.assertEqual(parsed.session_token, b"\x01\x02\x03\x04\x05\x06")
|
|
|
|
|
|
class CryptoTests(unittest.TestCase):
|
|
def test_authentication_digest_matches_captured_protocol_vector(self) -> None:
|
|
challenge = bytes.fromhex(
|
|
"9e6df7a47b1660aa0fec4b61e7594f8743f03dd63badbf7a4957fbf4ee31fc71"
|
|
)
|
|
imsi = bytes.fromhex("1f0c91cd9d858829")
|
|
client_nonce = bytes.fromhex("315d4cdfe640c62a5c50d56766672f0f")
|
|
|
|
self.assertEqual(
|
|
exploit.auth_digest(challenge, imsi, client_nonce).hex(),
|
|
"11d9a445157dd1ca2f55014b9fa5f9a9d3d35d34debf0e329a2a8d1428e2567d",
|
|
)
|
|
|
|
def test_session_and_nas_keys_match_captured_protocol_vector(self) -> None:
|
|
challenge = bytes.fromhex(
|
|
"9e6df7a47b1660aa0fec4b61e7594f8743f03dd63badbf7a4957fbf4ee31fc71"
|
|
)
|
|
client_nonce = bytes.fromhex("315d4cdfe640c62a5c50d56766672f0f")
|
|
server_nonce = bytes.fromhex("ff7dc7e4ecbde1bf660ae782b8c93002")
|
|
|
|
session_key, nas_key = exploit.derive_keys(challenge, client_nonce, server_nonce)
|
|
|
|
self.assertEqual(
|
|
session_key.hex(),
|
|
"e98f71f3c530347ad4f3d1ca348e4efc9e6d000c22bff891e3a9c9b0346a6513",
|
|
)
|
|
self.assertEqual(
|
|
nas_key.hex(),
|
|
"1fbbddc4a618d41212be8b282b210f524041758514e097ceef501766d49d1e80",
|
|
)
|
|
|
|
def test_hchacha20_matches_published_block_vector(self) -> None:
|
|
key = bytes(range(32))
|
|
nonce = bytes.fromhex("000000090000004a0000000031415927")
|
|
|
|
self.assertEqual(
|
|
exploit.hchacha20(key, nonce).hex(),
|
|
"82413b4227b27bfed30e42508a877d73a0f9e4d58a74a853c12ec41326d3ecdc",
|
|
)
|
|
|
|
def test_poly1305_matches_rfc_vector(self) -> None:
|
|
key = bytes.fromhex(
|
|
"85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b"
|
|
)
|
|
|
|
self.assertEqual(
|
|
exploit.poly1305_mac(b"Cryptographic Forum Research Group", key).hex(),
|
|
"a8061dc1305136c6c22b8baf0c0127a9",
|
|
)
|
|
|
|
def test_xchacha20_poly1305_decrypts_authenticated_ciphertext(self) -> None:
|
|
key = bytes.fromhex(
|
|
"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f"
|
|
)
|
|
nonce = bytes.fromhex("404142434445464748494a4b4c4d4e4f5051525354555657")
|
|
aad = bytes.fromhex("50515253c0c1c2c3c4c5c6c7")
|
|
ciphertext_and_tag = bytes.fromhex(
|
|
"bd6d179d3e83d43b9576579493c0e939572a1700252bfaccbed2902c21396cbb"
|
|
"731c7f1b0b4aa6440bf3a82f4eda7e39ae64c6708c54c216cb96b72e1213b452"
|
|
"2f8c9ba40db5d945b11b69b982c1bb9e3f3fac2bc369488f76b2383565d3fff9"
|
|
"21f9664c97637da9768812f615c68b13b52e"
|
|
"c0875924c1c7987947deafd8780acf49"
|
|
)
|
|
|
|
self.assertEqual(
|
|
exploit.xchacha20poly1305_decrypt(ciphertext_and_tag, aad, nonce, key),
|
|
b"Ladies and Gentlemen of the class of '99: If I could offer you only one tip "
|
|
b"for the future, sunscreen would be it.",
|
|
)
|
|
with self.assertRaises(exploit.AuthenticationError):
|
|
exploit.xchacha20poly1305_decrypt(
|
|
ciphertext_and_tag, b"bad aad", nonce, key
|
|
)
|
|
|
|
|
|
class AttackDataTests(unittest.TestCase):
|
|
def test_extracts_unique_victims_by_normalized_target(self) -> None:
|
|
data = {
|
|
"towercore": {
|
|
"10.80.19.2": [
|
|
{
|
|
"users": [
|
|
{"cell": 1001, "tmsi": 0x161B74BE},
|
|
{"cell": 1002, "tmsi": 0x3D287CA8},
|
|
{"cell": 1001, "tmsi": 0x161B74BE},
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
victims = exploit.extract_victims(data, "10.80.19.2:7250")
|
|
|
|
self.assertEqual(
|
|
[(victim.cell, victim.tmsi) for victim in victims],
|
|
[(1001, 0x161B74BE), (1002, 0x3D287CA8)],
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|