Skip to content

Commit

Permalink
ipaddr2list.py, ndpi2timeline.py: reformatted (#2509)
Browse files Browse the repository at this point in the history
pasabanov authored Jul 18, 2024
1 parent 92d0b8d commit be0b2c2
Showing 2 changed files with 47 additions and 47 deletions.
51 changes: 26 additions & 25 deletions example/ndpi2timeline.py
Original file line number Diff line number Diff line change
@@ -34,46 +34,46 @@
protos = {}
lastId = 1


def get_timestamp(seen):
tok = seen.split(".")
return int(tok[0]) * 1000 + int(tok[1])


def get_record(toks, csv_fields):
global protos
global lastId

if len(toks) < 11:
return None

record = dict()
record = {}
ndpiProtocol = toks[10]

ndpi_protos = ndpiProtocol.split(".")
if(len(ndpi_protos) == 1):
ndpi_protos = ndpiProtocol.split(".")
if len(ndpi_protos) == 1:
app_proto = ndpi_protos[0]
else:
app_proto = ndpi_protos[1]

id = protos.get(ndpiProtocol)
if(id == None):

if protos.get(ndpiProtocol) is None:
lastId = lastId + 1
protos[ndpiProtocol] = lastId
id = lastId
#print(ndpiProtocol+"="+str(id))
# print(ndpiProtocol + "=" + str(id))

ip_address = toks[5]
server_name = toks[11]
record["cat"] = "flow"
record["pid"] = ip_address
record["tid"] = ndpiProtocol # id
record["tid"] = ndpiProtocol # id
record["ts"] = get_timestamp(toks[2])
record["ph"] = "X"
record["name"] = app_proto

if(server_name == ""):
if server_name == "":
args = {}
else:
args = { "name": server_name }
args = {"name": server_name}
record["args"] = args
record["dur"] = get_timestamp(toks[3]) - record["ts"]

@@ -82,21 +82,22 @@ def get_record(toks, csv_fields):
return record

# Otherwise we just add everything we find as a string
if(0):
idx = 0
for tok in toks:
name = csv_fields[idx]
idx += 1
record["args"][name] = str(tok)
# if 0:
# idx = 0
# for tok in toks:
# name = csv_fields[idx]
# idx += 1
# record["args"][name] = str(tok)

return record


def get_record_dict(filename):
csv_fields = None
records = []
fin = open(filename, "r");
fin = open(filename, "r")
for line in fin:
line = line.replace("\n","")
line = line.replace("\n", "")

# Get the legend if present
if line[0] == '#':
@@ -116,20 +117,20 @@ def get_record_dict(filename):

records.append(record)

json_dict = dict()
json_dict["traceEvents"] = records
json_dict = {"traceEvents": records}

return json_dict


if __name__ == "__main__":
if len(sys.argv) != 3:
print("ndpi2json <csv_file> <json_file>")
sys.exit(0)

record_dict = get_record_dict(sys.argv[1])
#print(record_dict)
#json_string = json.dumps(json_dict)
#print(json_string)
# print(record_dict)
# json_string = json.dumps(json_dict)
# print(json_string)

with open(sys.argv[2], 'w') as fp:
json.dump(record_dict, fp)
43 changes: 21 additions & 22 deletions utils/ipaddr2list.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
#!/usr/bin/env python3

import sys
import socket, struct
import socket

# This scripts is mainly used to create "ip -> protocols" lists.
# However it is also used to create "ip -> risk" lists
# These scripts are mainly used to create "ip -> protocols" lists.
# However, it is also used to create "ip -> risk" lists
proto = "NDPI_PROTOCOL_XYX"
append_name = ""
if len (sys.argv) < 2 :
if len(sys.argv) < 2:
print("Usage: ipaddr2list.py <file> <protocol> [file6] [<append_name>]")
sys.exit (1)
sys.exit(1)

if len (sys.argv) >= 3:
if len(sys.argv) >= 3:
proto = sys.argv[2]

if len (sys.argv) >= 5:
if len(sys.argv) >= 5:
append_name = sys.argv[4]


print("""/*
*
* This file is generated automatically and part of nDPI
@@ -41,53 +40,53 @@
""")

print("static ndpi_network "+proto.lower()+append_name+"_protocol_list[] = {")
print("static ndpi_network " + proto.lower() + append_name + "_protocol_list[] = {")

lines = 0
with open(sys.argv[1]) as fp:
for cnt, line in enumerate(fp):
line = line.rstrip()

if(line != ""):
if line != "":
lines += 1
x = line.split("/")

if(len(x) == 2):
if len(x) == 2:
ipaddr = x[0]
cidr = x[1]
cidr = x[1]
else:
ipaddr = line
cidr = "32"

if(ipaddr != ""):
print(" { 0x"+socket.inet_aton(ipaddr).hex().upper()+" /* "+ipaddr+"/"+cidr+" */, "+cidr+", "+proto+" },")
if ipaddr != "":
print(" { 0x" + socket.inet_aton(ipaddr).hex().upper() + " /* " + ipaddr + "/" + cidr + " */, " + cidr + ", " + proto + " },")

print(" /* End */")
print(" { 0x0, 0, 0 }")
print("};")

print("");
print("static ndpi_network6 "+proto.lower()+append_name+"_protocol_list_6[] = {")
print("")
print("static ndpi_network6 " + proto.lower() + append_name + "_protocol_list_6[] = {")

if(len (sys.argv) >= 4):
if len(sys.argv) >= 4:

with open(sys.argv[3]) as fp:
for cnt, line in enumerate(fp):
line = line.rstrip()

if(line != ""):
if line != "":
lines += 1
x = line.split("/")

if(len(x) == 2):
if len(x) == 2:
ipaddr = x[0]
cidr = x[1]
cidr = x[1]
else:
ipaddr = line
cidr = "128"

if(ipaddr != ""):
print(" { \""+ipaddr+"\", "+cidr+", "+proto+" },")
if ipaddr != "":
print(" { \"" + ipaddr + "\", " + cidr + ", " + proto + " },")

print(" /* End */")
print(" { NULL, 0, 0 }")

0 comments on commit be0b2c2

Please sign in to comment.