""" Copyright (c) 2008, Gianluca Lini All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY Gianluca Lini ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Gianluca Lini BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ #!/usr/bin/python import urllib2 import os import re import smtplib class DROP: global netmask netmask = {"/0" : "0.0.0.0","/1" : "128.0.0.0","/2" : "192.0.0.0","/3" : "224.0.0.0","/4" : "240.0.0.0","/5" : "248.0.0.0","/6" : "252.0.0.0","/7" : "254.0.0.0","/8" : "255.0.0.0","/9" : "255.128.0.0","/10" : "255.192.0.0","/11" : "255.224.0.0","/12" : "255.240.0.0","/13" : "255.248.0.0","/14" : "255.252.0.0","/15" : "255.254.0.0","/16" : "255.255.0.0","/17" : "255.255.128.0","/18" : "255.255.192.0","/19" : "255.255.224.0","/20" : "255.255.240.0","/21" : "255.255.248.0","/22" : "255.255.252.0","/23" : "255.255.254.0","/24" : "255.255.255.0","/25" : "255.255.255.128","/26" : "255.255.255.192","/27" : "255.255.255.224","/28" : "255.255.255.240","/29" : "255.255.255.248","/30" : "255.255.255.252","/31" : "255.255.255.254","/32" : "255.255.255.255" } def savefile(self, data): dlist = open("droplist.new", "w") dlist.write(data) dlist.close() return 1 def readfile(self,name): list = open(name, "r") file = list.read() list.close() return file def download(self): try: opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'Mozilla/5.0')] sock = opener.open("http://www.spamhaus.org/drop/drop.lasso") droplist = sock.read() sock.close() self.savefile(droplist) return 1 except: return 0 def savebckup(self, data): dlist = open("droplist.bkp", "w") dlist.write(data) dlist.close() return 1 def prepare(self, data): matcher = re.compile('\d+\.\d+\.\d+\.\d+.\d+') matched = matcher.findall(data) #print matched return matched def route2insert(self, newlist,oldlist): com = filter( lambda x: x in oldlist, newlist) return filter( lambda x: x not in com, newlist) def route2delete(self, newlist,oldlist): com = filter( lambda x: x in oldlist, newlist) return filter( lambda x: x not in com, oldlist) def sendmail(self, to, msg): fromaddr = "sdaa@xxx.it" toaddrs = to subject = "SpamHaus DropList Automated Alert" headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (fromaddr, toaddrs, subject) message = headers + msg server = smtplib.SMTP('localhost') #debug level #server.set_debuglevel(1) server.sendmail(fromaddr, toaddrs, message) server.quit() def sendmailauth(self, to, msg, login, passwd): fromaddr = "sdaa@xxx.it" toaddrs = to subject = "SpamHaus DropList Automated Alert" headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (fromaddr, toaddrs, subject) message = headers + msg server = smtplib.SMTP('localhost') #debug level #server.set_debuglevel(1) server.login(login,passwd) server.sendmail(fromaddr, toaddrs, message) server.quit() def rotteadd(self, data): strrotte = "" for i in data: matcher = re.compile('\/\d+') matched = matcher.search(i) nmask = netmask[matched.group()] matcher = re.compile('\d+\.\d+\.\d+\.\d+') matched = matcher.search(i) strrotte += "ip route " + matched.group() + " " + nmask + " Null0\n" return strrotte def rottedel(self, data): strrotte = "" for i in data: matcher = re.compile('\/\d+') matched = matcher.search(i) nmask = netmask[matched.group()] matcher = re.compile('\d+\.\d+\.\d+\.\d+') matched = matcher.search(i) strrotte += "no ip route " + matched.group() + " " + nmask + " Null0\n" return strrotte null0 = DROP() null0.download() if not os.path.isfile('droplist.bkp'): null0.savebckup("") newlist = null0.prepare(null0.readfile('droplist.new')) oldlist = null0.prepare(null0.readfile('droplist.bkp')) r2in = null0.route2insert(newlist,oldlist) r2del = null0.route2delete(newlist,oldlist) mailbody = null0.rotteadd(r2in) + null0.rottedel(r2del) #print mailbody if not mailbody: mailbody = "Nessuna modifica alla lista" null0.sendmail("yourmail@yourdomain", mailbody) else: null0.sendmail("yourmail@yourdomain", mailbody) null0.savebckup(null0.readfile('droplist.new'))