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

fedora_code_trw_reduc.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from silk import *
  2. myNum = 0
  3. sipList=[]
  4. sip_hash = {}
  5. startDate = "2009/04/20"
  6. endDate = "2009/04/22"
  7. p = 2
  8. ##################################
  9. # TRW con reduccion #
  10. # Para Silk's Data Set #
  11. #################################
  12. def ipConversion(number, position):
  13. mystr = ''
  14. ipadd = number.split(".") #Devuelve un arreglo
  15. #print ipadd
  16. for i in range(position+1):
  17. if i == position:
  18. #print ipadd[i]
  19. mystr = mystr + ipadd[i]
  20. else:
  21. # print ipadd[i]
  22. mystr = mystr + ipadd[i] + '.'
  23. return mystr #devuelve los numeros en notacion string
  24. def AnalisisReduciendo(sipList, num):
  25. sip_hash = {}
  26. #print sipList
  27. flow_counter = 0
  28. for filename in FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/data/silk.conf", data_rootdir="/data"):
  29. for rec in silkfile_open(filename, READ):#reading the flow file
  30. flow_counter += 1
  31. if (':' in str(rec.sip)) or (num != 0 and ipConversion(str(rec.sip), num-1) not in sipList):#Si en el paso anterior se vio que no
  32. #tiene el length de puertos requerido, se ignora
  33. continue
  34. else:
  35. connection = [0] * 2 #Lista para contener los valores de conecciones fallidas y conecciones buenas
  36. sip = ipConversion(str(rec.sip), num) #Devuelve el ip en notacion punto-decimal
  37. flags = str(rec.tcpflags) #array of all tcp flags that are set
  38. if 'A' in flags: #if the acknowledge flag is set
  39. connection[1]=1 #good conections
  40. else:
  41. connection [0] =1 #failed conections
  42. if sip in sip_hash: #si sip esta en ratioHash => que posA esta en sampleHash
  43. #por lo tanto ya se puede sumar las conecciones al ratio del dip
  44. sip_hash[sip][0]+=connection[0]
  45. sip_hash[sip][1] += connection[1]
  46. else: #si sip no esta en ratioHash tampoco
  47. sip_hash[sip] = [connection[0], connection[1]]
  48. #print sip_hash
  49. #print flow_counter
  50. return sip_hash
  51. while myNum <4:
  52. sip_connections_list = AnalisisReduciendo(sipList, myNum)
  53. sipList = []
  54. for sip in sip_connections_list:
  55. if (sip_connections_list[sip][1] != 0) and ((sip_connections_list[sip][0] / sip_connections_list[sip][1]) < 1) : #si la cantidad de succesful
  56. #b #g #connections es mas que failed connections
  57. #not scanner, ignore
  58. continue
  59. elif (sip_connections_list[sip][1] != 0) and ((sip_connections_list[sip][0] / sip_connections_list[sip][1]) < p): #mas failed que succesful, pero no llega al threshold
  60. #not scanner, ignore
  61. continue #se debe tener en cuenta que es suspicious pero no taaanto
  62. elif (sip_connections_list[sip][1] == 0 and sip_connections_list[sip][0] > 10): #el ratio de failed a succesful llega al threshold pautado
  63. #scanner, oh oh
  64. sipList.append(sip)
  65. else:
  66. #scanner, oh oh
  67. sipList.append(sip)
  68. #print sipList
  69. #print myNum
  70. myNum += 1
  71. #print len(sipList)