Sfoglia il codice sorgente

use special header replacement to skip emptied headers

pull/58/head
Kristijan Mitrovic 4 anni fa
parent
commit
bd4aa2ad46
1 ha cambiato i file con 40 aggiunte e 1 eliminazioni
  1. +40
    -1
      bubble-server/src/main/resources/packer/roles/mitmproxy/files/bubble_api.py

+ 40
- 1
bubble-server/src/main/resources/packer/roles/mitmproxy/files/bubble_api.py Vedi File

@@ -25,6 +25,7 @@ from bubble_config import bubble_port, debug_capture_fqdn, \
from mitmproxy import http
from mitmproxy.net.http import headers as nheaders
from mitmproxy.proxy.protocol.async_stream_body import AsyncStreamBody
from mitmproxy.utils import strutils

bubble_log = logging.getLogger(__name__)

@@ -449,6 +450,44 @@ def original_flex_ip(client_addr, fqdns):
return None


def _replace_in_headers(headers, pattern: str, replacement: str):
"""
Taken from original mitmproxy's Header class implementation with sligh change to allow replacement with empty string
(resulting with actual removal/skip of the header line).

Replaces a regular expression pattern with repl in each "name: value"
header line.

Returns:
The number of replacements made.
"""
pattern = re.compile(strutils.escaped_str_to_bytes(pattern))
replacement = strutils.escaped_str_to_bytes(replacement)
repl_count = 0
fields = []

for name, value in headers.fields:
line, n = pattern.subn(replacement, name + b": " + value)

if len(line) == 0:
# Skip/remove this header line and go with the next one:
continue

try:
name, value = line.split(b": ", 1)
except ValueError:
# We get a ValueError if the replacement removed the ": "
# There's not much we can do about this, so we just keep the header as-is.
pass
else:
repl_count += n

fields.append((name, value))

headers.fields = tuple(fields)
return repl_count


def response_header_modify(flow):
return None if flow.response is None else _header_modify(flow.client_conn.address[0], flow.response.headers)

@@ -461,7 +500,7 @@ def _header_modify(client_addr, headers):
if modifiers:
for modifier in modifiers:
modifier_config = json.loads(modifier)
repl_count += headers.replace(modifier_config['regex'], modifier_config['replacement'])
repl_count += _replace_in_headers(headers, modifier_config['regex'], modifier_config['replacement'])

if bubble_log.isEnabledFor(DEBUG):
bubble_log.debug('_header_modify: replacing headers - replacements count: ' + repl_count)


Caricamento…
Annulla
Salva