Beau Kujath
2 years ago
3 changed files with 0 additions and 250 deletions
@ -1,182 +0,0 @@ |
|||
import asyncio |
|||
import threading |
|||
from scapy.all import * |
|||
import argparse |
|||
import base64 |
|||
|
|||
DEBUG=False |
|||
|
|||
def print_dbg(s): |
|||
if DEBUG: |
|||
print(s) |
|||
|
|||
class NGServer: |
|||
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 = None # asyncio.Queue() |
|||
self.outgoing_queue = None # 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 |
|||
self.client_writer = writer |
|||
while True: |
|||
data = await reader.read(1024) |
|||
if not data: |
|||
writer.close() |
|||
break |
|||
await self.incoming_queue.put(data) |
|||
|
|||
async def handle_incoming(self, reader, writer): |
|||
self.incoming_queue = asyncio.Queue() |
|||
while True: |
|||
data = await self.incoming_queue.get() |
|||
writer.write(data) |
|||
await writer.drain() |
|||
|
|||
async def handle_outgoing(self, reader, writer): |
|||
self.server_reader = reader |
|||
self.server_writer = writer |
|||
print_dbg("handle_outgoing - ENTER") |
|||
self.outgoing_queue = asyncio.Queue() |
|||
while True: |
|||
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=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=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=self.ioip, |
|||
port=self.outgoing_port) |
|||
async with server: |
|||
await server.serve_forever() |
|||
|
|||
async def start_thread(ng): |
|||
""" """ |
|||
await asyncio.gather( |
|||
ng.start_server(), |
|||
ng.start_incoming_server(), |
|||
ng.start_outgoing_server(), |
|||
) |
|||
|
|||
await asyncio.gather( |
|||
ng.read_from_server(), |
|||
ng.handle_incoming(), |
|||
ng.handle_outgoing(), |
|||
) |
|||
|
|||
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 base64_decode_with_plus(data): |
|||
data = data.rstrip('+') |
|||
padding = 4 - len(data) % 4 |
|||
data += '=' * padding |
|||
return base64.b64decode(data) |
|||
|
|||
def base64_encode_with_plus(data): |
|||
encoded_data = base64.b64encode(data).rstrip(b'=') |
|||
padding = (4 - len(encoded_data) % 4) % 4 |
|||
encoded_data += b'+' * padding |
|||
return encoded_data.decode('ascii') |
|||
|
|||
|
|||
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(): |
|||
""" |
|||
The NGServer class implements our NetGenie debug/bridge server. |
|||
""" |
|||
# Get the arguments |
|||
args = parseArgs() |
|||
# Start the server. |
|||
startServerAndWait(args) |
|||
|
|||
if __name__ == '__main__': |
|||
main() |
@ -1,67 +0,0 @@ |
|||
# 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. |
|||
``` |
|||
|
|||
You can also start NetGenieSrv from a python REPL as follows: |
|||
|
|||
```bash |
|||
In [1]: import NetGenieSrv as ngs |
|||
In [2]: ng = ngs.NGServer(bridge_ip='0.0.0.0') |
|||
In [3]: ngs.startThread(ng) |
|||
|
|||
``` |
|||
|
|||
## 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. |
|||
|
|||
``` |
Write
Preview
Loading…
Cancel
Save
Reference in new issue