Browse Source

Adding Current Work files

SaraBeatriz 5 years ago
parent
commit
68d9a881b1

+ 62
- 0
Programas/Last_work/Multiprocess/bruteforce_sip-dip_threehash.py View File

@@ -0,0 +1,62 @@
1
+# Guarda lista de puertos de cada dip por cada sip
2
+
3
+from silk import *
4
+
5
+
6
+startDate = "2018/09/1"
7
+endDate = "2018/09/30"
8
+#Para filtrar por puertos. Pero no queremos todavia
9
+#minPort = 20
10
+#maxPort = 5000
11
+
12
+
13
+def verify_type():
14
+    x = 0
15
+    dportHash = {} #contains amount of dport per each sip
16
+    for filename in FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/etc/silk/conf-v9/silk.conf", data_rootdir="/home/scratch/flow/rwflowpack/"):
17
+        for rec in silkfile_open(filename, READ):#reading the flow file
18
+            sip = str(rec.sip)
19
+            dip = str(rec.dip)
20
+            dport = rec.dport
21
+            if (':' in sip): #Si en el paso anterior se vio que no
22
+                # print "heeloo", x
23
+                # x+=1
24
+                #                                             #tiene el length de puertos requerido, se ignora
25
+                continue
26
+            else:
27
+                if sip in dportHash:
28
+                    if dip in dportHash[sip]:
29
+                        if dport in dportHash[sip][dip]:
30
+                            dportHash[sip][dip][dport] += 1
31
+                        else:
32
+                            dportHash[sip][dip][dport] = 1
33
+                    else:
34
+                        dportHash[sip][dip] = {dport : 1}
35
+                else:
36
+                    dportHash[sip] = { dip: {dport: 1} }
37
+    return dportHash
38
+
39
+
40
+#MAIN
41
+otherHash = {}
42
+counter = 0
43
+files = FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/etc/silk/conf-v9/silk.conf", data_rootdir="/home/scratch/flow/rwflowpack/")
44
+files = [x for x in files]
45
+print "Flow", len(files)
46
+flowHash = verify_type()
47
+print "After flow", len(flowHash)
48
+for sips in flowHash: #se itera por todos los dip y sus counters o puertos
49
+    for dips, dports in flowHash[sips].items():
50
+        if len(dports) >= 100: #si la cantidad de puertos es mayor o igual a 100, nos interesan
51
+                                #y por lo tanto se guardan en un hash
52
+            print "DIP", dips, len(dports)
53
+            if sips in otherHash:
54
+                otherHash[sips][dips] = dports
55
+            else:
56
+                otherHash[sips] = {dips: dports}
57
+
58
+for dips, dports in otherHash.items():
59
+    counter +=1 #para contar los elementos del hash
60
+
61
+print counter
62
+#print otherHash

+ 98
- 0
Programas/Last_work/Multiprocess/map_bruteforce_three.py View File

@@ -0,0 +1,98 @@
1
+# Guarda lista de puertos de cada dip por cada sip
2
+#ftp remote edit
3
+from silk import *
4
+import multiprocessing as mp
5
+
6
+
7
+#Para filtrar por puertos. Pero no queremos todavia
8
+#minPort = 20
9
+#maxPort = 5000
10
+
11
+
12
+def verify_type(filename):
13
+
14
+    dportHash = {} #contains amount of dport per each sip
15
+    filename = [filename]
16
+    #print "stooy aqui"
17
+
18
+    for file in filename:
19
+
20
+        for rec in silkfile_open(file, READ):#reading the flow file
21
+            sip = str(rec.sip)
22
+            dip = str(rec.dip)
23
+            dport = rec.dport
24
+            if (':' in sip): #Si en el paso anterior se vio que n                                                    #tiene el length de puertos requerido, se ignora
25
+
26
+                # x+=1
27
+                continue
28
+            else:
29
+                if sip in dportHash:
30
+                    if dip in dportHash[sip]:
31
+                        if dport in dportHash[sip][dip]:
32
+                            dportHash[sip][dip][dport] += 1
33
+                        else:
34
+                            dportHash[sip][dip][dport] = 1
35
+                    else:
36
+                        dportHash[sip][dip] = {dport : 1}
37
+                else:
38
+                    dportHash[sip] = { dip: {dport: 1} }
39
+
40
+    return dportHash
41
+
42
+def join_hash(list):
43
+    complete_hash ={}
44
+    for i in list:
45
+        for sip, hash in i.items():
46
+            if sip in complete_hash:
47
+                #print "hello", sip
48
+                for dip, dports in i[sip].items():
49
+                    #print dip
50
+                    if dip in complete_hash[sip]:
51
+                        #print "wassup"
52
+                        for number, value in dports.items():
53
+                            if number in complete_hash[sip]:
54
+                                print "DPORTS", number
55
+                                complete_hash[sip][dip][number] += value
56
+                            else:
57
+                                complete_hash[sip][dip][number]= value
58
+                    else:
59
+                        complete_hash[sip][dip]= dports
60
+            else:
61
+                complete_hash[sip]= hash
62
+    return complete_hash
63
+
64
+
65
+def main():
66
+    startDate = "2018/09/1"
67
+    endDate = "2018/09/30"
68
+    otherHash = {}
69
+    counter = 0
70
+    process_num = 8
71
+    pool = mp.Pool(processes=process_num)
72
+    files = FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/etc/silk/conf-v9/silk.conf", data_rootdir="/home/scratch/flow/rwflowpack/")
73
+
74
+    files = [x for x in files]
75
+    print len(files)
76
+    fileHash = pool.map(verify_type, files) # FGlob(classname="all", type="all", start_date=startDate, end_date=endDate, site_config_file="/etc/silk/conf-v9/silk.conf", data_rootdir="/home/scratch/flow/rwflowpack/"))
77
+    flowHash = join_hash(fileHash)
78
+    print "FLOW", len(flowHash)
79
+    for sips in flowHash: #se itera por todos los dip y sus counters o puertos
80
+        #print sips
81
+        for dips, dports in flowHash[sips].items():
82
+            #print "Dip", dips, dports
83
+            if len(dports) >= 100: #si la cantidad de puertos es mayor o igual a 100, nos interesan
84
+                                #y por lo tanto se guardan en un hash
85
+                print "DIP", dips, len(dports)
86
+                if sips in otherHash:
87
+                    otherHash[sips][dips] = dports
88
+                else:
89
+                    otherHash[sips] = {dips: dports}
90
+
91
+    for dips, dports in otherHash.items():
92
+        counter +=1 #para contar los elementos del hash
93
+
94
+    print counter
95
+#print otherHash
96
+
97
+if __name__== "__main__":
98
+  main()

+ 61
- 0
Programas/Last_work/Multiprocess/trw.py View File

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