utils.py
1 # Copyright (c) 2024 Anoduck 2 # 3 # This software is released under the MIT License. 4 # https://opensource.org/licenses/MIT 5 import struct 6 from scapy.arch import str2mac, get_if_raw_hwaddr 7 8 9 class Utils: 10 11 def __init__(self, log) -> None: 12 self.log = log 13 14 def hex_offset_to_string(self, byte_array): 15 temp = byte_array.replace("\n", "") 16 temp = temp.replace(" ", "") 17 return temp.decode("hex") 18 19 def get_frequency(self, channel): 20 if channel == 14: 21 freq = 2484 22 else: 23 freq = 2407 + (channel * 5) 24 25 freq_string = struct.pack("<h", freq) 26 27 return freq_string 28 29 def mac_to_bytes(self, mac): 30 return ''.join(chr(int(x, 16)) for x in mac.split(':')) 31 32 def bytes_to_mac(self, byte_array): 33 return ':'.join("{:02x}".format(ord(byte)) for byte in byte_array) 34 35 # Scapy sees mon0 interface as invalid address family, so we write our own 36 def if_hwaddr(self, iff): 37 return str2mac(get_if_raw_hwaddr(iff)[1])