Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function get_working_if() returns the wrong default working interface in win10 #2245

Closed
StoneForests opened this issue Sep 12, 2019 · 9 comments

Comments

@StoneForests
Copy link

StoneForests commented Sep 12, 2019

Environment :Win10, python 3.7 and scapy 2.4.3
My route is as below. The ZeroTier is a virtual internal interface and the I219 is my real external interface that connects the Internet. According to the Metric, the I219 is the default working iface and conf.iface returns right meanwhile get_working_if() returns the ZeroTier.

Network          Netmask          Gateway         Iface                                        Output IP        Metric
0.0.0.0          0.0.0.0          25.255.255.254  ZeroTier One Virtual Port                    172.28.33.102    10034
0.0.0.0          0.0.0.0          10.11.91.254    Intel(R) Ethernet Connection (5) I219-V      10.11.91.161     25

Maybe the reason is that get_working_if() returns the interface associated with the route with smallest mask but not the smallest metric accroding to the file /Lib/site-packages/scapy/arch/windows/__init__.py

def get_working_if():
    """Return an interface that works"""
    try:
        # return the interface associated with the route with smallest
        # mask (route by default if it exists)
        iface = min(conf.route.routes, key=lambda x: x[1])[3]  // I think it should be lambda x: x[5] while 1 means mask and 5 means metric 

Does anyone else have the same problem?

@guedou
Copy link
Member

guedou commented Sep 13, 2019

Could you share the content of conf.route.routes?

@p-l-
Copy link
Member

p-l- commented Sep 16, 2019

The function get_working_if() is not supposed to return the default interface, but "an interface that works".

Does that behavior cause troubles in one of your use cases?

@StoneForests
Copy link
Author

StoneForests commented Sep 23, 2019

Could you share the content of conf.route.routes?

Thanks for reply. My routes are as below and I just delete the GUIDs.

>>> conf.route.routes
[(0,
  0,
  '25.255.255.254',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  10034),
 (0,
  0,
  '10.11.91.254',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  25),
 (168516352,
  4294967040,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  281),
 (168516513,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  281),
 (168516607,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  281),
 (2130706432,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2130706433,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2147483647,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2851995648,
  4294901760,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2852031514,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2852061183,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2887516160,
  4294901760,
  '0.0.0.0',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  291),
 (2887524710,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  291),
 (2887581695,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  291),
 (3232239360,
  4294967040,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet8] {}>,
  '192.168.15.1',
  291),
 (3232239361,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet8] {}>,
  '192.168.15.1',
  291),
 (3232239615,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet8] {}>,
  '192.168.15.1',
  291),
 (3232291840,
  4294967040,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet1] {}>,
  '192.168.220.1',
  291),
 (3232291841,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet1] {}>,
  '192.168.220.1',
  291),
 (3232292095,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet1] {}>,
  '192.168.220.1',
  291),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [SVN Adapter V1.0] {}>,
  '169.254.112.118',
  311),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet1] {}>,
  '192.168.220.1',
  291),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [TAP-Windows Adapter V9] {}>,
  '169.254.26.68',
  291),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet8] {}>,
  '192.168.15.1',
  291),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  281),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Bluetooth Device (Personal Area Network) #2] {}>,
  '169.254.227.190',
  321),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Dual Band Wireless-AC 8265 #2] {}>,
  '169.254.62.191',
  281),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  291),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Microsoft Wi-Fi Direct Virtual Adapter #3] {}>,
  '169.254.173.211',
  281),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Microsoft Wi-Fi Direct Virtual Adapter #4] {}>,
  '169.254.192.230',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [SVN Adapter V1.0] {}>,
  '169.254.112.118',
  311),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet1] {}>,
  '192.168.220.1',
  291),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [TAP-Windows Adapter V9] {}>,
  '169.254.26.68',
  291),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet8] {}>,
  '192.168.15.1',
  291),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Bluetooth Device (Personal Area Network) #2] {}>,
  '169.254.227.190',
  321),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Dual Band Wireless-AC 8265 #2] {}>,
  '169.254.62.191',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  291),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Microsoft Wi-Fi Direct Virtual Adapter #3] {}>,
  '169.254.173.211',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Microsoft Wi-Fi Direct Virtual Adapter #4] {}>,
  '169.254.192.230',
  281)]

@StoneForests
Copy link
Author

The function get_working_if() is not supposed to return the default interface, but "an interface that works".

Does that behavior cause troubles in one of your use cases?

Yeah I need a function/variable to return the default interface. So how can I get the correct one especially when the network has been changed?

