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

PS_orig.py 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #Port Scan without reduction
  2. import ipaddress
  3. import json
  4. #Version 1
  5. #Read all the flows and append to a hash their destination ip and destination port
  6. #Check which flows share the same destination ip though differente dports.
  7. x = 20
  8. y = 60000
  9. deletevar = []
  10. newHash={}
  11. PATH = '/Users/Sara/Documents/Univ Classes/Investigacion/Programas/netflows.txt'
  12. myFile = open(PATH, 'r')
  13. ip = myFile.read()
  14. flow = json.loads(ip)
  15. for i in flow["flows"]: #itera por cada elemento del diccionario de flows
  16. #if (i['dport'] >= 1 and i['dport'] < x) or i['dport'] > y: #verifica que sean puertos (se me fue la palabra...)
  17. #continue
  18. #else: #agrega a un hash cada puerto con su destination ip
  19. if i['sip'] == 2291263257:
  20. if i['dip'] in newHash:
  21. newHash[i['dip']].append(i['dport'])
  22. else:
  23. newHash[i['dip']] = [i['dport']]
  24. #LO SIGUIENTE ESTA COMENTADO Y LO EXPLICO EN EL WORD
  25. # for k, v in newHash.items(): #itera por cada ip address y sus puertos
  26. # if len(v) >= 100:
  27. # #print ("something suspicious...")
  28. # continue
  29. # else: #Si no tiene una cantidad considerable de puertos, agrega el ip a una lista
  30. # if k in deletevar:
  31. # continue
  32. # else:
  33. # deletevar.append(k)
  34. #for i in deletevar: #borra todos los elementos que estan en la lista deletevar
  35. #del hash con todos los ip y sus puertos
  36. # newHash.pop(i)
  37. counter = 0 #Para contar total de elementos en el hash
  38. for k, v in newHash.items(): #imprime los destination ip address con sus puertos
  39. counter = counter+1
  40. k = str(ipaddress.IPv4Address(k))
  41. print ("{}:{}".format(k,v))
  42. print (counter)
  43. print( "Done checking:")