def in6_getLocalUniquePrefix(): """ Returns a pseudo-randomly generated Local Unique prefix. Function follows recommandation of Section 3.2.2 of RFC 4193 for prefix generation. """ # Extracted from RFC 1305 (NTP) : # NTP timestamps are represented as a 64-bit unsigned fixed-point number, # in seconds relative to 0h on 1 January 1900. The integer part is in the # first 32 bits and the fraction part in the last 32 bits. # epoch = (1900, 1, 1, 0, 0, 0, 5, 1, 0) # x = time.time() # from time import gmtime, strftime, gmtime, mktime # delta = mktime(gmtime(0)) - mktime(self.epoch) # x = x-delta tod = time.time() # time of day. Will bother with epoch later i = int(tod) j = int((tod - i)*(2**32)) tod = struct.pack("!II", i,j) # TODO: Add some check regarding system address gathering from scapy.arch import get_if_raw_hwaddr rawmac = get_if_raw_hwaddr(conf.iface6)[1] mac = ":".join(map(lambda x: "%.02x" % ord(x), list(rawmac))) # construct modified EUI-64 ID eui64 = inet_pton(socket.AF_INET6, '::' + in6_mactoifaceid(mac))[8:] import sha globalid = sha.new(tod+eui64).digest()[:5] return inet_ntop(socket.AF_INET6, '\xfd' + globalid + '\x00'*10)
def in6_getLocalUniquePrefix(): """ Returns a pseudo-randomly generated Local Unique prefix. Function follows recommandation of Section 3.2.2 of RFC 4193 for prefix generation. """ # Extracted from RFC 1305 (NTP) : # NTP timestamps are represented as a 64-bit unsigned fixed-point number, # in seconds relative to 0h on 1 January 1900. The integer part is in the # first 32 bits and the fraction part in the last 32 bits. # epoch = (1900, 1, 1, 0, 0, 0, 5, 1, 0) # x = time.time() # from time import gmtime, strftime, gmtime, mktime # delta = mktime(gmtime(0)) - mktime(self.epoch) # x = x-delta tod = time.time() # time of day. Will bother with epoch later i = int(tod) j = int((tod - i) * (2**32)) tod = struct.pack("!II", i, j) # TODO: Add some check regarding system address gathering from scapy.arch import get_if_raw_hwaddr rawmac = get_if_raw_hwaddr(conf.iface6)[1] mac = ":".join(map(lambda x: "%.02x" % ord(x), list(rawmac))) # construct modified EUI-64 ID eui64 = inet_pton(socket.AF_INET6, '::' + in6_mactoifaceid(mac))[8:] import sha globalid = sha.new(tod + eui64).digest()[:5] return inet_ntop(socket.AF_INET6, '\xfd' + globalid + '\x00' * 10)
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 dhcp_request(iface=None,**kargs): if conf.checkIPaddr != 0: warning("conf.checkIPaddr is not 0, I may not be able to match the answer") if iface is None: iface = conf.iface fam,hw = get_if_raw_hwaddr(iface) return srp1(Ether(dst="ff:ff:ff:ff:ff:ff")/IP(src="0.0.0.0",dst="255.255.255.255")/UDP(sport=68,dport=67) /BOOTP(chaddr=hw)/DHCP(options=[("message-type","discover"),"end"]),iface=iface,**kargs)
def __init__(self, wlanmon, target_ssid): try: from scapy.arch import str2mac, get_if_raw_hwaddr except ImportError: raise ImportError("Could not import str2mac and get_if_raw") self.tar_ssid = target_ssid self.iface = wlanmon self.mac = str2mac(get_if_raw_hwaddr(wlanmon)[1])
def dhcp_request(iface=None, **kargs): if conf.checkIPaddr != 0: warning("conf.checkIPaddr is not 0, I may not be able to match the answer") if iface is None: iface = conf.iface fam, hw = get_if_raw_hwaddr(iface) return srp1(Ether(dst="ff:ff:ff:ff:ff:ff") / IP(src="0.0.0.0", dst="255.255.255.255") / UDP(sport=68, dport=67) / BOOTP(chaddr=hw) / DHCP(options=[("message-type", "discover"), "end"]), iface=iface, **kargs)
def __init__(self, iface, ssid, wap_ip=None, channel=None): try: from scapy.arch import str2mac, get_if_raw_hwaddr except ImportError: raise ImportError("Could not import str2mac and get_if_raw") self.ssids = [i for i in ssid] self.iface = iface self.ch = channel if channel is not None else 1 self.mac = str2mac(get_if_raw_hwaddr(iface)[1]) self.wpa = 0 self.802_1_x = 0 self.l_filter = None self.hidden = False self.ip = wap_ip if wap_ip is not None else '10.10.0.1' self.bpffilter = "not ( wlan type mgt subtype beacon ) and ((ether dst host " + self.mac +") or (ether dst host ff:ff:ff:ff:ff:ff))"
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 __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 use_l2_packet(self, interface): addrfamily, mac = get_if_raw_hwaddr(interface) return addrfamily in [ARPHDR_ETHER, ARPHDR_LOOPBACK]
def if_hwaddr(iff): return str2mac(get_if_raw_hwaddr(iff)[1])
def get_my_addr(self, iface): family, hwaddr = get_if_raw_hwaddr(iface) hwaddr = binascii.hexlify(hwaddr) hwaddr = ':'.join(hwaddr[i:i + 2] for i in range(0, 12, 2)) return hwaddr
def if_hwaddr(iff): return str2mac(get_if_raw_hwaddr(iff)[1])