1212
1313#include < osquery/core/system.h>
1414#include < osquery/core/tables.h>
15+ #include < osquery/logger/logger.h>
1516#include < osquery/tables/networking/darwin/wifi_utils.h>
17+ #include < osquery/utils/darwin/system_profiler.h>
1618
1719namespace osquery {
1820namespace tables {
1921
2022QueryData genWifiStatus (QueryContext& context) {
2123 QueryData results;
2224 @autoreleasepool {
23- std::string interfaceName = " en0 " ;
25+ // Get the list of Wifi interfaces on the system.
2426 NSArray <CWInterface*>* interfaces =
2527 [[CWWiFiClient sharedWiFiClient ] interfaces ];
2628 if (interfaces == nil || [interfaces count ] == 0 ) {
2729 return results;
2830 }
31+
32+ // Var to hold a map of interface names to network names.
33+ NSMutableDictionary * wifiNetworks = [NSMutableDictionary dictionary ];
34+
35+ // To get the network name we need to use the system profiler.
36+ // Since this is a performance hit, only do it if we need to.
37+ if (context.isColumnUsed (" network_name" )) {
38+ // Get Airport data from system profiler, in order to get the current
39+ // network name for systems that don't return it via CWWiFiClient.
40+ NSDictionary * __autoreleasing result;
41+ Status status = getSystemProfilerReport (" SPAirPortDataType" , result);
42+ if (!status.ok ()) {
43+ LOG (ERROR) << " failed to get SPAirPortDataType config: "
44+ << status.getMessage ();
45+ result = [NSDictionary dictionary ];
46+ }
47+
48+ for (NSDictionary * item in [result objectForKey: @" _items" ]) {
49+ // Get the item's airport intefaces, which will usually include at least
50+ // a wifi card and an Apple Wireless Direct Link (AWDL) interface.
51+ NSArray * airportInterfaces =
52+ [item objectForKey: @" spairport_airport_interfaces" ];
53+ // Get the wifi interface (the one starting with "en").
54+ NSDictionary * wifiInterface = [[airportInterfaces
55+ filteredArrayUsingPredicate:
56+ [NSPredicate predicateWithFormat: @" _name BEGINSWITH %@" , @" en" ]]
57+ lastObject ];
58+ // Add the network name to the map, indexed by interface name.
59+ wifiNetworks[[wifiInterface objectForKey: @" _name" ]] = [[wifiInterface
60+ objectForKey: @" spairport_current_network_information" ]
61+ objectForKey: @" _name" ];
62+ }
63+ }
64+
2965 for (CWInterface* interface in interfaces) {
3066 Row r;
3167 r[" interface" ] = std::string ([[interface interfaceName ] UTF8String ]);
@@ -34,10 +70,14 @@ QueryData genWifiStatus(QueryContext& context) {
3470 if (strptr != nil ) {
3571 r[" bssid" ] = std::string ([strptr UTF8String ]);
3672 }
37- strptr = [interface ssid ];
38- if (strptr != nil ) {
39- r[" network_name" ] = std::string ([strptr UTF8String ]);
73+
74+ NSString * networkName =
75+ [wifiNetworks objectForKey: [interface interfaceName ]];
76+ if (networkName == nil ) {
77+ networkName = @" " ;
4078 }
79+ r[" network_name" ] = std::string ([networkName UTF8String ]);
80+
4181 NSString * country_code = [interface countryCode ];
4282 if (country_code != nil ) {
4383 r[" country_code" ] = std::string ([country_code UTF8String ]);
0 commit comments