Skip to content

Commit 7dd7308

Browse files
committed
Added WiFi ifindex discovery and enhanced error handling
1 parent cf0f00a commit 7dd7308

File tree

1 file changed

+110
-50
lines changed

1 file changed

+110
-50
lines changed

modules/auxiliary/scanner/snmp/sbg6580_enum.rb

Lines changed: 110 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ def run_host(ip)
4949
"RADIUS Server", "RADIUS Port", "RADIUS Key"
5050
]
5151

52-
output_data = {}
5352
output_data = {"Host IP" => ip}
5453

55-
if snmp.get_value('sysDescr.0').to_s =~ /SBG6580/
54+
sys_descr = snmp.get_value('sysDescr.0')
55+
if is_valid_snmp_value(sys_descr) and sys_descr.to_s =~ /SBG6580/
5656
# print connected status after the first query so if there are
5757
# any timeout or connectivity errors; the code would already
5858
# have jumped to error handling where the error status is
@@ -63,81 +63,114 @@ def run_host(ip)
6363
# using the CableHome cabhPsDevMib MIB module which defines the
6464
# basic management objects for the Portal Services (PS) logical element
6565
# of a CableHome compliant Residential Gateway device
66-
device_ui_selection = snmp.get_value('1.3.6.1.4.1.4491.2.4.1.1.6.1.3.0').to_i
67-
if device_ui_selection == 1
66+
device_ui_selection = snmp.get_value('1.3.6.1.4.1.4491.2.4.1.1.6.1.3.0')
67+
if is_valid_snmp_value(device_ui_selection) and device_ui_selection.to_i == 1
6868
# manufacturerLocal(1) - indicates Portal Services is using the vendor
6969
# web user interface shipped with the device
70-
device_ui_username = snmp.get_value('1.3.6.1.4.1.4491.2.4.1.1.6.1.1.0').to_s
71-
output_data["Username"] = device_ui_username.strip
70+
device_ui_username = snmp.get_value('1.3.6.1.4.1.4491.2.4.1.1.6.1.1.0')
71+
if is_valid_snmp_value(device_ui_username)
72+
output_data["Username"] = device_ui_username.to_s
73+
end
7274

73-
device_ui_password = snmp.get_value('1.3.6.1.4.1.4491.2.4.1.1.6.1.2.0').to_s
74-
output_data["Password"] = device_ui_password.strip
75+
device_ui_password = snmp.get_value('1.3.6.1.4.1.4491.2.4.1.1.6.1.2.0')
76+
if is_valid_snmp_value(device_ui_password)
77+
output_data["Password"] = device_ui_password.to_s
78+
end
7579
end
7680

77-
primary_wifi_state = snmp.get_value('1.3.6.1.2.1.2.2.1.8.32').to_i
78-
if primary_wifi_state != 1
79-
# primary Wifi interface is not up
81+
wifi_ifindex = get_primary_wifi_ifindex(snmp)
82+
if wifi_ifindex < 1
8083
print_status("Primary WiFi is disabled on the device")
8184
end
8285

83-
ssid = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.1.14.1.3.32').to_s
84-
output_data["SSID"] = ssid.strip
86+
ssid = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.1.14.1.3.#{wifi_ifindex}")
87+
if is_valid_snmp_value(ssid)
88+
output_data["SSID"] = ssid.to_s
89+
end
8590

86-
wireless_band = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.1.18.0').to_i
87-
output_data["802.11 Band"] = get_wireless_band_name(wireless_band)
91+
wireless_band = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.1.18.0')
92+
if is_valid_snmp_value(wireless_band)
93+
output_data["802.11 Band"] = get_wireless_band_name(wireless_band.to_i)
94+
end
8895

89-
network_auth_mode = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.1.14.1.5.32').to_i
90-
network_auth_mode_name = get_network_auth_mode_name(network_auth_mode)
91-
output_data["Network Authentication Mode"] = network_auth_mode_name
96+
network_auth_mode = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.1.14.1.5.#{wifi_ifindex}")
97+
if is_valid_snmp_value(network_auth_mode)
98+
network_auth_mode = network_auth_mode.to_i
99+
network_auth_mode_name = get_network_auth_mode_name(network_auth_mode)
100+
output_data["Network Authentication Mode"] = network_auth_mode_name
101+
end
92102

