def _parse_wireless_settings(html): regex = '//\s+nvram = \{(.+?)\};\n\nxob = ' match_object = re.compile(regex, re.DOTALL).search(html) if match_object is None: raise RouterParseError('Cannot parse wireless settings') array = _parse_data_structure(match_object.group(1)) try: settings = WirelessSettings() settings.add_security_support(WirelessSettings.SECURITY_TYPE_WEP64) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WEP128) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WPA) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WPA2) settings.set_reboot_requirement_status(False) settings.set_auto_channel_support(False) settings.set_ascii_wep_password_support_status(False) # Let's preserve all the settings, so that it's # easier to generate the data later settings.set_internal_param('nvram', array) sec_type = array['security_mode2'] settings.set_security_type(WirelessSettings.SECURITY_TYPE_NONE) settings.set_password('') if sec_type == 'wep': if array['wl_wep_bit'] == '64': settings.set_security_type( WirelessSettings.SECURITY_TYPE_WEP64) else: settings.set_security_type( WirelessSettings.SECURITY_TYPE_WEP128) settings.set_password(array['wl_key1']) elif sec_type == 'wpa_personal': settings.set_security_type(WirelessSettings.SECURITY_TYPE_WPA) settings.set_password(array['wl_wpa_psk']) elif sec_type == 'wpa2_personal': settings.set_security_type(WirelessSettings.SECURITY_TYPE_WPA2) settings.set_password(array['wl_wpa_psk']) settings.set_ssid(array['wl_ssid']) settings.set_channel(array['wl_channel']) settings.set_ssid_broadcast_status(array['wl_closed'] == '0') settings.set_enabled_status(array['wl_radio'] == '1') return settings except (KeyError, ValueError): raise RouterParseError('Bad nvram for wireless settings')
def _parse_wireless_settings_WR340G(html_settings): """Extracts the Wireless settings from the page contents for a WR340G router.""" obj = WirelessSettings() obj.add_security_support(WirelessSettings.SECURITY_TYPE_WEP64) obj.add_security_support(WirelessSettings.SECURITY_TYPE_WEP128) obj.add_security_support(WirelessSettings.SECURITY_TYPE_WPA) obj.add_security_support(WirelessSettings.SECURITY_TYPE_WPA2) settings_array = _extract_js_array_data(html_settings, 'wlanPara') wlan_list_array = _extract_js_array_data(html_settings, 'wlanList') security_type = int(settings_array[18]) if security_type == 1: # WEP of some sort bit_length = int(wlan_list_array[1]) if bit_length == 13: # 128 bit length security_type = WirelessSettings.SECURITY_TYPE_WEP128 else: security_type = WirelessSettings.SECURITY_TYPE_WEP64 elif security_type == 3: # WPA-PSK (WPA or WPA2) # string like '331', '332', '333' # we're interested in the 3rd char, which deals with WPA-PSK/WPA2-PSK # 3rd char possible values {1: WPA, 2: WPA2, 3: Automatic (WPA + WPA2)} security_options = settings_array[19] if int(security_options[2]) == 1: security_type = WirelessSettings.SECURITY_TYPE_WPA else: security_type = WirelessSettings.SECURITY_TYPE_WPA2 else: # type is either 0 (no security) or 2 (WPA-Enterprise) security_type = WirelessSettings.SECURITY_TYPE_NONE obj.set_security_type(security_type) password = wlan_list_array[0] if obj.security_type_is_wep else settings_array[26] obj.set_password(password) obj.set_ssid(settings_array[2]) obj.set_enabled_status(settings_array[8] != 0) obj.set_ssid_broadcast_status(settings_array[9] == 1) # We don't need to reboot manually.. # The router reboots by itself when settings are pushed. obj.set_reboot_requirement_status(False) obj.set_channel(settings_array[6]) obj.set_internal_param('region', settings_array[4]) obj.set_internal_param('mode', settings_array[7]) return obj
def _parse_wireless_settings(html): regex = '//\s+nvram = \{(.+?)\};\n\nxob = ' match_object = re.compile(regex, re.DOTALL).search(html) if match_object is None: raise RouterParseError('Cannot parse wireless settings') array = _parse_data_structure(match_object.group(1)) try: settings = WirelessSettings() settings.add_security_support(WirelessSettings.SECURITY_TYPE_WEP64) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WEP128) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WPA) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WPA2) settings.set_reboot_requirement_status(False) settings.set_auto_channel_support(False) settings.set_ascii_wep_password_support_status(False) # Let's preserve all the settings, so that it's # easier to generate the data later settings.set_internal_param('nvram', array) sec_type = array['security_mode2'] settings.set_security_type(WirelessSettings.SECURITY_TYPE_NONE) settings.set_password('') if sec_type == 'wep': if array['wl_wep_bit'] == '64': settings.set_security_type(WirelessSettings.SECURITY_TYPE_WEP64) else: settings.set_security_type(WirelessSettings.SECURITY_TYPE_WEP128) settings.set_password(array['wl_key1']) elif sec_type == 'wpa_personal': settings.set_security_type(WirelessSettings.SECURITY_TYPE_WPA) settings.set_password(array['wl_wpa_psk']) elif sec_type == 'wpa2_personal': settings.set_security_type(WirelessSettings.SECURITY_TYPE_WPA2) settings.set_password(array['wl_wpa_psk']) settings.set_ssid(array['wl_ssid']) settings.set_channel(array['wl_channel']) settings.set_ssid_broadcast_status(array['wl_closed'] == '0') settings.set_enabled_status(array['wl_radio'] == '1') return settings except (KeyError, ValueError): raise RouterParseError('Bad nvram for wireless settings')
def _parse_wireless_settings(html_settings, html_security): """Extracts the Wireless settings from the page contents for a WR740N (and some other models).""" obj = WirelessSettings() obj.add_security_support(WirelessSettings.SECURITY_TYPE_WEP64) obj.add_security_support(WirelessSettings.SECURITY_TYPE_WEP128) obj.add_security_support(WirelessSettings.SECURITY_TYPE_WPA) obj.add_security_support(WirelessSettings.SECURITY_TYPE_WPA2) settings_array = _extract_js_array_data(html_settings, 'wlanPara') obj.set_ssid(settings_array[3]) obj.set_enabled_status(settings_array[8] == 1) obj.set_ssid_broadcast_status(settings_array[9] == 1) obj.set_channel(settings_array[10]) obj.set_internal_param('region', settings_array[5]) obj.set_internal_param('mode', settings_array[7]) obj.set_internal_param('chanWidth', settings_array[11]) obj.set_internal_param('rate', settings_array[12]) security_array = _extract_js_array_data(html_security, 'wlanPara') wlan_list_array = _extract_js_array_data(html_security, 'wlanList') security_type = int(security_array[2]) if security_type == 1: # WEP of some sort bit_length = int(wlan_list_array[1]) if bit_length == 13: # 128 bit length security_type = WirelessSettings.SECURITY_TYPE_WEP128 else: security_type = WirelessSettings.SECURITY_TYPE_WEP64 elif security_type == 3: # WPA-PSK (WPA or WPA2) # string like '331', '332', '333' # we're interested in the 3rd char, which deals with WPA-PSK/WPA2-PSK # 3rd char possible values {1: WPA, 2: WPA2, 3: Automatic (WPA + WPA2)} security_options = security_array[3] if int(security_options[2]) == 1: security_type = WirelessSettings.SECURITY_TYPE_WPA else: security_type = WirelessSettings.SECURITY_TYPE_WPA2 else: # type is either 0 (no security) or 2 (WPA-Enterprise) security_type = WirelessSettings.SECURITY_TYPE_NONE obj.set_security_type(security_type) password = wlan_list_array[ 0] if obj.security_type_is_wep else security_array[9] obj.set_password(password) return obj
def _parse_wireless_settings(html_settings, html_security): """Extracts the Wireless settings from the page contents for a WR740N (and some other models).""" obj = WirelessSettings() obj.add_security_support(WirelessSettings.SECURITY_TYPE_WEP64) obj.add_security_support(WirelessSettings.SECURITY_TYPE_WEP128) obj.add_security_support(WirelessSettings.SECURITY_TYPE_WPA) obj.add_security_support(WirelessSettings.SECURITY_TYPE_WPA2) settings_array = _extract_js_array_data(html_settings, "wlanPara") obj.set_ssid(settings_array[3]) obj.set_enabled_status(settings_array[8] == 1) obj.set_ssid_broadcast_status(settings_array[9] == 1) obj.set_channel(settings_array[10]) obj.set_internal_param("region", settings_array[5]) obj.set_internal_param("mode", settings_array[7]) obj.set_internal_param("chanWidth", settings_array[11]) obj.set_internal_param("rate", settings_array[12]) security_array = _extract_js_array_data(html_security, "wlanPara") wlan_list_array = _extract_js_array_data(html_security, "wlanList") security_type = int(security_array[2]) if security_type == 1: # WEP of some sort bit_length = int(wlan_list_array[1]) if bit_length == 13: # 128 bit length security_type = WirelessSettings.SECURITY_TYPE_WEP128 else: security_type = WirelessSettings.SECURITY_TYPE_WEP64 elif security_type == 3: # WPA-PSK (WPA or WPA2) # string like '331', '332', '333' # we're interested in the 3rd char, which deals with WPA-PSK/WPA2-PSK # 3rd char possible values {1: WPA, 2: WPA2, 3: Automatic (WPA + WPA2)} security_options = security_array[3] if int(security_options[2]) == 1: security_type = WirelessSettings.SECURITY_TYPE_WPA else: security_type = WirelessSettings.SECURITY_TYPE_WPA2 else: # type is either 0 (no security) or 2 (WPA-Enterprise) security_type = WirelessSettings.SECURITY_TYPE_NONE obj.set_security_type(security_type) password = wlan_list_array[0] if obj.security_type_is_wep else security_array[9] obj.set_password(password) return obj
def _parse_wireless_settings(security_type, html_basic, html_advanced): settings = WirelessSettings() settings.add_security_support(WirelessSettings.SECURITY_TYPE_WEP64) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WEP128) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WPA) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WPA2) settings.set_ascii_wep_password_support_status(False) settings.set_reboot_requirement_status(False) # Determine the submit token.. some WGR614v9 models have such a token # It's either some form of CSRF protection or something else.. match_object = re.compile( '<form method="POST" action="wireless.cgi\?id=([0-9]+)">').search( html_basic) if match_object is None: settings.set_internal_param('submit_token', None) else: settings.set_internal_param('submit_token', int(match_object.group(1))) if security_type == 'WEP': if '<option selected value="1">64bit</option>' in html_basic: settings.set_security_type(WirelessSettings.SECURITY_TYPE_WEP64) elif '<option selected value="2">128bit</option>' in html_basic: settings.set_security_type(WirelessSettings.SECURITY_TYPE_WEP128) else: raise RouterParseError('Cannot determine WEP key length') settings.set_password(__parse_wep_password(html_basic)) elif security_type == 'WPA-PSK': settings.set_security_type(WirelessSettings.SECURITY_TYPE_WPA) # password extraction is done below elif security_type in ('WPA2-PSK', 'WPA-AUTO-PSK'): settings.set_security_type(WirelessSettings.SECURITY_TYPE_WPA2) # password extraction is done below else: # security_type = `Disable` or something else that we don't handle.. settings.set_security_type(WirelessSettings.SECURITY_TYPE_NONE) settings.set_password('') if settings.security_type_is_wpa: regex = re.compile( '<input type="text" name="passphrase" size=20 maxLength=64 value="(.+?)" onFocus' ) match_object = regex.search(html_basic) if match_object is None: raise RouterParseError('Cannot determine WPA password') password = match_object.group(1) if '*****' in password: # WGR614v7 doesn't present us with the real password, but substitutes it with * chars # that's not the case for v8 and v9 though password = None settings.set_password(password) regex = re.compile('<input type="text" name="ssid" value="(.+?)"') match_object = regex.search(html_basic) if match_object is None: raise RouterParseError('Cannot determine SSID') settings.set_ssid(match_object.group(1)) regex = re.compile( '<input type="hidden" name="initChannel" value="([0-9]+)">') match_object = regex.search(html_basic) if match_object is None: raise RouterParseError('Cannot determine channel') settings.set_channel(int(match_object.group(1))) if '<input type="checkbox" checked name="enable_ap" value="enable_ap">' in html_advanced: is_enabled = True else: is_enabled = False settings.set_enabled_status(is_enabled) if '<input type="checkbox" checked name="ssid_bc" value="ssid_bc">' in html_advanced: is_broadcasting = True else: is_broadcasting = False settings.set_ssid_broadcast_status(is_broadcasting) if '<input type="checkbox" checked name="enable_wmm" value="enable_wmm">' in html_advanced: is_wmm_enabled = True else: is_wmm_enabled = False settings.set_internal_param('enable_wmm', is_wmm_enabled) regex = re.compile( "Select Region(?:.+?)<option selected value=\"([0-9]+)\">") match_object = regex.search(html_basic) if match_object is None: raise RouterParseError('Cannot determine Region value') settings.set_internal_param('WRegion', int(match_object.group(1))) return settings
def _parse_wireless_settings(security_type, html_basic, html_advanced): settings = WirelessSettings() settings.add_security_support(WirelessSettings.SECURITY_TYPE_WEP64) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WEP128) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WPA) settings.add_security_support(WirelessSettings.SECURITY_TYPE_WPA2) settings.set_ascii_wep_password_support_status(False) settings.set_reboot_requirement_status(False) # Determine the submit token.. some WGR614v9 models have such a token # It's either some form of CSRF protection or something else.. match_object = re.compile('<form method="POST" action="wireless.cgi\?id=([0-9]+)">').search(html_basic) if match_object is None: settings.set_internal_param('submit_token', None) else: settings.set_internal_param('submit_token', int(match_object.group(1))) if security_type == 'WEP': if '<option selected value="1">64bit</option>' in html_basic: settings.set_security_type(WirelessSettings.SECURITY_TYPE_WEP64) elif '<option selected value="2">128bit</option>' in html_basic: settings.set_security_type(WirelessSettings.SECURITY_TYPE_WEP128) else: raise RouterParseError('Cannot determine WEP key length') settings.set_password(__parse_wep_password(html_basic)) elif security_type == 'WPA-PSK': settings.set_security_type(WirelessSettings.SECURITY_TYPE_WPA) # password extraction is done below elif security_type in ('WPA2-PSK', 'WPA-AUTO-PSK'): settings.set_security_type(WirelessSettings.SECURITY_TYPE_WPA2) # password extraction is done below else: # security_type = `Disable` or something else that we don't handle.. settings.set_security_type(WirelessSettings.SECURITY_TYPE_NONE) settings.set_password('') if settings.security_type_is_wpa: regex = re.compile('<input type="text" name="passphrase" size=20 maxLength=64 value="(.+?)" onFocus') match_object = regex.search(html_basic) if match_object is None: raise RouterParseError('Cannot determine WPA password') password = match_object.group(1) if '*****' in password: # WGR614v7 doesn't present us with the real password, but substitutes it with * chars # that's not the case for v8 and v9 though password = None settings.set_password(password) regex = re.compile('<input type="text" name="ssid" value="(.+?)"') match_object = regex.search(html_basic) if match_object is None: raise RouterParseError('Cannot determine SSID') settings.set_ssid(match_object.group(1)) regex = re.compile('<input type="hidden" name="initChannel" value="([0-9]+)">') match_object = regex.search(html_basic) if match_object is None: raise RouterParseError('Cannot determine channel') settings.set_channel(int(match_object.group(1))) if '<input type="checkbox" checked name="enable_ap" value="enable_ap">' in html_advanced: is_enabled = True else: is_enabled = False settings.set_enabled_status(is_enabled) if '<input type="checkbox" checked name="ssid_bc" value="ssid_bc">' in html_advanced: is_broadcasting = True else: is_broadcasting = False settings.set_ssid_broadcast_status(is_broadcasting) if '<input type="checkbox" checked name="enable_wmm" value="enable_wmm">' in html_advanced: is_wmm_enabled = True else: is_wmm_enabled = False settings.set_internal_param('enable_wmm', is_wmm_enabled) regex = re.compile("Select Region(?:.+?)<option selected value=\"([0-9]+)\">") match_object = regex.search(html_basic) if match_object is None: raise RouterParseError('Cannot determine Region value') settings.set_internal_param('WRegion', int(match_object.group(1))) return settings