- Strip attacker-controlled subscriber selectors from NAS read and inventory requests. - Preserve the existing session-owned UE path used by checker requests.
56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
import importlib
|
|
import importlib.util
|
|
import unittest
|
|
|
|
|
|
def frame(opcode: int, fields: list[tuple[int, bytes]]) -> bytes:
|
|
payload = b"".join(
|
|
tag.to_bytes(2, "little") + len(value).to_bytes(2, "little") + value
|
|
for tag, value in fields
|
|
)
|
|
return b"LT" + bytes((1, opcode)) + b"\0" * 10 + len(payload).to_bytes(2, "little") + payload
|
|
|
|
|
|
class GuardTests(unittest.TestCase):
|
|
def test_strips_single_subscriber_selector_from_read(self) -> None:
|
|
module = importlib.import_module("guard")
|
|
request = frame(
|
|
0x21,
|
|
[
|
|
(0x0B, b"victim01"),
|
|
(0x0D, b"\x01\0\0\0"),
|
|
(0x0E, b"\xff\xff\xff\xff"),
|
|
],
|
|
)
|
|
expected = frame(
|
|
0x21,
|
|
[(0x0D, b"\x01\0\0\0"), (0x0E, b"\xff\xff\xff\xff")],
|
|
)
|
|
self.assertEqual(module.sanitize_frame(request), expected)
|
|
|
|
def test_strips_all_subscriber_selectors_from_inventory(self) -> None:
|
|
module = importlib.import_module("guard")
|
|
request = frame(
|
|
0x23,
|
|
[(0x0B, b"owned-ue"), (0x0B, b"victimue")],
|
|
)
|
|
self.assertEqual(module.sanitize_frame(request), frame(0x23, []))
|
|
|
|
def test_keeps_checker_read_by_message_ids(self) -> None:
|
|
guard = importlib.util.find_spec("guard")
|
|
self.assertIsNotNone(guard, "Towercore guard is not implemented")
|
|
module = importlib.import_module("guard")
|
|
request = frame(0x23, [(0x0D, b"\x05\0\0\0"), (0x0D, b"\x06\0\0\0")])
|
|
self.assertEqual(module.sanitize_frame(request), request)
|
|
|
|
def test_keeps_other_commands_with_subscriber_field(self) -> None:
|
|
guard = importlib.util.find_spec("guard")
|
|
self.assertIsNotNone(guard, "Towercore guard is not implemented")
|
|
module = importlib.import_module("guard")
|
|
request = frame(0x07, [(0x0B, b"lookup01")])
|
|
self.assertEqual(module.sanitize_frame(request), request)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|