def __init__(self): self.listing = Listing() self.whitelisting = WhiteListing() self.rule = Rule() self.asnum = ASNum() self.subnet = Subnet() self.rir = RIR() self.country = Country() self.peering = Peering()
class API: def __init__(self): self.listing = Listing() self.whitelisting = WhiteListing() self.rule = Rule() self.asnum = ASNum() self.subnet = Subnet() self.rir = RIR() self.country = Country() self.peering = Peering() def add(self, data): (result, listing) = self.listing.add( ip=data['ip'], sensor=data['sensor'], sensor_host=data['sensor_host'], reason=data['reason'], reporter=data['username'], ) return (result, str(listing)) def get_rules(self): rules = {} (result, db_rules) = self.rule.all() if result: for r in db_rules: if not rules.has_key(r.sensor.name): rules[r.sensor.name] = [] rules[r.sensor.name].append([r.rule, r.pos_ip, r.pos_reason]) return (True, rules) else: return (False, db_rules) def get_peerings(self): peerings = [] (result, db_peerings) = self.peering.all() if result: for p in db_peerings: peerings.append({ "hostname": p.peer.hostname, "ip": p.peer.ip.ip, "af": p.peer.ip.af, "asnum": p.asnum.asnum, "key": p.key.data, }) return (True, peerings) else: return (False, db_peerings) def get_listings(self): listings = [] (result, db_listings) = self.listing.all() if result: for l in db_listings: listings.append((l.ip.ip, l.ip.last, l.ip.mask, l.ip.af)) (result, rir_listings) = self.rir.filter(listed=True) if result: for rir in rir_listings: (result, rir_subnets) = self.subnet.filter(rir=rir) if result: for s in rir_subnets: listings.append((s.subnet, s.last, s.mask, s.af)) (result, country_listings) = self.country.filter(listed=True) if result: for country in country_listings: (result, country_subnets) = self.subnet.filter(country=country) if result: for s in country_subnets: listings.append((s.subnet, s.last, s.mask, s.af)) (result, provider_listings) = self.asnum.filter(listed=True) if result: for asnum in provider_listings: (result, provider_subnets) = self.subnet.filter(asnum=asnum) if result: for s in provider_subnets: listings.append((s.subnet, s.last, s.mask, s.af)) (result, subnet_listings) = self.subnet.filter(listed=True) if result: for s in subnet_listings: listings.append((s.subnet, s.last, s.mask, s.af)) return (True, list(set(listings))) def get_whitelistings(self): listings = [] (result, db_listings) = self.whitelisting.all() if result: for l in db_listings: listings.append((l.ip.ip, l.ip.last, l.ip.mask, l.ip.af)) (result, rir_listings) = self.rir.filter(whitelisted=True) if result: for rir in rir_listings: (result, rir_subnets) = self.subnet.filter(rir=rir) if result: for s in rir_subnets: listings.append((s.subnet, s.last, s.mask, s.af)) (result, country_listings) = self.country.filter(whitelisted=True) if result: for country in country_listings: (result, country_subnets) = self.subnet.filter(country=country) if result: for s in country_subnets: listings.append((s.subnet, s.last, s.mask, s.af)) (result, provider_listings) = self.asnum.filter(whitelisted=True) if result: for asnum in provider_listings: (result, provider_subnets) = self.subnet.filter(asnum=asnum) if result: for s in provider_subnets: listings.append((s.subnet, s.last, s.mask, s.af)) (result, subnet_listings) = self.subnet.filter(whitelisted=True) if result: for s in subnet_listings: listings.append((s.subnet, s.last, s.mask, s.af)) return (True, list(set(listings)))
from blacklist.middleware.country import Country from blacklist.middleware.rir import RIR from blacklist.middleware.asnum import ASNum from blacklist.middleware.subnet import Subnet from blacklist.middleware.whitelisting import WhiteListing from blacklist.middleware.peering import Peering from blacklist.middleware.rule import Rule from blacklist.middleware.duration import Duration from blacklist.middleware.listing import Listing from blacklist.middleware.historylisting import HistoryListing config = Config() asnum = ASNum() country = Country() rir = RIR() subnet = Subnet() whitelisting = WhiteListing() peering = Peering() rule = Rule() duration = Duration() listing = Listing() historylisting = HistoryListing() def log(msg): stdout.write(msg) stdout.flush() def get_data(item): result = [] fname = "%s/r4ck-blacklist-%s.csv" % (basedir, item) counter = 0
#!/usr/bin/env python import sys import os.path import os sys.path.append("/www/blacklist/app") os.environ["DJANGO_SETTINGS_MODULE"] = "settings" from blacklist.common.search import Search from blacklist.middleware.subnet import Subnet if __name__ == "__main__": s = Search(model=Subnet(), fields={ "subnet": "subnet", "mask": "mask", "last": "last", "provider": "asnum__name", "af": "af", "code": "country__code", "country": "country__name", "rir": "rir__name", "regdate": "regdate", "listed": "listed", "whitelisted": "whitelisted", }) print s.find("subnet=213.154.229.25 or rir=afrinic")
def subnets(request): errmsg = "" listing_changed = False whitelisting_changed = False for item in request.GET.keys(): if item.startswith("id_"): (result, sn) = subnet.get(id=item[3:]) if result: if request.GET["bulk_action"] == "blacklist": sn.listed = not sn.listed listing_changed = True if sn.whitelisted: sn.whitelisted = False whitelisting_changed = True elif request.GET["bulk_action"] == "whitelist": sn.whitelisted = not sn.whitelisted whitelisting_changed = True if sn.listed: sn.listed = False listing_changed = True sn.save() if listing_changed: broker.update_listings() if whitelisting_changed: broker.update_whitelistings() if "q" in request.GET.keys() and len(request.GET["q"]) != 0: s = Search( model=Subnet(), fields={ "subnet": "subnet", "mask": "mask", "last": "last", "provider": "asnum__name", "af": "af", "code": "country__code", "country": "country__name", "rir": "rir__name", "regdate": "regdate", "listed": "listed", "whitelisted": "whitelisted", } ) (result, subnet_data) = s.find(request.GET["q"]) if not result: errmsg = subnet_data (result, subnet_data) = subnet.all() else: (result, subnet_data) = subnet.all() if not result: return HttpResponseRedirect("/") try: page = int(request.GET.get('page', '1')) except: page = 1 subnet_table = SubnetTable( subnet_data, order_by=request.GET.get('sort', 'title') ) if request.method == "GET": search_form = SearchForm(request.GET) else: search_form = SearchForm() return render_to_response("registry/subnet/overview.html", { "errmsg": errmsg, "search_form": search_form, "subnet_table": subnet_table, }, context_instance=RequestContext(request))