def load_winpcapy(): """This functions calls libpcap ``pcap_findalldevs`` function, and extracts and parse all the data scapy will need to build the Interface List. The data will be stored in ``conf.cache_pcapiflist`` """ from scapy.fields import FlagValue err = create_string_buffer(PCAP_ERRBUF_SIZE) devs = POINTER(pcap_if_t)() if_list = {} if pcap_findalldevs(byref(devs), err) < 0: return try: p = devs # Iterate through the different interfaces while p: name = plain_str(p.contents.name) # GUID description = plain_str( p.contents.description or "" ) # DESC flags = p.contents.flags # FLAGS ips = [] mac = "" a = p.contents.addresses while a: # IPv4 address family = a.contents.addr.contents.sa_family ap = a.contents.addr if family == socket.AF_INET: val = cast(ap, POINTER(sockaddr_in)) val = val.contents.sin_addr[:] elif family == socket.AF_INET6: val = cast(ap, POINTER(sockaddr_in6)) val = val.contents.sin6_addr[:] elif family == socket.AF_LINK: # Special case: MAC # (AF_LINK is mostly BSD specific) val = ap.contents.sa_data val = val[:6] mac = str2mac(bytes(bytearray(val))) a = a.contents.next continue else: # Unknown AF a = a.contents.next continue addr = inet_ntop(family, bytes(bytearray(val))) if addr != "0.0.0.0": ips.append(addr) a = a.contents.next flags = FlagValue(flags, _pcap_if_flags) if_list[name] = (description, ips, flags, mac) p = p.contents.next conf.cache_pcapiflist = if_list except Exception: raise finally: pcap_freealldevs(devs)
def parse_args(self, iface=None, server_port=None, client_port=None, client_ip=None, server_ip=None, server_mac=None, client_mac=None, **kargs): # NOTE: an external program should randomize MAC prior running this. Automaton.parse_args(self, **kargs) logger.debug('Automaton parsing args.') # in case iface change when going back to init?: self.iface = iface or conf.iface # link layer: # in case mac change when going back to init?: # chaddr if client_mac is None: _, client_mac = get_if_raw_hwaddr(self.iface) self.client_mac = str2mac(client_mac) else: self.client_mac = client_mac # upd layer self.server_port = server_port or SERVER_PORT self.client_port = client_port or CLIENT_PORT # dhcp logic self.max_discover_retries = MAX_DISCOVER_RETRIES self.max_num_offers = MAX_OFFERS_COLLECTED self.previous_state = None self.current_state = 'INIT' self.initialize(iface=iface, client_mac=client_mac, client_ip=client_ip, server_ip=server_ip, server_mac=server_mac)
def anonymize(self, value): binary = False if '\x00' in value: value = str(str2mac(value[0:6])) binary = True replacement = self.mac.get_replacement(value) if binary: if replacement != '': replacement = mac2str(replacement) return replacement
def test_address_from_hci_le_connection_complete_packet(): raw_data = binascii.unhexlify( "043e1301004800000110c8b8a5eac9380000002a0000") packet = HCI_Hdr(raw_data) address = Address.from_packet(packet) assert str2mac(address.bd_addr) == "c9:ea:a5:b8:c8:10" assert address.macstr() == "c9:ea:a5:b8:c8:10" assert address.address_type == AddressType.random assert address.is_random() assert not address.is_public()
def test_address_from_advertising_report_packet(): raw_data = binascii.unhexlify( "043e280201000110c8b8a5eac91c0201060303d9fe" "1109506562626c652054696d652043383130020a00d5") packet = HCI_Hdr(raw_data) address = Address.from_packet(packet) assert str2mac(address.bd_addr) == "c9:ea:a5:b8:c8:10" assert address.macstr() == "c9:ea:a5:b8:c8:10" assert address.address_type == AddressType.random assert address.is_random() assert not address.is_public()
def __attrs_post_init__(self): """Initializes attributes after attrs __init__. These attributes do not change during the life of the object. """ logger.debug('Creating new DHCPCAP obj.') if self.iface is None: self.iface = conf.iface if self.client_mac is None: _, client_mac = get_if_raw_hwaddr(self.iface) self.client_mac = str2mac(client_mac) if self.prl is None: self.prl = PRL if self.xid is None: self.xid = gen_xid() logger.debug('Modifying Lease obj, setting iface.') self.lease.interface = self.iface
def reset(self, iface=None, client_mac=None, xid=None, scriptfile=None): """Reset object attributes when state is INIT.""" logger.debug('Reseting attributes.') if iface is None: iface = conf.iface if client_mac is None: # scapy for python 3 returns byte, not tuple tempmac = get_if_raw_hwaddr(iface) if isinstance(tempmac, tuple) and len(tempmac) == 2: mac = tempmac[1] else: mac = tempmac client_mac = str2mac(mac) self.client = DHCPCAP(iface=iface, client_mac=client_mac, xid=xid) if scriptfile is not None: self.script = ClientScript(scriptfile) else: self.script = None self.time_sent_request = None self.discover_attempts = 0 self.request_attempts = 0 self.current_state = STATE_PREINIT self.offers = list()
def _get_mac(x): size = x["physical_address_length"] if size != 6: return "" data = bytearray(x["physical_address"]) return str2mac(bytes(data)[:size])
def m2i(self, pkt, x): return str2mac(x[::-1])
def __str__(self): return "{} {}, address_type={}".format( super(Address, self).__str__(), str2mac(self.bd_addr), self.address_type)
def macstr(self): return str2mac(self.bd_addr)
def discover(self, value): if '\x00' in value: value = str(str2mac(value[0:6])) self.mac.add_value(value)