bigsnitch

This commit is contained in:
Tim Blume 2021-04-23 19:49:00 +02:00
parent 514404bb02
commit d09907dfa5

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#
import pdb import pdb
from mitmproxy import ctx
from mitmproxy import ctx, Flow
import threading import threading
from queue import Queue, Empty from queue import Queue, Empty
import time import time
@ -8,7 +11,7 @@ import zmq
import json import json
from enum import Enum from enum import Enum
from dataclasses import dataclass from dataclasses import dataclass
from typing import List from typing import List, Dict
# this method is used to convert flow states (generated with get_state()) to json # this method is used to convert flow states (generated with get_state()) to json
def convert_to_strings(obj): def convert_to_strings(obj):
@ -55,6 +58,7 @@ class bRequest:
error: str error: str
# init from dict
def __init__(self, flow: dict): def __init__(self, flow: dict):
self.server_ip_address = flow["server_ip_address"] self.server_ip_address = flow["server_ip_address"]
self.tls = flow["server_conn"]["tls_established"] self.tls = flow["server_conn"]["tls_established"]
@ -103,28 +107,53 @@ class bFlow:
self.request = bRequest(flow["request"]) self.request = bRequest(flow["request"])
self.response = bRequest(flow["response"]) self.response = bRequest(flow["response"])
#
# Networkthread state machine types
#
@dataclass
class FlowState(Enum): class FlowState(Enum):
UNSENT_REQ = 0 UNSENT_REQ = 0
SENT_REQ = 1 SENT_REQ = 1
UNSENT_RES = 2 UNSENT_RES = 2
SENT_RES = 3 SENT_RES = 3
# current flow state in Mitmproxy
@dataclass
class MitmState(Enum):
ERROR = 0
REQUESTHEADERS = 1
REQUEST = 2
RESPONSEHEADERS = 3
RESPONSE = 4
# for use in NetworkThread queue
@dataclass
class FlowItem:
id: int
mitmstate: MitmState
state: FlowState
flow: Flow
time: float = 0
retries_left: int = 5
""" """
The network thread communicates with the bigsnitch plugin using zeromq. The network thread communicates with the bigsnitch plugin using zeromq.
""" """
@dataclass
class NetworkThread(threading.Thread): class NetworkThread(threading.Thread):
def __init__(self, name, queue): def __init__(self, name, queue):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.name = name self.name = name
# queue for communicating with the main mitmproxy thread # queue for communicating with the main mitmproxy thread
self.q = queue self.q = queue
# all current flows being handled by mitmproxy # for zmq use
# id : (state, flow, timer, retries left)
self.flows = {}
self.context = zmq.Context() self.context = zmq.Context()
# all current flows being handled by mitmproxy
self.flows: Dict[FlowItem]
# timer for sending pings to check if the connection broke # timer for sending pings to check if the connection broke
self.timer = time.monotonic() self.timer = time.monotonic()
# retries left for reconnecting / resending a broken flow # retries left for reconnecting / resending a broken flow
@ -147,9 +176,9 @@ class NetworkThread(threading.Thread):
# csave the new flows, if necessary # csave the new flows, if necessary
if typ == "request": if typ == "request":
self.flows[i] = (FlowState.UNSENT_REQ, flow, time.monotonic(), 5) self.flows[i] = bFlow(FlowState.UNSENT_REQ, flow, time.monotonic(), 5)
elif typ == "response": elif typ == "response":
self.flows[i] = (FlowState.UNSENT_RES, flow, time.monotonic(), 5) self.flows[i] = bFlow(FlowState.UNSENT_RES, flow, time.monotonic(), 5)
except Empty: except Empty:
break break
@ -164,7 +193,6 @@ class NetworkThread(threading.Thread):
msg = b"" msg = b""
# send the request # send the request
self.send(msg) self.send(msg)
pass
elif state == FlowState.SENT_REQ: elif state == FlowState.SENT_REQ:
# check timer, try resend # check timer, try resend
pass pass