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

fedora_code_scanner.py 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. ##########################################################
  2. # NS or PS Verifier
  3. # Receives a List of Source IP Addresses, and depending on the
  4. # ratio of dips and dports, classifies the ip address as ps or ns
  5. #########################################################
  6. from silk import *
  7. startDate = "2009/04/20"
  8. endDate = "2009/04/22"
  9. minPort = 20
  10. maxPort = 5000
  11. verifyHash = {}
  12. def verify_type():
  13. dportHash = {} #contains amount of dport per each sip
  14. for filename in FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/data/silk.conf", data_rootdir="/data"):
  15. for rec in silkfile_open(filename, READ):#reading the flow file
  16. sip = str(rec.sip)
  17. dip = str(rec.dip)
  18. if (rec.dport >= 1 and rec.dport < minPort) or rec.dport > maxPort: #verifica que sean puertos validos (creo que se dice asi)
  19. continue
  20. else: #agrega a un hash cada puerto con un counter de sus destination ips
  21. if sip in dportHash:
  22. if dip in dportHash[sip]:
  23. dportHash[sip][dip] += 1
  24. else:
  25. dportHash[sip][dip] = 1
  26. else:
  27. dportHash[sip] = { dip: 1 }
  28. return dportHash
  29. total_dports = 0
  30. total_dips = 0
  31. sipList = ' ' #esta lista viene de los codigos de trw
  32. verifyHash = verify_type()
  33. psList = [] #list of ip adresses of port scanners
  34. nsList = [] #list of ip adresses of network scanners
  35. for sip in verifyHash: #itera por cada ip address y sus puertos
  36. for i in verifyHash[sip]:
  37. total_dports = total_dports + verifyHash[sip][i]
  38. total_dips = len(verifyHash[sip])
  39. #check if it is network scan or port scan
  40. #casoA mas dports que dips por mucho. Que el ratio sea 100:1 o mas
  41. if total_dports / total_dips >= 5:
  42. #print ("something suspicious...")
  43. #print "This IP Adress %s is a Port Scanner "
  44. psList.append(sip)
  45. #caso B mas dports que dips pero que el ratio sea 5:1 o menos
  46. elif total_dports / total_dips <= 5:
  47. #print "This IP Adress %s is a Network Scanner "
  48. nsList.append(sip)