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-01-31 00:26:56 +00:00
|
|
|
import subprocess, base64, sys
|
2011-01-26 21:52:42 +00:00
|
|
|
from contrib import bson
|
2011-01-25 02:02:48 +00:00
|
|
|
import proxy, threading
|
|
|
|
|
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-25 02:02:48 +00:00
|
|
|
class ReplayConnection:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
err = proxy.Error(self.flow.connection, v.msg)
|
|
|
|
err.send(self.masterq)
|
2011-01-27 01:19:48 +00:00
|
|
|
# end nocover
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Flow:
|
|
|
|
def __init__(self, connection):
|
|
|
|
self.connection = connection
|
|
|
|
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()
|
|
|
|
data = bson.dumps(data)
|
|
|
|
return base64.encodestring(data)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def script_deserialize(klass, data):
|
|
|
|
try:
|
2011-02-02 23:16:03 +00:00
|
|
|
data = base64.decodestring(data)
|
2011-01-31 00:26:56 +00:00
|
|
|
data = bson.loads(data)
|
|
|
|
# bson.loads doesn't define a particular exception on error...
|
|
|
|
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-01-27 02:03:53 +00:00
|
|
|
def dump(self):
|
|
|
|
data = dict(
|
|
|
|
flows = [self.get_state()]
|
|
|
|
)
|
|
|
|
return bson.dumps(data)
|
|
|
|
|
2011-01-26 01:52:03 +00:00
|
|
|
def get_state(self):
|
|
|
|
return dict(
|
|
|
|
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-01-31 00:26:56 +00:00
|
|
|
def load_state(self, state):
|
2011-01-26 01:52:03 +00:00
|
|
|
if state["request"]:
|
2011-01-31 00:26:56 +00:00
|
|
|
self.request = proxy.Request.from_state(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):
|
|
|
|
if not self._backup:
|
|
|
|
self._backup = [
|
|
|
|
self.connection.copy() if self.connection else None,
|
|
|
|
self.request.copy() if self.request else None,
|
|
|
|
self.response.copy() if self.response else None,
|
|
|
|
self.error.copy() if self.error else None,
|
|
|
|
]
|
|
|
|
|
|
|
|
def revert(self):
|
|
|
|
if self._backup:
|
|
|
|
restore = [i.copy() if i else None for i in self._backup]
|
|
|
|
self.connection, self.request, self.response, self.error = restore
|
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):
|
|
|
|
return isinstance(self.connection, ReplayConnection)
|
|
|
|
|
|
|
|
def kill(self):
|
2011-01-27 00:32:24 +00:00
|
|
|
if self.request and not self.request.acked:
|
|
|
|
self.request.kill = True
|
|
|
|
self.request.ack()
|
|
|
|
elif self.response and not self.response.acked:
|
|
|
|
self.response.kill = True
|
|
|
|
self.response.ack()
|
|
|
|
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)
|
|
|
|
self.flow_map[f.connection] = f
|
|
|
|
|
|
|
|
def add_request(self, req):
|
|
|
|
"""
|
|
|
|
Add a request to the state. Returns the matching flow.
|
|
|
|
"""
|
|
|
|
f = self.flow_map.get(req.connection)
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
f = self.flow_map.get(resp.request.connection)
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
f = self.flow_map.get(err.connection)
|
|
|
|
if not f:
|
|
|
|
return None
|
|
|
|
f.error = err
|
|
|
|
return f
|
|
|
|
|
2011-01-26 03:50:17 +00:00
|
|
|
def dump_flows(self):
|
2011-01-26 21:52:42 +00:00
|
|
|
data = dict(
|
|
|
|
flows =[i.get_state() for i in self.view]
|
|
|
|
)
|
|
|
|
return bson.dumps(data)
|
2011-01-26 03:50:17 +00:00
|
|
|
|
2011-01-30 22:44:52 +00:00
|
|
|
def load_flows(self, js):
|
2011-01-26 21:52:42 +00:00
|
|
|
data = bson.loads(js)
|
2011-01-30 22:44:52 +00:00
|
|
|
data = [Flow.from_state(i) for i in data["flows"]]
|
2011-01-26 03:50:17 +00:00
|
|
|
self.flow_list.extend(data)
|
|
|
|
|
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-01-25 02:02:48 +00:00
|
|
|
def get_connection(self, itm):
|
|
|
|
if isinstance(itm, (proxy.BrowserConnection, ReplayConnection)):
|
|
|
|
return itm
|
|
|
|
elif hasattr(itm, "connection"):
|
|
|
|
return itm.connection
|
|
|
|
elif hasattr(itm, "request"):
|
|
|
|
return itm.request.connection
|
|
|
|
|
|
|
|
def lookup(self, itm):
|
|
|
|
"""
|
|
|
|
Checks for matching connection, using a Flow, Replay Connection,
|
|
|
|
BrowserConnection, Request, Response or Error object. Returns None
|
|
|
|
if not found.
|
|
|
|
"""
|
|
|
|
connection = self.get_connection(itm)
|
|
|
|
return self.flow_map.get(connection)
|
|
|
|
|
|
|
|
def delete_flow(self, f):
|
|
|
|
if not f.intercepting:
|
|
|
|
c = self.get_connection(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):
|
|
|
|
"""
|
|
|
|
Replaces the matching connection object with a ReplayConnection object.
|
|
|
|
"""
|
|
|
|
conn = self.get_connection(f)
|
2011-01-26 09:17:42 +00:00
|
|
|
if conn in self.flow_map:
|
|
|
|
del self.flow_map[conn]
|
2011-01-25 02:02:48 +00:00
|
|
|
f.revert()
|
|
|
|
self.flow_map[f.connection] = f
|
|
|
|
|
|
|
|
def replay(self, f, masterq):
|
|
|
|
"""
|
|
|
|
Replaces the matching connection object with a ReplayConnection object.
|
|
|
|
|
|
|
|
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()
|
|
|
|
conn = self.get_connection(f)
|
2011-01-26 03:50:17 +00:00
|
|
|
if conn in self.flow_map:
|
|
|
|
del self.flow_map[conn]
|
2011-01-25 02:02:48 +00:00
|
|
|
rp = ReplayConnection()
|
|
|
|
f.connection = rp
|
|
|
|
f.request.connection = rp
|
|
|
|
if f.request.content:
|
|
|
|
f.request.headers["content-length"] = [str(len(f.request.content))]
|
|
|
|
f.response = None
|
|
|
|
f.error = None
|
|
|
|
self.flow_map[rp] = f
|
|
|
|
rt = ReplayThread(f, masterq)
|
|
|
|
rt.start()
|
|
|
|
#end nocover
|