当前位置:   article > 正文

Centos7利用pyshark解析QUIC报文_scapy解析quic

scapy解析quic

一:环境准备

        利用pyshark解析报文,如果是http,https等常见的协议,正常的版本一般也能搞定,但是如果解析比较新的协议,比如我这次要解析QUIC就得使用比较新的tshark版本。

        本次实践的环境是Centos7,装的tshark版本是3.2.3,采用的是源码安装方式

1,下载安装包

http://ftp.uni-kl.de/pub/wireshark/src/all-versions/wireshark-3.2.3.tar.xz

2,安装依赖

yum install cmake3 libpcap libgcrypt-devel glib2-devel qt-devel qt5-qtbase-devel qt5-linguist qt5-qtmultimedia-devel qt5-qtsvg-devel libcap-devel libcap-ng-devel gnutls-devel krb5-devel libxml2-devel lua-devel lz4-devel snappy-devel spandsp-devel libssh2-devel bcg729-devel libmaxminddb-devel sbc-devel libsmi-devel libnl3-devel libnghttp2-devel libssh-devel libpcap-devel c-ares-devel redhat-rpm-config rpm-build gtk+-devel gtk3-devel desktop-file-utils portaudio-devel rubygem-asciidoctor docbook5-style-xsl docbook-style-xsl systemd-devel gcc gcc-c++ flex bison doxygen gettext-devel libxslt cmake

3,源码安装wireshark

  1. tar -xvf wireshark-3.2.3.tar.xz
  2. cd wireshark-3.2.3
  3. cmake3 .
  4. make -i -j 16
  5. make install

注意:安装的过程中不要把上述的几个命令粘贴复制就不管了,特别是cmake3执行的时候,会检查需要的库和依赖,在cmake3结束的时候会给提示成功或者失败

比如我就遇到不少错误,但是提示也很明显

 上面如果都顺利执行完了以后,检查一下tshark版本

  1. [root@g7j9z sbin]# tshark -v
  2. Running as user "root" and group "root". This could be dangerous.
  3. TShark (Wireshark) 3.2.3 (Git commit f39b50865a13)
  4. Copyright 1998-2020 Gerald Combs <gerald@wireshark.org> and contributors.
  5. License GPLv2+: GNU GPL version 2 or later <https://www.gnu.org/licenses/gpl-2.0.html>
  6. This is free software; see the source for copying conditions. There is NO
  7. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. Compiled (64-bit) with libpcap, with POSIX capabilities (Linux), with libnl 3,
  9. with GLib 2.56.1, with zlib 1.2.7, with SMI 0.4.8, with c-ares 1.10.0, with Lua
  10. 5.1.4, with GnuTLS 3.3.29, with Gcrypt 1.5.3, with MIT Kerberos, with MaxMind DB
  11. resolver, with nghttp2 1.33.0, without brotli, with LZ4, without Zstandard, with
  12. Snappy, with libxml2 2.9.1.
  13. Running on Linux 3.10.0-327.el7.x86_64, with Intel(R) Xeon(R) CPU E5-2630 v4 @
  14. 2.20GHz (with SSE4.2), with 322184 MB of physical memory, with locale
  15. en_US.UTF-8, with libpcap version 1.5.3, with GnuTLS 3.3.29, with Gcrypt 1.5.3,
  16. with zlib 1.2.7, binary plugins supported (0 loaded).
  17. Built using gcc 4.8.5 20150623 (Red Hat 4.8.5-44).

4,安装pyshark

  1. [root@g7j9z quicParas]# pip3 install pyshark
  2. Collecting pyshark
  3. Downloading https://mirrors.zte.com.cn/pypi/packages/e5/96/ebc5fb6cd63242c6851acdfa8a0ac14fbaec2d6c53f6d64d0a5ca06cd1af/pyshark-0.4.3-py3-none-any.whl
  4. Collecting py (from pyshark)
  5. Downloading https://mirrors.zte.com.cn/pypi/packages/67/32/6fe01cfc3d1a27c92fdbcdfc3f67856da8cbadf0dd9f2e18055202b2dc62/py-1.10.0-py2.py3-none-any.whl (97kB)
  6. 100% |████████████████████████████████| 102kB 2.8MB/s
  7. Collecting lxml (from pyshark)
  8. Downloading https://mirrors.zte.com.cn/pypi/packages/bd/78/56a7c88a57d0d14945472535d0df9fb4bbad7d34ede658ec7961635c790e/lxml-4.6.2-cp36-cp36m-manylinux1_x86_64.whl (5.5MB)
  9. 100% |████████████████████████████████| 5.5MB 3.5MB/s
  10. Installing collected packages: py, lxml, pyshark
  11. Successfully installed lxml-4.6.2 py-1.10.0 pyshark-0.4.3

二:解析QUIC

        本次解析的quic版本是Q023,其他版本的QUIC没有做尝试

        可以先看看wireshark打开的报文

 接下来就是尝试用pyshark去解包了

1,打开本地的pcap文件

cap = pyshark.FileCapture('./gquic_q023.pcap')[0]

我们这次只取报文的第一个包做实验,取索引0

2,定位到QUIC的应用层

我们可以先看看支持哪些属性和方法

  1. >>> cap = pyshark.FileCapture('./gquic_q023.pcap')[0]
  2. >>> dir(cap)
  3. ['__bool__', '__class__', '__contains__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_packet_string', 'captured_length', 'eth', 'frame_info', 'get_multiple_layers', 'get_raw_packet', 'gquic', 'highest_layer', 'interface_captured', 'ip', 'layers', 'length', 'number', 'pretty_print', 'show', 'sniff_time', 'sniff_timestamp', 'transport_layer', 'udp']
  4. >>>

里面有个highest_layer就是定位到应用层

我们可以打印一下

  1. >>> cap.highest_layer
  2. 'GQUIC'

3,获取QUIC层码流

cap[cap.highest_layer]

我们可以打印一下解析出来的QUIC字段

 比如我们想要获取QUIC的SNI字段,就可以直接调用

  1. >>> cap[cap.highest_layer].tag_sni
  2. 'www.googleapis.com'
  3. >>>

接下来看一个完整的上述分析代码及其打印

  1. import pyshark
  2. # 打开存储的捕获文件
  3. cap = pyshark.FileCapture('./gquic_q023.pcap')[0]
  4. print("*****************GQUIC LAYER PRINT*******************")
  5. print(cap[cap.highest_layer])
  6. print("*****************GQUIC LAYER PRINT*******************")
  7. print("QUIC SNI:",cap[cap.highest_layer].tag_sni)
  1. [root@g7j9z quicParas]# python3 quicParac.py
  2. *****************GQUIC LAYER PRINT*******************
  3. Layer GQUIC:
  4. Public Flags: 0x0d
  5. .... ...1 = Version: Yes
  6. .... ..0. = Reset: No
  7. .... 11.. = CID Length: 8 Bytes (0x3)
  8. ..00 .... = Packet Number Length: 1 Byte (0x0)
  9. .0.. .... = Multipath: No
  10. 0... .... = Reserved: 0x0
  11. CID: 10123107773473542882
  12. Version: Q023
  13. Packet Number: 1
  14. Message Authentication Hash: 020e7c2363fc8725a6caf935
  15. Private Flags: 0x01
  16. .... ...1 = Entropy: Yes
  17. .... ..0. = FEC Group: No
  18. .... .0.. = FEC: No
  19. 0000 0... = Reserved: 0x00
  20. STREAM (Special Frame Type) Stream ID: 1, Type: CHLO (Client Hello)
  21. Frame Type: STREAM (Special Frame Type) (0xa0)
  22. 1... .... = Stream: True
  23. .0.. .... = FIN: False
  24. ..1. .... = Data Length: 2 Bytes
  25. ...0 00.. = Offset Length: 0 Byte (0)
  26. .... ..00 = Stream Length: 1 Byte (0)
  27. Stream ID: 1 (Reserved for (G)QUIC handshake, crypto, config updates...)
  28. Data Length: 1300
  29. Tag: CHLO (Client Hello)
  30. Tag Number: 5
  31. Padding: 0000
  32. Tag/value: PAD (Padding) (l=1210)
  33. Tag Type: PAD (Padding)
  34. Tag offset end: 1210
  35. Tag length: 1210
  36. Tag/value: 2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d\xe2\x80\xa6
  37. Padding: 2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d\xe2\x80\xa6
  38. Server Name Indication: www.googleapis.com
  39. Version: Q023
  40. Common certificate sets: 0x399ff95340f7fec9
  41. Proof demand: X509
  42. Padding Length: 18
  43. Padding: 000000000000000000000000000000000000
  44. PADDING Length: 18
  45. Frame Type: PADDING (0x00)
  46. Tag/value: SNI (Server Name Indication) (l=18): www.googleapis.com
  47. Tag/value: VER (Version) (l=4): Q023
  48. Tag/value: CCS (Common Certificate Sets) (l=16)
  49. Tag/value: PDMD (Proof Demand) (l=4): X509
  50. Tag Type: SNI (Server Name Indication)
  51. Tag Type: VER (Version)
  52. Tag Type: CCS (Common Certificate Sets)
  53. Tag Type: PDMD (Proof Demand)
  54. Tag offset end: 1228
  55. Tag offset end: 1232
  56. Tag offset end: 1248
  57. Tag offset end: 1252
  58. Tag length: 18
  59. Tag length: 4
  60. Tag length: 16
  61. Tag length: 4
  62. Tag/value: 7777772e676f6f676c65617069732e636f6d
  63. Tag/value: 51303233
  64. Tag/value: 399ff95340f7fec97b26e9e7e45c71ff
  65. Tag/value: 58353039
  66. Common certificate sets: 0x7b26e9e7e45c71ff
  67. *****************GQUIC LAYER PRINT*******************
  68. QUIC SNI: www.googleapis.com

三:总结

        用过scapy的看到上面是不是有种非常熟悉的感觉,PyShark的强大在于可以调用tshark内建的所有数据包解码器,本文只是以pyshark来进行解包举例,其用途可不止如此,还可以使用pyshark在网络接口上进行嗅探。另外需要注意的是随着协议在不断的更新,由于其解码功能依赖tshark,如果需要解析比较新的协议,就需要同步升级tshark(前提是新版本支持解析的情况)。毕竟技术在发展,我们也要勤动手进行更新。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/156870?site
推荐阅读
相关标签
  

闽ICP备14008679号