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

fedora_code_trw.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ##################################
  2. # TRW sin reduccion #
  3. # Para Silk's Data Set #
  4. ################################
  5. from silk import *
  6. startDate = "2009/04/20"
  7. endDate = "2009/04/22"
  8. p = 2
  9. def Analisis():
  10. counter = 0 # borrar luego
  11. sampleHash={} #hash para contener los dip con el numero de conecciones y failed coneccciones
  12. flow_counter = 0
  13. for filename in FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/data/silk.conf", data_rootdir="/data"):
  14. for rec in silkfile_open(filename, READ):#reading the flow file
  15. flow_counter += 1
  16. if (':' in str(rec.sip)):
  17. continue
  18. else:
  19. connection = [0] * 2 #Lista para contener los valores de conecciones failed y conecciones buenas
  20. sip = str(rec.sip) #Devuelve el ip en notacion punto-decimal
  21. flags = str(rec.tcpflags)
  22. #print sip, flags
  23. #counter +=1
  24. if 'A' in flags: ####arreglar
  25. connection[1]=1 #good conections
  26. else:
  27. connection [0] =1 #failed conections
  28. if sip in sampleHash:
  29. sampleHash[sip][0]+= connection[0]
  30. sampleHash[sip][1]+= connection[1]
  31. else:
  32. sampleHash[sip] = [connection[0], connection[1]]
  33. #print sampleHash
  34. #print flow_counter
  35. return sampleHash
  36. sip_connections_list = Analisis()
  37. #print sip_connections_list
  38. sipList = []
  39. for sip in sip_connections_list:
  40. if (sip_connections_list[sip][1] != 0) and ((sip_connections_list[sip][0] / sip_connections_list[sip][1]) < 1) : #si la cantidad de succesful
  41. #b #g #connections es mas que failed connections
  42. #not scanner, ignore
  43. continue
  44. 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
  45. #not scanner, ignore
  46. continue #se debe tener en cuenta que es suspicious pero no taaanto
  47. elif (sip_connections_list[sip][1] == 0 and sip_connections_list[sip][0] > 10): #el ratio de failed a succesful llega al threshold pautado
  48. #scanner, oh oh
  49. sipList.append(sip)
  50. else:
  51. #scanner, oh oh
  52. sipList.append(sip)
  53. #print len(sipList)