Refactor and add Logger
This commit is contained in:
0
code/iottb/__init__.py
Normal file
0
code/iottb/__init__.py
Normal file
38
code/iottb/__main__.py
Normal file
38
code/iottb/__main__.py
Normal file
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
|
||||
from iottb.subcommands.capture import setup_capture_parser
|
||||
from iottb.subcommands.add_device import setup_init_device_root_parser
|
||||
|
||||
|
||||
######################
|
||||
# Argparse setup
|
||||
######################
|
||||
def setup_argparse():
|
||||
# create top level parser
|
||||
root_parser = argparse.ArgumentParser(prog="iottb")
|
||||
subparsers = root_parser.add_subparsers(title="subcommands", required=True, dest="command")
|
||||
|
||||
setup_capture_parser(subparsers)
|
||||
setup_init_device_root_parser(subparsers)
|
||||
|
||||
return root_parser
|
||||
|
||||
|
||||
def main():
|
||||
parser = setup_argparse()
|
||||
args = parser.parse_args()
|
||||
print(args)
|
||||
if args.command:
|
||||
try:
|
||||
args.func(args)
|
||||
except KeyboardInterrupt:
|
||||
print("Received keyboard interrupt. Exiting...")
|
||||
exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
# create_capture_directory(args.device_name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
26
code/iottb/definitions.py
Normal file
26
code/iottb/definitions.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from datetime import datetime
|
||||
from enum import Flag, unique, global_enum
|
||||
|
||||
DEVICE_METADATA_FILE = "device_metadata.json"
|
||||
CAPTURE_METADATA_FILE = "capture_metadata.json"
|
||||
TODAY_DATE_STRING = datetime.now().strftime("%d%b%Y").lower() # TODO convert to function in utils or so
|
||||
|
||||
CAPTURE_FOLDER_BASENAME = "capture_###"
|
||||
|
||||
AFFIRMATIVE_USER_RESPONSE = {"yes", "y", "true", "Y", "Yes", "YES"}
|
||||
NEGATIVE_USER_RESPONSE = {"no", "n", "N", "No"}
|
||||
YES_DEFAULT = AFFIRMATIVE_USER_RESPONSE.union({"", " "})
|
||||
NO_DEFAULT = NEGATIVE_USER_RESPONSE.union({"", " "})
|
||||
|
||||
|
||||
@unique
|
||||
@global_enum
|
||||
class ReturnCodes(Flag):
|
||||
SUCCESS = 0
|
||||
ABORTED = 1
|
||||
FAILURE = 2
|
||||
UNKNOWN = 3
|
||||
FILE_NOT_FOUND = 4
|
||||
FILE_ALREADY_EXISTS = 5
|
||||
INVALID_ARGUMENT = 6
|
||||
INVALID_ARGUMENT_VALUE = 7
|
||||
28
code/iottb/logger.py
Normal file
28
code/iottb/logger.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import logging
|
||||
import sys
|
||||
from logging.handlers import RotatingFileHandler
|
||||
|
||||
|
||||
def setup_logging():
|
||||
logger_obj = logging.getLogger('iottbLogger')
|
||||
logger_obj.setLevel(logging.INFO)
|
||||
|
||||
file_handler = RotatingFileHandler('iottb.log')
|
||||
console_handler = logging.StreamHandler(sys.stdout)
|
||||
|
||||
file_handler.setLevel(logging.DEBUG)
|
||||
console_handler.setLevel(logging.INFO)
|
||||
|
||||
file_fmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
console_fmt = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
|
||||
|
||||
file_handler.setFormatter(file_fmt)
|
||||
console_handler.setFormatter(console_fmt)
|
||||
|
||||
logger_obj.addHandler(file_handler)
|
||||
logger_obj.addHandler(console_handler)
|
||||
|
||||
return logger_obj
|
||||
|
||||
|
||||
logger = setup_logging()
|
||||
0
code/iottb/models/__init__.py
Normal file
0
code/iottb/models/__init__.py
Normal file
157
code/iottb/models/capture_metadata_model.py
Normal file
157
code/iottb/models/capture_metadata_model.py
Normal file
@@ -0,0 +1,157 @@
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Optional, Any
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from iottb.definitions import ReturnCodes, CAPTURE_METADATA_FILE
|
||||
from iottb.models.device_metadata_model import DeviceMetadata
|
||||
|
||||
|
||||
class CaptureMetadata(BaseModel):
|
||||
# Required Fields
|
||||
device_metadata: DeviceMetadata = Field(exclude=True)
|
||||
capture_id: uuid.UUID = Field(default_factory=lambda: str(uuid.uuid4()))
|
||||
capture_dir: Path
|
||||
capture_file: str
|
||||
capture_date: str = Field(default_factory=lambda: datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower())
|
||||
|
||||
# Statistics
|
||||
start_time: str
|
||||
stop_time: str
|
||||
|
||||
# tcpdump
|
||||
packet_count: Optional[int]
|
||||
pcap_filter: str = ""
|
||||
tcpdump_command: str = ""
|
||||
interface: str = ""
|
||||
|
||||
# Optional Fields
|
||||
device_ip_address: Optional[str] = "No IP Address set"
|
||||
device_mac_address: Optional[str] = None
|
||||
|
||||
app: Optional[str] = None
|
||||
app_version: Optional[str] = None
|
||||
firmware_version: Optional[str] = None
|
||||
|
||||
def __init__(self, device_metadata: DeviceMetadata, capture_dir: Path, /, **data: Any):
|
||||
super().__init__(**data) # Pycharms orders
|
||||
self.device_metadata = device_metadata
|
||||
self.capture_dir = capture_dir
|
||||
assert capture_dir.is_dir()
|
||||
|
||||
# Getters
|
||||
def get_device_id(self) -> str:
|
||||
return self.device_id
|
||||
|
||||
def get_start_time(self) -> str:
|
||||
return self.start_time
|
||||
|
||||
def get_stop_time(self) -> str:
|
||||
return self.stop_time
|
||||
|
||||
def get_packet_count(self) -> int:
|
||||
return self.packet_count
|
||||
|
||||
def get_pcap_filter(self) -> str:
|
||||
return self.pcap_filter
|
||||
|
||||
def get_device_ip_address(self) -> str:
|
||||
return self.device_ip_address
|
||||
|
||||
def get_device_mac_address(self) -> str:
|
||||
return self.device_mac_address
|
||||
|
||||
def get_app(self) -> str:
|
||||
return self.app
|
||||
|
||||
def get_app_version(self) -> str:
|
||||
return self.app_version
|
||||
|
||||
def get_firmware_version(self) -> str:
|
||||
return self.firmware_version
|
||||
|
||||
def get_capture_id(self) -> UUID:
|
||||
return self.capture_id
|
||||
|
||||
def get_capture_date(self) -> str:
|
||||
return self.capture_date
|
||||
|
||||
def get_capfile_name(self):
|
||||
return self.capture_file
|
||||
|
||||
def get_device_metadata(self) -> DeviceMetadata:
|
||||
return self.device_metadata
|
||||
|
||||
def get_interface(self):
|
||||
return self.interface
|
||||
|
||||
# Setters
|
||||
def set_capture_dir(self, capture_dir: Path):
|
||||
self.capture_dir = capture_dir
|
||||
|
||||
def set_capture_file(self, capture_file: str):
|
||||
self.capture_file = capture_file
|
||||
|
||||
def set_capture_date(self, capture_date: str):
|
||||
self.capture_date = capture_date
|
||||
|
||||
def set_start_time(self, start_time: str):
|
||||
self.start_time = start_time
|
||||
|
||||
def set_stop_time(self, stop_time: str):
|
||||
self.stop_time = stop_time
|
||||
|
||||
def set_packet_count(self, packet_count: int):
|
||||
self.packet_count = packet_count
|
||||
|
||||
def set_pcap_filter(self, pcap_filter: str):
|
||||
self.pcap_filter = pcap_filter
|
||||
|
||||
def set_device_ip_address(self, device_ip_address: str):
|
||||
self.device_ip_address = device_ip_address
|
||||
|
||||
def set_device_mac_address(self, device_mac_address: str):
|
||||
self.device_mac_address = device_mac_address
|
||||
|
||||
def set_app(self, app: str):
|
||||
self.app = app
|
||||
|
||||
def set_app_version(self, app_version: str):
|
||||
self.app_version = app_version
|
||||
|
||||
def set_firmware_version(self, firmware_version: str):
|
||||
self.firmware_version = firmware_version
|
||||
self.device_metadata.set_device_firmware_version(firmware_version)
|
||||
|
||||
def set_interface(self, interface: str):
|
||||
self.interface = interface
|
||||
|
||||
def set_tcpdump_command(self, tcpdump_command: str):
|
||||
self.tcpdump_command = tcpdump_command
|
||||
|
||||
# Other
|
||||
|
||||
def build_capture_file_name(self):
|
||||
prefix = ""
|
||||
if self.app is None:
|
||||
prefix = self.device_metadata.get_device_short_name()
|
||||
else:
|
||||
assert str(self.app).strip() not in {"", " "}, f"app is not a valid name: {self.app}"
|
||||
prefix = self.get_app()
|
||||
# assert self.capture_dir is not None, f"{self.capture_dir} does not exist"
|
||||
filename = f"{prefix}_{str(self.capture_id)}.pcap"
|
||||
self.set_capture_file(filename)
|
||||
|
||||
def save_capture_metadata_to_json(self, file_path: Path = Path(CAPTURE_METADATA_FILE)):
|
||||
assert self.capture_dir.is_dir(), f"capture_dir is not a directory: {self.capture_dir}"
|
||||
if file_path.is_file():
|
||||
print(f"File {file_path} already exists, update instead.")
|
||||
return ReturnCodes.FILE_ALREADY_EXISTS
|
||||
metadata = self.model_dump_json(indent=2, exclude_unset=True, exclude_none=True)
|
||||
with file_path.open('w') as file:
|
||||
json.dump(metadata, file)
|
||||
return ReturnCodes.SUCCESS
|
||||
128
code/iottb/models/device_metadata_model.py
Normal file
128
code/iottb/models/device_metadata_model.py
Normal file
@@ -0,0 +1,128 @@
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Optional, List, Any
|
||||
|
||||
# iottb modules
|
||||
from iottb.definitions import ReturnCodes, DEVICE_METADATA_FILE
|
||||
# 3rd party libs
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
IMMUTABLE_FIELDS = {"device_name", "device_short_name", "device_id", "date_created"}
|
||||
|
||||
|
||||
class DeviceMetadata(BaseModel):
|
||||
# Required fields
|
||||
device_name: str
|
||||
device_short_name: str
|
||||
device_id: str = Field(default_factory=lambda: str(uuid.uuid4()))
|
||||
date_created: str = Field(default_factory=lambda: datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower())
|
||||
|
||||
device_root_path: Path
|
||||
# Optional Fields
|
||||
aliases: List[str] = Field(default_factory=lambda: [])
|
||||
device_type: Optional[str] = None
|
||||
device_serial_number: Optional[str] = None
|
||||
device_firmware_version: Optional[str] = None
|
||||
date_updated: Optional[str] = None
|
||||
|
||||
capture_files: Optional[List[str]] = []
|
||||
|
||||
def __init__(self, device_name: str, device_root_dir: Path, /, **data: Any):
|
||||
super().__init__(**data)
|
||||
self.device_name = device_name
|
||||
self.device_short_name = device_name.lower().replace(" ", "_")
|
||||
# assert dir_contains_device_metadata(device_root_dir), \
|
||||
# f"Directory {device_root_dir} is missing a {DEVICE_METADATA_FILE} file"
|
||||
self.device_root_dir = device_root_dir
|
||||
|
||||
def get_device_id(self) -> str:
|
||||
return self.device_id
|
||||
|
||||
def get_device_name(self) -> str:
|
||||
return self.device_name
|
||||
|
||||
def get_device_short_name(self) -> str:
|
||||
return self.device_short_name
|
||||
|
||||
def get_device_type(self) -> str:
|
||||
return self.device_type
|
||||
|
||||
def get_device_serial_number(self) -> str:
|
||||
return self.device_serial_number
|
||||
|
||||
def get_device_firmware_version(self) -> str:
|
||||
return self.device_firmware_version
|
||||
|
||||
def get_date_updated(self) -> str:
|
||||
return self.date_updated
|
||||
|
||||
def get_capture_files(self) -> List[str]:
|
||||
return self.capture_files
|
||||
|
||||
def get_aliases(self) -> List[str]:
|
||||
return self.aliases
|
||||
|
||||
def set_device_type(self, device_type: str) -> None:
|
||||
self.device_type = device_type
|
||||
self.date_updated = datetime.now().strftime('%d-%m-%YT%H:%M:%S')
|
||||
|
||||
def set_device_serial_number(self, device_serial_number: str) -> None:
|
||||
self.device_serial_number = device_serial_number
|
||||
self.date_updated = datetime.now().strftime('%d-%m-%YT%H:%M:%S')
|
||||
|
||||
def set_device_firmware_version(self, device_firmware_version: str) -> None:
|
||||
self.device_firmware_version = device_firmware_version
|
||||
self.date_updated = datetime.now().strftime('%d-%m-%YT%H:%M:%S')
|
||||
|
||||
def set_device_name(self, device_name: str) -> None:
|
||||
self.device_name = device_name
|
||||
self.device_short_name = device_name.lower().replace(" ", "_")
|
||||
self.date_updated = datetime.now().strftime('%d-%m-%YT%H:%M:%S')
|
||||
|
||||
@classmethod
|
||||
def load_from_json(cls, device_file_path: Path):
|
||||
assert device_file_path.is_file(), f"{device_file_path} is not a file"
|
||||
assert device_file_path.name == DEVICE_METADATA_FILE, f"{device_file_path} is not a {DEVICE_METADATA_FILE}"
|
||||
device_meta_filename = device_file_path
|
||||
with device_meta_filename.open('r') as file:
|
||||
metadata_json = json.load(file)
|
||||
metadata_model_obj = cls.model_validate_json(metadata_json)
|
||||
return metadata_model_obj
|
||||
|
||||
def save_to_json(self, file_path: Path):
|
||||
if file_path.is_file():
|
||||
print(f"File {file_path} already exists, update instead.")
|
||||
return ReturnCodes.FILE_ALREADY_EXISTS
|
||||
metadata = self.model_dump_json(indent=2)
|
||||
with file_path.open('w') as file:
|
||||
json.dump(metadata, file)
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
@classmethod
|
||||
def update_metadata_in_json(cls, file_path: Path, **kwargs):
|
||||
# TODO Maybe not needed at all.
|
||||
assert file_path.is_file()
|
||||
for field in IMMUTABLE_FIELDS:
|
||||
if field in kwargs:
|
||||
print(f"Field {field} is immutable")
|
||||
return ReturnCodes.IMMUTABLE
|
||||
metadata = cls.load_from_json(file_path)
|
||||
for field, value in kwargs.items():
|
||||
if field in metadata.model_fields_set:
|
||||
setattr(metadata, field, value)
|
||||
metadata.date_updated = datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower()
|
||||
pass
|
||||
|
||||
|
||||
def dir_contains_device_metadata(dir_path: Path):
|
||||
if not dir_path.is_dir():
|
||||
return False
|
||||
else:
|
||||
meta_file_path = dir_path / DEVICE_METADATA_FILE
|
||||
print(f"Device metadata file path {str(meta_file_path)}")
|
||||
if not meta_file_path.is_file():
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
0
code/iottb/subcommands/__init__.py
Normal file
0
code/iottb/subcommands/__init__.py
Normal file
59
code/iottb/subcommands/add_device.py
Normal file
59
code/iottb/subcommands/add_device.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import pathlib
|
||||
|
||||
from iottb import definitions
|
||||
from iottb.definitions import DEVICE_METADATA_FILE, ReturnCodes
|
||||
from iottb.models.device_metadata_model import DeviceMetadata
|
||||
from iottb.utils.device_metadata_utils import *
|
||||
|
||||
|
||||
def setup_init_device_root_parser(subparsers):
|
||||
parser = subparsers.add_parser("add-device", aliases=["add-device-root", "add"])
|
||||
parser.add_argument("--root_dir", type=pathlib.Path, default=pathlib.Path.cwd())
|
||||
group = parser.add_mutually_exclusive_group()
|
||||
group.add_argument("--guided", action="store_true", help="Guided setup", default=False)
|
||||
group.add_argument("--name", action="store", type=str, help="name of device")
|
||||
parser.set_defaults(func=handle_add)
|
||||
|
||||
|
||||
def handle_add(args):
|
||||
print("Entered add-device-root")
|
||||
|
||||
if args.guided:
|
||||
metadata = guided_setup(args.root_dir)
|
||||
else:
|
||||
device_name = args.name
|
||||
args.root_dir.mkdir(parents=True, exist_ok=True)
|
||||
args.root_dir.chdir()
|
||||
metadata = DeviceMetadata(device_name, args.root_dir)
|
||||
|
||||
file_path = args.root_dir / DEVICE_METADATA_FILE
|
||||
response = input(f"Confirm device metadata: {metadata.model_dump()} [y/N]")
|
||||
if response.lower() not in definitions.AFFIRMATIVE_USER_RESPONSE.add(""):
|
||||
configure_metadata()
|
||||
assert False, "TODO implement dynamic setup"
|
||||
assert metadata.model_dump() != ""
|
||||
if metadata.save_to_json(file_path) == ReturnCodes.FILE_ALREADY_EXISTS:
|
||||
print("Directory already contains a device metadata file. Aborting operation.")
|
||||
return ReturnCodes.ABORTED
|
||||
assert Path(file_path).exists(), f"{file_path} does not exist"
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
|
||||
def configure_metadata():
|
||||
pass
|
||||
|
||||
|
||||
def guided_setup(device_root) -> DeviceMetadata:
|
||||
response = "N"
|
||||
device_name = ""
|
||||
while response.upper() == "N":
|
||||
device_name = input("Please enter name of device: ")
|
||||
if device_name == "" or device_name is None:
|
||||
print("Name cannot be empty")
|
||||
response = input(f"Confirm device name: {device_name} [y/N] ")
|
||||
|
||||
assert response.lower() in definitions.AFFIRMATIVE_USER_RESPONSE.add(""), f"{response.upper()} not supported"
|
||||
assert device_name != ""
|
||||
assert device_name is not None
|
||||
return DeviceMetadata(device_name, device_root)
|
||||
|
||||
170
code/iottb/subcommands/capture.py
Normal file
170
code/iottb/subcommands/capture.py
Normal file
@@ -0,0 +1,170 @@
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from iottb.definitions import *
|
||||
from iottb.models.capture_metadata_model import CaptureMetadata
|
||||
from iottb.models.device_metadata_model import DeviceMetadata, dir_contains_device_metadata
|
||||
from iottb.utils.capture_utils import get_capture_src_folder, make_capture_src_folder
|
||||
|
||||
|
||||
def setup_capture_parser(subparsers):
|
||||
parser = subparsers.add_parser('sniff', help='Sniff packets with tcpdump')
|
||||
# metadata args
|
||||
parser.add_argument("-a", "--ip-address", help="IP address of the device to sniff", dest="device_ip")
|
||||
# tcpdump args
|
||||
parser.add_argument("device_root", help="Root folder for device to sniff",
|
||||
type=Path, default=Path.cwd())
|
||||
parser.add_argument("-s", "--safe", help="Ensure correct device root folder before sniffing", action="store_true")
|
||||
parser.add_argument("--app", help="Application name to sniff", dest="app_name", default=None)
|
||||
|
||||
parser_sniff_tcpdump = parser.add_argument_group('tcpdump arguments')
|
||||
parser_sniff_tcpdump.add_argument("-i", "--interface", help="Interface to capture on.", dest="capture_interface",
|
||||
required=True)
|
||||
parser_sniff_tcpdump.add_argument("-I", "--monitor-mode", help="Put interface into monitor mode",
|
||||
action="store_true")
|
||||
parser_sniff_tcpdump.add_argument("-n", help="Deactivate name resolution. True by default.",
|
||||
action="store_true", dest="no_name_resolution")
|
||||
parser_sniff_tcpdump.add_argument("-#", "--number",
|
||||
help="Print packet number at beginning of line. True by default.",
|
||||
action="store_true")
|
||||
parser_sniff_tcpdump.add_argument("-e", help="Print link layer headers. True by default.",
|
||||
action="store_true", dest="print_link_layer")
|
||||
parser_sniff_tcpdump.add_argument("-t", action="count", default=0,
|
||||
help="Please see tcpdump manual for details. Unused by default.")
|
||||
|
||||
cap_size_group = parser.add_mutually_exclusive_group(required=False)
|
||||
cap_size_group.add_argument("-c", "--count", type=int, help="Number of packets to capture.", default=1000)
|
||||
cap_size_group.add_argument("--mins", type=int, help="Time in minutes to capture.", default=1)
|
||||
|
||||
parser.set_defaults(func=handle_capture)
|
||||
|
||||
|
||||
def cwd_is_device_root_dir() -> bool:
|
||||
device_metadata_file = Path.cwd() / DEVICE_METADATA_FILE
|
||||
return device_metadata_file.is_file()
|
||||
|
||||
|
||||
def start_guided_device_root_dir_setup():
|
||||
assert False, "Not implemented"
|
||||
|
||||
|
||||
def handle_metadata():
|
||||
assert not cwd_is_device_root_dir()
|
||||
print(f"Unable to find {DEVICE_METADATA_FILE} in current working directory")
|
||||
print("You need to setup a device root directory before using this command")
|
||||
response = input("Would you like to be guided through the setup? [y/n]")
|
||||
if response.lower() == "y":
|
||||
start_guided_device_root_dir_setup()
|
||||
else:
|
||||
print("'iottb init-device-root --help' for more information.")
|
||||
exit(ReturnCodes.ABORTED)
|
||||
# device_id = handle_capture_metadata()
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
|
||||
def get_device_metadata_from_file(device_metadata_filename: Path) -> str:
|
||||
assert device_metadata_filename.is_file(), f"Device metadata file '{device_metadata_filename} does not exist"
|
||||
device_metadata = DeviceMetadata.load_from_json(device_metadata_filename)
|
||||
return device_metadata
|
||||
|
||||
|
||||
def run_tcpdump(cmd):
|
||||
# TODO: Maybe specify files for stout and stderr
|
||||
try:
|
||||
p = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
||||
if p.returncode != 0:
|
||||
print(f"Error running tcpdump {p.stderr}")
|
||||
else:
|
||||
print(f"tcpdump run successfully\n: {p.stdout}")
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
||||
def handle_capture(args):
|
||||
assert args.device_root is not None, f"Device root directory is required"
|
||||
assert dir_contains_device_metadata(args.device_root), f"Device metadata file '{args.device_root}' does not exist"
|
||||
# get device metadata
|
||||
if args.safe and not dir_contains_device_metadata(args.device_root):
|
||||
print(f"Supplied folder contains no device metadata. "
|
||||
f"Please setup a device root directory before using this command")
|
||||
exit(ReturnCodes.ABORTED)
|
||||
elif dir_contains_device_metadata(args.device_root):
|
||||
device_metadata_filename = args.device_root / DEVICE_METADATA_FILE
|
||||
device_data = DeviceMetadata.load_from_json(device_metadata_filename)
|
||||
else:
|
||||
name = input("Please enter a device name: ")
|
||||
args.device_root.mkdir(parents=True, exist_ok=True)
|
||||
device_data = DeviceMetadata(name, args.device_root)
|
||||
# start constructing environment for capture
|
||||
capture_dir = get_capture_src_folder(args.device_root)
|
||||
make_capture_src_folder(capture_dir)
|
||||
capture_metadata = CaptureMetadata(device_data, capture_dir)
|
||||
|
||||
capture_metadata.set_interface(args.capture_interface)
|
||||
cmd = ['sudo', 'tcpdump', '-i', args.capture_interface]
|
||||
cmd = build_tcpdump_args(args, cmd, capture_metadata)
|
||||
capture_metadata.set_tcpdump_command(cmd)
|
||||
|
||||
print('Executing: ' + ' '.join(cmd))
|
||||
|
||||
# run capture
|
||||
try:
|
||||
start_time = datetime.now().strftime('%H:%M:%S')
|
||||
run_tcpdump(cmd)
|
||||
stop_time = datetime.now().strftime('%H:%M:%S')
|
||||
capture_metadata.set_start_time(start_time)
|
||||
capture_metadata.set_stop_time(stop_time)
|
||||
except KeyboardInterrupt:
|
||||
print("Received keyboard interrupt.")
|
||||
exit(ReturnCodes.ABORTED)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Failed to capture packet: {e}")
|
||||
exit(ReturnCodes.FAILURE)
|
||||
except Exception as e:
|
||||
print(f"Failed to capture packet: {e}")
|
||||
exit(ReturnCodes.FAILURE)
|
||||
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
|
||||
def build_tcpdump_args(args, cmd, capture_metadata: CaptureMetadata):
|
||||
if args.monitor_mode:
|
||||
cmd.append('-I')
|
||||
if args.no_name_resolution:
|
||||
cmd.append('-n')
|
||||
if args.number:
|
||||
cmd.append('-#')
|
||||
if args.print_link_layer:
|
||||
cmd.append('-e')
|
||||
|
||||
if args.count:
|
||||
cmd.append('-c')
|
||||
cmd.append(str(args.count))
|
||||
elif args.mins:
|
||||
assert False, "Unimplemented option"
|
||||
|
||||
if args.app_name is not None:
|
||||
capture_metadata.set_app_name(args.app_name)
|
||||
|
||||
capture_metadata.build_capture_file_name()
|
||||
cmd.append('-w')
|
||||
cmd.append(capture_metadata.get_capfile_name())
|
||||
|
||||
if args.safe:
|
||||
cmd.append(f'host {args.device_ip}') # if not specified, filter 'any' implied by tcpdump
|
||||
capture_metadata.set_device_ip_address(args.device_ip)
|
||||
|
||||
return cmd
|
||||
|
||||
|
||||
# def capture_file_cmd(args, cmd, capture_dir, capture_metadata: CaptureMetadata):
|
||||
# capture_file_prefix = capture_metadata.get_device_metadata().get_device_short_name()
|
||||
# if args.app_name is not None:
|
||||
# capture_file_prefix = args.app_name
|
||||
# capture_metadata.set_app(args.app_name)
|
||||
# capfile_name = capture_file_prefix + "_" + str(capture_metadata.get_capture_id()) + ".pcap"
|
||||
# capture_metadata.set_capture_file(capfile_name)
|
||||
# capfile_abs_path = capture_dir / capfile_name
|
||||
# capture_metadata.set_capture_file(capfile_name)
|
||||
# cmd.append('-w')
|
||||
# cmd.append(str(capfile_abs_path))
|
||||
0
code/iottb/utils/__init__.py
Normal file
0
code/iottb/utils/__init__.py
Normal file
38
code/iottb/utils/capture_metadata_utils.py
Normal file
38
code/iottb/utils/capture_metadata_utils.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from iottb.definitions import ReturnCodes
|
||||
|
||||
|
||||
def set_device_ip_address(ip_addr: str, file_path: Path):
|
||||
assert ip_addr is not None
|
||||
assert file_path.is_file()
|
||||
with file_path.open('r') as f:
|
||||
data = json.load(f)
|
||||
current_ip = data["device_ip_address"]
|
||||
if current_ip is not None:
|
||||
print(f"Device IP Address is set to {current_ip}")
|
||||
response = input(f"Do you want to change the recorded IP address to {ip_addr}? [Y/N] ")
|
||||
if response.upper() == "N":
|
||||
print("Aborting change to device IP address")
|
||||
return ReturnCodes.ABORTED
|
||||
with file_path.open('w') as f:
|
||||
json.dump(data, f)
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
|
||||
def set_device_mac_address(mac_addr: str, file_path: Path):
|
||||
assert mac_addr is not None
|
||||
assert file_path.is_file()
|
||||
with file_path.open('r') as f:
|
||||
data = json.load(f)
|
||||
current_mac = data["device_mac_address"]
|
||||
if current_mac is not None:
|
||||
print(f"Device MAC Address is set to {current_mac}")
|
||||
response = input(f"Do you want to change the recorded MAC address to {mac_addr}? [Y/N] ")
|
||||
if response.upper() == "N":
|
||||
print("Aborting change to device MAC address")
|
||||
return ReturnCodes.ABORTED
|
||||
with file_path.open('w') as f:
|
||||
json.dump(data, f)
|
||||
return ReturnCodes.SUCCESS
|
||||
44
code/iottb/utils/capture_utils.py
Normal file
44
code/iottb/utils/capture_utils.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from iottb.models.device_metadata_model import dir_contains_device_metadata
|
||||
from iottb.utils.utils import get_iso_date
|
||||
|
||||
|
||||
def get_capture_uuid():
|
||||
return str(uuid.uuid4())
|
||||
|
||||
|
||||
def get_capture_date_folder(device_root: Path):
|
||||
today_iso = get_iso_date()
|
||||
today_folder = device_root / today_iso
|
||||
if dir_contains_device_metadata(device_root):
|
||||
if not today_folder.is_dir():
|
||||
try:
|
||||
today_folder.mkdir()
|
||||
except FileExistsError:
|
||||
print(f"Folder {today_folder} already exists")
|
||||
return today_folder
|
||||
raise FileNotFoundError(f"Given path {device_root} is not a device root directory")
|
||||
|
||||
|
||||
def get_capture_src_folder(device_folder: Path):
|
||||
assert device_folder.is_dir(), f"Given path {device_folder} is not a folder"
|
||||
today_iso = get_iso_date()
|
||||
max_sequence_number = 1
|
||||
for d in device_folder.iterdir():
|
||||
if d.is_dir() and d.name.startswith(f'{today_iso}_capture_'):
|
||||
name = d.name
|
||||
num = int(name.split("_")[2])
|
||||
max_sequence_number = max(max_sequence_number, num)
|
||||
|
||||
next_sequence_number = max_sequence_number + 1
|
||||
return device_folder.joinpath(f"{today_iso}_capture_{next_sequence_number:03}")
|
||||
|
||||
|
||||
def make_capture_src_folder(capture_src_folder: Path):
|
||||
try:
|
||||
capture_src_folder.mkdir()
|
||||
except FileExistsError:
|
||||
print(f"Folder {capture_src_folder} already exists")
|
||||
finally:
|
||||
return capture_src_folder
|
||||
51
code/iottb/utils/device_metadata_utils.py
Normal file
51
code/iottb/utils/device_metadata_utils.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from iottb.definitions import ReturnCodes
|
||||
|
||||
|
||||
def update_firmware_version(version: str, file_path: Path):
|
||||
assert file_path.is_file()
|
||||
with file_path.open('r') as file:
|
||||
metadata = json.load(file)
|
||||
metadata['device_firmware_version'] = version
|
||||
metadata['date_updated'] = datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower()
|
||||
with file_path.open('w') as file:
|
||||
json.dump(metadata, file)
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
|
||||
def add_capture_file_reference(capture_file_reference: str, file_path: Path):
|
||||
assert file_path.is_file()
|
||||
with file_path.open('r') as file:
|
||||
metadata = json.load(file)
|
||||
metadata['capture_files'] = capture_file_reference
|
||||
metadata['date_updated'] = datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower()
|
||||
with file_path.open('w') as file:
|
||||
json.dump(metadata, file)
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
|
||||
def update_device_serial_number(device_id: str, file_path: Path):
|
||||
assert file_path.is_file()
|
||||
with file_path.open('r') as file:
|
||||
metadata = json.load(file)
|
||||
metadata['device_id'] = device_id
|
||||
metadata['date_updated'] = datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower()
|
||||
with file_path.open('w') as file:
|
||||
json.dump(metadata, file)
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
|
||||
def update_device_type(device_type: str, file_path: Path):
|
||||
assert file_path.is_file()
|
||||
with file_path.open('r') as file:
|
||||
metadata = json.load(file)
|
||||
metadata['device_type'] = device_type
|
||||
metadata['date_updated'] = datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower()
|
||||
with file_path.open('w') as file:
|
||||
json.dump(metadata, file)
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
|
||||
41
code/iottb/utils/tcpdump_utils.py
Normal file
41
code/iottb/utils/tcpdump_utils.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import ipaddress
|
||||
import shutil
|
||||
import subprocess
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def check_installed() -> bool:
|
||||
"""Check if tcpdump is installed and available on the system path."""
|
||||
return shutil.which('tcpdump') is not None
|
||||
|
||||
|
||||
def ensure_installed():
|
||||
"""Ensure that tcpdump is installed, raise an error if not."""
|
||||
if not check_installed():
|
||||
raise RuntimeError("tcpdump is not installed. Please install it to continue.")
|
||||
|
||||
|
||||
def list_interfaces() -> str:
|
||||
"""List available network interfaces using tcpdump."""
|
||||
ensure_installed()
|
||||
try:
|
||||
result = subprocess.run(['tcpdump', '--list-interfaces'], capture_output=True, text=True, check=True)
|
||||
return result.stdout
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Failed to list interfaces: {e}")
|
||||
return ""
|
||||
|
||||
|
||||
def is_valid_ipv4(ip: str) -> bool:
|
||||
try:
|
||||
ipaddress.IPv4Address(ip)
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
def str_to_ipv4(ip: str) -> (bool, Optional[ipaddress]):
|
||||
try:
|
||||
address = ipaddress.IPv4Address(ip)
|
||||
return address == ipaddress.IPv4Address(ip), address
|
||||
except ipaddress.AddressValueError:
|
||||
return False, None
|
||||
18
code/iottb/utils/utils.py
Normal file
18
code/iottb/utils/utils.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from iottb.definitions import TODAY_DATE_STRING, DEVICE_METADATA_FILE, CAPTURE_METADATA_FILE
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def get_iso_date():
|
||||
return datetime.now().strftime('%Y-%m-%d')
|
||||
|
||||
|
||||
def subfolder_exists(parent: Path, child: str):
|
||||
return parent.joinpath(child).exists()
|
||||
|
||||
|
||||
def generate_unique_string_with_prefix(prefix: str):
|
||||
return prefix + "_" + str(uuid.uuid4())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user