当前位置: 首页 > 工具软件 > Netzob > 使用案例 >

尝试netzob(协议逆向)(一)

庄兴发
2023-12-01
自己尝试的记录,不一定适用所有人,我是从零开始的,所有有些内容可能有些冗余?(大概)
(一)的内容包括安装netzob,使用readFile函数读取pcap包

一、安装netzob

据查阅相关博客和官网文档,好像已知的都是在Ubuntu环境下尝试的(相关博客实在太少,就找到了两个有价值的博客)如下为两个博客的地址:

(11条消息) 【Netzob学习】(一)安装netzob——ubuntu_uruz_L的博客-CSDN博客

网络协议逆向工具netzob安装过程 - Traveller_Lee - 博客园 (cnblogs.com)

在无数次试错后,如下为我成功安装的过程

(1)安装Vmware,安装Ubuntu18.04(我的版本)

这一部分教程很多,此处不再赘述

(2)安装python环境

在Ubuntu18.04中虽然自带python环境,但版本对于netzob版本较低,无法使用,需要安装版本更高的python环境。对于安装python环境,我参考的是这篇博客:

(11条消息) 【记录】环境|Ubuntu18.04 中搭建 Python 开发和调试环境的完整记录_ubuntu python开发环境_shandianchengzi的博客-CSDN博客

使用的是其中的pyenv安装。之前我也尝试过其他的方法,到后来安装时总会出现各种其他的bug,安装完后接着切换本地python版本,仍然是以上博客的推荐方式,具体参见上述博客

pyenv local 3.9.12

安装过程可能也会出现一些小问题,我基本都百度解决了。

(3)安装pycharm

在控制台写代码太难受,所以我安装了个pycharm,直接在Ubuntu下载即可,参考的博客。

(11条消息) Ubuntu 18.04 安装 PyCharm_ubuntu安装pycharm_梦dancing的博客-CSDN博客

激活pycharm我是用的学生账号,这一步就八仙过海吧hh。

pycharm激活成功后,为了以防关闭后找不到,我创建了快捷方式,在工具栏tools就可以创建。不用快捷方式也可用指令启动pycharm,什么.sh什么的。

然后是修改pycharm的interpreter,我在按照安装python环境的那个博客安装完后,我的python3.8.5的地址是/home/crossacid/.pyenv/shims/python3.8,可用用whereis python找到你们的新python位置。

然后就可以随便创建一个python程序,helloworld一下,尝试下自己的环境是否配置完成。

(4)安装netzob

这一步我自己是首先按照官方文档安装了一下,但是在pycharm里import不了。如下为官网文档地址:

Netzob documentation — Netzob Documentation

在控制台完成以上步骤的时候也会出现挺多问题,但也还可以解决(之前自己瞎装python环境,在这一步倒下了,按那个博客后,问题就少了,勉强结局了)。

我在pycharm引入失败后,直接让pycharm给我下载了,万幸也没有出现问题,至此netzob安装完成。

二、netzob初尝试

(1)准备工作

在上述的netzob文档中没有对未知协议逆向的内容,但在老版本的api文档中存在示例,网址如下:

发现Netzob的功能 — Netzob文档

示例中pcap文件在文档中的地址失效了,大可以在netzob的GitHub中找到,地址如下

GitHub - netzob/netzob: Netzob: Protocol Reverse Engineering, Modeling and Fuzzing

pcap文件的位置是netzob/test/resources/pcaps,其中的target_src_v1_session1.pcap等文件。

(2)从pcap包中读取信息

pcap包的相关概念,也是自行查阅的,相关代码也可以在netzob的文档里看到,如下所示:

from netzob.all import *

messages_session1 = PCAPImporter.readFile("target_src_v1_session1.pcap").values()
messages_session2 = PCAPImporter.readFile("target_src_v1_session2.pcap").values()

messages = messages_session1 + messages_session2

for message in messages:
    print(message)

readFile函数可以选择采用更多参数来指定 BPF 过滤器、导入层或要捕获的数据包数,此处参数为空。如下为readFile函数的介绍。它可以读取标准的pcap文件,第一个参数是文件地址,第二个是抓包过滤器,可以剔除pcap包中不想要的流量,相关语法类似于sql,第三个参数是层数,可以选择链路层、网络层、传输层、应用层等,第四个参数是包的数目,第五个参数是将同一来源和目的地的连续数据包合并。

