From cf0674ce22198a3519023f6077ccbc1f98133b2f Mon Sep 17 00:00:00 2001 From: Chris McCarthy Date: Thu, 29 Feb 2024 13:00:27 +0000 Subject: [PATCH] #2326 - Network Interface port name/num fixed so that it carries through to sys log and PCAP outputs. --- CHANGELOG.md | 2 +- src/primaite/simulator/network/airspace.py | 4 +++- .../simulator/network/hardware/base.py | 15 ++++++++++---- .../wireless/wireless_access_point.py | 2 +- .../wireless/wireless_nic.py | 2 +- .../network/hardware/nodes/host/host_node.py | 2 +- .../hardware/nodes/network/firewall.py | 12 ++++++++++- .../network/hardware/nodes/network/router.py | 2 +- .../hardware/nodes/network/wireless_router.py | 5 ++++- .../simulator/system/core/packet_capture.py | 20 +++++++++++++++---- 10 files changed, 50 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcff5934..55202de4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,7 +107,7 @@ SessionManager. ### Fixed - Addressed network transmission issues that previously allowed ARP requests to be incorrectly routed and repeated across different subnets. This fix ensures ARP requests are correctly managed and confined to their appropriate network segments. - Resolved problems in `Node` and its subclasses where the default gateway configuration was not properly utilized for communications across different subnets. This correction ensures that nodes effectively use their configured default gateways for outbound communications to other network segments, thereby enhancing the network's routing functionality and reliability. - +- Network Interface Port name/num being set properly for sys log and PCAP output. ## [2.0.0] - 2023-07-26 diff --git a/src/primaite/simulator/network/airspace.py b/src/primaite/simulator/network/airspace.py index d264f751..5ceedc8e 100644 --- a/src/primaite/simulator/network/airspace.py +++ b/src/primaite/simulator/network/airspace.py @@ -168,7 +168,9 @@ class WirelessNetworkInterface(NetworkInterface, ABC): self.enabled = True self._connected_node.sys_log.info(f"Network Interface {self} enabled") - self.pcap = PacketCapture(hostname=self._connected_node.hostname, interface_num=self.port_num) + self.pcap = PacketCapture( + hostname=self._connected_node.hostname, port_num=self.port_num, port_name=self.port_name + ) AIR_SPACE.add_wireless_interface(self) def disable(self): diff --git a/src/primaite/simulator/network/hardware/base.py b/src/primaite/simulator/network/hardware/base.py index ff79f314..991913dd 100644 --- a/src/primaite/simulator/network/hardware/base.py +++ b/src/primaite/simulator/network/hardware/base.py @@ -94,6 +94,9 @@ class NetworkInterface(SimComponent, ABC): port_num: Optional[int] = None "The port number assigned to this interface on the connected node." + port_name: Optional[str] = None + "The port name assigned to this interface on the connected node." + pcap: Optional[PacketCapture] = None "A PacketCapture instance for capturing and analysing packets passing through this interface." @@ -248,7 +251,7 @@ class NetworkInterface(SimComponent, ABC): :return: A string combining the port number and the mac address """ - return f"Port {self.port_num}: {self.mac_address}" + return f"Port {self.port_name if self.port_name else self.port_num}: {self.mac_address}" class WiredNetworkInterface(NetworkInterface, ABC): @@ -293,7 +296,9 @@ class WiredNetworkInterface(NetworkInterface, ABC): self.enabled = True self._connected_node.sys_log.info(f"Network Interface {self} enabled") - self.pcap = PacketCapture(hostname=self._connected_node.hostname, interface_num=self.port_num) + self.pcap = PacketCapture( + hostname=self._connected_node.hostname, port_num=self.port_num, port_name=self.port_name + ) if self._connected_link: self._connected_link.endpoint_up() @@ -1024,7 +1029,7 @@ class Node(SimComponent): self.sys_log.info("Resetting") self.power_off() - def connect_nic(self, network_interface: NetworkInterface): + def connect_nic(self, network_interface: NetworkInterface, port_name: Optional[str] = None): """ Connect a Network Interface to the node. @@ -1036,7 +1041,9 @@ class Node(SimComponent): new_nic_num = len(self.network_interfaces) self.network_interface[new_nic_num] = network_interface network_interface._connected_node = self - network_interface._port_num_on_node = new_nic_num + network_interface.port_num = new_nic_num + if port_name: + network_interface.port_name = port_name network_interface.parent = self self.sys_log.info(f"Connected Network Interface {network_interface}") if self.operating_state == NodeOperatingState.ON: diff --git a/src/primaite/simulator/network/hardware/network_interface/wireless/wireless_access_point.py b/src/primaite/simulator/network/hardware/network_interface/wireless/wireless_access_point.py index bc24270e..721814f8 100644 --- a/src/primaite/simulator/network/hardware/network_interface/wireless/wireless_access_point.py +++ b/src/primaite/simulator/network/hardware/network_interface/wireless/wireless_access_point.py @@ -83,4 +83,4 @@ class WirelessAccessPoint(IPWirelessNetworkInterface): :return: A string combining the port number, MAC address and IP address of the NIC. """ - return f"Port {self.port_num}: {self.mac_address}/{self.ip_address}" + return f"Port {self.port_name if self.port_name else self.port_num}: {self.mac_address}/{self.ip_address}" diff --git a/src/primaite/simulator/network/hardware/network_interface/wireless/wireless_nic.py b/src/primaite/simulator/network/hardware/network_interface/wireless/wireless_nic.py index 32acc08a..7b8f6f54 100644 --- a/src/primaite/simulator/network/hardware/network_interface/wireless/wireless_nic.py +++ b/src/primaite/simulator/network/hardware/network_interface/wireless/wireless_nic.py @@ -80,4 +80,4 @@ class WirelessNIC(IPWirelessNetworkInterface): :return: A string combining the port number, MAC address and IP address of the NIC. """ - return f"Port {self.port_num}: {self.mac_address}/{self.ip_address}" + return f"Port {self.port_name if self.port_name else self.port_num}: {self.mac_address}/{self.ip_address}" diff --git a/src/primaite/simulator/network/hardware/nodes/host/host_node.py b/src/primaite/simulator/network/hardware/nodes/host/host_node.py index 977380be..14a237a4 100644 --- a/src/primaite/simulator/network/hardware/nodes/host/host_node.py +++ b/src/primaite/simulator/network/hardware/nodes/host/host_node.py @@ -250,7 +250,7 @@ class NIC(IPWiredNetworkInterface): :return: A string combining the port number, MAC address and IP address of the NIC. """ - return f"Port {self.port_num}: {self.mac_address}/{self.ip_address}" + return f"Port {self.port_name if self.port_name else self.port_num}: {self.mac_address}/{self.ip_address}" class HostNode(Node): diff --git a/src/primaite/simulator/network/hardware/nodes/network/firewall.py b/src/primaite/simulator/network/hardware/nodes/network/firewall.py index f2305652..7912d5d6 100644 --- a/src/primaite/simulator/network/hardware/nodes/network/firewall.py +++ b/src/primaite/simulator/network/hardware/nodes/network/firewall.py @@ -85,7 +85,17 @@ class Firewall(Router): if not kwargs.get("sys_log"): kwargs["sys_log"] = SysLog(hostname) - super().__init__(hostname=hostname, num_ports=3, **kwargs) + super().__init__(hostname=hostname, num_ports=0, **kwargs) + + self.connect_nic( + RouterInterface(ip_address="127.0.0.1", subnet_mask="255.0.0.0", gateway="0.0.0.0", port_name="external") + ) + self.connect_nic( + RouterInterface(ip_address="127.0.0.1", subnet_mask="255.0.0.0", gateway="0.0.0.0", port_name="internal") + ) + self.connect_nic( + RouterInterface(ip_address="127.0.0.1", subnet_mask="255.0.0.0", gateway="0.0.0.0", port_name="dmz") + ) # Initialise ACLs for internal and dmz interfaces with a default DENY policy self.internal_inbound_acl = AccessControlList( diff --git a/src/primaite/simulator/network/hardware/nodes/network/router.py b/src/primaite/simulator/network/hardware/nodes/network/router.py index aa6eec3a..b63fb43c 100644 --- a/src/primaite/simulator/network/hardware/nodes/network/router.py +++ b/src/primaite/simulator/network/hardware/nodes/network/router.py @@ -998,7 +998,7 @@ class RouterInterface(IPWiredNetworkInterface): :return: A string combining the port number, MAC address and IP address of the NIC. """ - return f"Port {self.port_num}: {self.mac_address}/{self.ip_address}" + return f"Port {self.port_name if self.port_name else self.port_num}: {self.mac_address}/{self.ip_address}" class Router(NetworkNode): diff --git a/src/primaite/simulator/network/hardware/nodes/network/wireless_router.py b/src/primaite/simulator/network/hardware/nodes/network/wireless_router.py index 91833d6a..3e8d715f 100644 --- a/src/primaite/simulator/network/hardware/nodes/network/wireless_router.py +++ b/src/primaite/simulator/network/hardware/nodes/network/wireless_router.py @@ -80,7 +80,10 @@ class WirelessAccessPoint(IPWirelessNetworkInterface): :return: A string combining the port number, MAC address and IP address of the NIC. """ - return f"Port {self.port_num}: {self.mac_address}/{self.ip_address} ({self.frequency})" + return ( + f"Port {self.port_name if self.port_name else self.port_num}: " + f"{self.mac_address}/{self.ip_address} ({self.frequency})" + ) class WirelessRouter(Router): diff --git a/src/primaite/simulator/system/core/packet_capture.py b/src/primaite/simulator/system/core/packet_capture.py index fb8a1624..5419dde6 100644 --- a/src/primaite/simulator/system/core/packet_capture.py +++ b/src/primaite/simulator/system/core/packet_capture.py @@ -21,7 +21,13 @@ class PacketCapture: The PCAPs are logged to: //__pcap.log """ - def __init__(self, hostname: str, ip_address: Optional[str] = None, interface_num: Optional[int] = None): + def __init__( + self, + hostname: str, + ip_address: Optional[str] = None, + port_num: Optional[int] = None, + port_name: Optional[str] = None, + ): """ Initialize the PacketCapture process. @@ -32,9 +38,12 @@ class PacketCapture: "The hostname for which PCAP logs are being recorded." self.ip_address: str = ip_address "The IP address associated with the PCAP logs." - self.interface_num = interface_num + self.port_num = port_num "The interface num on the Node." + self.port_name = port_name + "The interface name on the Node." + self.inbound_logger = None self.outbound_logger = None @@ -42,6 +51,7 @@ class PacketCapture: self.setup_logger(outbound=False) self.setup_logger(outbound=True) + print(port_name) def setup_logger(self, outbound: bool = False): """Set up the logger configuration.""" @@ -79,10 +89,12 @@ class PacketCapture: def _get_logger_name(self, outbound: bool = False) -> str: """Get PCAP the logger name.""" + if self.port_name: + return f"{self.hostname}_{self.port_name}_{'outbound' if outbound else 'inbound'}_pcap" if self.ip_address: return f"{self.hostname}_{self.ip_address}_{'outbound' if outbound else 'inbound'}_pcap" - if self.interface_num: - return f"{self.hostname}_port-{self.interface_num}_{'outbound' if outbound else 'inbound'}_pcap" + if self.port_num: + return f"{self.hostname}_port-{self.port_num}_{'outbound' if outbound else 'inbound'}_pcap" return f"{self.hostname}_{'outbound' if outbound else 'inbound'}_pcap" def _get_log_path(self, outbound: bool = False) -> Path: