bigsnitch/mitmaddon/littlesnitch.py

122 lines
4.5 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-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):
2020-08-13 03:35:22 +00:00
print("thread started")
2020-08-12 13:11:29 +00:00
self.connect()
2020-08-13 03:35:22 +00:00
while True:
timer = time.monotonic()
a = None
if not a:
try:
a = self.q.get(block=False)
except Empty:
pass
if a:
self.send_msg_and_ack(a)
timer = time.monotonic()
if timer - time.monotonic() < -5:
self.send_msg_and_ack({"msg": "ping"})
2020-08-12 13:11:29 +00:00
def disconnect(self):
self.socket.setsockopt(zmq.LINGER,0)
self.socket.close()
2020-08-13 03:35:22 +00:00
print("disconnected")
2020-08-12 13:11:29 +00:00
def reconnect(self):
self.disconnect()
2020-08-13 03:35:22 +00:00
time.sleep(1)
2020-08-12 13:11:29 +00:00
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-13 03:35:22 +00:00
self.send_msg_and_ack({"msg": "ping"})
print("successfully connected")
2020-08-08 11:44:16 +00:00
2020-08-13 03:35:22 +00:00
def send_msg_and_ack(self, msg):
while True:
2020-08-21 18:40:37 +00:00
print("m sending")
2020-08-12 13:11:29 +00:00
a = convert_to_strings(msg)
self.socket.send(str.encode(json.dumps(a)))
2020-08-21 18:40:37 +00:00
if (self.socket.poll(50) & zmq.POLLIN) != 0:
2020-08-12 13:11:29 +00:00
msg = self.socket.recv()
try:
if msg:
result = json.loads(msg)
2020-08-13 03:35:22 +00:00
if result["msg"] == "ack":
2020-08-21 18:40:37 +00:00
print("m ack received")
2020-08-12 13:11:29 +00:00
return result
else:
print("got unexpected message {result}")
except json.JSONDecodeError:
print(f"malformed message received {msg}")
2020-08-13 03:35:22 +00:00
print("no ack received, reconnecting...")
2020-08-12 13:11:29 +00:00
self.reconnect()
2020-08-19 00:17:42 +00:00
return NO_MSG
2020-08-05 00:08:41 +00:00
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()
2020-09-02 22:23:37 +00:00
2020-08-05 00:08:41 +00:00
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
2020-09-02 22:23:37 +00:00
def requestheaders(self, flow):
self.q.put({'msg': 'requestheaders', 'flow': flow.get_state()})
2020-08-05 00:08:41 +00:00
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
2020-09-02 22:23:37 +00:00
def responseheaders(self, flow):
self.q.put({'msg': 'responseheaders', 'flow': flow.get_state()})
def error(self, flow):
self.q.put({'msg': 'error', 'flow': flow.get_state()})
def websocket_handshake(self):
self.q.put({'msg': 'websocket_handshake', 'flow': flow.get_state()})
def websocket_start(self, flow):
self.q.put({'msg': 'websocket_start', 'flow': flow.get_state()})
def websocket_message(self, flow):
self.q.put({'msg': 'websocket_message', 'flow': flow.get_state()})
def websocket_error(self, flow):
self.q.put({'msg': 'websocket_error', 'flow': flow.get_state()})
def websocket_end(self, flow):
self.q.put({'msg': 'websocket_end', 'flow': flow.get_state()})
2020-08-05 00:08:41 +00:00
addons = [
2020-08-12 13:11:29 +00:00
LittleSnitchBridge()
2020-08-05 00:08:41 +00:00
]