def main(ifname): iw = IW() ip = IPRoute() ifindex = ip.link_lookup(ifname=ifname)[0] ip.close() # CMD_GET_SCAN doesn't require root privileges. # Can use 'nmcli device wifi' or 'nmcli d w' to trigger a scan which will # fill the scan results cache for ~30 seconds. # See also 'iw dev $yourdev scan dump' msg = nl80211_scan.NL80211_GetScan(ifindex) # msg['cmd'] = NL80211_NAMES['NL80211_CMD_GET_SCAN'] # msg['attrs'] = [['NL80211_ATTR_IFINDEX', ifindex]] scan_dump = iw.nlm_request(msg, msg_type=iw.prid, msg_flags=NLM_F_REQUEST | NLM_F_DUMP) for network in scan_dump: for attr in network['attrs']: if attr[0] == 'NL80211_ATTR_BSS': # handy debugging; see everything we captured for bss_attr in attr[1]['attrs']: logger.debug("bss attr=%r", bss_attr) bss = dict(attr[1]['attrs']) print_bss(bss) iw.close()
def __winterface_name_to_device_dict(interface: str) -> Any: """ Return a dict containing device details (from pyroute2.IW). """ with IW() as iw: list_dev_dict = iw.list_dev() for device_dict in list_dev_dict: if device_dict["attrs"][1][1] == interface: return device_dict return None
def run(ifname): iw = IW() ip = IPRoute() ifindex = ip.link_lookup(ifname=ifname)[0] ip.close() # CMD_GET_SCAN doesn't require root privileges. # Can use 'nmcli device wifi' or 'nmcli d w' to trigger a scan which will # fill the scan results cache for ~30 seconds. # See also 'iw dev $yourdev scan dump' msg = nl80211_scan.NL80211_GetScan(ifindex) scan_dump = iw.nlm_request(msg, msg_type=iw.prid, msg_flags=NLM_F_REQUEST | NLM_F_DUMP) jsonator = to_json(scan_dump) return json.dumps({n["bssid"]: n for n in jsonator})
def getifaces(): global ifaces, desc iw = IW() for q in iw.get_interfaces_dump(): phyname = 'phy%i' % int(q.get_attr('NL80211_ATTR_WIPHY')) ifname = q.get_attr('NL80211_ATTR_IFNAME') mac = q.get_attr('NL80211_ATTR_MAC') desc.append( '%s\t%s\t%s\t%s' % (q.get_attr('NL80211_ATTR_IFINDEX'), phyname, ifname, mac)) i = { 'NL80211_ATTR_WIPHY': int(q.get_attr('NL80211_ATTR_WIPHY')), 'NL80211_ATTR_IFINDEX': q.get_attr('NL80211_ATTR_IFINDEX'), 'NL80211_ATTR_IFNAME': ifname, 'NL80211_ATTR_MAC': mac } ifaces[ifname] = i ifaces[phyname] = i ifaces[mac] = i iw.close()
def main(ifname): iw = IW() ip = IPRoute() ifindex = ip.link_lookup(ifname=ifname)[0] ip.close() msg = NL80211_GetScan(ifindex) # msg['cmd'] = NL80211_NAMES['NL80211_CMD_GET_SCAN'] # msg['attrs'] = [['NL80211_ATTR_IFINDEX', ifindex]] scan_dump = iw.nlm_request(msg, msg_type=iw.prid, msg_flags=NLM_F_REQUEST | NLM_F_DUMP) print("SSID BSSID CHAN RATE S:N INT CAPS") for network in scan_dump: for attr in network['attrs']: if attr[0] == 'NL80211_ATTR_BSS': bss = dict(attr[1]['attrs']) print_bss(bss)
from pyroute2.iwutil import IW from pyroute2.netlink import NLM_F_REQUEST from pyroute2.netlink import NLM_F_DUMP from pyroute2.netlink.nl80211 import nl80211cmd from pyroute2.netlink.nl80211 import NL80211_NAMES logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("scandump") logger.setLevel(level=logging.INFO) # interface name to dump scan results ifname = sys.argv[1] iw = IW() ip = IPRoute() ifindex = ip.link_lookup(ifname=ifname)[0] ip.close() # CMD_GET_SCAN doesn't require root privileges. # Can use 'nmcli device wifi' or 'nmcli d w' to trigger a scan which will fill # the scan results cache for ~30 seconds. # See also 'iw dev $yourdev scan dump' msg = nl80211cmd() msg['cmd'] = NL80211_NAMES['NL80211_CMD_GET_SCAN'] msg['attrs'] = [['NL80211_ATTR_IFINDEX', ifindex]] scan_dump = iw.nlm_request(msg, msg_type=iw.prid,
#!/usr/bin/env python # -*- coding: utf-8 -*- # from pyroute2.iwutil import IW iw = IW() for q in iw.get_interfaces_dump(): phyname = 'phy%i' % int(q.get_attr('NL80211_ATTR_WIPHY')) print('%s\t%s\t%s\t%s' % ( q.get_attr('NL80211_ATTR_IFINDEX'), phyname, q.get_attr('NL80211_ATTR_IFNAME'), q.get_attr('NL80211_ATTR_MAC'), )) iw.close()
#!/usr/bin/env python # -*- coding: utf-8 -*- # # fcukall.py from pyroute2.iwutil import IW iw = IW() for q in iw.get_interfaces_dump(): phyname = 'phy%i' % int(q.get_attr('NL80211_ATTR_WIPHY')[:2]) print('%i\t%s\t%s\t%s' % (q.get_attr('NL80211_ATTR_IFINDEX'), phyname, q.get_attr('NL80211_ATTR_IFNAME'), q.get_attr('NL80211_ATTR_MAC')))
#!/usr/bin/env python # -*- coding: utf-8 -*- # from pyroute2.iwutil import IW iw = IW() for q in iw.get_interfaces_dump(): phyname = 'phy%i' % int(q.get_attr('NL80211_ATTR_WIPHY')) print('%i\t%s\t%s\t%s' % (q.get_attr('NL80211_ATTR_IFINDEX'), phyname, q.get_attr('NL80211_ATTR_IFNAME'), q.get_attr('NL80211_ATTR_MAC'))) iw.close()
def startap(self, ifindex=-1): if (ifindex == -1): n = None if isinstance(self.opts.iface, str): n = self.opts.iface elif len(self.opts.args) > 0: n = self.opts.args.pop(0) try: from pyroute2.iwutil import IW iw = IW() for q in iw.get_interfaces_dump(): ifname = q.get_attr('NL80211_ATTR_IFNAME') phyname = 'phy%i' % int(q.get_attr('NL80211_ATTR_WIPHY')) if ifname == n or phyname == n: ifindex = q.get_attr('NL80211_ATTR_IFINDEX') break finally: pass if ifindex == -1: raise AP.NoIface() print("Get interfaces") msg = nl80211cmd() msg['cmd'] = NL80211_CMD_GET_INTERFACE msg['attrs'] = [['NL80211_ATTR_IFINDEX', ifindex]] r = self.nlm_request(msg, msg_type=28, msg_flags=NLM_F_REQUEST | NLM_F_ACK) pprint(r) wiphy = r[0]['attrs'][2][1] # 'NL80211_ATTR_WIPHY' print(" > wiphy: %d" % (wiphy)) print("Proto featires") msg = nl80211cmd() msg['cmd'] = NL80211_CMD_GET_PROTOCOL_FEATURES msg['attrs'] = [] r = self.nlm_request(msg, msg_type=28, msg_flags=NLM_F_REQUEST | NLM_F_ACK) pprint(r) print("Get wiphy") msg = nl80211cmd() msg['cmd'] = NL80211_CMD_GET_WIPHY msg['attrs'] = [['NL80211_ATTR_IFINDEX', ifindex], ['NL80211_ATTR_SPLIT_WIPHY_DUMP', '']] r = self.nlm_request(msg, msg_type=28, msg_flags=NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP) pprint(r) print("Get iface") msg = nl80211cmd() msg['cmd'] = NL80211_CMD_GET_INTERFACE msg['attrs'] = [['NL80211_ATTR_IFINDEX', ifindex]] r = self.nlm_request(msg, msg_type=28, msg_flags=NLM_F_REQUEST | NLM_F_ACK) pprint(r) print("Set iface") msg = nl80211cmd() msg['cmd'] = NL80211_CMD_SET_INTERFACE msg['attrs'] = [['NL80211_ATTR_IFINDEX', ifindex], ['NL80211_ATTR_IFTYPE', 3]] r = self.nlm_request(msg, msg_type=28, msg_flags=NLM_F_REQUEST | NLM_F_ACK) pprint(r) time.sleep(1) #for i in [ 'b0:00', '00:00', '20:00', 'a0:00', 'c0:00', 'd0:00', '40:00']: print("Register Actions %d" % (ifindex)) msg = nl80211cmd() msg['cmd'] = NL80211_CMD_REGISTER_FRAME msg['attrs'] = [['NL80211_ATTR_IFINDEX', ifindex], ['NL80211_ATTR_FRAME_TYPE', 'b0:00'], ['NL80211_ATTR_FRAME_MATCH', '']] r = self.nlm_request(msg, msg_type=28, msg_flags=NLM_F_REQUEST | NLM_F_ACK) pprint(r) # NL80211_CMD_UNEXPECTED_FRAME print("Register beacon") msg = nl80211cmd() msg['cmd'] = NL80211_CMD_REGISTER_BEACONS msg['attrs'] = [['NL80211_ATTR_WIPHY', wiphy]] r = self.nlm_request(msg, msg_type=28, msg_flags=NLM_F_REQUEST | NLM_F_ACK) pprint(r) print("Start ap") msg = nl80211cmd() msg['cmd'] = NL80211_CMD_START_AP msg['attrs'] = [ ['NL80211_ATTR_IFINDEX', ifindex], [ 'NL80211_ATTR_BEACON_HEAD', '80:00:00:00:ff:ff:ff:ff:ff:ff:f4:f2:6d:1c:df:6a:f4:f2:6d:1c:df:6a:00:00:00:00:00:00:00:00:00:00:64:00:01:04:00:0a:68:6f:73:74:61:70:50:61:73:73:01:08:82:84:8b:96:0c:12:18:24:03:01:06' ], [ 'NL80211_ATTR_BEACON_TAIL', '2a:01:04:32:04:30:48:60:6c:7f:08:00:00:00:02:00:00:00:40' ], ['NL80211_ATTR_BEACON_INTERVAL', '64:00:00:00'], ['NL80211_ATTR_DTIM_PERIOD', '02:00:00:00'], ['NL80211_ATTR_SSID', 'hostapPass'], ['NL80211_ATTR_HIDDEN_SSID', '00:00:00:00'], ['NL80211_ATTR_SMPS_MODE', '00:00:00:00'], ['NL80211_ATTR_IE', '7f:08:00:00:00:02:00:00:00:40'], ['NL80211_ATTR_IE_PROBE_RESP', '7f:08:00:00:00:02:00:00:00:40'], ['NL80211_ATTR_IE_ASSOC_RESP', '7f:08:00:00:00:02:00:00:00:40'] ] r = self.nlm_request(msg, msg_type=28, msg_flags=NLM_F_REQUEST | NLM_F_ACK) pprint(r) return
# end NL_80211_ATTR_BSS # end for attr print(f'{i}) {ssid}: {mac}') def getSSID(ap): return ap['attrs'][3][1]['attrs'][2][1]['SSID'].decode() ######################## # # # Env Init # # # ######################## iw = IW() ip = IPRoute() indices = ip.link_lookup(ifname="wlan0") if len(indices) == 1: wlanX = indices[0] else: raise IndexError(f'WLAN0: expected len 1, found {len(indices)}') indices = ip.link_lookup(ifname="eth0") if len(indices) == 1: ethX = indices[0] else: raise IndexError(f'ETH0: expected len 1, found {len(indices)}')