From 1ff119d260b9e7ae0782f6207f8294f0a44745c1 Mon Sep 17 00:00:00 2001 From: hornet Date: Sat, 25 Mar 2023 15:56:56 -0400 Subject: [PATCH] break out menu, implement sniff packet parse --- main.py | 36 +------------------------------ menu.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++ sniff/ip_ctype.py | 43 +++++++++++++++++++++++++++++++++++++ sniff/ip_struct.py | 34 +++++++++++++++++++++++++++++ sniff/udp.py | 33 ++++++++++++++++++++++++++++- 5 files changed, 163 insertions(+), 36 deletions(-) create mode 100644 menu.py create mode 100644 sniff/ip_ctype.py create mode 100644 sniff/ip_struct.py diff --git a/main.py b/main.py index dd08ba9..a5f6b18 100644 --- a/main.py +++ b/main.py @@ -1,38 +1,4 @@ - -class MenuState: - def __init__(self, title, desc): - self.title = title - self.description = desc - self.parent = None - self.subs = [] - - def add_submenu(self, menu): - self.subs.append(menu) - menu.parent = self - - def display_CLI(self): - display_text = f'{self.title}\n' - if self.parent != None: - display_text += f' b. Back' - for i in range(0, len(self.subs)): - sub = self.subs[i] - display_text += f' {i}. {sub.title}\n' - display_text += f' - {sub.description}\n' - return display_text - - def nav_to_sub(self, sub_index): - if sub_index <= len(self.subs): - return self.subs[sub_index] - else: - return self - - def nav_to_parent(self): - if self.parent != None: - return self.parent - else: - return self - - +from menu import MenuState def init_menus(): main_menu = MenuState("NOS0S\nselect an option", "NOS0S MAIN") diff --git a/menu.py b/menu.py new file mode 100644 index 0000000..073acc9 --- /dev/null +++ b/menu.py @@ -0,0 +1,53 @@ + +class MenuState: + def __init__(self, title, desc): + self.title = title + self.description = desc + self.parent = None + self.subs = [] + + def add_submenu(self, menu): + self.subs.append(menu) + menu.parent = self + + def display_CLI(self): + display_text = f'{self.title}\n' + if self.parent != None: + display_text += f' b. Back' + for i in range(0, len(self.subs)): + sub = self.subs[i] + display_text += f' {i}. {sub.title}\n' + display_text += f' - {sub.description}\n' + return display_text + + def nav_to_sub(self, sub_index): + if sub_index <= len(self.subs): + return self.subs[sub_index] + else: + return self + + def nav_to_parent(self): + if self.parent != None: + return self.parent + else: + return self + + +class LeafMenu(MenuState): + def __init__(self, title, desc): + super.__init__(title, desc) + self.subs = None + self.actions = [] + + def add_submenu(self, menu): + return + + def add_action(self, action): + self.actions.append(action) + + def execute_action(self, act_index): + if act_index <= len(self.actions): + # what + self.actions[act_index]() + else: + return diff --git a/sniff/ip_ctype.py b/sniff/ip_ctype.py new file mode 100644 index 0000000..3190d01 --- /dev/null +++ b/sniff/ip_ctype.py @@ -0,0 +1,43 @@ +from ctypes import * +import socket +import struct +import ipaddress + +class IP_ctype(Structure): + _fields_ = [ + ("version", c_ubyte, 4), + ("ihl", c_ubyte, 4), + ("tos", c_ubyte, 8), + ("len", c_ushort, 16), + ("id", c_ushort, 16), + ("offset", c_ushort, 16), + ("ttl", c_ubyte, 8), + ("protocol_num", c_ubyte, 8), + ("sum", c_ushort, 16), + ("src", c_uint32, 32), + ("dst", c_uint32, 32) + ] + + def __new__(cls, socket_buffer=None): + return cls.from_buffer_copy(socket_buffer) + + def __init__(self, socket_buffer=None): + self.src_address = socket.inet_ntoa(struct.pack("> 4 + self.ihl = header[0] & 0x0F + self.tos = header[1] + self.len = header[2] + self.id = header[3] + self.offset = header[4] + self.ttl = header[5] + self.protocol_num = header[6] + self.sum = header[7] + self.src = header[8] + self.dst = header[9] + self.src_address = ipaddress.ip_address(self.src) + self.dst_address = ipaddress.ip_address(self.dst) + + # map protocol constants to their names + self.protocol_map = {1:"ICMP", 6:"TCP", 17: "UDP"} + + +# ICMP response packet +class ICMP_struct: + def __init__(self, buff): + header = struct.unpack('