import ipaddress import json #Para usar Silk #SilkFile object (represents a channel for writing to or reading from Silk flow file) #FGlob object (allows retireval of filenames in a silk data store.) #threshold of the ratio p = 5 flowHash={} otherHash= {} myNum = 0 counter = 0 PATH = '/Users/Sara/Documents/Univ Classes/Investigacion/Programas/netflows.txt' myFile = open(PATH, 'r') ip = myFile.read() flow = json.loads(ip) #Funcion que convierte y devuelve el int ipaddress en notacion punto-decimal def ipConversion(number, position): mystr = '' ipadd = (str(ipaddress.IPv4Address(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 AnalisisReduciendo(sipList, num): for i in flow["flows"]: #itera por cada elemento del diccionario de flows sip = ipConversion(i["sip"], num) #Devuelve el ip en notacion punto-decimal flags = i["tcpflags"].split(",") #array of all tcp flags that are set if num != 0 and ipConversion(i["sip"], num-1) not in sipList: #Si en el paso anterior se vio que no #tiene el length de puertos requerido, se ignora continue else: connection = [0] * 2 #Lista para contener los valores de conecciones fallidas y conecciones buenas if 'A' in flags: #if the acknowledge flag is set connection[1]=1 #good conections else: connection [0] =1 #failed conections if sip in ratioHash: #si sip esta en ratioHash => que posA esta en sampleHash #por lo tanto ya se puede sumar las conecciones al ratio del dip ratioHash[sip][0]+=connection[0] ratioHash[sip][1] += connection[1]] else: #si sip no esta en ratioHash tampoco ratioHash= {sip: [connection[0], connection[1]]} print (ratioHash) return ratioHash while myNum <4: #Se itera las cuatro veces de acuerdo con la notacion de ipv4 sip_hash= AnalisisReduciendo(sipList, myNum) sipList = [] #Se borra el hash para agregar elementos nuevos con la nueva etapa de la busqueda for sip in reduced_sip: #se itera por todos los sip if (ratioHash[sip][0] / ratioHash[sip][1]) < 1: #si la cantidad de succesful #b #g #connections es mas que failed connections #not scanner, ignore elif (ratioHash[sip][0] / ratioHash[sip][1]) < p: #mas failed que succesful, pero no llega al threshold #not scanner, ignore #se debe tener en cuenta que es suspicious pero no taaanto else: #el ratio de failed a succesful llega al threshold pautado #scanner, oh oh sipList.append(sip) myNum += 1 #print (flowHash)