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

realPs.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import ipaddress
  2. import netflows
  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. x = 20
  7. y = 60000
  8. ignoreip = []
  9. myHash={}
  10. myNum = 0
  11. counter = 0
  12. def ipConversion(number, position):
  13. mystr = ''
  14. ipadd = (str(ipaddress.IPv4Address(number))).split(".") #Devuelve un arreglo
  15. for i in range(position+1):
  16. if i ==position:
  17. mystr = mystr + ipadd[i]
  18. else:
  19. mystr = mystr + ipadd[i] + '.'
  20. return mystr
  21. def PrimerAnalisis(num):
  22. newHash={}
  23. for i in netflows.flowprinter["flows"]: #itera por cada elemento del diccionario de flows
  24. posA = ipConversion(i["dip"], num)
  25. if ignoreip != [] and posA[0:4*num] in ignoreip:
  26. continue
  27. elif (i['dport'] >= 1 and i['dport'] < x) or i['dport'] > y: #verifica que sean puertos (se me fue la palabra...)
  28. continue
  29. else: #agrega a un hash cada puerto con un counter de sus destination ips
  30. if num ==3:
  31. if posA in newHash:
  32. newHash[posA].append(i['dport'])
  33. else:
  34. newHash[posA] = [i['dport']]
  35. else:
  36. if posA in newHash:
  37. newHash[posA] += 1
  38. #print (posA)
  39. else:
  40. newHash[posA] = 1
  41. #print (posA)
  42. return newHash
  43. #MAIN:
  44. while myNum <4:
  45. myHash= PrimerAnalisis(myNum)
  46. ignoreip =[]
  47. for k, v in myHash.items():
  48. if myNum != 3 and v >= 100:
  49. continue
  50. elif myNum == 3 and len(v)>= 100:
  51. continue
  52. else:
  53. ignoreip.append(k)
  54. myNum += 1
  55. for i in ignoreip:
  56. myHash.pop(i)
  57. for k, v in myHash.items():
  58. counter +=1
  59. #print (ignoreip)
  60. print (counter)
  61. #Si el valor de la llave es menos que 100, no me interesa.