|
@@ -0,0 +1,115 @@
|
|
1
|
+#Itera por sip /16 y cuenta numero de puertos por cada dip
|
|
2
|
+
|
|
3
|
+from silk import *
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+startDate = "2009/04/20"
|
|
7
|
+endDate = "2009/04/22"
|
|
8
|
+#Para filtrar por puertos. Pero no queremos todavia
|
|
9
|
+#minPort = 20
|
|
10
|
+#maxPort = 5000
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+def ipConversion(number, position):
|
|
14
|
+ mystr = ''
|
|
15
|
+ ipadd = number.split(".") #Devuelve un arreglo
|
|
16
|
+ for i in range(position+1):
|
|
17
|
+ if i ==position:
|
|
18
|
+ mystr = mystr + ipadd[i]
|
|
19
|
+ else:
|
|
20
|
+ mystr = mystr + ipadd[i] + '.'
|
|
21
|
+ return mystr #devuelve los numeros en notacion string
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+def PrimerAnalisis(flowHash, num):
|
|
25
|
+ dportHash={}
|
|
26
|
+ flow_Counter=0
|
|
27
|
+ for filename in FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/data/silk.conf", data_rootdir="/data"):
|
|
28
|
+ for rec in silkfile_open(filename, READ):#reading the flow file
|
|
29
|
+ if (':' in str(rec.sip)) or (num != 0 and ipConversion(str(rec.sip), num-1) not in flowHash): #Si en el paso anterior se vio que no
|
|
30
|
+ #tiene el length de puertos requerido, se ignora
|
|
31
|
+ continue
|
|
32
|
+ else: #agrega a un hash cada puerto con un counter de sus destination ips
|
|
33
|
+ dip = str(rec.dip)
|
|
34
|
+ sip = ipConversion(str(rec.sip), num)
|
|
35
|
+ dport= rec.dport
|
|
36
|
+ if sip in dportHash:
|
|
37
|
+ if dip in dportHash[sip]:
|
|
38
|
+ dportHash[sip][dip] += 1
|
|
39
|
+ else:
|
|
40
|
+ dportHash[sip][dip] = 1
|
|
41
|
+ else:
|
|
42
|
+ dportHash[sip] = { dip: 1 }
|
|
43
|
+
|
|
44
|
+ #print flow_Counter
|
|
45
|
+ return dportHash
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+def UltimoAnalisis(flowHash, num):
|
|
49
|
+ dportHash={}
|
|
50
|
+ flow_Counter=0
|
|
51
|
+ for filename in FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/data/silk.conf", data_rootdir="/data"):
|
|
52
|
+ for rec in silkfile_open(filename, READ):#reading the flow file
|
|
53
|
+ if (':' in str(rec.sip)) or (num != 0 and ipConversion(str(rec.sip), num-1) not in flowHash): #Si en el paso anterior se vio que no
|
|
54
|
+ #tiene el length de puertos requerido, se ignora
|
|
55
|
+ continue
|
|
56
|
+ else: #agrega a un hash cada puerto con un counter de sus destination ips
|
|
57
|
+ dip = ipConversion(str(rec.dip), num)
|
|
58
|
+ sip = ipConversion(str(rec.sip), num)
|
|
59
|
+ dport= rec.dport
|
|
60
|
+ if sip in dportHash:
|
|
61
|
+ if dip in dportHash[sip]:
|
|
62
|
+ dportHash[sip][dip].append(dport)
|
|
63
|
+ else:
|
|
64
|
+ dportHash[sip][dip] = [dport]
|
|
65
|
+ else:
|
|
66
|
+ dportHash[sip] = { dip: [dport] }
|
|
67
|
+
|
|
68
|
+ #print flow_Counter
|
|
69
|
+ return dportHash
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+#main
|
|
79
|
+#SET en python
|
|
80
|
+
|
|
81
|
+myNum = 0
|
|
82
|
+otherHash = {}
|
|
83
|
+while myNum <3: #Se itera las cuatro veces de acuerdo con la notacion de ipv4
|
|
84
|
+ flowHash= PrimerAnalisis(otherHash, myNum)
|
|
85
|
+ otherHash = {} #Se borra el hash para agregar elementos nuevos con la nueva etapa de la busqueda
|
|
86
|
+ for sips in flowHash: #se itera por todos los dip y sus counters o puertos
|
|
87
|
+ for dips, dports in flowHash[sips].items():
|
|
88
|
+ if dports >= 100: #si la cantidad de puertos es mayor o igual a 100, nos interesan
|
|
89
|
+ #y por lo tanto se guardan en un hash
|
|
90
|
+ if sips in otherHash:
|
|
91
|
+ otherHash[sips][dips] = dports
|
|
92
|
+ else:
|
|
93
|
+ otherHash[sips] = {dips: dports}
|
|
94
|
+ myNum += 1
|
|
95
|
+
|
|
96
|
+ #print (flowHash)
|
|
97
|
+
|
|
98
|
+#print otherHash
|
|
99
|
+#Ultimo chequeo, utilizando el ip completo
|
|
100
|
+counter = 0
|
|
101
|
+flowHash = UltimoAnalisis(otherHash, myNum)
|
|
102
|
+otherHash = {} #Se borra el hash para agregar elementos nuevos con la nueva etapa de la busqueda
|
|
103
|
+for sips in flowHash: #se itera por todos los dip y sus counters o puertos
|
|
104
|
+ for dips, dports in flowHash[sips].items():
|
|
105
|
+ if len(dports) >= 100: #si la cantidad de puertos es mayor o igual a 100, nos interesan
|
|
106
|
+ #y por lo tanto se guardan en un hash
|
|
107
|
+ if sips in otherHash:
|
|
108
|
+ otherHash[sips][dips] = dports
|
|
109
|
+ else:
|
|
110
|
+ otherHash[sips] = {dips: dports}
|
|
111
|
+
|
|
112
|
+for dips, dports in otherHash.items():
|
|
113
|
+ counter +=1 #para contar los elementos del hash
|
|
114
|
+
|
|
115
|
+print counter
|