Skip to navigation
Script to extract all emails,domains,IP its MX record domains with IPS from email you send to.
07.12.13
script to extract all emails,domains,IP its MX record domains with IPS from email you send to. simply extract all this from the mail logs and create a postfix sender_access list marked as OK import os,sys import re import dns.resolver import socket import gzip from subprocess import call class Autopostwhite(): def __init__(self): self.maxlines = 10 self.save_file = 'sender_access' #self.save_file = '/etc/postfix/sender_access' path = "/var/log/" lines = self.get_text(path) emails = self.get_emails(lines) domain_ips = self.set_domain_ip(emails) email_domains = domain_ips[0] email_ips = domain_ips[1] mx_domains = self.get_mx(email_domains) mx_ips = self.set_mx_ip(mx_domains) self.save_whitelist(emails,email_domains,email_ips,mx_domains,mx_ips) self.create_postfix_db() def create_postfix_db(self): if(os.path.isfile(self.save_file)): call(["postmap", self.save_file]) def get_text(self,path): lines = [] for f in os.listdir(path): if(f.startswith('mail.log')): if(f.endswith('gz')): fz = gzip.open(path + f,'rb') lines += fz.readlines() else: fs = open(path + f, 'r') lines += fs.readlines() return lines def save_whitelist(self,emails,email_domains,email_ips,mx_domains,mx_ips): l = [] l.extend(emails) l.extend(email_domains) l.extend(mx_domains) l.extend(email_ips) l.extend(mx_ips) l = sorted(list(set(l))) s = '' if(os.path.isfile('reject_list.txt')): rf = open('reject_list.txt', 'r') rl = rf.readlines() for i in rl: entry = i.strip() if(entry): if(entry in l): l.remove(entry) s += str(entry) + " REJECT\n" for i in l: s += str(i) + " OK\n" f = open(self.save_file, 'w') f.write(s) f.close() def set_mx_ip(self,m): mxs = [] for i in m: try: ip = socket.gethostbyname(i) mxs.append(ip) except IOError, error: pass return mxs def get_mx(self,e): mxs = [] for i in e: answers = dns.resolver.query(i, 'MX') for rdata in answers: mxs.append(str(rdata.exchange)) return sorted(list(set(mxs))) def set_domain_ip(self,e): domains = [] ips = [] for i in e: a = i.split('@') try: ip = socket.gethostbyname(a[1]) domains.append(a[1]) ips.append(ip) except IOError, error: pass return [sorted(list(set(domains))),sorted(list(set(ips)))] def get_emails(self,lines): emails= [] x = 0 for l in lines: m1 = re.search(': to=<',l) m2 = re.search('postfix/smtp',l) if(m1 and m2): m3 = re.search(r'\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+',l) if(m3): email = m3.group().lower() emails.append(email) x +=1; if(x > self.maxlines): break return sorted(list(set(emails))) if __name__ == "__main__": a = Autopostwhite()
http://www.postfix.org/
Reply
Anonymous
below code does not work this one worked for me: import os,sys import re import dns.resolver import socket import gzip from subprocess import call class Autopostwhite(): def __init__(self): self.maxlines = 2000000 self.save_file = 'sender_access' #self.save_file = '/etc/postfix/sender_access' path = "/var/log/" lines = self.get_text(path) emails = self.get_emails(lines) domain_ips = self.set_domain_ip(emails) email_domains = domain_ips[0] email_ips = domain_ips[1] emails = domain_ips[2] mx_domains = self.get_mx(email_domains) mx_ips = self.get_mx_ip(mx_domains) self.save_whitelist(emails,email_domains,email_ips,mx_domains,mx_ips) self.create_postfix_db() def create_postfix_db(self): if(os.path.isfile(self.save_file)): call(["postmap", self.save_file]) def get_text(self,path): lines = [] for f in os.listdir(path): if(f.startswith('mail.log')): if(f.endswith('gz')): fz = gzip.open(path + f,'rb') lines.extend(fz.readlines()) else: fs = open(path + f, 'r') lines.extend(fs.readlines()) return lines def save_whitelist(self,emails,email_domains,email_ips,mx_domains,mx_ips): l = [] l.extend(emails) l.extend(email_domains) l.extend(email_ips) l.extend(mx_domains) l.extend(mx_ips) l = list(set(l)) s = '' if(os.path.isfile('reject_list.txt')): rf = open('reject_list.txt', 'r') rl = rf.readlines() for i in rl: entry = i.strip() if(entry): if(entry in l): l.remove(entry) s += str(entry) + " REJECT\n" l = set(l) for i in l: s += str(i) + " OK\n" f = open(self.save_file, 'w') f.write(s) f.close() def get_mx_ip(self,m): mxs = [] for i in m: try: ip = socket.gethostbyname(i) mxs.append(ip) except IOError, error: pass return sorted(list(set(mxs))) def get_mx(self,e): mxs = [] for i in e: try: answers = dns.resolver.query(i, 'MX') for rdata in answers: mxs.append(str(rdata.exchange)[:-1].lower()) except (dns.resolver.NoAnswer) as e: pass domains = list(set(mxs)) return sorted(domains) def set_domain_ip(self,e): domains = [] ips = [] emails = [] for i in e: a = i.split('@') try: ip = socket.gethostbyname(a[1]) domains.append(a[1].lower()) ips.append(ip) emails.append(i.lower()) except IOError, error: pass domains = list(set(domains)) ips = list(set(ips)) emails = list(set(emails)) return [sorted(domains),sorted(ips),sorted(emails)] def get_emails(self,lines): emails= [] x = 0 for l in lines: m1 = re.search(': to=<',l) m2 = re.search('postfix/smtp',l) if(m1 and m2): m3 = re.search(r'\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+',l) if(m3): email = m3.group().lower() emails.append(email) x +=1; if(x > self.maxlines): break return sorted(list(set(emails))) if __name__ == "__main__": a = Autopostwhite()
07.12.13
Reply
Anonymous
Information Epoch 1732521754
ACID (Atomicity, Consistency, Isolation, and Durability)
Home
Notebook
Contact us