def load_plugin(self, addr, port, session): url = 'http://' + addr + ':' + str(port) # try to connect routers try: r = HttpHelper.connect(session, url, 1) except Exception: raise ErrorTimeout if 'server' in r.headers: self.server = r.headers['server'] if 'www-authenticate' in r.headers: self.realm = r.headers['www-authenticate'] fingerprint_str = self.server + self.realm + r.content # match vendor for vendor_re in self.VENDOR_RES: vendor_pattern = re.compile(vendor_re[1], re.I) vendor_match = vendor_pattern.search(fingerprint_str) if vendor_match: # match module module_re_list = self.MODULE_RES[vendor_re[0]] for module_re in module_re_list: module_pattern = re.compile(module_re[0], re.S | re.I) vendor_match = module_pattern.search(fingerprint_str) if vendor_match: return module_re, self.server, self.realm, vendor_re[0] return '', self.server, self.realm, vendor_re[0] return '', self.server, self.realm, ''
def upgrade(self): try: self.post_file(self.post_url, self.firmware, 3) except requests.RequestException: self.print_with_lock(self.addr + ': send firmware failed') return else: self.print_with_lock(self.addr + ': send firmware success, then sleep 1 second') time.sleep(1) # send upgrade instruction 3 times to ensure send successfully # TODO: how to check weather upgrade successfully for x in xrange(3): try: self.headers['Referer'] = self.post_url HttpHelper.connect_auth_with_headers(None, self.upgrade_url, 2, (self.username, self.password), self.headers) except ErrorTimeout: pass self.print_with_lock(self.addr + ': send upgrade instruction')
def connect_type_rec(self): jump_url = self.base_url + '/BAS_basic.htm' r = HttpHelper.connect_auth_with_headers(None, jump_url, 3, (self.username, self.password), self.headers) type_re = 'select_basic="(.)"' type_ret = re.compile(type_re, re.I).search(r.content) if type_ret == '1': return 'dyna' elif type_ret == '0': return 'pppoe' else: return 'static'
def get_now_info(self, internet_type): bas_ref = self.base_url + '/BAS_basic.htm' self.headers[b'Referer'] = bas_ref dyna_bas_url = self.base_url + '/BAS_ether.htm' pppoe_bas_url = self.base_url + '' static_bas_url = self.base_url + '' if internet_type == 'static': jump_url = static_bas_url elif internet_type == pppoe_bas_url: jump_url = '' else: jump_url = dyna_bas_url r = HttpHelper.connect_auth_with_headers(None, jump_url, 3, (self.username, self.password), self.headers) return r.content
def dns_set(self, dns): try: self.headers[b'Referer'] = self.base_url self.connect_auth_with_headers(self.base_url, 2) except ErrorTimeout: self.print_with_lock(self.addr + ': connect timout at first try') return try: type = self.connect_type_rec() except ErrorTimeout: self.print_with_lock(self.addr + ': connect timmeout at type recognition') return if self.debug: self.print_with_lock(self.addr + ': type ' + type) try: content = self.get_now_info(type) except ErrorTimeout: self.print_with_lock(self.addr + 'connect timeout at get info') return post_url_re = '<FORM method="POST" action="(.+?)"' post_url = self.base_url + re.compile(post_url_re, re.I).search(content).group(1).replace(' ', '%20') payload = self.__generate_payload(content, dns, type) if self.debug: self.print_with_lock('payload: ' + payload) self.print_with_lock('url: ' + post_url) try: r = HttpHelper.post_auth_with_headers(None, post_url, 3, (self.username, self.password), self.headers, payload) except ErrorTimeout: self.print_with_lock(self.addr + ': connect timeout at post') return self.print_with_lock(self.addr + ': success')
def connect_auth_with_headers(self, url, times): r = HttpHelper.connect_auth_with_headers(self.session, url, times, (self.username, self.password), self.headers) return r
def connect_with_headers(self, url, times): r = HttpHelper.connect_with_headers(self.session, url, times, self.headers) return r
def connect(self, url, times): r = HttpHelper.connect(self.session, url, times) return r
def dns_set(self, dns): url = 'http://' + self.addr + ':' + str( self.port) + self.wan_type_search self.headers['Referer'] = 'http://' + self.addr + ':' + str(self.port) try: r = HttpHelper.connect_auth_with_headers( None, url, 3, (self.username, self.password), self.headers) except ErrorTimeout: self.print_with_lock(self.addr + ': fail, connect timeout') return ref_re = 'location.href="(.+?)"' ref_re_index = 1 ref_pattern = re.compile(ref_re, re.I) match = ref_pattern.search(r.content) if match: wan_url = 'http://' + self.addr + ':' + str( self.port) + match.group(ref_re_index) else: self.print_with_lock(self.addr + ': fail, can not find the wan_url') return if wan_url.find('WanDynamic') > 0: payload = self.__dyna_payload(dns[0], dns[1]) dns_url = wan_url + payload if self.debug: self.print_with_lock(self.addr + ': Wan Dynamic') elif wan_url.find('PPPoE') > 0: payload = self.__ppp_payload(dns[0], dns[1]) dns_url = wan_url.replace('PPPoECfgRpm', 'PPPoECfgAdvRpm') + payload if self.debug: self.print_with_lock(self.addr + ': PPPoE') elif wan_url.find('WanStatic') > 0: try: r = HttpHelper.connect_auth_with_headers( None, 3, (self.username, self.password), self.headers) static_ip_info_re = 'var staticIpInf = new Array.+"(.+?)".{2}"(.+?)".{2}"(.+?)".{2}1500,' static_ip_info_pattern = re.compile(static_ip_info_re, re.I | re.S) static_ip_info_match = static_ip_info_pattern.search(r.content) if not static_ip_info_match: raise ErrorTimeout else: static_ip = static_ip_info_match.group(1) static_mask = static_ip_info_match.group(2) static_gateway = static_ip_info_match.group(3) except ErrorTimeout: self.print_with_lock( self.addr + ': timeout, static ip but can not load the info page') return payload = self.__static_payload(dns[0], dns[1], static_ip, static_mask, static_gateway) dns_url = wan_url + payload if self.debug: self.print_with_lock(self.addr + ': Static IP') else: self.printLock( self.addr + ': fail, can not find the dns change method in this router') return if self.debug: self.print_with_lock(dns_url) try: r = HttpHelper.connect_auth_with_headers( None, dns_url, 3, (self.username, self.password), self.headers) except ErrorTimeout: self.print_with_lock(self.addr + ': maybe fail, no response') else: if r.content.find(dns[0]) > 0: self.print_with_lock(self.addr + ': success') else: self.print_with_lock(self.addr + ': fail, change dns fail')
def dns_set(self, dns): url = 'http://' + self.addr + ':' + str(self.port) + self.wan_type_search self.headers['Referer'] = 'http://' + self.addr + ':' + str(self.port) try: r = HttpHelper.connect_auth_with_headers(None, url, 3, (self.username, self.password), self.headers) except ErrorTimeout: self.print_with_lock(self.addr + ': fail, connect timeout') return ref_re = 'location.href="(.+?)"' ref_re_index = 1 ref_pattern = re.compile(ref_re, re.I) match = ref_pattern.search(r.content) if match: wan_url = 'http://' + self.addr + ':' + str(self.port) + match.group(ref_re_index) else: self.print_with_lock(self.addr + ': fail, can not find the wan_url') return if wan_url.find('WanDynamic') > 0: payload = self.__dyna_payload(dns[0], dns[1]) dns_url = wan_url + payload if self.debug: self.print_with_lock(self.addr + ': Wan Dynamic') elif wan_url.find('PPPoE') > 0: payload = self.__ppp_payload(dns[0], dns[1]) dns_url = wan_url.replace('PPPoECfgRpm', 'PPPoECfgAdvRpm') + payload if self.debug: self.print_with_lock(self.addr + ': PPPoE') elif wan_url.find('WanStatic') > 0: try: r = HttpHelper.connect_auth_with_headers(None, 3, (self.username, self.password), self.headers) static_ip_info_re = 'var staticIpInf = new Array.+"(.+?)".{2}"(.+?)".{2}"(.+?)".{2}1500,' static_ip_info_pattern = re.compile(static_ip_info_re, re.I | re.S) static_ip_info_match = static_ip_info_pattern.search(r.content) if not static_ip_info_match: raise ErrorTimeout else: static_ip = static_ip_info_match.group(1) static_mask = static_ip_info_match.group(2) static_gateway = static_ip_info_match.group(3) except ErrorTimeout: self.print_with_lock(self.addr + ': timeout, static ip but can not load the info page') return payload = self.__static_payload(dns[0], dns[1], static_ip, static_mask, static_gateway) dns_url = wan_url + payload if self.debug: self.print_with_lock(self.addr + ': Static IP') else: self.printLock(self.addr + ': fail, can not find the dns change method in this router') return if self.debug: self.print_with_lock(dns_url) try: r = HttpHelper.connect_auth_with_headers(None, dns_url, 3, (self.username, self.password), self.headers) except ErrorTimeout: self.print_with_lock(self.addr + ': maybe fail, no response') else: if r.content.find(dns[0]) > 0: self.print_with_lock(self.addr + ': success') else: self.print_with_lock(self.addr + ': fail, change dns fail')