Source Code for network and port scanner, TRW algorithm, and reduction method implementations.

trw.py 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import ipaddress
  2. import json
  3. #Para usar Silk
  4. #SilkFile object (represents a channel for writing to or reading from Silk flow file)
  5. #FGlob object (allows retireval of filenames in a silk data store.)
  6. minPort = 20
  7. maxPort= 60000
  8. flowHash={}
  9. otherHash= {}
  10. myNum = 0
  11. counter = 0
  12. PATH = '/Users/Sara/Documents/Univ Classes/Investigacion/netflows.txt'
  13. myFile = open(PATH, 'r')
  14. ip = myFile.read()
  15. flow = json.loads(ip)
  16. #Funcion que convierte y devuelve el int ipaddress en notacion punto-decimal
  17. def ipConversion(number):
  18. ipadd = str(ipaddress.IPv4Address(number))
  19. return ipadd
  20. def Analisis(flowHash):
  21. sampleHash={} #hash para contener los dip con el numero de conecciones y failed coneccciones
  22. connection = [0] * 2 #Lista para contener los valores de conecciones failed y conecciones buenas
  23. for i in flow["flows"]: #itera por cada elemento del diccionario de flows
  24. posA = ipConversion(i["sip"]) #Devuelve el ip en notacion punto-decimal
  25. if i["connection"] == "synack": #No se cual es el formato de esto
  26. connection[1]=1 #good conections
  27. else:
  28. connection [0] =1 #failed conections
  29. if posA in sampleHash:
  30. sampleHash[posA][0]+=connection[0]
  31. sampleHash[posA][1]+= connection[1]
  32. #print (posA)
  33. else:
  34. sampleHash[posA] = [connection[0], connection[1]]
  35. #print (posA)
  36. return sampleHash