-
Notifications
You must be signed in to change notification settings - Fork 1
/
tshark-to-elastic-mapping.py
192 lines (157 loc) · 3.76 KB
/
tshark-to-elastic-mapping.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python
# to generate the template list do:
# tshark -G fields > tsharkfields
# cat tsharkfields |awk -F"\t" '{print "\""$4"\" : \"text\"," }' | sort | uniq
#
wireshark_ignore = [
"_ws_short",
"_ws_malformed",
"_ws_unreassembled",
"_ws_malformed_dissector_bug",
"_ws_malformed_reassembly",
"_ws_malformed_expert",
"_ws_type_length",
"_ws_type_length_mismatch",
"_ws_number_string_decoding_error",
"_ws_number_string_decoding_error_failed",
"_ws_number_string_decoding_error_erange"
]
dissectors = [
"tcp",
"ip",
"eth"
]
wireshark_to_elastic_types = {
"" : "text",
"FT_ABSOLUTE_TIME" : "text",
"FT_AX25" : "text",
"FT_BOOLEAN" : "boolean",
"FT_BYTES" : "text",
"FT_DOUBLE" : "double",
"FT_ETHER" : "text",
"FT_EUI64" : "text",
"FT_FCWWN" : "text",
"FT_FLOAT" : "float",
"FT_FRAMENUM" : "text",
"FT_GUID" : "text",
"FT_IEEE_11073_FLOAT" : "text",
"FT_IEEE_11073_SFLOAT" : "text",
"FT_INT16" : "short",
"FT_INT24" : "text",
"FT_INT32" : "integer",
"FT_INT64" : "long",
"FT_INT8" : "byte",
"FT_IPXNET" : "text",
"FT_IPv4" : "ip",
"FT_IPv6" : "ip",
"FT_NONE" : "text",
"FT_OID" : "text",
"FT_PROTOCOL" : "text",
"FT_RELATIVE_TIME" : "text",
"FT_REL_OID" : "text",
"FT_STRING" : "text",
"FT_STRINGZ" : "text",
"FT_STRINGZPAD" : "text",
"FT_SYSTEM_ID" : "text",
"FT_UINT16" : "short",
"FT_UINT24" : "integer",
"FT_UINT32" : "integer",
"FT_UINT40" : "long",
"FT_UINT48" : "long",
"FT_UINT64" : "long",
"FT_UINT8" : "byte",
"FT_UINT_BYTES" : "text",
"FT_UINT_STRING" : "text",
"FT_VINES" : "text",
}
preamble = """{
"mappings": {
"pcap_file": {
"properties": {
"layers": {
"properties": {
"""
imbetween ="""
"tcp_dstport": {
"type": "short",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"""
tailer = """
}
}
}
}
},
"timestamp": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
"""
# extract tshark field types
# tshark -G fields |awk -F"\t" '{print $3","$4","$2}' | sort > sortedfields
import csv
import sys
sys.stdout.write(preamble)
first = True
last = False
firstafterproto = True
countelements = 0
with open('ethernet.mapping', 'r') as csvfile:
fieldreader = csv.reader(csvfile, delimiter=',')
for row in fieldreader:
# elastic default in put from tshark ek is underscores so we have to adapt
displayfilter = row[0].replace(".","_")
tsharktype = row[1]
description = row[2]
elastictype = wireshark_to_elastic_types[tsharktype]
# ignore strange types
if displayfilter in wireshark_ignore:
continue
# check if it is a new protocol
if not "_" in displayfilter:
if displayfilter not in dissectors:
continue
firstafterproto = True
if not first:
if not countelements == 0:
print " }"
print " }"
print " },"
else:
first=False
countelements = 0
print " \""+displayfilter+"\": {"
print " \"properties\": {"
else:
doit = False
for dissector in dissectors:
if displayfilter.startswith(dissector+"_"):
doit=True
if doit == False:
continue
countelements = countelements + 1
if firstafterproto:
firstafterproto = False
else:
print " },"
# default to text
if elastictype == None:
elastictype = "text"
print " \""+displayfilter+"\": {"
print " \"type\": \""+elastictype+"\""
print tailer