2011-01-25 02:02:48 +00:00
|
|
|
"""
|
|
|
|
This module provides more sophisticated flow tracking. These match requests
|
|
|
|
with their responses, and provide filtering and interception facilities.
|
|
|
|
"""
|
2011-02-16 01:33:04 +00:00
|
|
|
import subprocess, base64, sys, json
|
|
|
|
import proxy, threading, netstring
|
2011-01-25 02:02:48 +00:00
|
|
|
|
2011-02-01 22:44:28 +00:00
|
|
|
class RunException(Exception):
|
|
|
|
def __init__(self, msg, returncode, errout):
|
|
|
|
Exception.__init__(self, msg)
|
|
|
|
self.returncode = returncode
|
|
|
|
self.errout = errout
|
|
|
|
|
2011-01-31 00:26:56 +00:00
|
|
|
|
2011-01-27 01:19:48 +00:00
|
|
|
# begin nocover
|
2011-01-25 02:02:48 +00:00
|
|
|
class ReplayThread(threading.Thread):
|
|
|
|
def __init__(self, flow, masterq):
|
|
|
|
self.flow, self.masterq = flow, masterq
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
server = proxy.ServerConnection(self.flow.request)
|
|
|
|
response = server.read_response()
|
|
|
|
response.send(self.masterq)
|
|
|
|
except proxy.ProxyError, v:
|
2011-02-03 01:51:32 +00:00
|
|
|
err = proxy.Error(self.flow.client_conn, v.msg)
|
2011-01-25 02:02:48 +00:00
|
|
|
err.send(self.masterq)
|
2011-01-27 01:19:48 +00:00
|
|
|
# end nocover
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Flow:
|
2011-02-03 01:51:32 +00:00
|
|
|
def __init__(self, client_conn):
|
|
|
|
self.client_conn = client_conn
|
2011-01-25 02:02:48 +00:00
|
|
|
self.request, self.response, self.error = None, None, None
|
|
|
|
self.intercepting = False
|
|
|
|
self._backup = None
|
|
|
|
|
2011-01-31 00:26:56 +00:00
|
|
|
def script_serialize(self):
|
|
|
|
data = self.get_state()
|
2011-02-16 02:10:00 +00:00
|
|
|
return json.dumps(data)
|
2011-01-31 00:26:56 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def script_deserialize(klass, data):
|
|
|
|
try:
|
2011-02-16 02:10:00 +00:00
|
|
|
data = json.loads(data)
|
2011-01-31 00:26:56 +00:00
|
|
|
except Exception:
|
|
|
|
return None
|
|
|
|
return klass.from_state(data)
|
|
|
|
|
|
|
|
def run_script(self, path):
|
2011-01-30 22:44:52 +00:00
|
|
|
"""
|
2011-02-01 21:08:24 +00:00
|
|
|
Run a script on a flow.
|
2011-01-31 00:26:56 +00:00
|
|
|
|
2011-02-01 21:08:24 +00:00
|
|
|
Returns a (flow, stderr output) tuple, or raises RunException if
|
|
|
|
there's an error.
|
2011-01-30 22:44:52 +00:00
|
|
|
"""
|
2011-02-01 22:44:28 +00:00
|
|
|
self.backup()
|
2011-01-31 00:26:56 +00:00
|
|
|
data = self.script_serialize()
|
|
|
|
try:
|
2011-02-01 21:08:24 +00:00
|
|
|
p = subprocess.Popen(
|
|
|
|
[path],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
)
|
2011-01-31 00:26:56 +00:00
|
|
|
except OSError, e:
|
2011-02-01 22:44:28 +00:00
|
|
|
raise RunException(e.args[1], None, None)
|
2011-01-31 00:26:56 +00:00
|
|
|
so, se = p.communicate(data)
|
|
|
|
if p.returncode:
|
2011-02-01 22:44:28 +00:00
|
|
|
raise RunException(
|
|
|
|
"Script returned error code %s"%p.returncode,
|
|
|
|
p.returncode,
|
|
|
|
se
|
|
|
|
)
|
2011-01-31 00:26:56 +00:00
|
|
|
f = Flow.script_deserialize(so)
|
|
|
|
if not f:
|
2011-02-01 22:44:28 +00:00
|
|
|
raise RunException(
|
|
|
|
"Invalid response from script.",
|
|
|
|
p.returncode,
|
|
|
|
se
|
|
|
|
)
|
2011-02-01 21:08:24 +00:00
|
|
|
return f, se
|
2011-01-30 22:44:52 +00:00
|
|
|
|
2011-02-05 21:28:43 +00:00
|
|
|
def get_state(self, nobackup=False):
|
|
|
|
d = dict(
|
2011-01-26 01:52:03 +00:00
|
|
|
request = self.request.get_state() if self.request else None,
|
|
|
|
response = self.response.get_state() if self.response else None,
|
|
|
|
error = self.error.get_state() if self.error else None,
|
2011-02-05 21:28:43 +00:00
|
|
|
client_conn = self.client_conn.get_state()
|
2011-01-26 01:52:03 +00:00
|
|
|
)
|
2011-02-05 21:28:43 +00:00
|
|
|
if nobackup:
|
|
|
|
d["backup"] = None
|
|
|
|
else:
|
|
|
|
d["backup"] = self._backup
|
|
|
|
return d
|
2011-01-26 01:52:03 +00:00
|
|
|
|
2011-01-31 00:26:56 +00:00
|
|
|
def load_state(self, state):
|
2011-02-05 21:28:43 +00:00
|
|
|
self.client_conn = proxy.ClientConnection.from_state(state["client_conn"])
|
|
|
|
self._backup = state["backup"]
|
2011-01-26 01:52:03 +00:00
|
|
|
if state["request"]:
|
2011-02-05 21:28:43 +00:00
|
|
|
self.request = proxy.Request.from_state(self.client_conn, state["request"])
|
2011-01-26 01:52:03 +00:00
|
|
|
if state["response"]:
|
2011-01-31 00:26:56 +00:00
|
|
|
self.response = proxy.Response.from_state(self.request, state["response"])
|
2011-01-26 01:52:03 +00:00
|
|
|
if state["error"]:
|
2011-01-31 00:26:56 +00:00
|
|
|
self.error = proxy.Error.from_state(state["error"])
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_state(klass, state):
|
|
|
|
f = klass(None)
|
|
|
|
f.load_state(state)
|
2011-01-26 01:52:03 +00:00
|
|
|
return f
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.get_state() == other.get_state()
|
|
|
|
|
2011-02-01 22:44:28 +00:00
|
|
|
def modified(self):
|
|
|
|
# FIXME: Save a serialization in backup, compare current with
|
|
|
|
# backup to detect if flow has _really_ been modified.
|
|
|
|
if self._backup:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2011-01-25 02:02:48 +00:00
|
|
|
def backup(self):
|
2011-02-05 21:28:43 +00:00
|
|
|
self._backup = self.get_state(nobackup=True)
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def revert(self):
|
|
|
|
if self._backup:
|
2011-02-05 21:28:43 +00:00
|
|
|
self.load_state(self._backup)
|
2011-02-01 22:44:28 +00:00
|
|
|
self._backup = None
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def match(self, pattern):
|
|
|
|
if pattern:
|
|
|
|
if self.response:
|
|
|
|
return pattern(self.response)
|
|
|
|
elif self.request:
|
|
|
|
return pattern(self.request)
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_replay(self):
|
2011-02-03 21:05:07 +00:00
|
|
|
return self.client_conn.is_replay()
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def kill(self):
|
2011-01-27 00:32:24 +00:00
|
|
|
if self.request and not self.request.acked:
|
2011-02-03 22:39:28 +00:00
|
|
|
self.request.ack(None)
|
2011-01-27 00:32:24 +00:00
|
|
|
elif self.response and not self.response.acked:
|
2011-02-03 22:39:28 +00:00
|
|
|
self.response.ack(None)
|
2011-01-27 00:32:24 +00:00
|
|
|
self.intercepting = False
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def intercept(self):
|
|
|
|
self.intercepting = True
|
|
|
|
|
|
|
|
def accept_intercept(self):
|
|
|
|
if self.request:
|
|
|
|
if not self.request.acked:
|
|
|
|
self.request.ack()
|
|
|
|
elif self.response and not self.response.acked:
|
|
|
|
self.response.ack()
|
|
|
|
self.intercepting = False
|
|
|
|
|
|
|
|
|
|
|
|
class State:
|
|
|
|
def __init__(self):
|
|
|
|
self.flow_map = {}
|
|
|
|
self.flow_list = []
|
|
|
|
# These are compiled filt expressions:
|
|
|
|
self.limit = None
|
|
|
|
self.intercept = None
|
|
|
|
|
|
|
|
def add_browserconnect(self, f):
|
|
|
|
"""
|
|
|
|
Start a browser connection.
|
|
|
|
"""
|
|
|
|
self.flow_list.insert(0, f)
|
2011-02-03 01:51:32 +00:00
|
|
|
self.flow_map[f.client_conn] = f
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def add_request(self, req):
|
|
|
|
"""
|
|
|
|
Add a request to the state. Returns the matching flow.
|
|
|
|
"""
|
2011-02-03 01:51:32 +00:00
|
|
|
f = self.flow_map.get(req.client_conn)
|
2011-01-25 02:02:48 +00:00
|
|
|
if not f:
|
|
|
|
return False
|
|
|
|
f.request = req
|
|
|
|
return f
|
|
|
|
|
|
|
|
def add_response(self, resp):
|
|
|
|
"""
|
|
|
|
Add a response to the state. Returns the matching flow.
|
|
|
|
"""
|
2011-02-03 01:51:32 +00:00
|
|
|
f = self.flow_map.get(resp.request.client_conn)
|
2011-01-25 02:02:48 +00:00
|
|
|
if not f:
|
|
|
|
return False
|
|
|
|
f.response = resp
|
|
|
|
return f
|
|
|
|
|
|
|
|
def add_error(self, err):
|
|
|
|
"""
|
|
|
|
Add an error response to the state. Returns the matching flow, or
|
|
|
|
None if there isn't one.
|
|
|
|
"""
|
2011-02-03 01:51:32 +00:00
|
|
|
f = self.flow_map.get(err.client_conn)
|
2011-01-25 02:02:48 +00:00
|
|
|
if not f:
|
|
|
|
return None
|
|
|
|
f.error = err
|
|
|
|
return f
|
|
|
|
|
2011-02-16 02:10:00 +00:00
|
|
|
def load_flows(self, flows):
|
|
|
|
self.flow_list.extend(flows)
|
|
|
|
for i in flows:
|
2011-02-03 21:05:07 +00:00
|
|
|
self.flow_map[i.client_conn] = i
|
2011-01-26 03:50:17 +00:00
|
|
|
|
2011-01-25 02:02:48 +00:00
|
|
|
def set_limit(self, limit):
|
|
|
|
"""
|
|
|
|
Limit is a compiled filter expression, or None.
|
|
|
|
"""
|
|
|
|
self.limit = limit
|
|
|
|
|
2011-01-26 03:50:17 +00:00
|
|
|
@property
|
|
|
|
def view(self):
|
|
|
|
if self.limit:
|
|
|
|
return tuple([i for i in self.flow_list if i.match(self.limit)])
|
|
|
|
else:
|
|
|
|
return tuple(self.flow_list[:])
|
|
|
|
|
2011-02-03 01:51:32 +00:00
|
|
|
def get_client_conn(self, itm):
|
2011-02-03 21:05:07 +00:00
|
|
|
if isinstance(itm, proxy.ClientConnection):
|
2011-01-25 02:02:48 +00:00
|
|
|
return itm
|
2011-02-03 01:51:32 +00:00
|
|
|
elif hasattr(itm, "client_conn"):
|
|
|
|
return itm.client_conn
|
2011-01-25 02:02:48 +00:00
|
|
|
elif hasattr(itm, "request"):
|
2011-02-03 01:51:32 +00:00
|
|
|
return itm.request.client_conn
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def lookup(self, itm):
|
|
|
|
"""
|
2011-02-03 01:51:32 +00:00
|
|
|
Checks for matching client_conn, using a Flow, Replay Connection,
|
|
|
|
ClientConnection, Request, Response or Error object. Returns None
|
2011-01-25 02:02:48 +00:00
|
|
|
if not found.
|
|
|
|
"""
|
2011-02-03 01:51:32 +00:00
|
|
|
client_conn = self.get_client_conn(itm)
|
|
|
|
return self.flow_map.get(client_conn)
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def delete_flow(self, f):
|
|
|
|
if not f.intercepting:
|
2011-02-03 01:51:32 +00:00
|
|
|
c = self.get_client_conn(f)
|
2011-01-26 03:50:17 +00:00
|
|
|
if c in self.flow_map:
|
|
|
|
del self.flow_map[c]
|
2011-01-25 02:02:48 +00:00
|
|
|
self.flow_list.remove(f)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
for i in self.flow_list[:]:
|
|
|
|
self.delete_flow(i)
|
|
|
|
|
|
|
|
def accept_all(self):
|
|
|
|
for i in self.flow_list[:]:
|
|
|
|
i.accept_intercept()
|
|
|
|
|
|
|
|
def kill_flow(self, f):
|
|
|
|
f.kill()
|
|
|
|
self.delete_flow(f)
|
|
|
|
|
|
|
|
def revert(self, f):
|
2011-02-03 01:51:32 +00:00
|
|
|
conn = self.get_client_conn(f)
|
2011-01-25 02:02:48 +00:00
|
|
|
f.revert()
|
|
|
|
|
|
|
|
def replay(self, f, masterq):
|
|
|
|
"""
|
|
|
|
Returns None if successful, or error message if not.
|
|
|
|
"""
|
|
|
|
#begin nocover
|
|
|
|
if f.intercepting:
|
|
|
|
return "Can't replay while intercepting..."
|
|
|
|
if f.request:
|
|
|
|
f.backup()
|
2011-02-03 01:51:32 +00:00
|
|
|
conn = self.get_client_conn(f)
|
2011-02-03 21:05:07 +00:00
|
|
|
f.client_conn.set_replay()
|
2011-01-25 02:02:48 +00:00
|
|
|
if f.request.content:
|
|
|
|
f.request.headers["content-length"] = [str(len(f.request.content))]
|
|
|
|
f.response = None
|
|
|
|
f.error = None
|
|
|
|
rt = ReplayThread(f, masterq)
|
|
|
|
rt.start()
|
|
|
|
#end nocover
|
2011-02-16 01:33:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FlowWriter:
|
|
|
|
def __init__(self, fo):
|
|
|
|
self.fo = fo
|
|
|
|
self.ns = netstring.FileEncoder(fo)
|
|
|
|
|
|
|
|
def add(self, flow):
|
|
|
|
d = flow.get_state()
|
|
|
|
s = json.dumps(d)
|
|
|
|
self.ns.write(s)
|
|
|
|
|
|
|
|
|
|
|
|
class FlowReader:
|
|
|
|
def __init__(self, fo):
|
|
|
|
self.fo = fo
|
|
|
|
self.ns = netstring.decode_file(fo)
|
|
|
|
|
|
|
|
def stream(self):
|
|
|
|
"""
|
|
|
|
Yields Flow objects from the dump.
|
|
|
|
"""
|
|
|
|
for i in self.ns:
|
|
|
|
data = json.loads(i)
|
|
|
|
yield Flow.from_state(data)
|
|
|
|
|