def map_local_file(self, context, flow): host = flow.request.host if host in self.local_rule: rule, pathname = self.local_rule[host], flow.request.path.split( "?")[0] for match in rule: if match in pathname: form = flow.request.urlencoded_form local_file = rule[match] + pathname.replace( '/', sep).replace(match, '', 1).lstrip(sep) striped_file = re.sub( r'[a-f0-9]{6}\.(js|css|jpg|png|jpeg|gif)$', r'.\1', local_file) if path.isfile(local_file) or path.isfile(striped_file): if path.exists(striped_file): local_file = striped_file content_type = mimetypes.guess_type(local_file)[0] if content_type[0] is None: break body = str(open(local_file).read()) h = self.custom_header(host, content_type, str(len(body))) response = HTTPResponse(http_version=b"HTTP/1.1", status_code=200, reason="local", headers=h, content=body) response.is_replay = True flow.response = response context.log( "\n%s\n%s\nReplied with Local File:\n%s\n%s\n%s\n" % (flow.request.path, '-' * 60, '-' * 80, local_file, "-" * 80)) break else: if flow.live and DEFAULT_PROXY is not None: self.proxy_request_to_upstream(context, flow, DEFAULT_PROXY)
def _restore(flow, rawhead): # Building usable headers headers = Headers() lines = rawhead.decode('utf-8')[:-2].split("\r\n") for line in lines: temp = line.split(": ") headers[temp[0]] = temp[1] body = cache.get('http.cache.body.%s' % flow.request.pretty_url) if len(body) == 0: print("Cache hit but body empty, let's doing a real request") cache.delete('http.cache.body.%s' % flow.request.pretty_url) cache.delete('http.cache.head.%s' % flow.request.pretty_url) return # Building response from cache response = HTTPResponse(b"HTTP/1.1", 200, b"OK", headers, body) print(response) response.headers["X-GIG-Cache"] = "from-cache" # Send forged response flow.reply.send(response)
def inline_flash(self, swf): content = base64.standard_b64encode(swf) return HTTPResponse( "HTTP/1.1", 200, "OK", Headers(content_type="text/html", content_length=str(len(content))), content)
def map_local_file(self, flow): _host = flow.request.host if _host in self.local_rule: (rule, pathname ) = self.local_rule[_host], flow.request.path.split("?")[0] for match in rule: if match in pathname: local_file = rule[match] + pathname.replace( '/', sep).replace(match, '', 1).lstrip(sep) #process hash filename like my_program_xxxxxx.js to local_file my_program_.js' striped_file = re.sub( r'[a-f0-9]{6}\.(js|css|jpg|png|jpeg|gif)$', r'.\1', local_file) if path.isfile(local_file) or path.isfile(striped_file): if path.exists(striped_file): local_file = striped_file content_type = mimetypes.guess_type(local_file)[0] if content_type is None: break body = str(open(local_file).read()) header = self.custom_header(_host, content_type, len(body)) _response = HTTPResponse(b"HTTP/1.1", 200, "local", header, body, is_replay=True) flow.response = _response self.log( "\n%s\n%s\nReplied with Local File:\n%s\n%s\n%s\n" % (flow.request.path, '=' * 80, '-' * 80, local_file, "-" * 80)) break elif _host == 'config.qq.com': header = self.custom_header(_host, 'text/html', 0) flow.response = HTTPResponse(b"HTTP/1.1", 404, "not found", header, '', is_replay=True) self.log("\n%s\n%s\nReplied with empty file%s\n" % (flow.request.path, '=' * 80, '-' * 80)) break elif DEFAULT_PROXY is not None: self.proxy_request_to_upstream(flow, DEFAULT_PROXY)
def response_config_rule(self, context, flow): host, path = flow.request.host, flow.request.path h = self.custom_header(host, 'text/plain', '7') h['location'] = 'http://' + host + path response = HTTPResponse(http_version=b"HTTP/1.1", status_code=200, reason='ok', headers=h, content='{ret:0}') response.is_replay = True flow.reply(response)
def redirect_https_to_http(self, context, flow): host, path = flow.request.host, flow.request.path h = self.custom_header(host, None, '0') h['location'] = 'http://' + host + path response = HTTPResponse(http_version=b"HTTP/1.1", status_code=302, reason='redirect', headers=h, content='') response.is_replay = True flow.reply(response)
def redirect_https_to_http(flow): (_host, _path) = flow.request.host, flow.request.path header = Smarthost.custom_header(_host, None, 0) header['location'] = 'http://' + _host + _path _response = HTTPResponse(b"HTTP/1.1", 302, 'redirect temporary', header, '', is_replay=True) flow.response = _response
def response_config_rule(self, flow): (_host, _path) = flow.request.host, flow.request.path header = self.custom_header(_host, 'text/plain', 7) header['location'] = 'http://' + _host + _path _response = HTTPResponse(b"HTTP/1.1", 200, 'ok', header, content='{ret:0}', is_replay=True) flow.response = _response
def request(context, flow): # pretty_host takes the "Host" header of the request into account, # which is useful in transparent mode where we usually only have the IP # otherwise. # Method 1: Answer with a locally generated response if flow.request.pretty_host.endswith("example.com"): resp = HTTPResponse("HTTP/1.1", 200, "OK", Headers(Content_Type="text/html"), "helloworld") flow.reply(resp) # Method 2: Redirect the request to a different server if flow.request.pretty_host.endswith("example.org"): flow.request.host = "mitmproxy.org"
def handle_request(self, flow): hid = (flow.request.host, flow.request.port) host = flow.request.pretty_host path = flow.request.path[1:] if self.is_supported_api(host) and os.path.isfile(path): print "Stubbing: %s/%s" % (host, path) f = open(path, 'r') resp = HTTPResponse("HTTP/1.1", 200, "OK", Headers(Content_Type="text/json"), f.read()) f.close() flow.reply(resp) elif self.is_supported_host(host): print "Should we capture ?: %s/%s" % (host, path) flow.reply() else: flow.reply()
def dummy_response(self, flow): resp = HTTPResponse("HTTP/1.1", 444, "Blocked", Headers(Content_Type="text/html"), "You got blocked by CDNReaper") flow.reply(resp)
def respond(flow, content): resp = HTTPResponse("HTTP/1.1", 200, "OK", Headers(Content_Type="text/xml"), content) flow.reply(resp)
def landing_page(self): return HTTPResponse( "HTTP/1.1", 200, "OK", Headers(Content_Type="text/html"), self.options.lpage)
def simple_flash(self, swf): return HTTPResponse( "HTTP/1.1", 200, "OK", Headers(content_type="application/x-shockwave-flash-%s" % \ self.options.tag, content_length=str(len(swf))), swf)
def simple_http(self): return HTTPResponse( "HTTP/1.1", 200, "OK", Headers(Content_Type="text/html"), "<html><body>Hello world!</body></html>")
def instrument_error(self): return HTTPResponse( "HTTP/1.1", 400, "Bad request", Headers(Content_Type="text/html"), "<html><body>Instrument error</body></html>")