import abc import html import json import logging from mitmproxy import flowfilter from mitmproxy.http import HTTPFlow logger = logging.getLogger(__name__) class InjectionGenerator: """Abstract class for an generator of the injection content in order to inject the URL index.""" ENCODING = "UTF8" @abc.abstractmethod def inject(self, index, flow: HTTPFlow): """Injects the given URL index into the given flow.""" pass class HTMLInjection(InjectionGenerator): """Injects the URL index either by creating a new HTML page or by appending is to an existing page.""" def __init__(self, insert: bool = False): """Initializes the HTMLInjection. Args: insert: boolean to decide whether to insert the URL index to an existing page (True) or to create a new page containing the URL index. """ self.insert = insert @classmethod def _form_html(cls, url): return f"
" @classmethod def _link_html(cls, url): return f"link to {url}" @classmethod def index_html(cls, index): link_htmls = [] for scheme_netloc, paths in index.items(): for path, methods in paths.items(): url = scheme_netloc + path if "POST" in methods: link_htmls.append(cls._form_html(url)) if "GET" in methods: link_htmls.append(cls._link_html(url)) return " br>".join(link_htmls) @classmethod def landing_page(cls, index): return ( "" + cls.index_html(index) + "" ) def inject(self, index, flow: HTTPFlow): if flow.response is not None: if flow.response.status_code != 404 and not self.insert: logger.warning( f"URL '{flow.request.url}' didn't return 404 status, " f"index page would overwrite valid page.") elif self.insert: content = (flow.response .content .decode(self.ENCODING, "backslashreplace")) if "