bigsnitch/mitmaddon/littlesnitch.py

139 lines
4.9 KiB
Python
Raw Normal View History

2020-08-05 00:08:41 +00:00
#!/usr/bin/env python3
import pdb
from mitmproxy import ctx
import threading
2020-08-09 12:38:41 +00:00
from queue import Queue, Empty
2020-08-05 00:08:41 +00:00
import time
import zmq
2020-08-08 11:44:16 +00:00
import json
2020-08-10 22:24:08 +00:00
from enum import Enum
2020-08-05 00:08:41 +00:00
2020-08-09 12:38:41 +00:00
NO_MSG = {"msg": None}
2020-08-08 11:44:16 +00:00
INIT_MSG = {"msg": "init"}
ACK_MSG = {"msg": "ack"}
PING_MSG = {"msg": "ping"}
PONG_MSG = {"msg": "pong"}
2020-08-10 22:24:08 +00:00
class NetworkState(Enum):
DISCONNECTED = auto()
CONNECTED = auto()
PING = auto()
SENDING = auto()
2020-08-12 13:11:29 +00:00
2020-08-08 11:44:16 +00:00
def convert_to_strings(obj):
if isinstance(obj, dict):
return {convert_to_strings(key): convert_to_strings(value)
for key, value in obj.items()}
elif isinstance(obj, list) or isinstance(obj, tuple):
return [convert_to_strings(element) for element in obj]
elif isinstance(obj, bytes):
return str(obj)[2:-1]
return obj
2020-08-12 13:11:29 +00:00
class NetworkThread(threading.Thread):
def __init__(self, name, queue):
threading.Thread.__init__(self)
self.name = name
self.q = queue
self.context = zmq.Context()
2020-08-08 11:44:16 +00:00
2020-08-12 13:11:29 +00:00
def run(self):
self.connect()
msg = self.send_msg_and_expect()
2020-08-08 11:44:16 +00:00
2020-08-12 13:11:29 +00:00
def disconnect(self):
self.socket.setsockopt(zmq.LINGER,0)
self.socket.close()
def reconnect(self):
self.disconnect()
self.connect()
2020-08-08 11:44:16 +00:00
2020-08-12 13:11:29 +00:00
def connect(self):
self.socket = self.context.socket(zmq.PAIR)
self.socket.connect("tcp://127.0.0.1:12345")
2020-08-08 11:44:16 +00:00
2020-08-12 13:11:29 +00:00
def send_msg_and_expect(this, msg, expect, timeout=5, retries=3):
while retries:
a = convert_to_strings(msg)
self.socket.send(str.encode(json.dumps(a)))
if (client.poll(REQUEST_TIMEOUT) & zmq.POLLIN) != 0:
msg = self.socket.recv()
try:
if msg:
result = json.loads(msg)
if result["msg"] in expect:
return result
else:
print("got unexpected message {result}")
except json.JSONDecodeError:
print(f"malformed message received {msg}")
retries -= 1
self.reconnect()
return NO_MSG
"""
2020-08-05 00:08:41 +00:00
def networking(q):
print("starting thread")
2020-08-12 13:11:29 +00:00
state = NetworkState.DISCONNECTED
2020-08-09 12:07:14 +00:00
a = None
2020-08-12 13:11:29 +00:00
while state == NetworkState.DISCONNECTED:
2020-08-08 11:44:16 +00:00
socket = context.socket(zmq.PAIR)
socket.connect("tcp://127.0.0.1:12345")
msg = get_msg(socket)
if msg["msg"] == "init":
send_msg(ACK_MSG, socket)
2020-08-12 13:11:29 +00:00
state = NetworkState.CONNECTED
2020-08-08 11:44:16 +00:00
2020-08-12 13:11:29 +00:00
2020-08-08 11:44:16 +00:00
timer = time.monotonic()
2020-08-12 13:11:29 +00:00
while state != NetworkState.DISCONNECTED:
if state == NetworkState.CONNECTED and timer - time.monotonic() >= 5:
2020-08-08 11:44:16 +00:00
timer = time.monotonic()
send_msg(PING_MSG,socket)
msg = get_msg(socket)
if msg["msg"] != "pong":
2020-08-12 13:11:29 +00:00
state = NetworkState.
2020-08-08 11:44:16 +00:00
msg = get_msg(socket)
if msg['msg'] == "ping":
send_msg(PONG_MSG, socket)
timer = time.monotonic()
2020-08-09 12:07:14 +00:00
if not a:
2020-08-09 12:38:41 +00:00
try:
a = q.get(block=False)
except Empty:
pass
2020-08-08 11:44:16 +00:00
if a:
2020-08-09 12:07:14 +00:00
send_msg(a, socket)
2020-08-08 11:44:16 +00:00
msg = get_msg(socket)
if msg["msg"] == "ack":
timer = time.monotonic()
2020-08-09 12:07:14 +00:00
a = None
self.q.task_done()
2020-08-08 11:44:16 +00:00
else:
connected = False
2020-08-12 13:11:29 +00:00
"""
class LittleSnitchBridge:
2020-08-05 00:08:41 +00:00
def __init__(self):
self.q = Queue()
2020-08-12 13:11:29 +00:00
self.thread = NetworkThread("network", self.q)
2020-08-05 00:08:41 +00:00
self.thread.start()
def request(self, flow):
2020-08-09 12:07:14 +00:00
self.q.put({'msg': 'request', 'flow': flow.get_state()})
2020-08-05 00:08:41 +00:00
self.q.join()
def response(self, flow):
2020-08-09 12:07:14 +00:00
self.q.put({'msg': 'response', 'flow': flow.get_state()})
2020-08-05 00:08:41 +00:00
self.q.join()
addons = [
2020-08-12 13:11:29 +00:00
LittleSnitchBridge()
2020-08-05 00:08:41 +00:00
]