@guedou
Copy link
Member

guedou commented Sep 24, 2019 via email

@p-l-
Copy link
Member

p-l- commented Sep 24, 2019

OK so you have two issues there:

  • the network has changed
  • you want to get the interface who has the default route

For the first, you need to run conf.route.resync() to read again the interfaces and routes from your host network stack.

For the second, conf.route.route('0.0.0.0') returns the route (as a Python tuple) for 0.0.0.0, so you can use conf.route.route('0.0.0.0')[0] to get the interface.

@StoneForests
Copy link
Author

You forgot the final “s” while typing the command. Can you try again and share the result (a Python list) ?

Yeah. The new result is as below.

>>> conf.route.routes
[(0,
  0,
  '25.255.255.254',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  10034),
 (0,
  0,
  '10.11.91.254',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  25),
 (168516352,
  4294967040,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  281),
 (168516513,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  281),
 (168516607,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  281),
 (2130706432,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2130706433,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2147483647,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2851995648,
  4294901760,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2852031514,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2852061183,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (2887516160,
  4294901760,
  '0.0.0.0',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  291),
 (2887524710,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  291),
 (2887581695,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  291),
 (3232239360,
  4294967040,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet8] {}>,
  '192.168.15.1',
  291),
 (3232239361,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet8] {}>,
  '192.168.15.1',
  291),
 (3232239615,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet8] {}>,
  '192.168.15.1',
  291),
 (3232291840,
  4294967040,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet1] {}>,
  '192.168.220.1',
  291),
 (3232291841,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet1] {}>,
  '192.168.220.1',
  291),
 (3232292095,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet1] {}>,
  '192.168.220.1',
  291),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [SVN Adapter V1.0] {}>,
  '169.254.112.118',
  311),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet1] {}>,
  '192.168.220.1',
  291),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [TAP-Windows Adapter V9] {}>,
  '169.254.26.68',
  291),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet8] {}>,
  '192.168.15.1',
  291),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  281),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Bluetooth Device (Personal Area Network) #2] {}>,
  '169.254.227.190',
  321),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Dual Band Wireless-AC 8265 #2] {}>,
  '169.254.62.191',
  281),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  291),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Microsoft Wi-Fi Direct Virtual Adapter #3] {}>,
  '169.254.173.211',
  281),
 (3758096384,
  4278190080,
  '0.0.0.0',
  <NetworkInterface [Microsoft Wi-Fi Direct Virtual Adapter #4] {}>,
  '169.254.192.230',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [SVN Adapter V1.0] {}>,
  '169.254.112.118',
  311),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet1] {}>,
  '192.168.220.1',
  291),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [TAP-Windows Adapter V9] {}>,
  '169.254.26.68',
  291),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [VMware Virtual Ethernet Adapter for VMnet8] {}>,
  '192.168.15.1',
  291),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Ethernet Connection (5) I219-V #2] {}>,
  '10.11.91.161',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Npcap Loopback Adapter] {}>,
  '127.0.0.1',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Bluetooth Device (Personal Area Network) #2] {}>,
  '169.254.227.190',
  321),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Intel(R) Dual Band Wireless-AC 8265 #2] {}>,
  '169.254.62.191',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [ZeroTier One Virtual Port] {}>,
  '172.28.33.102',
  291),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Microsoft Wi-Fi Direct Virtual Adapter #3] {}>,
  '169.254.173.211',
  281),
 (4294967295,
  4294967295,
  '0.0.0.0',
  <NetworkInterface [Microsoft Wi-Fi Direct Virtual Adapter #4] {}>,
  '169.254.192.230',
  281)]

@StoneForests
Copy link
Author

OK so you have two issues there:

  • the network has changed
  • you want to get the interface who has the default route

For the first, you need to run conf.route.resync() to read again the interfaces and routes from your host network stack.

For the second, conf.route.route('0.0.0.0') returns the route (as a Python tuple) for 0.0.0.0, so you can use conf.route.route('0.0.0.0')[0] to get the interface.

Thanks for reply. It seems to get the right interface by using conf.route.resync() and conf.route.route('0.0.0.0')[0]. But the get_working_if() function still returns wrong. Can we change the function from 1 to 5?
iface = min(conf.route.routes, key=lambda x: x[1])[3] // I think it should be lambda x: x[5] while 1 means mask and 5 means metric

@p-l-
Copy link
Member

p-l- commented Sep 25, 2019

The return value of get_working_if() does not seem "wrong". As I said, it is not supposed to return the default interface but rather "an interface that works".

@p-l- p-l- closed this as completed Sep 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants