diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 78b5003..117d02f 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -11,6 +11,7 @@ - @mmetince - @niallmerrigan - @auraltension +- @HAMIDx9 #Unintentional contributors and/or projects that I stole code from diff --git a/plugins/inject.py b/plugins/inject.py index a005215..27bda03 100644 --- a/plugins/inject.py +++ b/plugins/inject.py @@ -57,27 +57,37 @@ class Inject(Plugin): def response(self, response, request, data): + encoding = None ip = response.getClientIP() hn = response.getRequestHostname() - mime = response.headers['Content-Type'] + + try: + mime = response.headers['Content-Type'] + except KeyError: + return + + if "charset" in mime: + match = re.search('charset=(.*)', mime) + if match: + encoding = match.group(1).strip().replace('"', "") + else: + try: + encoding = chardet.detect(data)["encoding"] + except: + pass + else: + try: + encoding = chardet.detect(data)["encoding"] + except: + pass if self._should_inject(ip, hn) and self._ip_filter(ip) and self._host_filter(hn) and (hn not in self.ip) and ("text/html" in mime): - if "charset" in mime: - match = re.search('charset=(.*)', mime) - if match: - encoding = match.group(1).strip().replace('"', "") - else: - try: - encoding = chardet.detect(data)["encoding"] - except: - encoding = None + if encoding is not None: + html = BeautifulSoup(data.decode(encoding, "ignore"), "lxml") + else: + html = BeautifulSoup(data, "lxml") - if encoding: - html = BeautifulSoup(data.decode(encoding, "ignore"), "lxml") - else: - html = BeautifulSoup(data, "lxml") # let bs find the encoding - if html.body: if self.html_url: diff --git a/requirements.txt b/requirements.txt index c4d86b4..e1c67d1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ git+git://github.com/kti/python-netfilterqueue pyinotify -pycrypto>=2.6 -pyasn1>=0.1.7 +pycrypto +pyasn1 cryptography Pillow netaddr @@ -23,4 +23,4 @@ python-magic msgpack-python requests pypcap -chardet +chardet \ No newline at end of file