Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- __author__ = 'mtur'
- import csv
- import os,sys
- import includes.IPsManager as ipmanager
- import xlrd,xlsxwriter
- import xlwt
- import json
- book = xlrd.open_workbook("Data/Assets being scanned in schedules.xlsx")
- sheet = book.sheet_by_index(0)
- scans = {}
- def read_csv(filepath):
- ffile = filepath
- print "\n- Utilizaremos el CSV: " + ffile
- f = open(ffile, 'rt')
- tree = {}
- firmas = {}
- cliente = {}
- try:
- count = 0
- reader = csv.reader(f,delimiter=';')
- for row in reader:
- #print row
- firma = row[0] + ';' + row[1].strip()
- firma = unicode(firma,errors='replace').encode('utf-8',errors='replace')
- firmas[firma] = {"Scans":{},"RangoFirmas":[],"TotalRangoIps":0,"TotalScansIps":0}
- for ipmask in row[2].strip().split('\n'):
- #print ipmask
- count += 1
- try:
- splitter = '/'
- if '\\' in ipmask: splitter = '\\'
- ipmask = ipmask.replace('?','').strip()
- ip = ipmask.strip().split(splitter)[0]
- mask = ipmask.strip().split(splitter)[1]
- #print ip
- first = ip
- last = get_last_IP_From_CIDR_ipmask(first,mask)
- #print last
- lfirst = ipmanager.IPtoLong(first)
- llast = ipmanager.IPtoLong(last)
- firstpartip = ip.split('.')[0] #+ '.' + ip.split('.')[1]
- if not firstpartip in tree:
- tree[firstpartip] = []
- tree[firstpartip].append({'first':lfirst,'last':llast,'id':count, 'firma':firma,'rango':ipmask})
- if(llast-lfirst<0):
- print("ESTE ES NEGATIVOOOO")
- print(firma)
- print("EL SEGUNDO "+str(llast))
- print("EL PRIMER "+str(lfirst))
- print("IP primera "+str(ip))
- print("IP ultima "+str(last))
- print("++++++++++++++++++++++++")
- firmas[firma]['RangoFirmas'].append(ipmask)
- firmas[firma]['TotalRangoIps'] += llast - lfirst
- except Exception, e:
- #print "ERROR: " + str(e)
- #print row
- #print ipmask
- #print "\n-------------\n"
- continue
- arrTempRet = [tree,firmas]
- return arrTempRet
- finally:
- f.close()
- def get_last_IP_From_CIDR(cidr):
- first = unicode(cidr.split('/')[0])
- mask = cidr.split('/')[1]
- num = 0
- for i in range(0,int(mask)):
- num = num + 2**(31-i)
- fmask = ipmanager.int2ip(num)
- lastip = ipmanager.get_last_rang(first,fmask)
- return lastip
- def get_last_IP_From_CIDR_ipmask(first,mask):
- num = 0
- for i in range(0,int(mask)):
- num = num + 2**(31-i)
- fmask = ipmanager.int2ip(num)
- lastip = ipmanager.get_last_rang(first,fmask)
- return lastip
- def calculate_ip(tree,ip):
- #print "IP a analizar: {0}".format(ip)
- lIP = ""
- llIP = ""
- if '/' in ip:
- lIP = ipmanager.IPtoLong(ip.split('/')[0])
- llIP = ipmanager.IPtoLong(get_last_IP_From_CIDR(ip))
- else:
- lIP = ipmanager.IPtoLong(ip)
- llIP = lIP
- fpart = ip.split('.')[0]
- if fpart in tree:
- iprangs = tree[fpart]
- maxRang = -1
- lastGoodRang = {}
- for iprang in iprangs:
- if iprang['first'] <= lIP and iprang['last'] >= llIP:
- if maxRang < 0 or (iprang['last'] - iprang['first']) < maxRang:
- maxRang = iprang['last'] - iprang['first']
- lastGoodRang = iprang
- if maxRang != -1:
- #print "- IP " + ip + ":\n\t- firma:\t" + str(lastGoodRang['firma'])\n\t- Rango:\t" + ipmanager.int2ip(lastGoodRang['first']) + " - " + ipmanager.int2ip(lastGoodRang['last'])
- #comprobacion para rangos
- return str(lastGoodRang['firma'])
- else:
- #print "No se ha encontrado la IP " + ip + " en los direccionamientos del excel."
- return False
- else:
- #print "La IP " + ip + " no esta en el rango de IPs del excel."
- return False
- def introduce_IP():
- print "\n- Introduce the new IP or the CIDR: (Press '0' to exit) \n"
- def matchIP(IP,tree):
- newIP = IP
- try:
- b = True
- if '/' in newIP:
- cip = newIP.split('/')[0]
- b = ipmanager.check_IP_Mask(newIP)
- else: cip = newIP
- if ipmanager.isGoodIP(cip) and b:
- firma = calculate_ip(tree,newIP)
- return firma
- else:
- print "\n- Wrong IP format: {0}".format(newIP)
- if not b: print "- Wrong Mask!"
- # print ipmanager.to_ip(get_last_IP_From_CIDR(newIP))
- except Exception, e:
- print "- Wrong IP Format: {0}".format(newIP)
- print e
- pass
- return False
- def update(d1,d2):
- d3 = {k: (d1[k], v) for k, v in d2.iteritems() if k in d1}
- return d3
- def getTotalIps(primero,ultimo):
- numero1 = primero.split('.')
- numero2 = ultimo.split('.')
- total = int(numero2[len(numero2)-1])-int(numero1[len(numero1)-1])
- return total
- def get_scan_data(firmas,nombretemp,firma,IP,lastIP=None,iprank=None):
- if nombretemp not in firmas[firma]['Scans']:
- firmas[firma]['Scans'][nombretemp] = {}
- firmas[firma]['Scans'][nombretemp]['ips'] = []
- firmas[firma]['Scans'][nombretemp]['totalIpsScan'] = 0
- if not iprank: iprank = IP
- firmas[firma]['Scans'][nombretemp]['ips'].append(iprank)
- totalIps = 1
- if lastIP: totalIps = getTotalIps(IP, lastIP)
- firmas[firma]['Scans'][nombretemp]['totalIpsScan'] += totalIps
- firmas[firma]['TotalScansIps'] += totalIps
- return firmas
- def makeDic(ip,nombretemp,tree,tree2,firmas,firmas2,malasIps):
- firmas = firmas
- if '-' in ip:
- #print ip
- #POR CADA IP QUE HAYA, EN COMAS
- iptemp = ip.split('-')
- firma = matchIP(iptemp[0],tree)
- firmal = matchIP(iptemp[len(iptemp) - 1], tree)
- if firma and firmal:
- if firmal != firma: print "Rango raro...\nrango:\t{0}\nFirma1:\t{1}\nFirma1:\t{2}\nScan:\t{0}\n".format(ip,firma,firmal,nombretemp)
- else: firmas = get_scan_data(firmas,nombretemp, firma, iptemp[0],iptemp[len(iptemp)-1],ip)
- else:
- firma = matchIP(iptemp[0], tree2)
- firmal = matchIP(iptemp[len(iptemp) - 1], tree2)
- if firma and firmal:
- if firmal != firma:
- print "Rango raro...\nrango:\t{0}\nFirma1:\t{1}\nFirma1:\t{2}\nScan:\t{0}\n".format(ip,firma,firmal,nombretemp)
- else:
- firmas2 = get_scan_data(firmas2, nombretemp, firma, iptemp[0], iptemp[len(iptemp) - 1],ip)
- else:
- if nombretemp not in malasIps:
- malasIps[nombretemp] = []
- malasIps[nombretemp].append(ip)
- else:
- firma = matchIP(ip,tree)
- if firma:
- firmas = get_scan_data(firmas, nombretemp, firma, ip)
- else:
- firma = matchIP(ip, tree2)
- if firma: firmas2 = get_scan_data(firmas2, nombretemp, firma, ip)
- else:
- if nombretemp not in malasIps:
- malasIps[nombretemp] = []
- malasIps[nombretemp].append(ip)
- return firmas,firmas2,malasIps
- def create_EXCEL(filename,firmas):
- workbook = xlsxwriter.Workbook(filename)
- bold = workbook.add_format({'bold': 1})
- worksheetS = workbook.add_worksheet('Scans')
- header = ["Firma","RangosFirma","#IPsRango","ScanName","Rangos Scan","#IPsScan"]
- worksheetS.write_row(0,0,header,bold)
- worksheetF = workbook.add_worksheet('Firmas')
- header = ["Firma","RangosFirma","#IPsRango","#IPsScans"]
- worksheetF.write_row(0,0,header,bold)
- rowS = 1
- rowF = 1
- for firma in firmas:
- firnamn = unicode(firma,errors='replace')#.encode('utf-8',errors='replace')
- datosf = firmas[firma]
- ipsRangos = datosf['TotalRangoIps']
- ipsScans = datosf['TotalScansIps']
- rangoFirma = ';'.join(datosf['RangoFirmas'])
- #rangoFirma = rangoFirma.encode('utf-8',errors='replace')
- scans = datosf['Scans']
- worksheetF.write_row(rowF,0, [firnamn,rangoFirma,ipsRangos,ipsScans])
- for scan in scans:
- scann = unicode(scan,errors='replace')#.encode('utf-8',errors='replace')
- rangoScan = ';'.join(scans[scan]['ips'])
- #rangoScan = rangoScan.encode('utf-8',errors='replace')
- ipsScan = scans[scan]['totalIpsScan']
- worksheetS.write_row(rowS,0,[firnamn,rangoFirma,ipsRangos,scann,rangoScan,ipsScan])
- rowS += 1
- rowF += 1
- workbook.close()
- def main():
- dic = {}
- path2 = "Data/RangosPorMemberFirmPrivada.csv"
- path1 = "Data/RangosPorMemberFirmPublica.csv"
- print "Creando arboles de decision:"
- valueTempArr = read_csv(path1)
- valueTempArr2 = read_csv(path2)
- tree = valueTempArr[0]
- tree2 = valueTempArr2[0]
- firmas = valueTempArr[1]
- firmas2 = valueTempArr2[1]
- f = open('treePrivado.json', 'w')
- f.write(json.dumps(tree, indent=2))
- f.close()
- f = open('treePublico.json', 'w')
- f.write(json.dumps(tree2, indent=2))
- f.close()
- print "Arboles creados. Empieza la clasificacion:"
- malasIps = {}
- for q in range(sheet.nrows):
- temp = sheet.cell_value(q,2)
- nombretemp = sheet.cell_value(q,1).encode('utf-8',errors='replace')
- temp = temp.replace(" ","")
- if ',' in temp: #SI TIENE COMAS Y POR LO TANTO MAS DE UNA IP
- for k in temp.split(','):
- if len(k) < 5: continue
- elif 'ranges' in k: continue
- firmas,firmas2,malasIps = makeDic(k,nombretemp,tree,tree2,firmas,firmas2,malasIps)
- #scans = firmas["scans"]
- else: #ELSE DE SI NO TIENE COMAS Y ES UNA SOLA IP O RANGO
- firmas,firmas2,malasIps = makeDic(temp,nombretemp,tree,tree2,firmas,firmas2,malasIps)
- #scans = firmas["scans"]
- f = open('firmasPrivado.json', 'w')
- f.write(json.dumps(firmas, indent=2))
- f.close()
- f = open('firmasPublico.json', 'w')
- f.write(json.dumps(firmas2, indent=2))
- f.close()
- f = open('RangosMalos.json','w')
- f.write(json.dumps(malasIps,indent=2))
- f.close()
- #create_EXCEL('FirmasPrivadas.xlsx', firmas)
- #create_EXCEL('FirmasPublicas.xlsx', firmas2)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement