|
@@ -0,0 +1,217 @@
|
|
1
|
+#Version 2 #Itera por sip /16 y cuenta numero de puertos por cada dip
|
|
2
|
+#hello
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+from silk import *
|
|
6
|
+
|
|
7
|
+global_num = 2 #posicion del subnetwork. 1 implica A.x.x.x, 2 implica A.B.x.x, etc.
|
|
8
|
+
|
|
9
|
+#funcion que recibe un string del ip address y devuelve otro string hasta el subnetwork
|
|
10
|
+#que indica el numero de posicion
|
|
11
|
+def ipConversion(number, position):
|
|
12
|
+ mystr = '.'
|
|
13
|
+ ipadd = number.split(".")
|
|
14
|
+ return mystr.join(ipadd[:position])
|
|
15
|
+
|
|
16
|
+#Esta funcion recorre la lista de flows, las manda por la funcion de Filter para verificar
|
|
17
|
+#que sean puertos no comunes, luego guarda una lista de esos flows especificos en un file
|
|
18
|
+#y devuelve un hash de los flows filtrados.
|
|
19
|
+def FilterBySIP(flows, flowHash, num):
|
|
20
|
+
|
|
21
|
+ fc = 0
|
|
22
|
+ dportHash = {}
|
|
23
|
+
|
|
24
|
+ fdout = open("tmpfile%s" % num, "w")
|
|
25
|
+
|
|
26
|
+ for filename in flows:
|
|
27
|
+ for rec in silkfile_open(filename, READ):#reading the flow file
|
|
28
|
+ fc += 1
|
|
29
|
+ 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
|
|
30
|
+ continue
|
|
31
|
+ dip = str(rec.dip)
|
|
32
|
+ sip = str(rec.sip)
|
|
33
|
+ dport= rec.dport
|
|
34
|
+ sport= rec.sport
|
|
35
|
+
|
|
36
|
+ Filter(fdout, ipConversion(sip, num+1), dip, sport, dport, dportHash)
|
|
37
|
+ fdout.write("%s:%s:%s:%s\n" % (sip, dip, sport, dport))
|
|
38
|
+
|
|
39
|
+ # print fc
|
|
40
|
+ fdout.close()
|
|
41
|
+ return dportHash
|
|
42
|
+
|
|
43
|
+#Esta funcion recorre la lista de flows guardados en el file creado en el step anterior,
|
|
44
|
+#verifica que esten en el hash creado en el step anterior, las manda por la funcion de Filter para verificar
|
|
45
|
+#que sean puertos no comunes, luego guarda una lista de esos flows especificos en un file
|
|
46
|
+#y devuelve un hash de los flows filtrados.
|
|
47
|
+def FilterBySIPTMP(flowHash, num):
|
|
48
|
+
|
|
49
|
+ dportHash = {}
|
|
50
|
+
|
|
51
|
+ fdout = open("tmpfile%s" % num, "w")
|
|
52
|
+
|
|
53
|
+ with open("tmpfile%s" % (num - 1), "r") as f:
|
|
54
|
+ for flow in f:
|
|
55
|
+ sip, dip, sport, dport = flow.split(":")
|
|
56
|
+ #print ipConversion(sip, num)
|
|
57
|
+ sport = int(sport)
|
|
58
|
+ dport = int(dport)
|
|
59
|
+ if ipConversion(sip, num) not in flowHash:
|
|
60
|
+ continue
|
|
61
|
+ Filter(fdout, ipConversion(sip, num+1), dip, sport, dport, dportHash)
|
|
62
|
+ fdout.write("%s:%s:%s:%s\n" % (sip, dip, sport, dport))
|
|
63
|
+ fdout.close()
|
|
64
|
+ return dportHash
|
|
65
|
+
|
|
66
|
+#argumentos consisten del file con los flows, source y destination ip y puertos y
|
|
67
|
+#un hash vacio para guardar los source ips con la lista de puertos a donde se conecta
|
|
68
|
+#la funcion verifica que los puertos no sean puertos comunes, para asi agregar el flow al hash
|
|
69
|
+def Filter(fd, sip, dip, sport, dport, dportHash):
|
|
70
|
+
|
|
71
|
+ if dport > 1024 and (sport <= 1024 or (sport >= 8000 and sport < 9000)):
|
|
72
|
+ return
|
|
73
|
+ if sip in dportHash:
|
|
74
|
+# if dip in dportHash[sip]["dips"]:
|
|
75
|
+# dportHash[sip]["dips"][dip] += 1
|
|
76
|
+# else:
|
|
77
|
+# dportHash[sip]["dips"][dip] = 1
|
|
78
|
+ if dport in dportHash[sip]["dports"]:
|
|
79
|
+ dportHash[sip]["dports"][dport] += 1
|
|
80
|
+ #return
|
|
81
|
+ else:
|
|
82
|
+ dportHash[sip]["dports"][dport] = 1
|
|
83
|
+ else:
|
|
84
|
+ dportHash[sip] = {"dports": {}}
|
|
85
|
+ dportHash[sip]["dips"] = {}
|
|
86
|
+ #fd.write("%s:%s:%s:%s\n" % (sip, dip, sport, dport))
|
|
87
|
+
|
|
88
|
+def FilterBySIPFull(flowHash, num):
|
|
89
|
+ flow_Counter=0
|
|
90
|
+ dportHash = {}
|
|
91
|
+
|
|
92
|
+ 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/"):
|
|
93
|
+
|
|
94
|
+ for rec in silkfile_open(filename, READ):#reading the flow file
|
|
95
|
+ 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
|
|
96
|
+ continue
|
|
97
|
+ dip = str(rec.dip)
|
|
98
|
+ sip = ipConversion(str(rec.sip), num+1)
|
|
99
|
+ dport= rec.dport
|
|
100
|
+ if sip in dportHash:
|
|
101
|
+ if dip in dportHash[sip]:
|
|
102
|
+ if dport in dportHash[sip][dip]:
|
|
103
|
+ dportHash[sip][dip][dport] += 1
|
|
104
|
+ else:
|
|
105
|
+ dportHash[sip][dip][dport] = 1
|
|
106
|
+ else:
|
|
107
|
+ dportHash[sip][dip] = {dport : 1}
|
|
108
|
+ else:
|
|
109
|
+ dportHash[sip] = { dip: {dport: 1} }
|
|
110
|
+
|
|
111
|
+ return dportHash
|
|
112
|
+
|
|
113
|
+#Hace lo mismo que la funcion FilterBySIPTMP, lo unico que esta vez no hace falta verificar los puertos
|
|
114
|
+#y ademas crea otro hash de los source ips con sus destination ips con sus puertos
|
|
115
|
+def last_step(flowHash, num):
|
|
116
|
+ dportHash = {}
|
|
117
|
+
|
|
118
|
+ with open("tmpfile%s" % (num), "r") as f:
|
|
119
|
+ for flow in f:
|
|
120
|
+ sip, dip, sport, dport = flow.split(":")
|
|
121
|
+ sport = int(sport)
|
|
122
|
+ dport = int(dport)
|
|
123
|
+ if (':' in sip) or (num > global_num and sip not in flowHash):
|
|
124
|
+ print sip
|
|
125
|
+ print "Do we ever enter here?"
|
|
126
|
+ continue
|
|
127
|
+ if sip in dportHash:
|
|
128
|
+ if dip in dportHash[sip]:
|
|
129
|
+ if dport in dportHash[sip][dip]:
|
|
130
|
+ dportHash[sip][dip][dport] += 1
|
|
131
|
+ else:
|
|
132
|
+ dportHash[sip][dip][dport] = 1
|
|
133
|
+ else:
|
|
134
|
+ dportHash[sip][dip] = {dport : 1}
|
|
135
|
+ else:
|
|
136
|
+ dportHash[sip] = { dip: {dport: 1} }
|
|
137
|
+ return dportHash
|
|
138
|
+
|
|
139
|
+#Funcion que verifica que el total de puertos por cada source ip con su destination ip
|
|
140
|
+#sea mayor que 100. Devuelve un hash con los flows filtrados.
|
|
141
|
+def filter_laststep(flowhash):
|
|
142
|
+ otherhash ={}
|
|
143
|
+
|
|
144
|
+ for sips in flowhash: #se itera por todos los dip y sus counters o puertos
|
|
145
|
+ for dips, dports in flowhash[sips].items():
|
|
146
|
+ #print "Filter", len(dports)
|
|
147
|
+ if len(dports) >= 100: #si la cantidad de puertos es mayor o igual a 100, nos interesan
|
|
148
|
+ #y por lo tanto se guardan en un hash
|
|
149
|
+ if sips in otherhash:
|
|
150
|
+ otherhash[sips][dips] = dports
|
|
151
|
+ else:
|
|
152
|
+ otherhash[sips] = {dips: dports}
|
|
153
|
+ return otherhash
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+def main():
|
|
158
|
+#variable del intervalo de los flows a verificar
|
|
159
|
+ startDate = "2018/08/01"
|
|
160
|
+ endDate = "2018/08/31"
|
|
161
|
+
|
|
162
|
+ myNum = global_num #variable que dicta la posicion del subnetwork
|
|
163
|
+
|
|
164
|
+ #lista de flows total
|
|
165
|
+ 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/")
|
|
166
|
+
|
|
167
|
+ otherHash = {}
|
|
168
|
+
|
|
169
|
+ flowHash = FilterBySIP(flows, otherHash, myNum) #hash de todos los sourceip en el subnetwork de posicion myNum con sus puertos
|
|
170
|
+
|
|
171
|
+ print "Before thresh 1", len(flowHash)
|
|
172
|
+
|
|
173
|
+ #Verifica source ips sospechosos por cantidad de puertos conectados
|
|
174
|
+ #devuelve hash con los source ips filtrados
|
|
175
|
+ for sip in flowHash:
|
|
176
|
+ if len(flowHash[sip]["dports"]) >= 100:
|
|
177
|
+ otherHash[sip] = flowHash[sip]
|
|
178
|
+
|
|
179
|
+ print "After thresh 1", len(otherHash)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+ while myNum <3:
|
|
183
|
+
|
|
184
|
+ flowHash= FilterBySIPTMP(otherHash, myNum + 1)
|
|
185
|
+
|
|
186
|
+ #print "Before thresh 2", len(flowHash)
|
|
187
|
+ otherHash = {}
|
|
188
|
+ for sip in flowHash: #se itera por todos los dip y sus counters o puertos
|
|
189
|
+ if len(flowHash[sip]["dports"]) >= 100:
|
|
190
|
+ otherHash[sip] = flowHash[sip]
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+ #print "After thresh 2", len(otherHash)
|
|
194
|
+ myNum += 1
|
|
195
|
+
|
|
196
|
+ final_hash = last_step(otherHash, myNum)
|
|
197
|
+ #print final_hash
|
|
198
|
+ # for sips in final_hash: #se itera por todos los dip y sus counters o puertos
|
|
199
|
+ # for dips, dports in final_hash[sips].items():
|
|
200
|
+ # print "final", len(dports)"
|
|
201
|
+ filtered_final_hash = filter_laststep(final_hash)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+ fc = 0
|
|
205
|
+ fo=0
|
|
206
|
+ for sip in filtered_final_hash:
|
|
207
|
+ fc +=1
|
|
208
|
+ for dip in filtered_final_hash[sip]:
|
|
209
|
+ fo +=1
|
|
210
|
+ #print sip, dip, filtered_final_hash[sip][dip]
|
|
211
|
+ #print (flowHash)
|
|
212
|
+ print fc
|
|
213
|
+ print fo
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+if __name__== "__main__":
|
|
217
|
+ main()
|