2023-03-28 17:33:34 +01:00
|
|
|
# Crown Copyright (C) Dstl 2022. DEFCON 703. Shared in confidence.
|
2023-05-25 11:42:19 +01:00
|
|
|
"""Used to tes the ACL functions."""
|
2023-03-28 17:33:34 +01:00
|
|
|
|
2023-05-25 10:31:37 +01:00
|
|
|
from primaite.acl.access_control_list import AccessControlList
|
2023-05-25 11:42:19 +01:00
|
|
|
from primaite.acl.acl_rule import ACLRule
|
2023-03-28 17:33:34 +01:00
|
|
|
|
|
|
|
|
|
2023-05-25 11:42:19 +01:00
|
|
|
def test_acl_address_match_1():
|
|
|
|
|
"""Test that matching IP addresses produce True."""
|
2023-06-20 11:47:20 +01:00
|
|
|
acl = AccessControlList(True, "DENY", 10)
|
2023-03-28 17:33:34 +01:00
|
|
|
|
|
|
|
|
rule = ACLRule("ALLOW", "192.168.1.1", "192.168.1.2", "TCP", "80")
|
|
|
|
|
|
|
|
|
|
assert acl.check_address_match(rule, "192.168.1.1", "192.168.1.2") == True
|
|
|
|
|
|
|
|
|
|
|
2023-05-25 11:42:19 +01:00
|
|
|
def test_acl_address_match_2():
|
|
|
|
|
"""Test that mismatching IP addresses produce False."""
|
2023-06-20 11:47:20 +01:00
|
|
|
acl = AccessControlList(True, "DENY", 10)
|
2023-03-28 17:33:34 +01:00
|
|
|
|
|
|
|
|
rule = ACLRule("ALLOW", "192.168.1.1", "192.168.1.2", "TCP", "80")
|
|
|
|
|
|
|
|
|
|
assert acl.check_address_match(rule, "192.168.1.1", "192.168.1.3") == False
|
|
|
|
|
|
|
|
|
|
|
2023-05-25 11:42:19 +01:00
|
|
|
def test_acl_address_match_3():
|
|
|
|
|
"""Test the ANY condition for source IP addresses produce True."""
|
2023-06-20 11:47:20 +01:00
|
|
|
acl = AccessControlList(True, "DENY", 10)
|
2023-03-28 17:33:34 +01:00
|
|
|
|
|
|
|
|
rule = ACLRule("ALLOW", "ANY", "192.168.1.2", "TCP", "80")
|
|
|
|
|
|
|
|
|
|
assert acl.check_address_match(rule, "192.168.1.1", "192.168.1.2") == True
|
|
|
|
|
|
|
|
|
|
|
2023-05-25 11:42:19 +01:00
|
|
|
def test_acl_address_match_4():
|
|
|
|
|
"""Test the ANY condition for dest IP addresses produce True."""
|
2023-06-20 11:47:20 +01:00
|
|
|
acl = AccessControlList(True, "DENY", 10)
|
2023-03-28 17:33:34 +01:00
|
|
|
|
|
|
|
|
rule = ACLRule("ALLOW", "192.168.1.1", "ANY", "TCP", "80")
|
|
|
|
|
|
|
|
|
|
assert acl.check_address_match(rule, "192.168.1.1", "192.168.1.2") == True
|
|
|
|
|
|
|
|
|
|
|
2023-05-25 11:42:19 +01:00
|
|
|
def test_check_acl_block_affirmative():
|
|
|
|
|
"""Test the block function (affirmative)."""
|
2023-03-28 17:33:34 +01:00
|
|
|
# Create the Access Control List
|
2023-06-20 11:47:20 +01:00
|
|
|
acl = AccessControlList(True, "DENY", 10)
|
2023-03-28 17:33:34 +01:00
|
|
|
|
|
|
|
|
# Create a rule
|
|
|
|
|
acl_rule_permission = "ALLOW"
|
|
|
|
|
acl_rule_source = "192.168.1.1"
|
|
|
|
|
acl_rule_destination = "192.168.1.2"
|
|
|
|
|
acl_rule_protocol = "TCP"
|
|
|
|
|
acl_rule_port = "80"
|
2023-06-13 10:01:55 +01:00
|
|
|
acl_position_in_list = "0"
|
2023-03-28 17:33:34 +01:00
|
|
|
|
2023-05-25 11:42:19 +01:00
|
|
|
acl.add_rule(
|
|
|
|
|
acl_rule_permission,
|
|
|
|
|
acl_rule_source,
|
|
|
|
|
acl_rule_destination,
|
|
|
|
|
acl_rule_protocol,
|
|
|
|
|
acl_rule_port,
|
2023-06-13 08:54:33 +01:00
|
|
|
acl_position_in_list,
|
2023-05-25 11:42:19 +01:00
|
|
|
)
|
2023-07-12 09:47:16 +01:00
|
|
|
print(len(acl.acl), "len of acl list\n", acl.acl[0])
|
2023-03-28 17:33:34 +01:00
|
|
|
assert acl.is_blocked("192.168.1.1", "192.168.1.2", "TCP", "80") == False
|
|
|
|
|
|
|
|
|
|
|
2023-05-25 11:42:19 +01:00
|
|
|
def test_check_acl_block_negative():
|
|
|
|
|
"""Test the block function (negative)."""
|
2023-03-28 17:33:34 +01:00
|
|
|
# Create the Access Control List
|
2023-06-20 11:47:20 +01:00
|
|
|
acl = AccessControlList(True, "DENY", 10)
|
2023-03-28 17:33:34 +01:00
|
|
|
|
|
|
|
|
# Create a rule
|
|
|
|
|
acl_rule_permission = "DENY"
|
|
|
|
|
acl_rule_source = "192.168.1.1"
|
|
|
|
|
acl_rule_destination = "192.168.1.2"
|
|
|
|
|
acl_rule_protocol = "TCP"
|
|
|
|
|
acl_rule_port = "80"
|
2023-06-13 10:01:55 +01:00
|
|
|
acl_position_in_list = "0"
|
2023-03-28 17:33:34 +01:00
|
|
|
|
2023-05-25 11:42:19 +01:00
|
|
|
acl.add_rule(
|
|
|
|
|
acl_rule_permission,
|
|
|
|
|
acl_rule_source,
|
|
|
|
|
acl_rule_destination,
|
|
|
|
|
acl_rule_protocol,
|
|
|
|
|
acl_rule_port,
|
2023-06-13 08:54:33 +01:00
|
|
|
acl_position_in_list,
|
2023-05-25 11:42:19 +01:00
|
|
|
)
|
2023-03-28 17:33:34 +01:00
|
|
|
|
|
|
|
|
assert acl.is_blocked("192.168.1.1", "192.168.1.2", "TCP", "80") == True
|
|
|
|
|
|
|
|
|
|
|
2023-05-25 11:42:19 +01:00
|
|
|
def test_rule_hash():
|
|
|
|
|
"""Test the rule hash."""
|
2023-03-28 17:33:34 +01:00
|
|
|
# Create the Access Control List
|
2023-06-20 11:47:20 +01:00
|
|
|
acl = AccessControlList(True, "DENY", 10)
|
2023-03-28 17:33:34 +01:00
|
|
|
|
|
|
|
|
rule = ACLRule("DENY", "192.168.1.1", "192.168.1.2", "TCP", "80")
|
|
|
|
|
hash_value_local = hash(rule)
|
|
|
|
|
|
2023-06-30 16:52:57 +01:00
|
|
|
hash_value_remote = acl.get_dictionary_hash("DENY", "192.168.1.1", "192.168.1.2", "TCP", "80")
|
2023-03-28 17:33:34 +01:00
|
|
|
|
|
|
|
|
assert hash_value_local == hash_value_remote
|