|
10 | 10 | from banner import display_banner
|
11 | 11 |
|
12 | 12 | def is_valid_interface(interface):
|
13 |
| - """Check if the interface name is valid.""" |
14 |
| - valid_prefixes = ['eth', 'wlan', 'ens'] |
15 |
| - return any(interface.startswith(prefix) for prefix in valid_prefixes) and all(c in string.ascii_letters + string.digits + ':-.' for c in interface) |
| 13 | + """Check if the interface name is valid based on known naming conventions.""" |
| 14 | + valid_prefixes = [ |
| 15 | + 'eth', 'wlan', 'ens', # Traditional Linux and newer Linux naming |
| 16 | + 'em', 're', 'ath', 'wi', # BSD systems |
| 17 | + 'en', 'lo', # macOS and loopback |
| 18 | + 'e1000g', 'bge' # Solaris |
| 19 | + ] |
| 20 | + |
| 21 | + # Match common patterns for interface names |
| 22 | + valid_patterns = [ |
| 23 | + r'^eth\d+$', # eth0, eth1, etc. |
| 24 | + r'^wlan\d+$', # wlan0, wlan1, etc. |
| 25 | + r'^ens\d+$', # ens33, ens160, etc. |
| 26 | + r'^em\d+$', # em0, em1, etc. |
| 27 | + r'^re\d+$', # re0, re1, etc. |
| 28 | + r'^ath\d+$', # ath0, ath1, etc. |
| 29 | + r'^wi\d+$', # wi0, wi1, etc. |
| 30 | + r'^e1000g\d+$', # e1000g0, e1000g1, etc. |
| 31 | + r'^bge\d+$', # bge0, bge1, etc. |
| 32 | + r'^en\d+$', # en0, en1, etc. (macOS) |
| 33 | + r'^lo$', # loopback |
| 34 | + ] |
| 35 | + |
| 36 | + # Check if the interface name matches any of the valid patterns |
| 37 | + return any(re.match(pattern, interface) for pattern in valid_patterns) |
16 | 38 |
|
17 | 39 | def configure_logging(verbose):
|
18 | 40 | """Configure logging based on verbosity."""
|
@@ -46,10 +68,21 @@ def interface_exists(interface, logger):
|
46 | 68 |
|
47 | 69 | def generate_mac_address(logger):
|
48 | 70 | """Generate a new MAC address with a local administered bit set."""
|
49 |
| - mac = [random.randint(0x02, 0x02)] + [random.randint(0x00, 0xff) for _ in range(5)] |
| 71 | + # Choose an even prefix for the first byte (0x02, 0x04, 0x06, ..., 0xFE) |
| 72 | + first_byte = random.choice([x for x in range(0x00, 0xFF + 1) if x % 2 == 0]) |
| 73 | + |
| 74 | + # Generate the rest of the MAC address |
| 75 | + mac = [first_byte] + [random.randint(0x00, 0xff) for _ in range(5)] |
| 76 | + |
| 77 | + # Format each byte as a two-digit hexadecimal number |
50 | 78 | mac_hex = [f"{x:02x}" for x in mac]
|
| 79 | + |
| 80 | + # Join the bytes with ':' to form the MAC address string |
51 | 81 | random_mac = ':'.join(mac_hex)
|
| 82 | + |
| 83 | + # Log the generated MAC address |
52 | 84 | logger.debug(f"Generated MAC address: {random_mac}")
|
| 85 | + |
53 | 86 | return random_mac
|
54 | 87 |
|
55 | 88 | def is_valid_mac(mac_address, logger):
|
|
0 commit comments