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

map_bruteforce_three.py 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Guarda lista de puertos de cada dip por cada sip
  2. #ftp remote edit
  3. from silk import *
  4. import multiprocessing as mp
  5. #Para filtrar por puertos. Pero no queremos todavia
  6. #minPort = 20
  7. #maxPort = 5000
  8. def verify_type(filename):
  9. dportHash = {} #contains amount of dport per each sip
  10. filename = [filename]
  11. #print "stooy aqui"
  12. for file in filename:
  13. for rec in silkfile_open(file, READ):#reading the flow file
  14. sip = str(rec.sip)
  15. dip = str(rec.dip)
  16. dport = rec.dport
  17. if (':' in sip): #Si en el paso anterior se vio que n #tiene el length de puertos requerido, se ignora
  18. # x+=1
  19. continue
  20. else:
  21. if sip in dportHash:
  22. if dip in dportHash[sip]:
  23. if dport in dportHash[sip][dip]:
  24. dportHash[sip][dip][dport] += 1
  25. else:
  26. dportHash[sip][dip][dport] = 1
  27. else:
  28. dportHash[sip][dip] = {dport : 1}
  29. else:
  30. dportHash[sip] = { dip: {dport: 1} }
  31. return dportHash
  32. def join_hash(list):
  33. complete_hash ={}
  34. for i in list:
  35. for sip, hash in i.items():
  36. if sip in complete_hash:
  37. #print "hello", sip
  38. for dip, dports in i[sip].items():
  39. #print dip
  40. if dip in complete_hash[sip]:
  41. #print "wassup"
  42. for number, value in dports.items():
  43. if number in complete_hash[sip]:
  44. print "DPORTS", number
  45. complete_hash[sip][dip][number] += value
  46. else:
  47. complete_hash[sip][dip][number]= value
  48. else:
  49. complete_hash[sip][dip]= dports
  50. else:
  51. complete_hash[sip]= hash
  52. return complete_hash
  53. def main():
  54. startDate = "2018/09/1"
  55. endDate = "2018/09/30"
  56. otherHash = {}
  57. counter = 0
  58. process_num = 8
  59. pool = mp.Pool(processes=process_num)
  60. files = FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/etc/silk/conf-v9/silk.conf", data_rootdir="/home/scratch/flow/rwflowpack/")
  61. files = [x for x in files]
  62. print len(files)
  63. fileHash = pool.map(verify_type, files) # FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/etc/silk/conf-v9/silk.conf", data_rootdir="/home/scratch/flow/rwflowpack/"))
  64. flowHash = join_hash(fileHash)
  65. print "FLOW", len(flowHash)
  66. for sips in flowHash: #se itera por todos los dip y sus counters o puertos
  67. #print sips
  68. for dips, dports in flowHash[sips].items():
  69. #print "Dip", dips, dports
  70. if len(dports) >= 100: #si la cantidad de puertos es mayor o igual a 100, nos interesan
  71. #y por lo tanto se guardan en un hash
  72. print "DIP", dips, len(dports)
  73. if sips in otherHash:
  74. otherHash[sips][dips] = dports
  75. else:
  76. otherHash[sips] = {dips: dports}
  77. for dips, dports in otherHash.items():
  78. counter +=1 #para contar los elementos del hash
  79. print counter
  80. #print otherHash
  81. if __name__== "__main__":
  82. main()