diff --git a/pcap_anonymize/layers/mac.py b/pcap_anonymize/layers/mac.py index 2ea88d80ca63648d377f45e222b8dfab2d605831..6b7557f77ebd113800978218b771eb5a8bdd579d 100644 --- a/pcap_anonymize/layers/mac.py +++ b/pcap_anonymize/layers/mac.py @@ -21,6 +21,32 @@ special_macs = [ ] +def mac_str_to_bytes(mac: str) -> bytes: + """ + Convert a MAC address string representation + to a bytes object. + + Args: + mac (str): MAC address to convert + Returns: + bytes: MAC address as a bytes object + """ + return bytes.fromhex(mac.replace(":", "")) + + +def mac_bytes_to_str(mac: bytes) -> str: + """ + Convert a MAC address bytes object + to its string representation. + + Args: + mac (bytes): MAC address to convert + Returns: + str: MAC address as a string + """ + return ":".join(f"{byte:02x}" for byte in mac) + + def get_ig_bit(mac: str) -> int: """ Get the I/G bit of a given MAC address. @@ -152,9 +178,8 @@ def anonymize_dhcp(dhcp: BOOTP) -> BOOTP: scapy.BOOTP: anonymized DHCP layer """ # Anonymize client hardware address - chaddr = dhcp.getfieldval("chaddr")[0:6] + chaddr = mac_bytes_to_str(dhcp.getfieldval("chaddr")[0:6]) dhcp.setfieldval("chaddr", anonymize_mac(chaddr)) - dhcp.show() # Check if BOOTP layer contains DHCP options options = dhcp.getfieldval("options") diff --git a/test/test_mac.py b/test/test_mac.py index 871b8eb40030d10aa5f5c4a822a6faedcd1a7dfb..d8b8b4c1757a71062e4b73598463aeefce62deba 100644 --- a/test/test_mac.py +++ b/test/test_mac.py @@ -1,5 +1,7 @@ from scapy.layers.l2 import Ether, ARP +from scapy.layers.dhcp import BOOTP, DHCP from pcap_anonymize.layers.mac import ( + mac_str_to_bytes, mac_bytes_to_str, get_ig_bit, get_ul_bit, anonymize_mac, anonymize_ether, @@ -11,12 +13,35 @@ from pcap_anonymize.layers.mac import ( ### TEST CONSTANTS ### mac_multicast = "01:00:00:00:00:00" +mac_multicast_bytes = b"\x01\x00\x00\x00\x00\x00" mac_laa = "02:00:00:00:00:00" +mac_laa_bytes = b"\x02\x00\x00\x00\x00\x00" mac_uaa = "00:11:22:33:44:55" +mac_uaa_bytes = b"\x00\x11\x22\x33\x44\x55" ### TEST FUNCTIONS ### +def test_mac_str_to_bytes() -> None: + """ + Test the function `mac_str_to_bytes`, + which converts a MAC address' string representation to bytes. + """ + assert mac_str_to_bytes(mac_multicast) == mac_multicast_bytes + assert mac_str_to_bytes(mac_laa) == mac_laa_bytes + assert mac_str_to_bytes(mac_uaa) == mac_uaa_bytes + + +def test_mac_bytes_to_str() -> None: + """ + Test the function `mac_bytes_to_str`, + which converts a MAC address' bytes representation to a string. + """ + assert mac_bytes_to_str(mac_multicast_bytes) == mac_multicast + assert mac_bytes_to_str(mac_laa_bytes) == mac_laa + assert mac_bytes_to_str(mac_uaa_bytes) == mac_uaa + + def test_get_ig_bit() -> None: """ Test the function `get_ig_bit`. @@ -145,3 +170,13 @@ def test_anonymize_arp_uaa() -> None: assert arp_uaa.hwsrc[10:] != mac_uaa[10:] assert arp_uaa.hwdst.startswith(mac_uaa[:8]) assert arp_uaa.hwdst[10:] != mac_uaa[10:] + + +# def test_anonymize_dhcp_multicast() -> None: +# """ +# Test the function `anonymize_dhcp`, +# with multicast addresses. +# """ +# dhcp = BOOTP(chaddr=mac_multicast) +# anonymize_dhcp(dhcp) +# assert dhcp.chaddr == mac_multicast