python3 zlib.error: Error -3 while decompressing data: invalid distance too far back

客户端上报的数据中有gzip压缩加urlencode加base64encode

服务端python逆向解析的时候偶发出现python3 zlib.error: Error -3 while decompressing data: invalid distance too far back,

出现这个问题一般都是数据转化过程中出现问题

    s2 = parse.unquote(s1.decode('utf-8'))
    s3 = s2.encode('ISO-8859-1')
    return gzip.decompress(s3)

  对应python中的代码是这样的,通过答应java和python的s3数据的时候发现两边的数据中有几行不相同, java这边打印的ord 是43, python这边是32, 43和32分别对应的字符是‘+’和‘ ’

我在unquote完之后替换掉+为‘ ’ , 发现还是偶发同样的错误, 只不过概率变低了, 所以知道自己的方向是对的, 只不过还有些地方有问题, 接着看python的源码, 发现相同包下还有一个

def unquote_plus(string, encoding='utf-8', errors='replace'):
    """Like unquote(), but also replace plus signs by spaces, as required for
    unquoting HTML form values.

    unquote_plus('%7e/abc+def') -> '~/abc def'
    """
    string = string.replace('+', ' ')
    return unquote(string, encoding, errors)

  顿时感觉这就是能解决问题的方法, 用个方法验证后果然解决了问题

发现是自己替换字符串的顺序不对, 应该在decode之前去替换, 很尴尬, 记录下这个方法, 下次发现如果有urlencode 和其他语言不一致的情况下优先替换成plus方法试一下

posted @ 2020-06-08 21:04  screte  阅读(6172)  评论(0编辑  收藏  举报