#Itera por sip /16 y cuenta numero de puertos por cada dip from silk import * startDate = "2009/04/20" endDate = "2009/04/22" #Para filtrar por puertos. Pero no queremos todavia #minPort = 20 #maxPort = 5000 def ipConversion(number, position): mystr = '' ipadd = number.split(".") #Devuelve un arreglo for i in range(position+1): if i ==position: mystr = mystr + ipadd[i] else: mystr = mystr + ipadd[i] + '.' return mystr #devuelve los numeros en notacion string def PrimerAnalisis(flowHash, num): dportHash={} flow_Counter=0 for filename in FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/data/silk.conf", data_rootdir="/data"): for rec in silkfile_open(filename, READ):#reading the flow file if (':' in str(rec.sip)) or (num != 0 and ipConversion(str(rec.sip), num-1) not in flowHash): #Si en el paso anterior se vio que no #tiene el length de puertos requerido, se ignora continue else: #agrega a un hash cada puerto con un counter de sus destination ips dip = str(rec.dip) sip = ipConversion(str(rec.sip), num) dport= rec.dport if sip in dportHash: if dip in dportHash[sip]: dportHash[sip][dip] += 1 else: dportHash[sip][dip] = 1 else: dportHash[sip] = { dip: 1 } #print flow_Counter return dportHash def UltimoAnalisis(flowHash, num): dportHash={} flow_Counter=0 for filename in FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/data/silk.conf", data_rootdir="/data"): for rec in silkfile_open(filename, READ):#reading the flow file if (':' in str(rec.sip)) or (num != 0 and ipConversion(str(rec.sip), num-1) not in flowHash): #Si en el paso anterior se vio que no #tiene el length de puertos requerido, se ignora continue else: #agrega a un hash cada puerto con un counter de sus destination ips dip = ipConversion(str(rec.dip), num) sip = ipConversion(str(rec.sip), num) dport= rec.dport if sip in dportHash: if dip in dportHash[sip]: dportHash[sip][dip].append(dport) else: dportHash[sip][dip] = [dport] else: dportHash[sip] = { dip: [dport] } #print flow_Counter return dportHash #main myNum = 0 otherHash = {} while myNum <3: #Se itera las cuatro veces de acuerdo con la notacion de ipv4 flowHash= PrimerAnalisis(otherHash, myNum) otherHash = {} #Se borra el hash para agregar elementos nuevos con la nueva etapa de la busqueda for sips in flowHash: #se itera por todos los dip y sus counters o puertos for dips, dports in flowHash[sips].items(): if dports >= 100: #si la cantidad de puertos es mayor o igual a 100, nos interesan #y por lo tanto se guardan en un hash if sips in otherHash: otherHash[sips][dips] = dports else: otherHash[sips] = {dips: dports} myNum += 1 #print (flowHash) #print otherHash #Ultimo chequeo, utilizando el ip completo counter = 0 flowHash = UltimoAnalisis(otherHash, myNum) otherHash = {} #Se borra el hash para agregar elementos nuevos con la nueva etapa de la busqueda for sips in flowHash: #se itera por todos los dip y sus counters o puertos for dips, dports in flowHash[sips].items(): if len(dports) >= 100: #si la cantidad de puertos es mayor o igual a 100, nos interesan #y por lo tanto se guardan en un hash if sips in otherHash: otherHash[sips][dips] = dports else: otherHash[sips] = {dips: dports} for dips, dports in otherHash.items(): counter +=1 #para contar los elementos del hash print counter