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

bruteforce_reduction_sip.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #Itera por sip /16 y cuenta numero de puertos por cada dip
  2. from silk import *
  3. startDate = "2009/04/20"
  4. endDate = "2009/04/22"
  5. #Para filtrar por puertos. Pero no queremos todavia
  6. #minPort = 20
  7. #maxPort = 5000
  8. def ipConversion(number, position):
  9. mystr = ''
  10. ipadd = number.split(".") #Devuelve un arreglo
  11. for i in range(position+1):
  12. if i ==position:
  13. mystr = mystr + ipadd[i]
  14. else:
  15. mystr = mystr + ipadd[i] + '.'
  16. return mystr #devuelve los numeros en notacion string
  17. def PrimerAnalisis(flowHash, num):
  18. dportHash={}
  19. flow_Counter=0
  20. for filename in FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/data/silk.conf", data_rootdir="/data"):
  21. for rec in silkfile_open(filename, READ):#reading the flow file
  22. 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
  23. #tiene el length de puertos requerido, se ignora
  24. continue
  25. else: #agrega a un hash cada puerto con un counter de sus destination ips
  26. dip = str(rec.dip)
  27. sip = ipConversion(str(rec.sip), num)
  28. dport= rec.dport
  29. if sip in dportHash:
  30. if dip in dportHash[sip]:
  31. dportHash[sip][dip] += 1
  32. else:
  33. dportHash[sip][dip] = 1
  34. else:
  35. dportHash[sip] = { dip: 1 }
  36. #print flow_Counter
  37. return dportHash
  38. def UltimoAnalisis(flowHash, num):
  39. dportHash={}
  40. flow_Counter=0
  41. for filename in FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/data/silk.conf", data_rootdir="/data"):
  42. for rec in silkfile_open(filename, READ):#reading the flow file
  43. 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
  44. #tiene el length de puertos requerido, se ignora
  45. continue
  46. else: #agrega a un hash cada puerto con un counter de sus destination ips
  47. dip = ipConversion(str(rec.dip), num)
  48. sip = ipConversion(str(rec.sip), num)
  49. dport= rec.dport
  50. if sip in dportHash:
  51. if dip in dportHash[sip]:
  52. dportHash[sip][dip].append(dport)
  53. else:
  54. dportHash[sip][dip] = [dport]
  55. else:
  56. dportHash[sip] = { dip: [dport] }
  57. #print flow_Counter
  58. return dportHash
  59. #main
  60. myNum = 0
  61. otherHash = {}
  62. while myNum <3: #Se itera las cuatro veces de acuerdo con la notacion de ipv4
  63. flowHash= PrimerAnalisis(otherHash, myNum)
  64. otherHash = {} #Se borra el hash para agregar elementos nuevos con la nueva etapa de la busqueda
  65. for sips in flowHash: #se itera por todos los dip y sus counters o puertos
  66. for dips, dports in flowHash[sips].items():
  67. if dports >= 100: #si la cantidad de puertos es mayor o igual a 100, nos interesan
  68. #y por lo tanto se guardan en un hash
  69. if sips in otherHash:
  70. otherHash[sips][dips] = dports
  71. else:
  72. otherHash[sips] = {dips: dports}
  73. myNum += 1
  74. #print (flowHash)
  75. #print otherHash
  76. #Ultimo chequeo, utilizando el ip completo
  77. counter = 0
  78. flowHash = UltimoAnalisis(otherHash, myNum)
  79. otherHash = {} #Se borra el hash para agregar elementos nuevos con la nueva etapa de la busqueda
  80. for sips in flowHash: #se itera por todos los dip y sus counters o puertos
  81. for dips, dports in flowHash[sips].items():
  82. if len(dports) >= 100: #si la cantidad de puertos es mayor o igual a 100, nos interesan
  83. #y por lo tanto se guardan en un hash
  84. if sips in otherHash:
  85. otherHash[sips][dips] = dports
  86. else:
  87. otherHash[sips] = {dips: dports}
  88. for dips, dports in otherHash.items():
  89. counter +=1 #para contar los elementos del hash
  90. print counter