Skip to content

Instantly share code, notes, and snippets.

@nrafter
Created May 23, 2018 03:05
Show Gist options
  • Save nrafter/24876758239aa997b4b63dc5c99d1fd1 to your computer and use it in GitHub Desktop.
Save nrafter/24876758239aa997b4b63dc5c99d1fd1 to your computer and use it in GitHub Desktop.
mitmproxy.py
from bs4 import BeautifulSoup
from mitmproxy import ctx, http
import argparse
class Injector:
def __init__(self, path):
self.path = path
def response(self, flow: http.HTTPFlow) -> None:
if self.path:
html = BeautifulSoup(flow.response.content, "html.parser")
print(self.path)
print(flow.response.headers["content-type"])
if flow.response.headers["content-type"] == 'text/html':
script = html.new_tag(
"script",
src=self.path,
type='application/javascript')
html.body.insert(0, script)
flow.response.content = str(html).encode("utf8")
print("Script injected.")
def start():
parser = argparse.ArgumentParser()
parser.add_argument("path", type=str)
args = parser.parse_args()
return Injector(args.path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment