-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_sender.py
140 lines (116 loc) · 4.3 KB
/
parse_sender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
import json
import os
import sys
import fileinput
# Reconocemos todos los .pcap del directorio
lista_ficheros = os.listdir()
capturas = []
for campo in lista_ficheros:
if "captura" not in campo:
continue
else:
capturas.append(campo)
# Parse a JSON
for nombre_captura in capturas:
nombre_json = nombre_captura.replace(".pcap", ".json")
os.system("tshark -r " + nombre_captura + " -T ek > " + nombre_json)
os.system("more titulo.txt")
# Etiquetado
# Envio a ELASTICSEARCH
lista_ficheros_prev = os.listdir()
lista_ficheros_json_prev = []
# Recuperamos los ficheros json del directorio
for campo in lista_ficheros_prev:
if ".json" not in campo:
continue
else:
lista_ficheros_json_prev.append(campo)
for json_file in lista_ficheros_json_prev:
print ("En el fichero [" + json_file + "]")
opcion_etiquetado = input ("Qué tipo de tráfico se ha generado?:\n\n [1] Normal\n\n [2] Anómalo\n\n [3] Ataque\n\n ----------------\n Opción: ")
if opcion_etiquetado == "1":
for i, line in enumerate(fileinput.input(json_file, inplace=1)):
sys.stdout.write(line.replace("}}}", "}},\"traffic_type\": \"normal\"}"))
sys.stdout.write(line.replace("}}}}", "}}},\"traffic_type\": \"normal\"}"))
elif opcion_etiquetado == "2":
for i, line in enumerate(fileinput.input(json_file, inplace=1)):
sys.stdout.write(line.replace("}}}", "}},\"traffic_type\": \"anomalo\"}"))
sys.stdout.write(line.replace("}}}}", "}}},\"traffic_type\": \"anomalo\"}"))
elif opcion_etiquetado == "3":
desc_ataque = input ("Qué tipo de ataque se ha producido?: ")
for i, line in enumerate(fileinput.input(json_file, inplace=1)):
sys.stdout.write(line.replace("}}}", "}},\"traffic_type\": \"attack - " + desc_ataque + "\"}"))
sys.stdout.write(line.replace("}}}}", "}}},\"traffic_type\": \"attack - " + desc_ataque +"\"}"))
# Envio a ELASTICSEARCH
lista_ficheros = os.listdir()
lista_ficheros_json = []
# Recuperamos los ficheros json del directorio
for campo in lista_ficheros:
if ".json" not in campo:
continue
else:
lista_ficheros_json.append(campo)
# Split, si es necesario
# Máximo 100mb por fichero
max_size = 100000000
for nombre_fichero in lista_ficheros_json:
json_size = os.path.getsize(nombre_fichero)
print(json_size)
if json_size > max_size: # json_size > max_size
lista_ficheros_json.remove(nombre_fichero)
# Hay que dividir
with open(nombre_fichero) as fp:
line = fp.readline()
cnt = 0
cnt_file = 1
for line in fp:
#print (line)
fichero_resultado = "fichero_resultado[" + str(cnt_file) + "].json"
file2 = open (fichero_resultado, "w")
line2 = fp.readline()
file2.write(line2)
file2.write(line)
cnt += 1
# Criterios de parada
# "pcap_file"}
# }}}}
if cnt == 2:
cnt = 0
cnt_file += 1
#print ("Fin fichero")
#file2.close()
# A AQUÍ
# Continuamos con el envio
ip_elastic_server_public = "elk404.ddns.net"
ip_elastic_server_private = "11.0.0.2"
direccion = input ("A qué servidor desea enviarlo?:\n\n [1] Público\n\n [2] Privado\n\n ----------------\n Opción: ")
target_server = ""
if direccion == "1":
print ("Enviando al servidor ELASTICSEARCH [" + ip_elastic_server_public + "]")
target_server = ip_elastic_server_public
else:
print ("Enviando al servidor ELASTICSEARCH")
target_server = ip_elastic_server_private
comando_envio = "curl -s -H \"Content-Type: application/x-ndjson\" -XPOST \"" + target_server + ":9200/_bulk\" --data-binary \"@NOMBRE.JSON\""
os.system("more envio.txt")
# Envio a ELASTICSEARCH
#for json_file in lista_ficheros_json:
# comando_envio = comando_envio.replace("NOMBRE.JSON", json_file)
# print(comando_envio)
#os.system(comando_envio)
# Envio a de los json fragmentados
os.system("rm *.swp")
lista_all = os.listdir()
lista_ficheros_json_frag = []
for campo in lista_all:
if "].json" not in campo:
continue
else:
lista_ficheros_json_frag.append(campo)
comando_envio_aux = "curl -s -H \"Content-Type: application/x-ndjson\" -XPOST \"11.0.0.2:9200/_bulk\" --data-binary \"@NOMBRE.JSON\""
for json_file in lista_ficheros_json_frag:
#json_file_formatted = json_file.replace("[", "\[")
#json_file_formatted = json_file.replace("]", "\]")
comando_envio_aux = comando_envio_aux.replace("NOMBRE.JSON", json_file)
os.system(comando_envio_aux)