Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add md5 file for the rulefile and generator #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rules/traffic-id.rules.md5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
48c5e2cca7cf67f7711a30da4c99c103
24 changes: 21 additions & 3 deletions traffic-id.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import sys
import os
import argparse

import hashlib
import yaml

ID_PREFIX = "traffic/id"
Expand All @@ -38,14 +38,17 @@
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

Expand Down Expand Up @@ -89,6 +92,7 @@ def print_tls_sni(args, output, config):

SID += 1


def print_rules(args, output, config):
global SID

Expand All @@ -100,7 +104,8 @@ def print_rules(args, output, config):
if "msg" in rule:
options += ["msg:\"SURICATA TRAFFIC-ID: %s\"" % (rule["msg"])]
else:
options += ["msg:\"SURICATA TRAFFIC-ID: %s\"" % as_list(rule["id"])]
options += ["msg:\"SURICATA TRAFFIC-ID: %s\"" %
as_list(rule["id"])]

if "http_host" in rule:
options += [
Expand Down Expand Up @@ -132,9 +137,11 @@ def print_rules(args, output, config):

SID += 1


def generate_rules(args):
if args.output:
output = open(args.output, "w")
md5_file = open(args.output+".md5", "w")
else:
output = sys.stdout

Expand All @@ -159,6 +166,14 @@ def generate_rules(args):
print_tls_sni(args, output, config[key])
elif key == "rules":
print_rules(args, output, config[key])
output.close()

if args.output:
# Generate the md5 hashsum file
hash_md5 = hashlib.md5(open(args.output, 'rb').read()).hexdigest()
md5_file.write(hash_md5)
md5_file.close()


def load_configs():
configs = []
Expand All @@ -169,12 +184,14 @@ def load_configs():
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("--disable-noalert",
action="store_true", default=False)
parser.add_argument("command", metavar="<command>",
help="Command to run")
args = parser.parse_args()
Expand Down Expand Up @@ -206,5 +223,6 @@ def main():
for name in names:
print(name)


if __name__ == "__main__":
sys.exit(main())