@staticmethod
    @typeCheck(str, str, int, int, bool)
    def readFile(filePath, bpfFilter="", importLayer=5, nbPackets=0, mergePacketsInFlow=False):
        """Read all messages from the specified PCAP file. A BPF filter
        can be set to limit the captured packets. The layer of import
        can also be specified:
          - When layer={1, 2}, it means we want to capture a raw layer (such as Ethernet).
          - If layer=3, we capture at the network level (such as IP).
          - If layer=4, we capture at the transport layer (such as TCP or UDP).
          - If layer=5, we capture at the applicative layer (such as the TCP or UDP payload) and merge consecutive messages with same source and destination.
         Finally, the number of packets to capture can be specified.

        :param filePath: the pcap path
        :type filePath: :class:`str`
        :param bpfFilter: a string representing a BPF filter.
        :type bpfFilter: :class:`str`
        :param importLayer: an integer representing the protocol layer to start importing.
        :type importLayer: :class:`int`
        :param nbPackets: the number of packets to import
        :type nbPackets: :class:`int`
        :param mergePacketsInFlow: if True, consecutive packets with same source and destination ar merged (i.e. to mimic a flow) 
        :type mergePacketsInFlow: :class:`bool`
        :return: a list of captured messages
        :rtype: a list of :class:`AbstractMessage <netzob.Model.Vocabulary.Messages.AbstractMessage>`
        """

        importer = PCAPImporter()
        return importer.readFiles([filePath], bpfFilter, importLayer,
                                  nbPackets, mergePacketsInFlow)

以上例子的read结果如下所示:

[1388154953.32 127.0.0.1:57831->127.0.0.1:4242] 'CMDidentify#\x07\x00\x00\x00Roberto'
[1388154953.32 127.0.0.1:4242->127.0.0.1:57831] 'RESidentify#\x00\x00\x00\x00\x00\x00\x00\x00'
[1388154953.32 127.0.0.1:57831->127.0.0.1:4242] 'CMDinfo#\x00\x00\x00\x00'
[1388154953.32 127.0.0.1:4242->127.0.0.1:57831] 'RESinfo#\x00\x00\x00\x00\x04\x00\x00\x00info'
[1388154953.32 127.0.0.1:57831->127.0.0.1:4242] 'CMDstats#\x00\x00\x00\x00'
[1388154953.32 127.0.0.1:4242->127.0.0.1:57831] 'RESstats#\x00\x00\x00\x00\x05\x00\x00\x00stats'
[1388154953.32 127.0.0.1:57831->127.0.0.1:4242] 'CMDauthentify#\n\x00\x00\x00aStrongPwd'
[1388154953.32 127.0.0.1:4242->127.0.0.1:57831] 'RESauthentify#\x00\x00\x00\x00\x00\x00\x00\x00'
[1388154953.32 127.0.0.1:57831->127.0.0.1:4242] 'CMDencrypt#\x06\x00\x00\x00abcdef'
[1388154953.32 127.0.0.1:4242->127.0.0.1:57831] "RESencrypt#\x00\x00\x00\x00\x06\x00\x00\x00$ !&'$"
[1388154953.32 127.0.0.1:57831->127.0.0.1:4242] "CMDdecrypt#\x06\x00\x00\x00$ !&'$"
[1388154953.32 127.0.0.1:4242->127.0.0.1:57831] 'RESdecrypt#\x00\x00\x00\x00\x06\x00\x00\x00abcdef'
[1388154953.33 127.0.0.1:57831->127.0.0.1:4242] 'CMDbye#\x00\x00\x00\x00'
[1388154953.33 127.0.0.1:4242->127.0.0.1:57831] 'RESbye#\x00\x00\x00\x00\x00\x00\x00\x00'
[1388154953.31 127.0.0.1:57831->127.0.0.1:4242] 'CMDidentify#\x04\x00\x00\x00fred'
[1388154953.31 127.0.0.1:4242->127.0.0.1:57831] 'RESidentify#\x00\x00\x00\x00\x00\x00\x00\x00'
[1388154953.31 127.0.0.1:57831->127.0.0.1:4242] 'CMDinfo#\x00\x00\x00\x00'
(...)
 类似资料: