Python 使用dpkt分析数据包

来源:互联网 发布:php中app接口代码 编辑:程序博客网 时间:2024/05/29 03:52


上周Lightless放了一题数据包分析的题,挺有意思的

题目地址: http://7xscw6.com1.z0.glb.clouddn.com/hahaha.pcapng

数据包分析这部分很简单,随便看看可以看出来这是个用sqlmap去艹服务器的数据包,然后flag使用盲注注出来的.

最简单的方法就是手工把盲注的包提取出来,手工分析得到flag.

不过我觉得手工太low了,所以有了这篇文章,使用Python的dpkt包对pcap包进行分析.

直接先贴代码:

12345678910111213141516171819202122232425262728293031323334353637383940414243
#!/usr/bin/env python# -*- coding:utf-8 -*-from dpkt.ethernet import Ethernetimport dpktimport reimport urllibresult = []f = file("test.pcapng")pcap = dpkt.pcapng.Reader(f)(ts, buf) = pcap.next()while True:    http_data = Ethernet(buf).data.data.data    request = dpkt.http.Request(http_data)    (ts, buf) = pcap.next()    http_data = Ethernet(buf).data.data.data    response = dpkt.http.Response(http_data)    payload = urllib.unquote(request.uri)    entity_body = response.entity_body    if "ctf.flag " in payload:        if "123456" in entity_body:            result.append({"payload": payload, "result": True})        else:            result.append({"payload": payload, "result": False})    try:        (ts, buf) = pcap.next()    except StopIteration:        breaknumber = 1flag = ""c = 0for y in result:    [(x, h)] = re.findall("1\),(\d+).+>(\d+)", y['payload'])    x, h = int(x), int(h)    if x > number:        number = x        flag += chr(c)    else:        if y['result']:            c = h + 1print flag

因为dpkt包的setfilter方法是没用的,感觉是作者还没把dpkt写完

12
def setfilter(self, value, optimize=1):    return NotImplementedError

所以我用wireshark过滤出了http包:http://7xscw6.com1.z0.glb.clouddn.com/test.pcapng

然后由于http包实体内容使用了gzip压缩,所以我魔改了http.py

12345678910111213141516
class Response(Message):    """Hypertext Transfer Protocol Response."""    __hdr_defaults__ = {        'version': '1.0',        'status': '200',        'reason': 'OK'    }    __proto = 'HTTP'    def __init__(self, *args, **kwargs):        Message.__init__(self, *args, **kwargs)        try:            entity = gzip.Gzip(self.body)            self.entity_body = gzip.Gzip.decompress(entity)        except:            self.entity_body = ""

加了一个__init__

然后,代码短,也容易看懂,不过还有需要优化的地方,不过跑这题确是没问题的

0 0
原创粉丝点击