From ac9395932101dd20961da06394a54c27f8dd5ac5 Mon Sep 17 00:00:00 2001 From: BB Date: Sun, 7 May 2023 20:29:38 -0600 Subject: [PATCH] netgenie-srv: Server is running. Added README --- NetGuard | 2 +- server/NetGenieSrv.py | 145 +++++++++++++++++++++++++++++++----------- server/README.md | 58 +++++++++++++++++ 3 files changed, 167 insertions(+), 38 deletions(-) create mode 100644 server/README.md diff --git a/NetGuard b/NetGuard index e3bbfed..990786c 160000 --- a/NetGuard +++ b/NetGuard @@ -1 +1 @@ -Subproject commit e3bbfed2c3548f815d740c9ae261329d708f6d9c +Subproject commit 990786c49eb008aaf82c0179a1944c8e2e0fd43c diff --git a/server/NetGenieSrv.py b/server/NetGenieSrv.py index 3325797..01dce9b 100644 --- a/server/NetGenieSrv.py +++ b/server/NetGenieSrv.py @@ -1,11 +1,31 @@ import asyncio +import threading +from scapy.all import * +import argparse + +DEBUG=False + +def print_dbg(s): + if DEBUG: + print(s) class NGServer: - def __init__(self): + def __init__(self, bridge_ip='127.0.0.1', bridge_port=8000, + ioip='127.0.0.1', + incoming_port=8001, outgoing_port=8002): self.incoming_queue = asyncio.Queue() self.outgoing_queue = asyncio.Queue() self.client_reader = None self.client_writer = None + self.server_reader = None + self.server_writer = None + self.server_thread = None + + self.bridge_ip = bridge_ip + self.bridge_port = bridge_port + self.ioip = ioip + self.incoming_port = incoming_port + self.outgoing_port = outgoing_port async def handle_client(self, reader, writer): self.client_reader = reader @@ -24,71 +44,122 @@ class NGServer: await writer.drain() async def handle_outgoing(self, reader, writer): + self.server_reader = reader + self.server_writer = writer + print_dbg("handle_outgoing - ENTER") while True: - data = await self.outgoing_queue.get() - writer.write(data) - await writer.drain() + print_dbg("handle_outgoing - IN LOOP") + data = await reader.read(1024) + print_dbg("handle_outgoing - IP(data))={}".format( + IP(data))) + self.client_writer.write(data) + await self.client_writer.drain() async def read_from_server(self): + print_dbg("read_from_server - ENTER") while True: + # Switch reader to writer + print_dbg("read_from_server - IN LOOP") data = await self.client_reader.read(1024) + print_dbg("read_from_server - IP(data))={}".format( + IP(data))) if not data: self.client_writer.close() break await self.outgoing_queue.put(data) async def start_server(self): - server = await asyncio.start_server(self.handle_client, host='localhost', port=8000) + server = await asyncio.start_server( + self.handle_client, + host=self.bridge_ip, + port=self.bridge_port) async with server: await server.serve_forever() async def start_incoming_server(self): - server = await asyncio.start_server(self.handle_incoming, host='localhost', port=8001) + server = await asyncio.start_server( + self.handle_incoming, + host=self.ioip, + port=self.incoming_port) async with server: await server.serve_forever() async def start_outgoing_server(self): - server = await asyncio.start_server(self.handle_outgoing, host='localhost', port=8002) + server = await asyncio.start_server( + self.handle_outgoing, + host=self.ioip, + port=self.outgoing_port) async with server: await server.serve_forever() -async def do_async_stuff(ng): - await asyncio.gather(ng.start_server(),ng.start_queue_server()) - - -async def main(): - """ - In this code, we're creating and running four separate coroutines: - - 1. start_server - 2. start_incoming_server - 3. start_outgoing_server - 4. read_from_server. - - We're also creating separate tasks for handling incoming and - outgoing data and adding them to the tasks list. - - """ - ng = NGServer() +async def start_thread(ng): + """ """ await asyncio.gather( ng.start_server(), ng.start_incoming_server(), ng.start_outgoing_server(), - ) - """ - tasks = [ + ) + + await asyncio.gather( ng.read_from_server(), ng.handle_incoming(), ng.handle_outgoing(), - ] - await asyncio.gather(*tasks) + ) + +def run_thread(ng): + asyncio.run(start_thread(ng)) + + +def startServerAndWait(args): + ng = NGServer(bridge_ip=args.bridge_ip, + bridge_port=args.bridge_port, + ioip=args.ioip, + incoming_port=args.incoming_port, + outgoing_port=args.outgoing_port) + ng.server_thread = threading.Thread( + target=run_thread, + args=(ng,)) + ng.server_thread.start() + ng.server_thread.join() + +def startThread(ng): + + ng.server_thread = threading.Thread( + target=run_thread, + args=(ng,)) + ng.server_thread.start() + +def connectIO(inaddr='127.0.0.1', inport=8001, outaddr='127.0.0.1', outport=8002): + insock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) + insock.connect((inaddr, inport)) + outsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) + outsock.connect((outaddr, outport)) + return insock, outsock + + +def parseArgs(): + args = argparse.ArgumentParser() + # The bridge ip and port + args.add_argument("-B","--bridge_ip", default='127.0.0.1', + help='The IP that listens for NetGenie mobile app client connections.') + args.add_argument("-b","--bridge_port", default=8000, help='The port that listens for NetGenie mobile app client connections.') + + # The IO ip and ports + args.add_argument("-I","--ioip", default='127.0.0.1', help='The IP that listens on the server for connections from whoever is debugging the phone.') + args.add_argument("-i","--incoming_port", default=8001, + help='The port that the person debugging will read packets from the device.') + args.add_argument("-o","--outgoing_port", default=8002, + help='The port that the person debugging will write packets to the device.') + return args.parse_args() + + +def main(): """ - -def example(): - """ - - TODO: Implement some test cases so users understand - how to use it. + The NGServer class implements our NetGenie debug/bridge server. """ -# asyncio.run(main()) + args = parseArgs() + startServerAndWait(args) + +if __name__ == '__main__': + main() diff --git a/server/README.md b/server/README.md new file mode 100644 index 0000000..f13dee6 --- /dev/null +++ b/server/README.md @@ -0,0 +1,58 @@ +# Quick Start + +## Starting the server + +On the server, run the following + +```bash +$ python3 -m venv .example +$ activate .example/bin/activate + +$ python3 NetGenieSrv.py +``` + +This opens three sockets: + +1. The bridge socket that listens for the phone to connect to. +2. An ``incoming'' socket to read packets sent from the device to the server +3. An ``outgoing'' socket to write packets from the server to the device. + +On the phone, connect to the server and port specified by the `-B` and `-b` arguments. + +This can be specified by changing the source code or using the following command-line +arguments: + +```bash +$ python3 NetGenieSrv.py -h +usage: NetGenieSrv.py [-h] [-B BRIDGE_IP] [-b BRIDGE_PORT] [-I IOIP] [-i INCOMING_PORT] [-o OUTGOING_PORT] + +options: + -h, --help show this help message and exit + -B BRIDGE_IP, --bridge_ip BRIDGE_IP + The IP that listens for NetGenie mobile app client connections. + -b BRIDGE_PORT, --bridge_port BRIDGE_PORT + The port that listens for NetGenie mobile app client connections. + -I IOIP, --ioip IOIP The IP that listens on the server for connections from whoever is debugging the phone. + -i INCOMING_PORT, --incoming_port INCOMING_PORT + The port that the person debugging will read packets from the device. + -o OUTGOING_PORT, --outgoing_port OUTGOING_PORT + The port that the person debugging will write packets to the device. +``` + +## Using the server + +```bash +$ ipython3 + +import NetGenieSrv as ngs +from scapy.all import * +ip = IP(src='10.10.20.1', dst='255.255.4.20') +tcp = TCP(sport=8080, dport=8443) +pkt = ip/tcp +a = pkt.build() + +insock, outsock = ngs.connectIO() +pktIn = insock.read(40) # Read a packet send from the phone +outsock.send(a) # Send a packet to the NetGenie client. + +```