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

cheo2.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #Version 2 #Itera por sip /16 y cuenta numero de puertos por cada dip
  2. from silk import *
  3. startDate = "2018/08/10"
  4. endDate = "2018/08/15"
  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. return mystr.join(ipadd[:position]) #devuelve los numeros en notacion string
  12. def FilterBySIP(flows, flowHash, num):
  13. fc = 0
  14. dportHash = {}
  15. fdout = open("tmpfile%s" % num, "w")
  16. for filename in flows:
  17. for rec in silkfile_open(filename, READ):#reading the flow file
  18. fc += 1
  19. if (':' in str(rec.sip)) or (num > global_num and ipConversion(str(rec.sip), num) not in flowHash): #Si en el paso anterior se vio que no
  20. continue
  21. dip = str(rec.dip)
  22. sip = str(rec.sip)
  23. dport= rec.dport
  24. sport= rec.sport
  25. Filter(fdout, ipConversion(sip, num+1), dip, sport, dport, dportHash)
  26. fdout.write("%s:%s:%s:%s\n" % (sip, dip, sport, dport))
  27. print fc
  28. fdout.close()
  29. return dportHash
  30. def FilterBySIPTMP(flowHash, num):
  31. dportHash = {}
  32. fdout = open("tmpfile%s" % num, "w")
  33. with open("tmpfile%s" % (num - 1), "r") as f:
  34. for flow in f:
  35. sip, dip, sport, dport = flow.split(":")
  36. sport = int(sport)
  37. dport = int(dport)
  38. if ipConversion(sip, num) not in flowHash:
  39. continue
  40. Filter(fdout, ipConversion(sip, num+1), dip, sport, dport, dportHash)
  41. fdout.write("%s:%s:%s:%s\n" % (sip, dip, sport, dport))
  42. fdout.close()
  43. return dportHash
  44. def Filter(fd, sip, dip, sport, dport, dportHash):
  45. if dport > 1024 and (sport <= 1024 or (sport >= 8000 and sport < 9000)):
  46. return
  47. if sip in dportHash:
  48. # if dip in dportHash[sip]["dips"]:
  49. # dportHash[sip]["dips"][dip] += 1
  50. # else:
  51. # dportHash[sip]["dips"][dip] = 1
  52. if dport in dportHash[sip]["dports"]:
  53. dportHash[sip]["dports"][dport] += 1
  54. #return
  55. else:
  56. dportHash[sip]["dports"][dport] = 1
  57. else:
  58. dportHash[sip] = {"dports": {}}
  59. dportHash[sip]["dips"] = {}
  60. #fd.write("%s:%s:%s:%s\n" % (sip, dip, sport, dport))
  61. def FilterBySIPFull(flowHash, num):
  62. flow_Counter=0
  63. dportHash = {}
  64. for filename in FGlob(type="all", start_date=startDate, end_date=endDate, site_config_file="/etc/silk/conf-v9/silk.conf", data_rootdir="/home/scratch/flow/rwflowpack/"):
  65. for rec in silkfile_open(filename, READ):#reading the flow file
  66. if (':' in str(rec.sip)) or (num > global_num and ipConversion(str(rec.sip), num) not in flowHash): #Si en el paso anterior se vio que no
  67. continue
  68. dip = str(rec.dip)
  69. sip = ipConversion(str(rec.sip), num+1)
  70. dport= rec.dport
  71. if sip in dportHash:
  72. if dip in dportHash[sip]:
  73. if dport in dportHash[sip][dip]:
  74. dportHash[sip][dip][dport] += 1
  75. else:
  76. dportHash[sip][dip][dport] = 1
  77. else:
  78. dportHash[sip][dip] = {dport : 1}
  79. else:
  80. dportHash[sip] = { dip: {dport: 1} }
  81. return dportHash
  82. def last_step(flowHash, num):
  83. dportHash = {}
  84. with open("tmpfile%s" % (num - 1), "r") as f:
  85. for flow in f:
  86. sip, dip, sport, dport = flow.split(":")
  87. sport = int(sport)
  88. dport = int(dport)
  89. if (':' in sip) or (num > global_num and sip not in flowHash):
  90. continue
  91. if sip in dportHash:
  92. if dip in dportHash[sip]:
  93. if dport in dportHash[sip][dip]:
  94. dportHash[sip][dip][dport] += 1
  95. else:
  96. dportHash[sip][dip][dport] = 1
  97. else:
  98. dportHash[sip][dip] = {dport : 1}
  99. else:
  100. dportHash[sip] = { dip: {dport: 1} }
  101. return dportHash
  102. def filter_laststep(flowhash):
  103. otherHash ={}
  104. for sips in flowHash: #se itera por todos los dip y sus counters o puertos
  105. for dips, dports in flowHash[sips].items():
  106. if len(dports) >= 100: #si la cantidad de puertos es mayor o igual a 100, nos interesan
  107. #y por lo tanto se guardan en un hash
  108. if sips in otherHash:
  109. otherHash[sips][dips] = dports
  110. else:
  111. otherHash[sips] = {dips: dports}
  112. return otherHash
  113. global_num = 2
  114. myNum = global_num
  115. flows = FGlob(type="all", start_date=startDate, end_date=endDate, site_config_file="/etc/silk/conf-v9/silk.conf", data_rootdir="/home/scratch/flow/rwflowpack/")
  116. otherHash = {}
  117. flowHash = FilterBySIP(flows, otherHash, myNum)
  118. print "Before thresh", len(flowHash)
  119. for sip in flowHash: #se itera por todos los dip y sus counters o puertos
  120. if len(flowHash[sip]["dports"]) >= 100:
  121. otherHash[sip] = flowHash[sip]
  122. print "After thresh", len(otherHash)
  123. myNum += 1
  124. while myNum <3: #Se itera las cuatro veces de acuerdo con la notacion de ipv4
  125. flowHash= FilterBySIPTMP(otherHash, myNum)
  126. print "Before thresh", len(flowHash)
  127. otherHash = {}
  128. for sip in flowHash: #se itera por todos los dip y sus counters o puertos
  129. if len(flowHash[sip]["dports"]) >= 100:
  130. otherHash[sip] = flowHash[sip]
  131. print "After thresh", len(otherHash)
  132. myNum += 1
  133. final_hash = last_step(otherHash, myNum)
  134. filtered_final_hash = filter_laststep(final_hash)
  135. fc = 0
  136. #print final_hash
  137. for sip in filtered_final_hash:
  138. fc +=1
  139. for dip in filtered_final_hash[sip]:
  140. print sip, dip, filtered_final_hash[sip][dip]
  141. #print (flowHash)
  142. print fc
  143. #for sip in otherHash:
  144. # print sip, sorted(otherHash[sip]["dports"].keys())