break out menu, implement sniff packet parse

This commit is contained in:
hornet 2023-03-25 15:56:56 -04:00
parent c3e1df7b1f
commit 1ff119d260
5 changed files with 163 additions and 36 deletions

36
main.py
View File

@ -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")

53
menu.py Normal file
View File

@ -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

43
sniff/ip_ctype.py Normal file
View File

@ -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("<L",self.src))
self.dst_address = socket.inet_ntoa(struct.pack("<L",self.dst))
# ICMP response packet
class ICMP_ctype(Structure):
_fields_ = [
("type", c_ubyte, 8),
("code", c_ubyte, 8),
("sum", c_ushort, 16),
("id", c_ushort, 16),
("seq", c_ushort, 16)
]
def __new__(cls, socket_buffer=None):
return cls.from_buffer_copy(socket_buffer)

34
sniff/ip_struct.py Normal file
View File

@ -0,0 +1,34 @@
import struct
import ipaddress
class IP_struct:
def __init__(self, buff=None):
header = struct.unpack('<BBHHHBBH4s4s', buff)
self.ver = header[0] >> 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('<BBHHH', buff)
self.type = header[0]
self.code = header[1]
self.sum = header[2]
self.identifier = header[3]
self.seq = header[4]

View File

@ -1,2 +1,33 @@
import socket
import os
import os
HOST = '192.168.50.100'
def udp_single_packet_sniff():
if os.name == 'nt':
socket_protocol = socket.IPPROTO_IP
else:
socket_protocol = socket.IPPROTO_ICMP
sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
sniffer.bind((HOST,0))
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
# turn on promiscuous mode
if os.name == 'nt':
sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
print(sniffer.recvfrom(65565))
if os.name == 'nt':
sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
def main():
udp_single_packet_sniff()
if __name__ == '__main__':
main()