def insert_data(insert_time, open_ip_ports): try: data = {'time': insert_time, 'data': open_ip_ports} if not mongo[Config.MONGO_C_MASSCAN].find_one({"time": insert_time}): mongo[Config.MONGO_C_MASSCAN].insert_one(data) except Exception as ex: logger.error(ex.message)
def read(self, response): try: if response: return response.read() except urllib2.HTTPError, e: errMsg = e.geturl() + " " + str(e) logger.error(errMsg)
def __accept(self, url): if url is None: errMsg = "url is None !" logger.error(errMsg) raise BloblastNoneDataException(errMsg) elif cmp(url[0:4], "http"): errMsg = "{" + url + "}" + " You must start with (http[s]://)" logger.error(errMsg) raise BloblastDataException(errMsg)
def __ip_sin(self, domain): ip = None try: subdomain, subject, suffix = tldextract.extract(domain) #过滤掉非法域名 if not '*' in subdomain and len(subject) and cmp(subject, 'com') and len(suffix): debMsg = '%s %s %s {%s}'%(subdomain, subject, suffix, domain) logger.debug(debMsg) ip = socket.getaddrinfo(domain,'http')[0][4][0] except Exception,e: errMsg = '%s {%s}'%(e, domain) logger.error(errMsg)
def __ip_bat(self, domain_arry): index = 0 ip_arry = [] for domain in domain_arry: try: (proto, substr, domain, resources, suffix) = separate(domain) ip = socket.getaddrinfo(domain,'http')[0][4][0] ip_arry.append(ip) except Exception,e: ip_arry.append('') errMsg = '%s {%s}'%(e, domain) logger.error(errMsg) index += 1
def loadScript(): path, script_name, suffix = transPathtoImpType(conf.SCRIPT_NAME) try: file, pathname, description = imp.find_module(script_name, [path]) script = imp.load_module('*', file, pathname, description) if not hasattr(script, Requied_function_name): msg = "Can not find the required function: '%s()' in the given script. " % Requied_function_name if suffix != '.py': msg += "Maybe the given script '%s%s' is not a Python file." % ( script_name, suffix) msg += 'Please check and reset.' sys.exit(logger.error(msg)) except ImportError as e: sys.exit(logger.error(str(e)))
def _ip_mask_mode(target): try: ip_list = IPy.IP(target) except Exception as e: sys.exit(logger.error(str(e))) for i in ip_list: print i
def _ip_range_mode(target): start = int(target.strip().split('-')[0].split('.')[-1]) stop = int(target.strip().split('-')[-1]) if start >= stop: msg = "The given IP range: '%s's lower bound IP is greater than upper bound. Please set IP range like this: 192.192.168.1.25-155." % target sys.exit(logger.error(msg)) elif start > 255 or stop > 255: msg = "The given IP range: '%s' is out of the normal range(0~255). Please check and reset." % target sys.exit(logger.error(msg)) body = target.strip().split('-')[0].split('.')[:3] ip_list = [] for num in xrange(start, stop + 1): ip_list.append('.'.join(body) + '.%s' % str(num)) for ip in ip_list: print ip
class Request(): def __init__(self, headers={}, context=None): self.headers = headers self.context = context self.timeout = 5 def __accept(self, url): if url is None: errMsg = "url is None !" logger.error(errMsg) raise BloblastNoneDataException(errMsg) elif cmp(url[0:4], "http"): errMsg = "{" + url + "}" + " You must start with (http[s]://)" logger.error(errMsg) raise BloblastDataException(errMsg) """def get(self, url = None, lamb = 'GET'): self.__accept(url) try: request = urllib2.Request(url.encode('utf-8'), headers = self.headers) request.get_method = lambda: lamb response = urllib2.urlopen(request, timeout = self.timeout, context = self.context) if not cmp(lamb, 'GET') and response.code == 200: logger.info(url + " 200 ok") except urllib2.HTTPError,e: response = e if hasattr(e, 'code'): warnMsg =url + " " + str(e.code) + " failed" logger.warn(warnMsg) else: errMsg = str(e) + " " +url logger.error(errMsg) #logger.exception("Exception Logged"); return response""" @staticmethod def urlencode(data): return urllib.urlencode(data).replace('+', '%20') def get(self, url, data=None): values = Request.urlencode(data) if values: url += '?' + values response = self.connect(url, lamb='GET') if response: if response.code == 200: logger.info(url + " 200 ok") else: logger.info(url + " %d " % response.code) return response def delete(self, url, data=None): values = Request.urlencode(data) if values: url += '?' + values response = self.connect(url, lamb='DELETE') if response: if response.code == 200: logger.info(url + " 200 ok") else: logger.info(url + " %d " % response.code) return response """def post(self, url = None, values = None, lamb = 'POST'): self.__accept(url) response = None data = None if not cmp(self.headers['Content-Type'], 'application/json'): data = json.dumps(values) elif values: data = Request.urlencode(values) try: request = urllib2.Request(url.encode('utf-8'), data, self.headers) request.get_method = lambda: lamb response = urllib2.urlopen(request, timeout = self.timeout, context = self.context) if not cmp(lamb, 'POST') and response.code == 200: logger.info(url + " 200 ok") except urllib2.HTTPError,e: response = e if hasattr(e, 'code'): warnMsg =url + " " + str(e.code) + " failed" logger.warn(warnMsg) else: errMsg = str(e) + " " +url logger.error(errMsg) #logger.exception("Exception Logged"); return response""" def connect(self, url, lamb, values=None): self.__accept(url) response = None data = None #JSON 请求 if values and self.headers.has_key('Content-Type') and not cmp( self.headers['Content-Type'], 'application/json'): data = json.dumps(values) elif values: data = Request.urlencode(values) print data data = Request.urlencode(values) print data try: request = urllib2.Request(url.encode('utf-8'), data, self.headers) request.get_method = lambda: lamb response = urllib2.urlopen(request, timeout=self.timeout, context=self.context) except urllib2.HTTPError, e: response = e """ print e.code print e.reason print e.geturl() print e.read() """ except Exception, e: if hasattr(e, 'code'): warnMsg = url + " " + str(e.code) + " failed" logger.warn(warnMsg) else: errMsg = str(e) + " " + url logger.error(errMsg)