wraith-wireless / PyRIC

Python wireless library for Linux
http://wraith-wireless.github.io/PyRIC
Other
93 stars 51 forks source link

List of wireless physical devices #17

Closed mchwalisz closed 7 years ago

mchwalisz commented 7 years ago

Is there a way to get list of just the physical interfaces?

Let's assume I have deleted all virtual devices via command line. In such situation I have no easy way (or I haven't found it) to check what wiphy interfaces are there to add devices from scratch. In all examples you start with at least one existing interface (that you often delete) and create new ones.

wraith-wireless commented 7 years ago

Sorry for the delay. I can see your point hope the below helps.

First, so I don't confuse you or myself, we'll call "interface" the logical device however we refer to it, i.e. by device name, physical index etc.

Second, the reason that I generally delete all associated interfaces in my example is because I have found that by doing so, network manager, wpa_supplicant etc will no longer interfere with the interface and I don't have to kill them prior to doing anything and remember to restart them afterward.

Each interface can be reference by dev (the device name) i.e. wlan0, or by phy (the physical index) i.e. the 0 in phy0 or by ifindex (interface index) and depending on what you want to do, you may need to refer to one or other. For example. changing the mac address requires the dev, enabling/disabling the powersave feature requires the ifindex and setting the current channel requires the physical index. The

The Card object collates these three reference values into one object. An interface's phy is always the same (in theory - a usb card may have a new phy everytime it is plugged in) while the other two may change. And a phy may have multiple interfaces (while dev and ifindex are unique to each interface). As long as you "remember" the phy, you can add interfaces or restore the original even if there are no current interfaces.

Let's assume that I only have one wireless interface, wlan0 and look at pentest.py in the examples folder. On line 53, every interface sharing a phy with wlan0 (including wlan0 is deleted). If we ran ifconfig or iwconfig at this point, there would not be any wireless interfaces present. The same situation occurs on line 81 where the monitor interface is deleted. To answer the first question, there is no way to check that wiphy interfaces exist because in essence they don't and the onus is on the programmer or program to recall prior instances. (The only to way, at this point, to check for interfaces would be to iterate through a list of integers and check each one) . As a check, assume you have card = Card(dev='wlan0',phy=0,idx=1) and you call

pyw.devdel(card) 

In a separate console try:

> iw dev wlan0 info

command failed: No such device (-19)

iw phy phy0 info Wiphy phy0 max # scan SSIDs: 20 ....

Yes, there are no interfaces, but the phy is still present.

To answer the second implied question: devadd is one of a few pyw functions that will accept either a Card object or a phy. Since, the phy is always present even if no interfaces are "attached" to it we can always add interfaces. And an artifact of Cards is that even though it may be invalid in the since that given Card(dev='wlan0',phy=0,idx=1) the interface associated with dev=wlan0 and ifindex=1 no longer exists, the underlying phy still does and in line 84 of pentest.py that is what is happening. You have to remember that after adding an interface as we do in line 84, the returned card is what we to use in future operations.

Hope this rambling provides the answer you were looking for.

I haven't gotten around to it yet but plan on added a function nameset which replicates iw. phy set name which will then allow you to do what airmon-ng does, namely change the device name and mode of the card. But, once again this assumes there are not any other interfaces associted with the phy that network manager may be using.

mchwalisz commented 7 years ago

This shows that my understanding was not off. However, there is still one (in my case common) case where you do not "remember" (as you have nicely explained) any phy.

You can still discover it with:

# iw list | grep Wiphy
Wiphy phy0

but you don't have any dev's to start with:

# iw dev
#

In that situation there is no easy way to start using your library.

wraith-wireless commented 7 years ago

Let me see if I understand. You have a situation where you may begin with no devs present i.e. your not deleting them like I do?

If you know the phy you could create a Card from that. devadd supports both a Card object or a phy so in your case it would be

card = pyw.devadd(0, 'wlan0', 'managed')

where 0 is the phy number.

Now would you like to see something implemented where you could list phy's as well? I can take at look at iw list if that is the case.

wraith-wireless commented 7 years ago

alright, I added a function phylist which should help you help.

No devices

# iw dev
# 

List phys and create an interface

>>> phys = pyw.phylist()
>>> phys
[(0,'phy0')]
>>> card = pyw.devadd(phys[0][0],'wlan0','managed')
>>>  card
Card(phy=0,dev=wlan0,ifindex=7)
>>>

And you're good to go

mchwalisz commented 7 years ago

That was exactly what I was looking for. Thanks.