From f465d46d92f0f8b9c8bdd2802f3bb80b426d34de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Aime?= Date: Fri, 8 Jan 2021 11:49:30 +0100 Subject: [PATCH] Adding infos related to the capture in the generated report --- analysis/classes/report.py | 8 +++++-- server/frontend/app/classes/capture.py | 29 ++++++++++++++++++++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/analysis/classes/report.py b/analysis/classes/report.py index 49a9b78..7c1281d 100644 --- a/analysis/classes/report.py +++ b/analysis/classes/report.py @@ -20,7 +20,8 @@ class Report(object): capture_directory, "assets/conns.json")) self.device = self.read_json(os.path.join( capture_directory, "assets/device.json")) - + self.capinfos = self.read_json(os.path.join( + capture_directory, "assets/capinfos.json")) try: with open(os.path.join(self.capture_directory, "capture.pcap"), "rb") as f: self.capture_sha1 = hashlib.sha1(f.read()).hexdigest() @@ -184,7 +185,10 @@ class Report(object): self.device["mac_address"]) header += "Report generated on {}
".format( datetime.now().strftime("%d/%m/%Y at %H:%M:%S")) - + header += "Capture duration: {}
".format( + self.capinfos["Capture duration"]) + header += "Number of packets: {}
".format( + self.capinfos["Number of packets"]) header += "Capture SHA1: {}
".format(self.capture_sha1) header += "

" header += "" diff --git a/server/frontend/app/classes/capture.py b/server/frontend/app/classes/capture.py index 7e0bc12..ff74457 100644 --- a/server/frontend/app/classes/capture.py +++ b/server/frontend/app/classes/capture.py @@ -7,6 +7,7 @@ from os import mkdir, path from flask import send_file, jsonify import datetime import shutil +import json import random import sys import re @@ -15,9 +16,6 @@ import re class Capture(object): def __init__(self): - self.capture_dir = False - self.assets_dir = False - self.capture_token = False self.random_choice_alphabet = "ABCDEF1234567890" def start_capture(self): @@ -98,14 +96,33 @@ class Capture(object): def stop_capture(self): """ - Stoping tshark if any instance present. + Stop tshark if any instance present & ask create_capinfos. :return: dict as a small confirmation. """ - - # Kill instance of tshark if any. if terminate_process("tshark"): + self.create_capinfos() return {"status": True, "message": "Capture stopped"} else: return {"status": False, "message": "No active capture"} + + def create_capinfos(self): + """ + Creates a capinfo json file. + :return: dict as a small confirmation. + """ + infos = sp.Popen(["capinfos", self.pcap], + stdout=sp.PIPE, stderr=sp.PIPE) + infos = infos.communicate()[0] + data = {} + for l in infos.decode().splitlines(): + try: + l = l.split(": ") if ": " in l else l.split("= ") + if len(l[0]) and len(l[1]): + data[l[0].strip()] = l[1].strip() + except: + continue + with open("{}capinfos.json".format(self.assets_dir), 'w') as f: + json.dump(data, f) + return True