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
def _parse_wireless_settings(html_basic, html_advanced, html_security, html_wep): 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) markup = '<input type=checkbox name="wlanDisabled0" value="ON" checked' settings.set_enabled_status(markup not in html_basic) regex = re.compile('defaultChan\[wlan_idx\]=([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))) regex = re.compile('<input type=text name="ssid0" size=33 ' 'maxlength=32 value="(.+?)"') match_object = regex.search(html_basic) if match_object is None: raise RouterParseError('Cannot determine SSID.') settings.set_ssid(match_object.group(1)) markup = '<input type=radio name="hiddenSSID" value="no"checked>' settings.set_ssid_broadcast_status(markup in html_advanced) if '<option selected value=1>WEP' in html_security: passwords = re.compile('form.key10.value = "(.*?)";').findall(html_wep) # We expect 3 matches: 1) irrelevant 2) wep128 pass 3) wep64 pass if len(passwords) != 3: raise RouterParseError('Wrong number of passwords retrieved:' ' %d' % len(passwords)) key_len_64 = '<input type=radio name="wepKeyLen0" value="wep64" checked>' if key_len_64 in html_security: settings.set_security_type(WirelessSettings.SECURITY_TYPE_WEP64) settings.set_password(passwords[2]) else: settings.set_security_type(WirelessSettings.SECURITY_TYPE_WEP128) settings.set_password(passwords[1]) elif '<option selected value=2>WPA' in html_security: settings.set_security_type(WirelessSettings.SECURITY_TYPE_WPA) elif '<option selected value=4>WPA2' in html_security: settings.set_security_type(WirelessSettings.SECURITY_TYPE_WPA2) else: settings.set_security_type(WirelessSettings.SECURITY_TYPE_NONE) return 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_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(basic, security, security_settings): 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) # Changes take effect immediately without needing to reboot. settings.set_reboot_requirement_status(False) match_obj = re.compile('var enablewireless= "([01])";').search(basic) if match_obj is None: raise RouterParseError('Cannot determine wireless enabled status.') settings.set_enabled_status(match_obj.group(1) == '1') match_obj = re.compile('var channel_index = "(\d+)";').search(basic) if match_obj is None: raise RouterParseError('Cannot determine wireless channel.') settings.set_channel(int(match_obj.group(1))) match_obj = re.compile('var broadcastssidEnable = "([01])";').search(basic) if match_obj is None: raise RouterParseError('Cannot determine broadcast status.') # this is reversed, 0 means enable.. settings.set_ssid_broadcast_status(match_obj.group(1) == '0') security_params = security_settings.strip().split('\r') settings.set_ssid(security_params[0]) sec_type = security_params[2] if sec_type == 'WPA2PSK': security_type = WirelessSettings.SECURITY_TYPE_WPA2 settings.set_password(security_params[13]) elif sec_type == 'WPAPSK': security_type = WirelessSettings.SECURITY_TYPE_WPA settings.set_password(security_params[13]) elif sec_type == 'WEPAUTO': # let's determine wep64 or wep128 by inspecting the first password wep_key_1 = security_params[6] if len(wep_key_1) in (5, 10): # 5 chars is for ASCII passwords, 10 is for HEX security_type = WirelessSettings.SECURITY_TYPE_WEP64 else: security_type = WirelessSettings.SECURITY_TYPE_WEP128 settings.set_password(wep_key_1) else: security_type = WirelessSettings.SECURITY_TYPE_NONE settings.set_security_type(security_type) return 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_main, html_basic, html_advanced, html_security): 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) obj.set_auto_channel_support(False) # the naming of the radio button is weird! is_enabled = '<input type="radio" name="wlanDisabled" value="yes" checked>' in html_main obj.set_enabled_status(is_enabled) match_object = re.compile('document.wlanSetup.ssid.value="(.+?)";').search( html_basic) if match_object is None: raise RouterParseError('Cannot determine SSID') obj.set_ssid(match_object.group(1)) match_object = re.compile('var defaultChan = ([0-9]+)[\s\n]').search( html_basic) if match_object is None: raise RouterParseError('Cannot determine channel') obj.set_channel(int(match_object.group(1))) is_broadcasting_ssid = '<input type="radio" name="hiddenSSID" value="no" checked>' in html_advanced obj.set_ssid_broadcast_status(is_broadcasting_ssid) match_object = re.compile('methodVal = ([0-3]);').search(html_security) if match_object is None: raise RouterParseError('Cannot determine security type') security_type = int(match_object.group(1)) if security_type == 0: obj.set_security_type(obj.__class__.SECURITY_TYPE_NONE) elif security_type == 1: # WEP of some sort match_object = re.compile( 'var wepTbl =\s+new Array\("([0-9]+)"\);').search(html_security) if match_object is None: raise RouterParseError('Cannot determine WEP bit length') if int(match_object.group(1)) in (0, 1): obj.set_security_type(obj.__class__.SECURITY_TYPE_WEP64) else: obj.set_security_type(obj.__class__.SECURITY_TYPE_WEP128) # WEP passwords cannot be extracted! It shows "***" only elif security_type == 2: # WPA of some sort match_object = re.compile( 'var wpaCipherTbl =(?:\s+)?new Array\("([0-9]+)"\);').search( html_security) if match_object is None: raise RouterParseError('Cannot determine WPA type') if int(match_object.group(1)) in (0, 1, 3): # 0, 1 = WPA; 3 = mixed obj.set_security_type(obj.__class__.SECURITY_TYPE_WPA) else: # all other values are WPA2 only obj.set_security_type(obj.__class__.SECURITY_TYPE_WPA2) match_object = re.compile( 'var pskValueTbl = new Array\("(.+?)"\);').search(html_security) if match_object is None: raise RouterParseError('Cannot determine WPA password') obj.set_password(match_object.group(1)) else: # 3 = WPA Radius raise NotImplementedError('Security type not supported') return obj
def _parse_wireless_settings(html_main, html_basic, html_advanced, html_security): 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) obj.set_auto_channel_support(False) # the naming of the radio button is weird! is_enabled = '<input type="radio" name="wlanDisabled" value="yes" checked>' in html_main obj.set_enabled_status(is_enabled) match_object = re.compile('document.wlanSetup.ssid.value="(.+?)";').search(html_basic) if match_object is None: raise RouterParseError('Cannot determine SSID') obj.set_ssid(match_object.group(1)) match_object = re.compile('var defaultChan = ([0-9]+)[\s\n]').search(html_basic) if match_object is None: raise RouterParseError('Cannot determine channel') obj.set_channel(int(match_object.group(1))) is_broadcasting_ssid = '<input type="radio" name="hiddenSSID" value="no" checked>' in html_advanced obj.set_ssid_broadcast_status(is_broadcasting_ssid) match_object = re.compile('methodVal = ([0-3]);').search(html_security) if match_object is None: raise RouterParseError('Cannot determine security type') security_type = int(match_object.group(1)) if security_type == 0: obj.set_security_type(obj.__class__.SECURITY_TYPE_NONE) elif security_type == 1: # WEP of some sort match_object = re.compile('var wepTbl =\s+new Array\("([0-9]+)"\);').search(html_security) if match_object is None: raise RouterParseError('Cannot determine WEP bit length') if int(match_object.group(1)) in (0, 1): obj.set_security_type(obj.__class__.SECURITY_TYPE_WEP64) else: obj.set_security_type(obj.__class__.SECURITY_TYPE_WEP128) # WEP passwords cannot be extracted! It shows "***" only elif security_type == 2: # WPA of some sort match_object = re.compile('var wpaCipherTbl =(?:\s+)?new Array\("([0-9]+)"\);').search(html_security) if match_object is None: raise RouterParseError('Cannot determine WPA type') if int(match_object.group(1)) in (0, 1, 3): # 0, 1 = WPA; 3 = mixed obj.set_security_type(obj.__class__.SECURITY_TYPE_WPA) else: # all other values are WPA2 only obj.set_security_type(obj.__class__.SECURITY_TYPE_WPA2) match_object = re.compile('var pskValueTbl = new Array\("(.+?)"\);').search(html_security) if match_object is None: raise RouterParseError('Cannot determine WPA password') obj.set_password(match_object.group(1)) else: # 3 = WPA Radius raise NotImplementedError('Security type not supported') return obj
def _parse_wireless_settings(html_basic, html_advanced, html_security, html_wep): 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) match_object = re.compile("var wps_ssid_old='(.+?)';").search(html_basic) if match_object is None: raise RouterParseError("Cannot find SSID!") settings.set_ssid(match_object.group(1)) match_object = re.compile("var wps_disabled=(0|1);").search(html_basic) if match_object is None: raise RouterParseError("Cannot determine wireless enabled status!") settings.set_enabled_status(match_object.group(1) == "0") match_object = re.compile("defaultChan\[wlan_idx\]=(.+?);").search( html_basic) if match_object is None: raise RouterParseError("Cannot determine wireless channel!") settings.set_channel(int(match_object.group(1))) if "name=\"hiddenSSID\" value=\"no\"checked" not in html_advanced: settings.set_ssid_broadcast_status(False) # This is the security type (WEP, WPA, WPA2..) match_object = re.compile("var wps_encrypt_old=([0-4]);").search( html_security) if match_object is None: raise RouterParseError("Cannot determine security type!") security_type = int(match_object.group(1)) if security_type == 1: # WEP match_object = re.compile("var wps_wep_keylen_old='([1-2])';").search( html_wep) if match_object is None: raise RouterParseError("Cannot determine WEP key length!") if int(match_object.group(1)) == 1: # 64bit settings.set_security_type(settings.__class__.SECURITY_TYPE_WEP64) else: # 128bit or something new that we don't handle settings.set_security_type(settings.__class__.SECURITY_TYPE_WEP128) elif security_type == 2: # WPA-PSK settings.set_security_type(settings.__class__.SECURITY_TYPE_WPA) elif security_type == 4: # WPA2-PSK settings.set_security_type(settings.__class__.SECURITY_TYPE_WPA2) else: # Either 0=No security or something else, which we don't handle settings.set_security_type(settings.__class__.SECURITY_TYPE_NONE) if settings.security_type_is_wpa: match_object = re.compile("var wps_psk_old='(.+?)';").search( html_security) if match_object is None: raise RouterParseError('Cannot determine wireless password!') settings.set_password(match_object.group(1)) elif settings.security_type_is_wep: # WEP passwords are rendered as '****' on the page settings.set_password(None) else: # No security or something else settings.set_password("") return settings
def parse_wireless_setting(security_type, html): 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.set_auto_channel_support(False) obj.set_reboot_requirement_status(False) regex = re.compile('if \(""==""\) document\.forms\[0\]\.ZN\.value=' 'unescape\("(.+?)"\);') match_object = regex.search(html) if match_object is None: raise RouterParseError("Cannot parse ssid!") ssid = match_object.group(1) obj.set_ssid(ssid) # security_type is either a valid value or 'wep', # in which case we've got to resolve it to WEP64 or WEP128 if security_type == "wep": regex = re.compile("(?:.+?)<OPTION CHECKED>(?:.+?)dw\(wlan\[25\]\)" "(?:.+?)</SCRIPT>", re.DOTALL) if regex.search(html) is not None: security_type = WirelessSettings.SECURITY_TYPE_WEP64 else: security_type = WirelessSettings.SECURITY_TYPE_WEP128 regex = re.compile('<INPUT NAME=ZO0 VALUE="(.+?)" SIZE=26') match_object = regex.search(html) if match_object is None: raise RouterParseError("Cannot parse WEP password!") obj.set_password(match_object.group(1)) elif security_type == WirelessSettings.SECURITY_TYPE_WPA: regex = re.compile('document\.forms\[0\]\.PK\.value=unescape\("(.+?)"\);') match_object = regex.search(html) if match_object is None: raise RouterParseError("Cannot parse WPA password!") obj.set_password(match_object.group(1)) else: # No security obj.set_password("") obj.set_security_type(security_type) regex = re.compile('if\(i==([0-9]+)\)\nsel="SELECTED"') match_object = regex.search(html) if match_object is None: raise RouterParseError("Cannot parse channel!") obj.set_channel(int(match_object.group(1))) # `_esv` is either 0 or 1 and controls SSID broadcasting if "_esv=1" in html: obj.set_ssid_broadcast_status(False) regex = re.compile('wdv0=\("(.+?)"=="(.+?)"\)\?true:false') match_object = regex.search(html) if match_object is None: raise RouterParseError("Cannot parse enabled status!") val1, val2 = match_object.groups() obj.set_enabled_status(val1 == val2) 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): 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_setting(security_type, html): 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.set_auto_channel_support(False) obj.set_reboot_requirement_status(False) regex = re.compile('if \(""==""\) document\.forms\[0\]\.ZN\.value=' 'unescape\("(.+?)"\);') match_object = regex.search(html) if match_object is None: raise RouterParseError('Cannot parse ssid!') ssid = match_object.group(1) obj.set_ssid(ssid) # security_type is either a valid value or 'wep', # in which case we've got to resolve it to WEP64 or WEP128 if security_type == 'wep': regex = re.compile( '(?:.+?)<OPTION CHECKED>(?:.+?)dw\(wlan\[25\]\)' '(?:.+?)</SCRIPT>', re.DOTALL) if regex.search(html) is not None: security_type = WirelessSettings.SECURITY_TYPE_WEP64 else: security_type = WirelessSettings.SECURITY_TYPE_WEP128 regex = re.compile('<INPUT NAME=ZO0 VALUE="(.+?)" SIZE=26') match_object = regex.search(html) if match_object is None: raise RouterParseError('Cannot parse WEP password!') obj.set_password(match_object.group(1)) elif security_type == WirelessSettings.SECURITY_TYPE_WPA: regex = re.compile( 'document\.forms\[0\]\.PK\.value=unescape\("(.+?)"\);') match_object = regex.search(html) if match_object is None: raise RouterParseError('Cannot parse WPA password!') obj.set_password(match_object.group(1)) else: # No security obj.set_password('') obj.set_security_type(security_type) regex = re.compile('if\(i==([0-9]+)\)\nsel="SELECTED"') match_object = regex.search(html) if match_object is None: raise RouterParseError('Cannot parse channel!') obj.set_channel(int(match_object.group(1))) # `_esv` is either 0 or 1 and controls SSID broadcasting if '_esv=1' in html: obj.set_ssid_broadcast_status(False) regex = re.compile('wdv0=\("(.+?)"=="(.+?)"\)\?true:false') match_object = regex.search(html) if match_object is None: raise RouterParseError('Cannot parse enabled status!') val1, val2 = match_object.groups() obj.set_enabled_status(val1 == val2) return obj
def _parse_wireless_settings(html_basic, html_advanced, html_security, html_wep): 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) match_object = re.compile("var wps_ssid_old='(.+?)';").search(html_basic) if match_object is None: raise RouterParseError("Cannot find SSID!") settings.set_ssid(match_object.group(1)) match_object = re.compile("var wps_disabled=(0|1);").search(html_basic) if match_object is None: raise RouterParseError("Cannot determine wireless enabled status!") settings.set_enabled_status(match_object.group(1) == "0") match_object = re.compile("defaultChan\[wlan_idx\]=(.+?);").search(html_basic) if match_object is None: raise RouterParseError("Cannot determine wireless channel!") settings.set_channel(int(match_object.group(1))) if "name=\"hiddenSSID\" value=\"no\"checked" not in html_advanced: settings.set_ssid_broadcast_status(False) # This is the security type (WEP, WPA, WPA2..) match_object = re.compile("var wps_encrypt_old=([0-4]);").search(html_security) if match_object is None: raise RouterParseError("Cannot determine security type!") security_type = int(match_object.group(1)) if security_type == 1: # WEP match_object = re.compile("var wps_wep_keylen_old='([1-2])';").search(html_wep) if match_object is None: raise RouterParseError("Cannot determine WEP key length!") if int(match_object.group(1)) == 1: # 64bit settings.set_security_type(settings.__class__.SECURITY_TYPE_WEP64) else: # 128bit or something new that we don't handle settings.set_security_type(settings.__class__.SECURITY_TYPE_WEP128) elif security_type == 2: # WPA-PSK settings.set_security_type(settings.__class__.SECURITY_TYPE_WPA) elif security_type == 4: # WPA2-PSK settings.set_security_type(settings.__class__.SECURITY_TYPE_WPA2) else: # Either 0=No security or something else, which we don't handle settings.set_security_type(settings.__class__.SECURITY_TYPE_NONE) if settings.security_type_is_wpa: match_object = re.compile("var wps_psk_old='(.+?)';").search(html_security) if match_object is None: raise RouterParseError('Cannot determine wireless password!') settings.set_password(match_object.group(1)) elif settings.security_type_is_wep: # WEP passwords are rendered as '****' on the page settings.set_password(None) else: # No security or something else settings.set_password("") return settings