【问题标题】:Decompressing gzip from HTTPS traffic with zlib, incorrect header check使用 zlib 从 HTTPS 流量中解压缩 gzip,标头检查不正确
【发布时间】:2018-07-12 08:06:32
【问题描述】:

我正在尝试使用 python 创建一个代理,该代理还可以读取请求和响应的内容,我正在使用它来执行此操作:https://github.com/inaz2/proxy2/blob/python3/proxy2.py

但由于某种原因,我无法解压缩任何 gzip 压缩的有效载荷。到目前为止我所尝试的:

@staticmethod
def decode_content_body(data, encoding):
    print(encoding) # -> 'gzip'
    if not data:
        return None

    if encoding == 'identity':
        text = data
    elif encoding in ('gzip', 'x-gzip'):
        try:
            data = data.encoded('latin_1')
            # data = str(data) # no luck
            # data = data.encoded() # no luck
            compressed_stream = StringIO(data)
            gzipper = gzip.GzipFile(fileobj=compressed_stream)
            text = gzipper.read() # -> TypeError: can't concat str to bytes

        except:
            # data has to be bytes like object, says zlib
            # text = zlib.decompress(data.encode()) # -> zlib.error: Error -3 while decompressing data: incorrect header check
            text = zlib.decompress(data.encode(), -zlib.MAX_WBITS) # -> zlib.error: Error -3 while decompressing data: invalid block type

    elif encoding == 'deflate':
        try:
            text = zlib.decompress(data)
        except zlib.error:
            text = zlib.decompress(data, -zlib.MAX_WBITS)

    else:
        raise Exception("Unknown Content-Encoding: {}".format(encoding))
    return text

数据不是人类可读的格式,因此它显然是用某些东西压缩的。代理正在使用 HTTPS 的网站。

【问题讨论】:

    标签: python python-3.x https decode zlib


    【解决方案1】:

    问题可能在于传递给您的函数的data

    它的类型为str,因此您用于读取请求数据的代码已经尝试将其接收到的bytes 数据解码str。可能出现以下两种情况之一:

    1. 它根本不希望bytes 包含压缩数据
    2. 它希望使用与实际使用的机制不同的机制来压缩数据

    因此:str 包含胡言乱语!

    我在使用stomp.py 包时遇到了同样的问题!就我而言,解决方案是在打开 Stomp 连接时显式设置 auto_decode=False 参数。

    希望有帮助!

    PS:有关如何解决我的特定问题的更多信息:https://groups.google.com/forum/#!topic/openraildata-talk/IsO206F5US8

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      相关资源
      最近更新 更多