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

get_mac_address() returns None by default when connected to corporate VPN #91

Open
istrupin opened this issue Jun 28, 2023 · 3 comments

Comments

@istrupin
Copy link

istrupin commented Jun 28, 2023

Describe the bug
When running the get_mac_address() not connected to VPN, everything works. However, when connected to my corporate VPN (via GlobalProtect), the response is None.

To Reproduce

  1. Connect to corporate VPN
  2. either run get_mac_address() or run getmac -v --debug

Expected behavior
MAC address of default networking device

System info
(please complete the following information):

  • OS name (e.g. Windows 10 x64): macOS Monterey
  • OS Version (e.g. 1804 build 17134): 12.6
  • Python version (e.g. CPython 3.6.5 x64): 3.8.16
  • getmac version (e.g. getmac 0.8.3): 0.94

Additional context
Output with verbose logging:

❯ poetry run getmac -v --debug
Configuration file exists at /Users/istrupinskiy/Library/Application Support/pypoetry, reusing this directory.

Consider moving configuration to /Users/istrupinskiy/Library/Preferences/pypoetry, as support for the legacy directory will be removed in an upcoming release.
DEBUG    Initializing 'default_iface' method cache (platform: 'darwin')
DEBUG    Current method cache: {'ip4': 'None', 'ip6': 'None', 'iface': 'None', 'default_iface': 'DefaultIfaceRouteGetCommand'}
DEBUG    Current fallback cache: {'ip4': '[]', 'ip6': '[]', 'iface': '[]', 'default_iface': '[]'}
DEBUG    Finished initializing 'default_iface' method cache
DEBUG    Attempting get() (method='DefaultIfaceRouteGetCommand', method_type='default_iface', arg='')
DEBUG    Initializing 'iface' method cache (platform: 'darwin')
DEBUG    Current method cache: {'ip4': 'None', 'ip6': 'None', 'iface': 'DarwinNetworksetupIface', 'default_iface': 'DefaultIfaceRouteGetCommand'}
DEBUG    Current fallback cache: {'ip4': '[]', 'ip6': '[]', 'iface': '[<getmac.getmac.IfconfigEther object at 0x10aede7c0>]', 'default_iface': '[]'}
DEBUG    Finished initializing 'iface' method cache
DEBUG    Attempting get() (method='DarwinNetworksetupIface', method_type='iface', arg='utun0')
WARNING  Cached Method 'DarwinNetworksetupIface' failed for 'iface' lookup with process exit code '4' != 1, marking unusable. Exception: Command '['/usr/sbin/networksetup', '-getmacaddress', 'utun0']' returned non-zero exit status 4.
WARNING  Falling back to 'IfconfigEther' for unusable method 'DarwinNetworksetupIface'
DEBUG    Attempting get() (method='IfconfigEther', method_type='iface', arg='utun0')
DEBUG    Method 'DarwinNetworksetupIface' failed for 'iface' lookup
DEBUG    Attempting get() (method='IfconfigEther', method_type='iface', arg='lo')
DEBUG    Method 'IfconfigEther' failed for 'iface' lookup
DEBUG    Raw MAC found: None
DEBUG    getmac took 0.1592 seconds

When specifying an interface I do get a result even with VPN:

❯ poetry run getmac -v --debug --interface en0
Configuration file exists at /Users/istrupinskiy/Library/Application Support/pypoetry, reusing this directory.

Consider moving configuration to /Users/istrupinskiy/Library/Preferences/pypoetry, as support for the legacy directory will be removed in an upcoming release.
DEBUG    Initializing 'iface' method cache (platform: 'darwin')
DEBUG    Current method cache: {'ip4': 'None', 'ip6': 'None', 'iface': 'DarwinNetworksetupIface', 'default_iface': 'None'}
DEBUG    Current fallback cache: {'ip4': '[]', 'ip6': '[]', 'iface': '[<getmac.getmac.IfconfigEther object at 0x10ab677f0>]', 'default_iface': '[]'}
DEBUG    Finished initializing 'iface' method cache
DEBUG    Attempting get() (method='DarwinNetworksetupIface', method_type='iface', arg='en0')
DEBUG    Raw MAC found: 3c:22:fb:bf:87:c9
DEBUG    getmac took 0.0965 seconds
3c:22:fb:bf:87:c9

I'd like to figure out how to get the MAC address without specifying a network interface as I am using a 3rd party library that uses this pattern, and I do not have access to modify their code. (I also need it to run on a variety of runtimes).

@istrupin
Copy link
Author

I think I've isolated it down to this area of code:

image

Namely _popen("route", "get default")

When running this locally when connected to VPN I get the following:

image

@GhostofGoes
Copy link
Owner

Hey Igor, thanks for the report. Apologies for the delay in responding, life continues to be zany 😅

Global Protect messes with the OS routing table to ensure all traffic flows through the VPN. How this happens in practice varies by platform, but generally it involves changing the default route. How getmac works if you call it it with no args is it attempts to lookup the default interface on the system (in this case, it should be getting utun0 while VPN is active). Then, it looks up the MAC for that default interface and returns it.

There are several things that would cause this to fail:

  • utun0 doesn't have a MAC address. You can verify this manually with networksetup -getmacaddress utun0, or possibly via other commands or a GUI (I'm not a Mac user so sorry I can't be more helpful here).
  • getmac failed to find the default interface. This could be because of a bug in it's parsing code or the OS isn't telling it. Based on the logs, it's finding the utun0 default just fine, so this isn't the reason, but worth mentioning here in case someone runs across this on Google.
  • getmac fails to find the MAC for the interface. This could be because of bug in the command output parsing code or bad command args.

Not sure if I'll be able to reproduce, since while I do have a system with Global Protect, it's Windows. I'll give it a try anyway next week when I have access to it again.

Next steps:

  • verify utun0 has a MAC address
  • While VPN is connected run:
getmac -v -dddd -i utun0
getmac -v -dddd
getmac -v -dddd en0

The -dddd raises debug level to 4, providing the most output.

@istrupin
Copy link
Author

Hey no worries! Thanks for getting back to me!

So I think the issue is that indeed utun0 device doesn't have a MAC address. Using the networksetup command returns an error (it works for "real" devices though). Likewise, using the UI I actually just the the MAC address of my wifi device en0, even though I'm connected to VPN.

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

2 participants