2015-03-15 01:53:05 +00:00
|
|
|
from __future__ import absolute_import, print_function
|
2015-09-11 17:03:50 +00:00
|
|
|
import traceback
|
2015-07-29 09:39:53 +00:00
|
|
|
|
2015-09-11 17:03:50 +00:00
|
|
|
import click
|
2015-09-12 11:49:16 +00:00
|
|
|
import itertools
|
2010-03-01 01:58:04 +00:00
|
|
|
|
2015-09-16 16:45:22 +00:00
|
|
|
from netlib.http import CONTENT_MISSING
|
2015-09-11 17:03:50 +00:00
|
|
|
import netlib.utils
|
2015-09-12 11:49:16 +00:00
|
|
|
from . import flow, filt, contentviews
|
2015-09-11 17:03:50 +00:00
|
|
|
from .exceptions import ContentViewException
|
2014-10-26 04:13:25 +00:00
|
|
|
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2014-10-26 04:13:25 +00:00
|
|
|
class DumpError(Exception):
|
|
|
|
pass
|
2011-02-16 21:18:38 +00:00
|
|
|
|
|
|
|
|
2011-02-16 10:03:46 +00:00
|
|
|
class Options(object):
|
2013-03-02 23:26:20 +00:00
|
|
|
attributes = [
|
2013-07-23 22:32:56 +00:00
|
|
|
"app",
|
2013-12-08 14:46:11 +00:00
|
|
|
"app_host",
|
|
|
|
"app_port",
|
2011-03-09 00:15:31 +00:00
|
|
|
"anticache",
|
2011-07-15 03:21:04 +00:00
|
|
|
"anticomp",
|
2011-03-09 00:15:31 +00:00
|
|
|
"client_replay",
|
2014-09-08 21:34:43 +00:00
|
|
|
"filtstr",
|
2014-03-12 21:39:23 +00:00
|
|
|
"flow_detail",
|
2011-03-09 00:15:31 +00:00
|
|
|
"keepserving",
|
2011-02-20 22:08:35 +00:00
|
|
|
"kill",
|
2011-05-13 22:44:25 +00:00
|
|
|
"no_server",
|
2012-03-10 00:42:10 +00:00
|
|
|
"nopop",
|
2011-03-10 22:56:10 +00:00
|
|
|
"refresh_server_playback",
|
2012-03-17 04:20:34 +00:00
|
|
|
"replacements",
|
2011-05-14 23:54:12 +00:00
|
|
|
"rfile",
|
2011-03-09 00:15:31 +00:00
|
|
|
"rheaders",
|
2012-08-18 12:14:16 +00:00
|
|
|
"setheaders",
|
2011-03-05 00:03:26 +00:00
|
|
|
"server_replay",
|
2013-06-13 14:04:04 +00:00
|
|
|
"scripts",
|
2013-03-17 04:53:39 +00:00
|
|
|
"showhost",
|
2011-03-09 00:15:31 +00:00
|
|
|
"stickycookie",
|
2011-03-20 04:31:54 +00:00
|
|
|
"stickyauth",
|
2014-07-21 19:06:55 +00:00
|
|
|
"stream_large_bodies",
|
2011-02-20 22:08:35 +00:00
|
|
|
"verbosity",
|
2014-12-11 18:21:33 +00:00
|
|
|
"outfile",
|
2014-10-03 10:29:44 +00:00
|
|
|
"replay_ignore_content",
|
|
|
|
"replay_ignore_params",
|
2014-12-18 20:56:27 +00:00
|
|
|
"replay_ignore_payload_params",
|
2015-03-07 16:38:18 +00:00
|
|
|
"replay_ignore_host"
|
2011-02-16 10:03:46 +00:00
|
|
|
]
|
2014-10-26 04:13:25 +00:00
|
|
|
|
2011-02-16 10:03:46 +00:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
setattr(self, k, v)
|
2013-03-02 23:26:20 +00:00
|
|
|
for i in self.attributes:
|
2011-02-16 21:18:38 +00:00
|
|
|
if not hasattr(self, i):
|
|
|
|
setattr(self, i, None)
|
2011-02-16 10:03:46 +00:00
|
|
|
|
|
|
|
|
2011-02-16 09:10:24 +00:00
|
|
|
class DumpMaster(flow.FlowMaster):
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2015-09-12 15:10:38 +00:00
|
|
|
def __init__(self, server, options, outfile=None):
|
2011-02-16 09:10:24 +00:00
|
|
|
flow.FlowMaster.__init__(self, server, flow.State())
|
2011-02-16 10:03:46 +00:00
|
|
|
self.outfile = outfile
|
|
|
|
self.o = options
|
2011-05-15 00:23:34 +00:00
|
|
|
self.anticache = options.anticache
|
2011-07-15 03:21:04 +00:00
|
|
|
self.anticomp = options.anticomp
|
2013-03-17 04:43:31 +00:00
|
|
|
self.showhost = options.showhost
|
2014-10-26 04:13:25 +00:00
|
|
|
self.replay_ignore_params = options.replay_ignore_params
|
2014-10-03 10:29:44 +00:00
|
|
|
self.replay_ignore_content = options.replay_ignore_content
|
2015-03-07 16:38:18 +00:00
|
|
|
self.replay_ignore_host = options.replay_ignore_host
|
2011-05-15 00:23:34 +00:00
|
|
|
self.refresh_server_playback = options.refresh_server_playback
|
2014-12-18 20:56:27 +00:00
|
|
|
self.replay_ignore_payload_params = options.replay_ignore_payload_params
|
2010-03-01 01:58:04 +00:00
|
|
|
|
2014-07-21 19:06:55 +00:00
|
|
|
self.set_stream_large_bodies(options.stream_large_bodies)
|
|
|
|
|
2014-09-08 21:34:43 +00:00
|
|
|
if options.filtstr:
|
|
|
|
self.filt = filt.parse(options.filtstr)
|
2011-02-16 21:44:08 +00:00
|
|
|
else:
|
|
|
|
self.filt = None
|
|
|
|
|
2011-02-25 04:32:03 +00:00
|
|
|
if options.stickycookie:
|
|
|
|
self.set_stickycookie(options.stickycookie)
|
|
|
|
|
2011-03-20 04:31:54 +00:00
|
|
|
if options.stickyauth:
|
|
|
|
self.set_stickyauth(options.stickyauth)
|
|
|
|
|
2014-12-11 18:21:33 +00:00
|
|
|
if options.outfile:
|
2016-01-10 10:45:26 +00:00
|
|
|
err = self.start_stream_to_path(
|
|
|
|
options.outfile[0],
|
|
|
|
options.outfile[1]
|
|
|
|
)
|
|
|
|
if err:
|
|
|
|
raise DumpError(err)
|
2011-02-16 21:18:38 +00:00
|
|
|
|
2012-03-17 04:29:25 +00:00
|
|
|
if options.replacements:
|
|
|
|
for i in options.replacements:
|
|
|
|
self.replacehooks.add(*i)
|
2012-03-17 04:20:34 +00:00
|
|
|
|
2012-08-18 12:14:16 +00:00
|
|
|
if options.setheaders:
|
|
|
|
for i in options.setheaders:
|
|
|
|
self.setheaders.add(*i)
|
|
|
|
|
2011-03-05 00:03:26 +00:00
|
|
|
if options.server_replay:
|
2011-03-05 22:21:31 +00:00
|
|
|
self.start_server_playback(
|
|
|
|
self._readflow(options.server_replay),
|
2011-03-06 04:08:56 +00:00
|
|
|
options.kill, options.rheaders,
|
2012-03-10 00:42:10 +00:00
|
|
|
not options.keepserving,
|
2014-10-03 10:29:44 +00:00
|
|
|
options.nopop,
|
|
|
|
options.replay_ignore_params,
|
2014-12-18 20:56:27 +00:00
|
|
|
options.replay_ignore_content,
|
|
|
|
options.replay_ignore_payload_params,
|
2015-03-07 16:38:18 +00:00
|
|
|
options.replay_ignore_host
|
2011-03-05 22:21:31 +00:00
|
|
|
)
|
2011-02-20 20:54:39 +00:00
|
|
|
|
2011-03-05 22:21:31 +00:00
|
|
|
if options.client_replay:
|
2011-03-06 03:54:49 +00:00
|
|
|
self.start_client_playback(
|
|
|
|
self._readflow(options.client_replay),
|
|
|
|
not options.keepserving
|
|
|
|
)
|
2011-03-05 22:21:31 +00:00
|
|
|
|
2013-06-17 14:48:06 +00:00
|
|
|
scripts = options.scripts or []
|
2014-01-12 10:01:59 +00:00
|
|
|
for command in scripts:
|
|
|
|
err = self.load_script(command)
|
2011-08-03 04:36:20 +00:00
|
|
|
if err:
|
|
|
|
raise DumpError(err)
|
2011-03-09 00:15:31 +00:00
|
|
|
|
2011-08-04 21:41:29 +00:00
|
|
|
if options.rfile:
|
|
|
|
try:
|
2015-01-02 00:26:22 +00:00
|
|
|
self.load_flows_file(options.rfile)
|
2015-05-30 00:03:28 +00:00
|
|
|
except flow.FlowReadError as v:
|
2015-01-02 00:26:22 +00:00
|
|
|
self.add_event("Flow file corrupted.", "error")
|
|
|
|
raise DumpError(v)
|
2011-08-04 21:41:29 +00:00
|
|
|
|
2013-07-23 22:32:56 +00:00
|
|
|
if self.o.app:
|
2014-09-13 23:46:01 +00:00
|
|
|
self.start_app(self.o.app_host, self.o.app_port)
|
2013-07-23 22:32:56 +00:00
|
|
|
|
2015-01-05 21:12:38 +00:00
|
|
|
def _readflow(self, paths):
|
2015-02-05 13:44:45 +00:00
|
|
|
"""
|
|
|
|
Utitility function that reads a list of flows
|
|
|
|
or raises a DumpError if that fails.
|
|
|
|
"""
|
2011-03-05 22:21:31 +00:00
|
|
|
try:
|
2015-02-05 13:44:45 +00:00
|
|
|
return flow.read_flows_from_paths(paths)
|
|
|
|
except flow.FlowReadError as e:
|
|
|
|
raise DumpError(e.strerror)
|
2011-02-25 04:32:03 +00:00
|
|
|
|
2014-03-13 00:04:45 +00:00
|
|
|
def add_event(self, e, level="info"):
|
2014-11-14 23:52:26 +00:00
|
|
|
needed = dict(error=0, info=1, debug=2).get(level, 1)
|
2014-03-12 21:39:23 +00:00
|
|
|
if self.o.verbosity >= needed:
|
2015-09-11 17:03:50 +00:00
|
|
|
self.echo(
|
|
|
|
e,
|
|
|
|
fg="red" if level == "error" else None,
|
2015-12-10 16:43:58 +00:00
|
|
|
dim=(level == "debug"),
|
|
|
|
err=(level == "error")
|
2015-09-11 17:03:50 +00:00
|
|
|
)
|
2011-07-23 00:57:54 +00:00
|
|
|
|
2015-03-15 02:08:45 +00:00
|
|
|
@staticmethod
|
2015-09-11 17:03:50 +00:00
|
|
|
def indent(n, text):
|
|
|
|
l = str(text).strip().splitlines()
|
2015-03-15 02:08:45 +00:00
|
|
|
pad = " " * n
|
|
|
|
return "\n".join(pad + i for i in l)
|
2011-02-17 01:26:50 +00:00
|
|
|
|
2015-09-11 17:03:50 +00:00
|
|
|
def echo(self, text, indent=None, **style):
|
|
|
|
if indent:
|
|
|
|
text = self.indent(indent, text)
|
|
|
|
click.secho(text, file=self.outfile, **style)
|
|
|
|
|
|
|
|
def _echo_message(self, message):
|
2015-03-15 01:53:05 +00:00
|
|
|
if self.o.flow_detail >= 2:
|
2015-09-11 17:03:50 +00:00
|
|
|
headers = "\r\n".join(
|
|
|
|
"{}: {}".format(
|
|
|
|
click.style(k, fg="blue", bold=True),
|
|
|
|
click.style(v, fg="blue"))
|
2016-01-27 09:12:18 +00:00
|
|
|
for k, v in message.headers.fields
|
2015-09-11 17:03:50 +00:00
|
|
|
)
|
|
|
|
self.echo(headers, indent=4)
|
2015-03-15 01:53:05 +00:00
|
|
|
if self.o.flow_detail >= 3:
|
2015-09-26 15:41:14 +00:00
|
|
|
if message.content == CONTENT_MISSING:
|
2015-09-11 17:03:50 +00:00
|
|
|
self.echo("(content missing)", indent=4)
|
2015-09-26 15:41:14 +00:00
|
|
|
elif message.content:
|
2015-09-11 17:03:50 +00:00
|
|
|
self.echo("")
|
2015-09-12 11:49:16 +00:00
|
|
|
|
2015-09-11 17:03:50 +00:00
|
|
|
try:
|
2015-09-12 11:49:16 +00:00
|
|
|
type, lines = contentviews.get_content_view(
|
2015-09-12 15:10:38 +00:00
|
|
|
contentviews.get("Auto"),
|
2015-09-26 15:41:14 +00:00
|
|
|
message.content,
|
2015-09-12 11:49:16 +00:00
|
|
|
headers=message.headers
|
2015-09-11 17:03:50 +00:00
|
|
|
)
|
|
|
|
except ContentViewException:
|
|
|
|
s = "Content viewer failed: \n" + traceback.format_exc()
|
|
|
|
self.add_event(s, "debug")
|
2015-09-12 11:49:16 +00:00
|
|
|
type, lines = contentviews.get_content_view(
|
2015-09-12 15:10:38 +00:00
|
|
|
contentviews.get("Raw"),
|
2015-09-26 15:41:14 +00:00
|
|
|
message.content,
|
2015-09-12 11:49:16 +00:00
|
|
|
headers=message.headers
|
2015-09-11 17:03:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
styles = dict(
|
|
|
|
highlight=dict(bold=True),
|
|
|
|
offset=dict(fg="blue"),
|
|
|
|
header=dict(fg="green", bold=True),
|
|
|
|
text=dict(fg="green")
|
|
|
|
)
|
|
|
|
|
|
|
|
def colorful(line):
|
2015-09-12 15:10:38 +00:00
|
|
|
yield u" " # we can already indent here
|
2015-09-11 17:03:50 +00:00
|
|
|
for (style, text) in line:
|
|
|
|
yield click.style(text, **styles.get(style, {}))
|
|
|
|
|
2015-09-12 11:49:16 +00:00
|
|
|
if self.o.flow_detail == 3:
|
2015-09-12 15:10:38 +00:00
|
|
|
lines_to_echo = itertools.islice(lines, 70)
|
2015-09-12 11:49:16 +00:00
|
|
|
else:
|
|
|
|
lines_to_echo = lines
|
|
|
|
|
2015-09-12 15:10:38 +00:00
|
|
|
lines_to_echo = list(lines_to_echo)
|
|
|
|
|
|
|
|
content = u"\r\n".join(
|
|
|
|
u"".join(colorful(line)) for line in lines_to_echo
|
2015-09-11 17:03:50 +00:00
|
|
|
)
|
2015-09-12 11:49:16 +00:00
|
|
|
|
2015-09-11 17:03:50 +00:00
|
|
|
self.echo(content)
|
2015-09-12 11:49:16 +00:00
|
|
|
if next(lines, None):
|
|
|
|
self.echo("(cut off)", indent=4, dim=True)
|
2015-09-11 17:03:50 +00:00
|
|
|
|
2015-03-15 02:08:45 +00:00
|
|
|
if self.o.flow_detail >= 2:
|
2015-09-11 17:03:50 +00:00
|
|
|
self.echo("")
|
2015-03-15 01:53:05 +00:00
|
|
|
|
2015-09-11 17:03:50 +00:00
|
|
|
def _echo_request_line(self, flow):
|
|
|
|
if flow.request.stickycookie:
|
|
|
|
stickycookie = click.style("[stickycookie] ", fg="yellow", bold=True)
|
|
|
|
else:
|
|
|
|
stickycookie = ""
|
|
|
|
|
|
|
|
if flow.client_conn:
|
|
|
|
client = click.style(flow.client_conn.address.host, bold=True)
|
|
|
|
else:
|
|
|
|
client = click.style("[replay]", fg="yellow", bold=True)
|
|
|
|
|
|
|
|
method = flow.request.method
|
2016-01-27 09:12:18 +00:00
|
|
|
method_color = dict(
|
2015-09-11 17:03:50 +00:00
|
|
|
GET="green",
|
|
|
|
DELETE="red"
|
|
|
|
).get(method.upper(), "magenta")
|
|
|
|
method = click.style(method, fg=method_color, bold=True)
|
2015-09-25 23:23:59 +00:00
|
|
|
if self.showhost:
|
|
|
|
url = flow.request.pretty_url
|
|
|
|
else:
|
|
|
|
url = flow.request.url
|
|
|
|
url = click.style(url, bold=True)
|
2015-09-11 17:03:50 +00:00
|
|
|
|
|
|
|
line = "{stickycookie}{client} {method} {url}".format(
|
|
|
|
stickycookie=stickycookie,
|
|
|
|
client=client,
|
|
|
|
method=method,
|
|
|
|
url=url
|
|
|
|
)
|
|
|
|
self.echo(line)
|
|
|
|
|
|
|
|
def _echo_response_line(self, flow):
|
|
|
|
if flow.response.is_replay:
|
|
|
|
replay = click.style("[replay] ", fg="yellow", bold=True)
|
|
|
|
else:
|
|
|
|
replay = ""
|
|
|
|
|
|
|
|
code = flow.response.status_code
|
|
|
|
code_color = None
|
|
|
|
if 200 <= code < 300:
|
|
|
|
code_color = "green"
|
|
|
|
elif 300 <= code < 400:
|
|
|
|
code_color = "magenta"
|
|
|
|
elif 400 <= code < 600:
|
|
|
|
code_color = "red"
|
|
|
|
code = click.style(str(code), fg=code_color, bold=True, blink=(code == 418))
|
2015-09-26 15:41:14 +00:00
|
|
|
reason = click.style(flow.response.reason, fg=code_color, bold=True)
|
2015-09-11 17:03:50 +00:00
|
|
|
|
|
|
|
if flow.response.content == CONTENT_MISSING:
|
|
|
|
size = "(content missing)"
|
|
|
|
else:
|
|
|
|
size = netlib.utils.pretty_size(len(flow.response.content))
|
|
|
|
size = click.style(size, bold=True)
|
|
|
|
|
|
|
|
arrows = click.style("<<", bold=True)
|
2011-03-11 00:06:51 +00:00
|
|
|
|
2015-09-26 15:41:14 +00:00
|
|
|
line = "{replay} {arrows} {code} {reason} {size}".format(
|
2015-09-11 17:03:50 +00:00
|
|
|
replay=replay,
|
|
|
|
arrows=arrows,
|
|
|
|
code=code,
|
2015-09-26 15:41:14 +00:00
|
|
|
reason=reason,
|
2015-09-11 17:03:50 +00:00
|
|
|
size=size
|
|
|
|
)
|
|
|
|
self.echo(line)
|
|
|
|
|
|
|
|
def echo_flow(self, f):
|
2015-03-15 01:53:05 +00:00
|
|
|
if self.o.flow_detail == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if f.request:
|
2015-09-11 17:03:50 +00:00
|
|
|
self._echo_request_line(f)
|
|
|
|
self._echo_message(f.request)
|
2015-03-15 01:53:05 +00:00
|
|
|
|
2011-03-11 00:06:51 +00:00
|
|
|
if f.response:
|
2015-09-11 17:03:50 +00:00
|
|
|
self._echo_response_line(f)
|
|
|
|
self._echo_message(f.response)
|
2015-03-15 01:53:05 +00:00
|
|
|
|
|
|
|
if f.error:
|
2015-09-11 17:03:50 +00:00
|
|
|
self.echo(" << {}".format(f.error.msg), bold=True, fg="red")
|
2015-03-15 01:53:05 +00:00
|
|
|
|
2015-09-12 15:10:38 +00:00
|
|
|
if self.outfile:
|
|
|
|
self.outfile.flush()
|
2011-03-11 00:06:51 +00:00
|
|
|
|
2015-09-11 17:03:50 +00:00
|
|
|
def _process_flow(self, f):
|
|
|
|
self.state.delete_flow(f)
|
|
|
|
if self.filt and not f.match(self.filt):
|
|
|
|
return
|
|
|
|
|
|
|
|
self.echo_flow(f)
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
def handle_request(self, f):
|
|
|
|
flow.FlowMaster.handle_request(self, f)
|
2013-02-16 23:42:48 +00:00
|
|
|
if f:
|
2014-09-03 14:57:56 +00:00
|
|
|
f.reply()
|
2013-02-16 23:42:48 +00:00
|
|
|
return f
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
def handle_response(self, f):
|
|
|
|
flow.FlowMaster.handle_response(self, f)
|
2011-02-16 10:03:46 +00:00
|
|
|
if f:
|
2014-09-03 14:57:56 +00:00
|
|
|
f.reply()
|
2011-03-11 00:06:51 +00:00
|
|
|
self._process_flow(f)
|
2011-03-05 22:21:31 +00:00
|
|
|
return f
|
2011-02-16 21:18:38 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
def handle_error(self, f):
|
|
|
|
flow.FlowMaster.handle_error(self, f)
|
2011-03-11 00:06:51 +00:00
|
|
|
if f:
|
|
|
|
self._process_flow(f)
|
|
|
|
return f
|
|
|
|
|
2012-06-09 01:55:55 +00:00
|
|
|
def shutdown(self): # pragma: no cover
|
2012-04-14 22:35:19 +00:00
|
|
|
return flow.FlowMaster.shutdown(self)
|
|
|
|
|
2012-06-09 01:55:55 +00:00
|
|
|
def run(self): # pragma: no cover
|
2011-05-14 23:54:12 +00:00
|
|
|
if self.o.rfile and not self.o.keepserving:
|
2014-02-04 17:40:12 +00:00
|
|
|
self.shutdown()
|
2011-05-14 23:54:12 +00:00
|
|
|
return
|
2011-02-16 09:10:24 +00:00
|
|
|
try:
|
|
|
|
return flow.FlowMaster.run(self)
|
2011-08-02 04:14:33 +00:00
|
|
|
except BaseException:
|
2011-02-17 23:40:45 +00:00
|
|
|
self.shutdown()
|
|
|
|
raise
|