网站下载 zeek
安装
最低只有18.04的,于是只能无奈改用了20.04的虚拟机
echo 'deb http://download.opensuse.org/repositories/security:/zeek/xUbuntu_20.04/ /' | sudo tee /etc/apt/sources.list.d/security:zeek.list
curl -fsSL https://download.opensuse.org/repositories/security:zeek/xUbuntu_20.04/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/security_zeek.gpg > /dev/null
sudo apt update
sudo apt install zeek-lts
安装之后还需要添加环境变量
vi ~/.bashrc
末尾添加
PATH=/opt/zeek/bin:$PATH
应用环境变量
source ~/.bashrc
更改/opt/zeek/etc目录下的node.cfg和network.cfg,使对应网卡和网段为虚拟机正在使用的网卡和网段。
输入
zeekctl
即可进入zeek;start启动,diag输出记录。
正在运行时日志记录在./logs/current里,stop后记录在隔壁文件夹下。
实验
安装好之后就可以在 在线网站上编写代码实验啦(确信)
zeek代码比较类似于C++和py(毕竟py也是用C写的嘛),大体上分为一些事件和普通函数
事件主要由各种协议和连接区分
event zeek_init()
event zeek_done()
event http_header(c:connection,is_orig:bool,name:string,value:string)
event http_reply(c: connection, version: string, code: count, reason: string)
event connection_established(c:connection)
···
···
由于zeek主要由事件驱动,所以譬如当有http连接出现时,会自动调用http事件函数进行处理。
其中zeek_init()在start前初始化,zeek_done()在stop前运行。
普通函数的调用和其它语言类似。
本次实验为检测代理,当http连接中检测到的一个ip对应多个user_agent,即可判定这个ip为proxy。
使用的数据结构为
table[addr] of set[string]
最终检测一个addr对应的set大小即可。
这种结构的初始化可以直接写成
local x: table[count] of set[string] =table()
假如有原始数据需要填入的话可以写成
local x: table[count] of set[string] = table([1]=set("one"));
驱动事件使用http_header
即可,其中user_agent的位置在connection的http成员中,引用写作c$http$user_agent
(感觉这里面的$有种指针的感觉)
具体查看各个成员位置的话可以看 zeek文档,里面每个成员的小标签都可以点开
具体代码可以参考 wtt 的博客
检测
这部分是在windows本机上完成的
写好代码后放到在线网站运行会方便一些,目前我们只需要准备一些http包。
在这个过程中出现了一点点小问题,本机上wireshark抓的包大部分都是https的,没有办法解析,给wirshark密钥什么的又比较麻烦,所以干脆使用Fiddler发了几个http包
需要多个agent时可以直接更改User-Agent
然后用wireshark抓包,追踪流发现user_agent为Fiddler,没有问题
开始检测,可以看到正常输出