First module of nos0s works

This commit is contained in:
hornet 2023-03-26 13:58:31 -04:00
parent 1ff119d260
commit 67d4c90ed1
10 changed files with 84 additions and 32 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
config.yaml
__pycache__

View File

@ -0,0 +1 @@
# comment to be gittable

View File

@ -1,3 +0,0 @@
server:
host: '127.0.0.1'
port: 35293

View File

@ -0,0 +1,6 @@
server:
host: "127.0.0.1"
port: 35293
sniff:
host: "127.0.0.1"

18
main.py
View File

@ -1,18 +1,30 @@
from menu import MenuState
from menu import MenuState, LeafMenu
from sniff import udp
from common import config_loader, log
def init_menus():
main_menu = MenuState("NOS0S\nselect an option", "NOS0S MAIN")
net = MenuState("net attacks", "tools to attack network devices")
net = MenuState("net recon", "tools to gain intel on network devices")
sniffer = LeafMenu("Sniffer", "Sniffs for network traffic. CTRL+C to cancel.")
sniffer.set_action(udp.sniff, (config_loader.config['sniff']['host'], log.warning))
net.add_submenu(sniffer)
main_menu.add_submenu(net)
return main_menu
def main():
log.set_lvl(log.LogLevel.DEBUG)
main_menu = init_menus()
current_menu = main_menu
while current_menu != None:
print(current_menu.display_CLI())
display, return_to_parent = current_menu.display_CLI()
print(display)
if(return_to_parent):
current_menu = current_menu.nav_to_parent()
continue
res = input('> ')
if res == 'q':
break

31
menu.py
View File

@ -1,4 +1,3 @@
class MenuState:
def __init__(self, title, desc):
self.title = title
@ -11,14 +10,14 @@ class MenuState:
menu.parent = self
def display_CLI(self):
display_text = f'{self.title}\n'
display_text = f'\n{self.title}\n'
if self.parent != None:
display_text += f' b. Back'
display_text += f' b. Back\n'
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
return display_text, False
def nav_to_sub(self, sub_index):
if sub_index <= len(self.subs):
@ -31,23 +30,13 @@ class MenuState:
return self.parent
else:
return self
class LeafMenu(MenuState):
class LeafMenu(MenuState):
def __init__(self, title, desc):
super.__init__(title, desc)
self.subs = None
self.actions = []
def set_action(self, action, args):
self.action = action
self.args = args
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
def display_CLI(self):
return self.action(self.args), True

1
sniff/__init__.py Normal file
View File

@ -0,0 +1 @@
# comment to be gittable

View File

@ -2,6 +2,7 @@ from ctypes import *
import socket
import struct
import ipaddress
import os
class IP_ctype(Structure):
_fields_ = [
@ -24,6 +25,12 @@ class IP_ctype(Structure):
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))
self.protocol_map = {1:"ICMP", 2:"IGMP", 6:"TCP", 17: "UDP"}
try:
self.protocol = self.protocol_map[self.protocol_num]
except Exception as e:
print(f'{e} No known protocol for {self.protocol_num}')

View File

@ -21,6 +21,10 @@ class IP_struct:
# map protocol constants to their names
self.protocol_map = {1:"ICMP", 6:"TCP", 17: "UDP"}
try:
self.protocol = self.protocol_map[self.protocol_num]
except Exception as e:
print(f'{e} No known protocol for {self.protocol_num}')
# ICMP response packet

View File

@ -1,16 +1,15 @@
import socket
import os
from .ip_ctype import IP_ctype
HOST = '192.168.50.100'
def udp_single_packet_sniff():
def udp_single_packet_sniff(host):
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.bind((host,0))
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
# turn on promiscuous mode
@ -22,11 +21,45 @@ def udp_single_packet_sniff():
if os.name == 'nt':
sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
def sniff(args):
(host, event_callback) = args
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)
packets_sniffed = 0
try:
while True:
# read a packet
raw_buffer = sniffer.recvfrom(65535)[0]
# parse that packet
ip_header = IP_ctype(raw_buffer[0:20])
try:
event_callback(f'Protocol: {ip_header.protocol}\t{ip_header.src_address} -> {ip_header.dst_address}')
packets_sniffed += 1
except AttributeError as e:
continue
except KeyboardInterrupt:
if os.name == 'nt':
sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
return f'Packets Sniffed: {packets_sniffed}'
def main():
udp_single_packet_sniff()
host = input('Host IP > ')
udp_single_packet_sniff(host)
sniff((host, print))
if __name__ == '__main__':