zoukankan      html  css  js  c++  java
  • Packetbeat协议扩展开发教程(1)

    Packetbeat ( https://www.elastic.co/products/beats/packetbeat )是一个开源的网络抓包与分析框架,内置了很多常见的协议解析,如HTPP、MySQL、Thrift等。但是网络协议有很多,如何扩展一个自己的协议呢,本文将为您介绍如何在Packetbeat基础上扩展实现您自己的协议。

    开发环境:

    1.Go语言
    Packetbeat是由Go语言编写,具有高性能和易部署的特点,有关Go语言的更多信息请访问:https://golang.org/。
    2.Git
    源码管理,相信大家都比较熟悉了。
    3.Tcpdump
    *nix下的抓包分析,可选,用于调试。
    4.Mac本一台
    Windows太伤,不建议。

    这个教程给大家介绍的是编写一个SMTP协议的扩展,SMTP就是我们发邮件使用的协议,加密的比较麻烦,为了方便,本教程使用不加密的名文传输的SMTP协议,对应的端口是25。

    A.源码签出

    登陆Github打开 https://github.com/elastic/beats

    fork后得到你自己的仓库,比如我的:https://github.com/medcl/packetbeat

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. git clone git@github.com:medcl/packetbeat.git  
    2. cd packetbeat  
    3.   
    4. #添加官方仓库为upstream源  
    5. git remote add upstream https://github.com/elastic/beats  
    6.   
    7. #获取上游最新的代码,如果是刚fork的话可不用管  
    8. git pull upstream master  


    注意,如果你之前签出过packetbeat和libbeat或者topbeat等相关项目,请先从$GOPATH/src/github.com/elastic下移除,新的beats都已经合并到一个目录里面去了,如果你不移除,处理依赖的时候会有问题。

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. #签出一个名为smtpbeat的分支,用于开发这个功能  
    2. git checkout -b smtpbeat  
    3.   
    4. #获取依赖信息  
    5. (mkdir -p $GOPATH/src/golang.org/x/&&cd $GOPATH/src/golang.org/x &&git clone https://github.com/golang/tools.git )  
    6. (mkdir -p $GOPATH/src/github.com/elastic/ && cd $GOPATH/src/github.com/elastic && git clone https://github.com/elastic/beats.git )  
    7.   
    8. cd packetbeat  
    9. go get github.com/tools/godep  
    10. make  


    编译出来的文件:packetbeat就在根目录

    现在我们测试一下

    修改etc/packetbeat.yml,在output下面的elasticsearch下面添加enabled: true,默认是不启用的,另外如果你的Elasticsearch安装了Shield,比如我的Elasticsearch的用户名和密码都是tribe_user,哦,忘了说了,我们的Elasticsearch跑在本机。
    packetbeat.yml的详细配置可参见:https://www.elastic.co/guide/en/beats/packetbeat/current/packetbeat-configuration.html

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. output:  
    2.   elasticsearch:  
    3.     enabled: true  
    4.     hosts: ["localhost:9200"]  
    5.     username: "tribe_user"  
    6.     password: "tribe_user"  


    现在可以运行命令启动packetbeat了,默认会监听所有内置的协议,如HTTP、DNS等。

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. ./packetbeat -e -c etc/packetbeat.yml  -d "publish"  


    介绍一下常用的参数:
    -N dry run模式,不实际output存储日志
    -e 控制台输出调试日志
    -d 仅显示对应logger的日志
    好的,我们打开几个网页,控制台会有相应的输出,如下:

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. 2015/12/29 14:24:39.965037 preprocess.go:37: DBG  Start Preprocessing  
    2. 2015/12/29 14:24:39.965366 publish.go:98: DBG  Publish: {  
    3.   "@timestamp": "2015-12-29T14:24:39.709Z",  
    4.   "beat": {  
    5.     "hostname": "medcls-MacBook.local",  
    6.     "name": "medcls-MacBook.local"  
    7.   },  
    8.   "bytes_in": 31,  
    9.   "bytes_out": 115,  
    10.   "client_ip": "192.168.3.10",  
    11.   "client_port": 53669,  
    12.   "client_proc": "",  
    13.   "client_server": "",  
    14.   "count": 1,  
    15.   "direction": "out",  
    16.   "dns": {  
    17.     "additionals_count": 0,  
    18.     "answers": [  
    19.       {  
    20.         "class": "IN",  
    21.         "data": "www.a.shifen.com",  
    22.         "name": "sp2.baidu.com",  
    23.         "ttl": 333,  
    24.         "type": "CNAME"  
    25.       }  
    26.     ],  
    27.     "answers_count": 1,  
    28.     "authorities": [  
    29.       {  
    30.         "class": "IN",  
    31.         "data": "ns1.a.shifen.com",  
    32.         "expire": 86400,  
    33.         "minimum": 3600,  
    34.         "name": "a.shifen.com",  
    35.         "refresh": 5,  
    36.         "retry": 5,  
    37.         "rname": "baidu_dns_master.baidu.com",  
    38.         "serial": 1512240003,  
    39.         "ttl": 12,  
    40.         "type": "SOA"  
    41.       }  
    42.     ],  
    43.     "authorities_count": 1,  
    44.     "flags": {  
    45.       "authoritative": false,  
    46.       "recursion_allowed": true,  
    47.       "recursion_desired": true,  
    48.       "truncated_response": false  
    49.     },  
    50.     "id": 7435,  
    51.     "op_code": "QUERY",  
    52.     "question": {  
    53.       "class": "IN",  
    54.       "name": "sp2.baidu.com",  
    55.       "type": "AAAA"  
    56.     },  
    57.     "response_code": "NOERROR"  
    58.   },  
    59.   "ip": "192.168.3.1",  
    60.   "method": "QUERY",  
    61.   "port": 53,  
    62.   "proc": "",  
    63.   "query": "class IN, type AAAA, sp2.baidu.com",  
    64.   "resource": "sp2.baidu.com",  
    65.   "responsetime": 18,  
    66.   "server": "",  
    67.   "status": "OK",  
    68.   "transport": "udp",  
    69.   "type": "dns"  
    70. }  
    71. 2015/12/29 14:24:39.965774 preprocess.go:94: DBG  Forward preprocessed events  
    72. 2015/12/29 14:24:39.965796 async.go:42: DBG  async forward to outputers (1)  
    73. 2015/12/29 14:24:40.099973 output.go:103: DBG  output worker: publish 2 events  


    然后Elasticsearch应该就会有数据进去了,果然:

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. curl http://localhost:9200/_cat/indices?pretty=true -u tribe_user:tribe_user  
    2. yellow open packetbeat-2015.12.29  5 1   135  0 561.2kb 561.2kb  


    至此,源码的build已经成功,我们整个开发流程已经跑通了,下一节正式开始介绍SMTP协议的扩展。

  • 相关阅读:
    android内存知识普及抱怨墨迹内存大的同学看过来
    ICS 截屏(Screenshot)代码流程小结
    ICS Overlay主要流程
    Android中的sp和wp指针
    Android Display buffer_handle_t的定义
    adb shell input 命令
    百度地图API之MyLocationOverlay的使用(Android)
    由Android系统智能手机解锁图案引出的排列组合问题
    <script runat=server>、<%%>和<%#%>前台是服务器方法
    Oracle 日期运算 集合
  • 原文地址:https://www.cnblogs.com/beautiful-code/p/6625511.html
Copyright © 2011-2022 走看看