def send_record(self, event_title, event_url, event_id, keyword_tag): """ 调用邮件发送函数并记录被发送的事件ID 没有返回值 函数内调用sendTextEmail() :param event_id: :param event_url: :param event_title: """ # check_result = self.events_id_check(event_id, self.events_id_list) # if 0 not in check_result: # try: # self.receiver_get(keyword_tag) # self.send_text_email(event_title, event_url, 'securityInfo') # except Exception as e: # error_text = exception_format(get_current_function_name(), e) # print error_text # # self.send_text_email( 'Program Exception' , error_text , 'ExceptionInfo' ) # else: # self.events_id_list.append(event_id) # self.events_id_add(event_id) # else: # print event_title, " Same thing was sent,did not send same mail to everyone" check_result = self.events_id_check(event_id) if not check_result: try: self.receiver_get(keyword_tag) self.send_text_email(event_title, event_url, 'securityInfo') except Exception as e: error_text = exception_format(get_current_function_name(), e) print error_text else: self.events_id_add(event_id) else: print event_title, " Same thing was sent,did not send same mail to everyone"
def key_words_read(self): """ 从文件中读取需要监看的关键字 返回一个json格式的数据 """ print 'key_words_read' if os.path.exists(self.key_file): with open(self.key_file) as keys: tmp = keys.read() try: keywordslist = json.loads(tmp) except (AttributeError, ValueError): error_text = "请检查关键词文件格式是否为正确的json格式" print error_text self.send_text_email("Program Exception", error_text, "ExceptionInfo") # raise AttributeError, ValueError exit(0) except Exception as e: error_text = exception_format(e) print error_text self.send_text_email("Program Exception", error_text, "ExceptionInfo") else: # print keywordslist return keywordslist else: print "关键词监看文件不存在" tmp = open(self.key_file, 'a') tmp.close()
def mail_init(self): """ 初始化邮件设置 返回邮件的参数 """ print "『" + 'mail_init' + "』\n" + "『邮箱初始化开始』" if self.Mail_choose: self.Mail = 'MailOne' else: self.Mail = 'MailTwo' try: self.smtp_server = self.config.get(self.Mail, 'SmtpServer').strip() self.smtp_server_port = self.config.get(self.Mail, "SmtpServer_Port").strip() self.sender = self.config.get(self.Mail, "SenderMail") self.username = self.config.get(self.Mail, "MailName").strip() self.password = self.config.get(self.Mail, "MailPassword").strip() self.receiver_admin = self.address.get('Admin_Address', "ReceiverMail_Admin").split(',') except ConfigParser.NoSectionError: print "*" * 34 print "*" * 10, "邮箱未进行配置", "*" * 10 print "*" * 34 print "*" * 12, "程序退出", "*" * 13 print "*" * 34 exit(0) except Exception as e: error_text = exception_format(e) print error_text else: print "『邮箱配置成功』"
def __init__(self, sender_name): """ sender : 邮件来源名 :type sender_name: wooyun,360或漏洞盒子 """ super(MailCreate, self).__init__() self.mailName = sender_name self.count = 0 # 邮箱认证尝试连接次数,超过3次则更换邮箱 self.Mail = 'MailOne' self.Mail_choose = True self.smtp_server = '0' self.smtp_server_port = '0' self.sender = '0' self.receiver = [] self.receiver_admin = '0' self.username = '******' self.password = '******' try: self.config = ConfigParser.ConfigParser() self.address = ConfigParser.ConfigParser() self.config.read("../Config/mailconfig.ini") self.address.read("../Config/mail_address.ini") except Exception as e: error_text = exception_format(e) print error_text self.mail_init() # mailInit函数只在此处被调用
def send_text_email(self, title, message, message_type): """ 发送文本邮件 没有返回值 函数内调用_format_address() :param message_type: :param message: :param title: """ print 'send_text_email %s ' % title msg = MIMEText(message, 'plain', 'utf-8') # 中文参数‘utf-8’,单字节字符不需要 msg['From'] = self._format_addr(u'%s<%s>' % (unicode(self.mailName, 'utf-8'), self.sender)) msg['Subject'] = Header(title) while 1: try: smtp = smtplib.SMTP() # smtp.set_debuglevel(1) print '开始尝试连接邮箱' smtp.connect(self.smtp_server, self.smtp_server_port) print '成功连接邮箱' print '开始尝试登陆邮箱' smtp.login(self.username, self.password) print '成功登陆邮箱' if message_type == "securityInfo": print '开始发送事件邮件' msg[ 'To' ] = self._format_addr(self.receiver) smtp.sendmail( self.sender , self.receiver , msg.as_string() ) print '成功发送事件邮件' print '成功发送事件邮件' elif message_type == "ExceptionInfo": print '开始发送问题邮件' msg[ 'To' ] = self._format_addr(self.receiver_admin,'Admin') smtp.sendmail( self.sender , self.receiver_admin , msg.as_string() ) print '成功发送问题邮件' elif message_type == "time_report": print '开始发送运行报告邮件' msg[ 'To' ] = self._format_addr(self.receiver_admin,'Admin') smtp.sendmail( self.sender , self.receiver_admin , msg.as_string() ) print '成功发送运行报告邮件' except smtplib.SMTPAuthenticationError: print '认证失败,邮箱连接可能出问题了' self.count += 1 if self.count < 3: time.sleep(10) continue else: print '更换邮箱后重试...' self.Mail_choose = not self.Mail_choose self.mail_init() self.count = 0 continue except Exception as e: error_text = exception_format(e) print error_text time.sleep(10) continue else: smtp.quit() self.count = 0 break
def insert_data(collection, database): try: if isinstance(database, list): collection.insert_many(database) elif isinstance(database, dict): collection.insert_one(database) except Exception as e: error_text = exception_format(e) print error_text
def connect_wooyun(): try: client = pymongo.MongoClient(host, port) except Exception as e: error_text = exception_format(e) print error_text else: db = client.WooYunand360 collection = db.wooyun return collection
def request(self, url, header=None): """ :param header: :param url: :return: """ print 'request' count = 0 while True: try: if count > 3: return None page = requests.get(url=url, headers=header, timeout=30, verify=True) except requests.exceptions.ConnectTimeout: time.sleep(60) count += 1 continue except requests.exceptions.ConnectionError: time.sleep(30) count += 1 continue except requests.exceptions.HTTPError as e: error_text = exception_format(e) count += 1 self.send_text_email('Important Program Exception', error_text, 'ExceptionInfo') time.sleep(600) continue except Exception as e: error_text = exception_format(e) count += 1 print error_text self.send_text_email('Program Exception', error_text, 'ExceptionInfo') continue else: if page.status_code == 200: return page # get page
def file_md5_get(self): """ 获取文件的MD5值 :return: 一个文件的MD5值 """ print 'file_md5_get' try: file_md5 = hashlib.md5() with open(self.key_file) as file_temp: file_md5.update(file_temp.read().strip()) md5temp = file_md5.hexdigest() except Exception as e: error_text = exception_format(e) self.send_text_email("Program Exception", error_text, "ExceptionInfo") else: return md5temp