93103
case network_auth_mode
94104
when 1, 6
95105
# WEP, WEP 802.1x Authentication
96-
wep_passphrase = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.1.1.3.32').to_s
97-
output_data["WEP Passphrase"] = wep_passphrase.strip
106+
wep_passphrase = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.1.1.3.#{wifi_ifindex}")
107+
if is_valid_snmp_value(wep_passphrase)
108+
output_data["WEP Passphrase"] = wep_passphrase.to_s
109+
end
98110

99-
wep_encryption = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.1.1.2.32').to_i
100-
wep_encryption_name = "unknown"
101-
wep_key1 = wep_key2 = wep_key3 = wep_key4 = ""
111+
wep_encryption = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.1.1.2.#{wifi_ifindex}")
112+
if is_valid_snmp_value(wep_encryption)
113+
wep_encryption = wep_encryption.to_i
114+
else
115+
wep_encryption = -1
116+
end
117+
118+
wep_encryption_name = "Unknown"
119+
wep_key1 = wep_key2 = wep_key3 = wep_key4 = nil
102120
# get appropriate WEP keys based on wep_encryption setting
103121
if wep_encryption == 1
104122
wep_encryption_name = "64-bit"
105-
wep_key1 = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.2.1.2.32.1')
106-
wep_key2 = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.2.1.2.32.2')
107-
wep_key3 = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.2.1.2.32.3')
108-
wep_key4 = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.2.1.2.32.4')
123+
wep_key1 = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.2.1.2.#{wifi_ifindex}.1")
124+
wep_key2 = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.2.1.2.#{wifi_ifindex}.2")
125+
wep_key3 = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.2.1.2.#{wifi_ifindex}.3")
126+
wep_key4 = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.2.1.2.#{wifi_ifindex}.4")
109127
elsif wep_encryption == 2
110128
wep_encryption_name = "128-bit"
111-
wep_key1 = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.3.1.2.32.1')
112-
wep_key2 = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.3.1.2.32.2')
113-
wep_key3 = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.3.1.2.32.3')
114-
wep_key4 = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.3.1.2.32.4')
129+
wep_key1 = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.3.1.2.#{wifi_ifindex}.1")
130+
wep_key2 = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.3.1.2.#{wifi_ifindex}.2")
131+
wep_key3 = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.3.1.2.#{wifi_ifindex}.3")
132+
wep_key4 = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.3.1.2.#{wifi_ifindex}.4")
115133
end
134+
116135
output_data["WEP Encryption"] = wep_encryption_name
117-
output_data["WEP Key 1"] = wep_key1.unpack('H*')[0]
118-
output_data["WEP Key 2"] = wep_key2.unpack('H*')[0]
119-
output_data["WEP Key 3"] = wep_key3.unpack('H*')[0]
120-
output_data["WEP Key 4"] = wep_key4.unpack('H*')[0]
136+
if is_valid_snmp_value(wep_key1)
137+
output_data["WEP Key 1"] = wep_key1.unpack('H*')[0]
138+
end
139+
if is_valid_snmp_value(wep_key2)
140+
output_data["WEP Key 2"] = wep_key2.unpack('H*')[0]
141+
end
142+
if is_valid_snmp_value(wep_key3)
143+
output_data["WEP Key 3"] = wep_key3.unpack('H*')[0]
144+
end
145+
if is_valid_snmp_value(wep_key4)
146+
output_data["WEP Key 4"] = wep_key4.unpack('H*')[0]
147+
end
121148

122149
# get current network key
123-
current_key = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.1.1.1.32').to_s
124-
output_data["Current Network Key"] = current_key
150+
current_key = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.1.1.1.#{wifi_ifindex}")
151+
if is_valid_snmp_value(current_key)
152+
output_data["Current Network Key"] = current_key.to_s
153+
end
125154

