From 9ec7963f8ed6694e7517952a5019d0ec8e841fb8 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 12 Sep 2014 02:42:45 +0200 Subject: [PATCH] update iframe injector example to use BeautifulSoup --- examples/iframe_injector.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/iframe_injector.py b/examples/iframe_injector.py index 42260a7e3..72563bed1 100644 --- a/examples/iframe_injector.py +++ b/examples/iframe_injector.py @@ -1,5 +1,6 @@ # Usage: mitmdump -s "iframe_injector.py url" # (this script works best with --anticache) +from bs4 import BeautifulSoup from libmproxy.protocol.http import decoded @@ -9,10 +10,13 @@ def start(context, argv): context.iframe_url = argv[1] -def handle_response(context, flow): +def response(context, flow): + if flow.request.host in context.iframe_url: + return with decoded(flow.response): # Remove content encoding (gzip, ...) - c = flow.response.replace( - '', - '' % context.iframe_url) - if c > 0: - context.log("Iframe injected!") \ No newline at end of file + html = BeautifulSoup(flow.response.content) + if html.body: + iframe = html.new_tag("iframe", src=context.iframe_url, frameborder=0, height=0, width=0) + html.body.insert(0, iframe) + flow.response.content = str(html) + context.log("Iframe inserted.") \ No newline at end of file