This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
traffic-id.py
executable file
·210 lines (167 loc) · 6.42 KB
/
traffic-id.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#! /usr/bin/env python3
#
# Copyright (C) 2017 Open Information Security Foundation
#
# You can copy, redistribute or modify this Program under the terms of
# the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# version 2 along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
"""This tools accepts Suricata appid definitions in YAML format and
outputs the appid rule set.
It is not intended for end-users of appid, instead end-users should
grab the generated .rules files.
"""
import sys
import os
import argparse
import yaml
ID_PREFIX = "traffic/id"
LABEL_PREFIX = "traffic/label"
SID = 300000000
LABELS = {}
IDMAP = {}
def print_err(msg):
print(msg, file=sys.stderr)
def as_list(_id):
if isinstance(_id, type([])):
return _id
return [_id]
def print_tls_sni(args, output, config):
global SID
template = """alert tls any any -> any %(dport)s (msg:"%(msg)s"; tls_sni; content:"%(content)s"; isdataat:!1,relative; flow:to_server,established; %(flowbits)s; %(noalert)ssid:%(sid)d; rev:1;)"""
for tls in config:
if not "id" in tls:
raise Exception("Missing id: %s" % (str(tls)))
msg = "SURICATA TRAFFIC-ID: %s" % (",".join(as_list(tls["id"])))
flowbits = []
for _id in as_list(tls["id"]):
flowbits.append("flowbits: set,%s/%s" % (ID_PREFIX, _id))
if "labels" in tls:
for label in as_list(tls["labels"]):
flowbits.append("flowbits:set,%s/%s" % (LABEL_PREFIX, label))
if not args.disable_noalert:
noalert = "noalert; "
else:
noalert = ""
if "ports" in tls:
ports = "[%s]" % (",".join([str(p) for p in tls["ports"]]))
else:
ports = "any"
for pattern in tls["patterns"]:
print(template % {
"msg": msg,
"content": pattern,
"flowbits": "; ".join(flowbits),
"sid": SID,
"noalert": noalert,
"dport": ports,
}, file=output)
SID += 1
def print_rules(args, output, config):
global SID
for rule in config:
proto = rule["proto"]
options = []
if "msg" in rule:
options += ["msg:\"SURICATA TRAFFIC-ID: %s\"" % (rule["msg"])]
else:
options += ["msg:\"SURICATA TRAFFIC-ID: %s\"" % as_list(rule["id"])]
if "http_host" in rule:
options += [
"content:\"%s\"" % (rule["http_host"]),
"http_host",
]
if "http_user_agent" in rule:
options += [
"content:\"%s\"" % (rule["http_user_agent"]),
"http_user_agent",
]
options.append("flow:to_server,established")
for _id in as_list(rule["id"]):
options.append("flowbits:set,%s/%s" % (ID_PREFIX, _id))
for label in as_list(rule["labels"]):
options.append("flowbits:set,%s/%s" % (LABEL_PREFIX, label))
if not args.disable_noalert:
options.append("noalert")
options += ["sid:%d" % (SID), "rev:1"]
print("alert %s any any -> any any (%s;)" % (
proto, "; ".join(options)), file=output)
SID += 1
def generate_rules(args):
if args.output:
output = open(args.output, "w")
else:
output = sys.stdout
# Print out the license.
with open("LICENSE.template") as fileobj:
lines = fileobj.readlines()
lines = ["# %s" % line for line in lines]
print("".join(lines), file=output)
for dirpath, dirnames, filenames in os.walk("."):
for filename in filenames:
if filename.endswith(".yaml"):
path = os.path.join(dirpath, filename)
with open(path) as fileobj:
config = yaml.load(fileobj, Loader=yaml.Loader)
if "labels" in config:
LABELS.update(config["labels"])
if "id-map" in config:
IDMAP.update(config["id-map"])
for key in config:
if key == "tls-sni-patterns":
print_tls_sni(args, output, config[key])
elif key == "rules":
print_rules(args, output, config[key])
def load_configs():
configs = []
for dirpath, dirnames, filenames in os.walk("."):
for filename in filenames:
if filename.endswith(".yaml"):
path = os.path.join(dirpath, filename)
configs.append(yaml.load(open(path)))
return configs
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", metavar="<filename>",
help="Output filename for rules")
parser.add_argument("--disable-noalert", action="store_true", default=False)
parser.add_argument("command", metavar="<command>",
help="Command to run")
args = parser.parse_args()
if args.command in ["gen", "generate"]:
generate_rules(args)
elif args.command == "list-labels":
labels = set()
configs = load_configs()
for config in configs:
for key in config:
if key == "tls-sni-patterns":
for label in config[key]:
for flowbit in label["flowbit"]:
if flowbit.startswith(LABEL_PREFIX):
labels.add(flowbit)
for label in labels:
print(label)
elif args.command == "list-ids":
names = set()
configs = load_configs()
for config in configs:
for key in config:
if key == "tls-sni-patterns":
for label in config[key]:
for flowbit in label["flowbit"]:
if flowbit.startswith(ID_PREFIX):
names.add(flowbit)
for name in names:
print(name)
if __name__ == "__main__":
sys.exit(main())