126155
if network_auth_mode == 6
127-
get_radius_info(snmp, output_data)
156+
get_radius_info(snmp, wifi_ifindex, output_data)
128157
end
129158

130159
when 2, 3, 4, 5, 7, 8
131160
# process all flavors of WPA
132-
wpa_encryption = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.4.1.1.32').to_i
133-
output_data["WPA Encryption"] = get_wpa_encryption_name(wpa_encryption)
161+
wpa_encryption = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.4.1.1.#{wifi_ifindex}")
162+
if is_valid_snmp_value(wpa_encryption)
163+
output_data["WPA Encryption"] = get_wpa_encryption_name(wpa_encryption.to_i)
164+
end
134165

135-
wpa_psk = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.4.1.2.32').to_s
136-
output_data["WPA Pre-Shared Key (PSK)"] = wpa_psk.strip
166+
wpa_psk = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.4.1.2.#{wifi_ifindex}")
167+
if is_valid_snmp_value(wpa_psk)
168+
output_data["WPA Pre-Shared Key (PSK)"] = wpa_psk.to_s
169+
end
137170

138171
case network_auth_mode
139172
when 4, 5, 8
140-
get_radius_info(snmp, output_data)
173+
get_radius_info(snmp, wifi_ifindex, output_data)
141174
end
142175
end
143176

@@ -193,6 +226,27 @@ def run_host(ip)
193226
end
194227
end
195228

229+
def get_primary_wifi_ifindex(snmp)
230+
# The ifTable contains interface entries where each row represents
231+
# management information for a particular interface. Locate the first
232+
# interface where ifType is 71 (ieee80211) and ifAdminStatus is 1 (up).
233+
wifi_ifindex = 0
234+
ifTable_columns = ["ifIndex", "ifDescr", "ifType", "ifAdminStatus"]
235+
snmp.walk(ifTable_columns) do |ifIndex, ifDescr, ifType, ifAdminStatus|
236+
if (wifi_ifindex < 1 and ifType.value == 71 and ifAdminStatus.value == 1)
237+
wifi_ifindex = ifIndex.value.to_i
238+
end
239+
end
240+
wifi_ifindex
241+
end
242+
243+
def is_valid_snmp_value(value)
244+
if value.nil? or value.to_s =~ /Null/ or value.to_s =~ /^noSuch/
245+
return false
246+
end
247+
return true
248+
end
249+
196250
def get_network_auth_mode_name(network_auth_mode)
197251
case network_auth_mode
198252
when 0
@@ -240,15 +294,21 @@ def get_wpa_encryption_name(wpa_encryption)
240294
end
241295
end
242296

243-
def get_radius_info(snmp, output_data)
244-
radius_server = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.5.1.2.32')
245-
output_data["RADIUS Server"] = radius_server.unpack("C4").join(".")
297+
def get_radius_info(snmp, wifi_ifindex, output_data)
298+
radius_server = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.5.1.2.#{wifi_ifindex}")
299+
if is_valid_snmp_value(radius_server)
300+
output_data["RADIUS Server"] = radius_server.unpack("C4").join(".")
301+
end
246302

247-
radius_port = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.5.1.3.32').to_s
248-
output_data["RADIUS Port"] = radius_port.strip
303+
radius_port = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.5.1.3.#{wifi_ifindex}")
304+
if is_valid_snmp_value(radius_port)
305+
output_data["RADIUS Port"] = radius_port.to_s.strip
306+
end
249307

250-
radius_key = snmp.get_value('1.3.6.1.4.1.4413.2.2.2.1.5.4.2.5.1.4.32').to_s
251-
output_data["RADIUS Key"] = radius_key.strip
308+
radius_key = snmp.get_value("1.3.6.1.4.1.4413.2.2.2.1.5.4.2.5.1.4.#{wifi_ifindex}")
309+
if is_valid_snmp_value(radius_key)
310+
output_data["RADIUS Key"] = radius_key.to_s
311+
end
252312
end
253313

254314
end

0 commit comments

Comments
 (0)