阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用树莓派部署网络监视器

164次阅读
没有评论

共计 7210 个字符,预计需要花费 19 分钟才能阅读完成。

本文所介绍的工具适合家庭环境下的“黑盒测试”,它可以帮助你记录网络中发生的所有事情。你可以用它来检测网络威胁,或将数据提供给相关专家来进行网络取证分析。

使用树莓派部署网络监视器

如果你需要的是企业环境下的解决方案,你可以参考 Security Onion 的【这篇文章】。

购物清单

1.      树莓派 3(外壳 + 电源 + 电路板)

2.      闪迪 Class 10 microSD 卡 64GB(80Mb/s)

3.      Debian OS-Linux RaspbianLite

4.      网件千兆交换机或其他支持端口镜像的设备,我使用的是 D -Link1100-08P

5.      Critical Stack API(Threat Intel/ IOCs)

6.      Mailgun 账号或类似支持警报 / 通知的邮件服务

总价值:约 75 英镑

概览图

使用树莓派部署网络监视器

关键技术介绍

什么是端口镜像?

将一个端口的流量数据复制到另一个端口(被动式),会增加交换机的运行负荷。

什么是 Bro?

一款 IDS 协议分析工具,你可以把它当作 Wireshark 的协议分析器,但是它没有 GUI 界面,而且速度更快。

什么是 Netsniff?

进行数据包捕获的守护进程,它使用了 AF-packet 来提升数据包的捕捉速度。

什么是 LOKI?

基于 YARA 的文件扫描守护进程。有些类似基于签名检测的反病毒产品,但是你可以自行制定检测规则。

什么是 Critical Stack?

一个威胁情报平台,你可以在树莓派上通过 API 来与该平台链接。

什么是 Team Cymru MHR?

一个恶意软件哈希库,你可以使用该数据库中的信息来对检测到的恶意程序哈希进行匹配。

开始动手

使用树莓派部署网络监视器

1.      把系统刷到树莓派中

2.      给树莓派分配一个 IP

3.      运行 bash 脚本

4.      搞定

一、把 Raspbian 刷到 microSD 卡中

我使用的是 MacBook,所以不同平台的方法可能会有所不同,其他平台用户可以参考【这篇教程】来获取更多内容。

插入 microSD 卡:

diskutil list

找到磁盘号:

diskutil unmountDisk /dev/disk<disk#from diskutil>

将 Raspbian 镜像刷入到 microSD 卡’disk’中:

sudo dd bs=1m if=image.imgof=/dev/rdisk<disk# from diskutil>

完成之后,卸载 microSD 卡:

diskutil unmountDisk /dev/disk<disk#from diskutil>

二、配置网络

使用默认配置登录。用户名:pi,密码:raspberry。

设置 wlan0(wifi)的 IP,用于受信管理访问:

sudo nano/etc/wpa_supplicant/wpa_supplicant.conf
network={ssid="The_ESSID_from_earlier"

   psk="Your_wifi_password"
sudo ifdown wlan0

sudo ifup wlan0

ifconfig wlan0

当你获取到了一个 DHCP IP 之后,你可以使用 SSH 访问这个节点了。接下来,将 eth0 留下当作镜像接口,它不需要设置 IP 地址。

sudo apt-get update && sudo apt-get-y install vim

sudo vim /etc/network/interfaces

添加下列代码:

iface eth0 inet static

static ip_address=0.0.0.0

重启 eth0 接口:

sudo ifconfig eth0 down && sudoifconfig eth0 up

三、部署

下载并运行 bash 脚本,脚本已在 Raspbian 上成功测试。

- 安装程序的核心组件

- 配置网络选项(禁用 NIC offloading)

- 给每一个程序创建服务

- 使用 Mailgun/SSMTP 创建邮件警报

- 配置 cron 任务

pi@foxhound:~# sudo su -

root@foxhound:~# apt-get install -y git

root@foxhound:~# cd ~

root@foxhound:~# git clonehttps://github.com/sneakymonk3y/foxhound-nsm.git

root@foxhound:~# chmod +xfoxhound-nsm/foxhound.sh

root@foxhound:~# ./foxhound-nsm/foxhound.sh

现在,环境部署已经完全完成啦!

使用树莓派部署网络监视器

接下来呢?当脚本完成运行之后,所有的服务都会被立刻激活,然后你就可以看到所有流入的数据啦!

性能

通过 pcap 数据来向镜像端口 eth0 发送一些垃圾信息,我使用的是一些提前准备好的数据:

pi@foxhound:~ $ sudo tcpreplay -t -K -q--loop=10 --intf1=eth0 /opt/foxhound-1476541443.pcap

Actual: 1048570 packets (1050923190 bytes)sent in 87.62 seconds.           Rated:11994102.0 bps, 91.51 Mbps, 11967.25 pps

下面是发送前和发送后的 broctl netstats 数据:

root@foxhound:/etc/network# broctl netstats

       bro: 1476547903.768150 recvd=1951368 dropped=5408 link=1956776
root@foxhound:/etc/network# broctl netstats

       bro: 1476548144.248161 recvd=3012168 dropped=14608 link=3026776

对于家庭或实验室环境来说,它的性能相对还算很好了(1000000 个数据包只会丢弃 10000 个)。我的带宽速度为 40Mbps,对于 IDS 系统来说已经足够了。如果你还需要提升性能,建议考虑换掉树莓派。

如果你想进行更多的 NIC perf 测试,你可以在服务器上运行下列命令:

mark@Ubuntu:~$ sudo apt install iperf3

mark@ubuntu:~$ iperf3 -s

-----------------------------------------------------------

Serverlistening on 5201

然后在树莓派上运行:

root@foxhound:~# apt install iperf3

root@foxhound:~# iperf3 -c 10.0.0.7 -i 1 -t20

Connecting to host 10.0.0.7, port 5201

[4]local 10.0.0.180 port 38562 connected to 10.0.0.7 port 5201

[ID] Interval           Transfer     Bandwidth       Retr  Cwnd

[40.00-1.00   sec 8.86 MBytes  74.3 Mbits/sec    0  89.1 KBytes      

...

...

...     

[419.00-20.00  sec 9.26 MBytes  77.7 Mbits/sec    0  1.23 MBytes      

- - - - - - - - - - - - - - - - - - - - - -- - -

[ID] Interval           Transfer     Bandwidth       Retr

[40.00-20.00  sec  185 MBytes  77.5 Mbits/sec  139             sender

[40.00-20.00  sec  184 MBytes  77.1 Mbits/sec                  receiver

 

iperf Done.

我假设这里的瓶颈是 microSD 卡,它的速度大约是 80MB/s。

Bro 基础

- 所有 Bro 日志的默认存储路径为 /nsm/bro/logs/

- 默认的脚本路径为 /usr/local/bro/share/bro/site/bro-scripts/

日志目录结构大致如下所示:

pi@foxhound:/nsm/bro/logs/current $ ls-lash

total 6.9M

4.0K drwxr-xr-x 3 root root  4.0K Oct 15 16:11 .

4.0K drwxr-xr-x 5 root staff 4.0K Oct 1516:50 ..

4.0K -rw-r--r-- 1 root root   349 Oct 15 16:51 app_stats.log

4.0K -rw-r--r-- 1 root root   121 Oct 15 15:51 .cmdline

 16K-rw-r--r-- 1 root root   14K Oct 15 16:30communication.log

2.9M -rw-r--r-- 1 root root  2.9M Oct 15 16:52 conn.log

 16K-rw-r--r-- 1 root root   14K Oct 15 16:52dhcp.log

384K -rw-r--r-- 1 root root  379K Oct 15 16:52 dns.log

4.0K -rw-r--r-- 1 root root   345 Oct 15 15:51 .env_vars

1.2M -rw-r--r-- 1 root root  1.2M Oct 15 16:52 files.log

1.6M -rw-r--r-- 1 root root  1.6M Oct 15 16:52 http.log

4.0K -rw-r--r-- 1 root root   291 Oct 15 16:44 known_hosts.log

4.0K -rw-r--r-- 1 root root   327 Oct 15 16:34 known_services.log

 12K-rw-r--r-- 1 root root   11K Oct 15 16:50notice.log

4.0K -rw-r--r-- 1 root root     5 Oct 15 15:51 .pid

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.communication

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.conn

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:01 .rotated.conn-summary

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.dhcp

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.dns

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.files

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.http

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.known_hosts

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.known_services

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.loaded_scripts

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.notice

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.packet_filter

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.software

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.ssl

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.weird

4.0K -rw-r--r-- 1 root root    18 Oct 15 16:00 .rotated.x509

4.0K -rw-r--r-- 1 root root  3.0K Oct 15 16:51 software.log

320K -rw-r--r-- 1 root root  314K Oct 15 16:52 ssl.log

4.0K -rw-r--r-- 1 root root    58 Oct 15 15:51 .startup

4.0K drwx------ 3 root root  4.0K Oct 15 15:51 .state

4.0K -rwx------ 1 root root    18 Oct 15 15:51 .status

4.0K -rw-r--r-- 1 root root    46Oct 15 15:51 stderr.log

4.0K -rw-r--r-- 1 root root   188 Oct 15 15:51 stdout.log

 24K-rw-r--r-- 1 root root   17K Oct 15 16:52weird.log

416K -rw-r--r-- 1 root root  412K Oct 15 16:52 x509.log

你可以使用 head 命令来查找文本域名称:

pi@foxhound:$ head  dns.log

#separator \x09

#set_separator         ,

#empty_field   (empty)

#unset_field    -

#path        dns

#open       2016-10-15-16-00-01

#fields      ts      uid   id.orig_h  id.orig_p  id.resp_h id.resp_p         proto        trans_id   query        qclass       qclass_name         qtype        qtype_name    rcode        rcode_name    AA    TC         RD    RA    Z       answers   TTLs rejected

#types      time string        addr port addr port enum        count         string        count        string        count        string        count         string        bool bool bool bool count        vector[string]         vector[interval]        bool

使用 bro-cut 来解析日志:

pi@foxhound:$ cat dns.log | bro-cut -D tsid.orig_h id.orig_p id.resp_h id.resp_p proto query answers TTLs

基础报告:

pi@foxhound:$ bro-cut query < dns.log |sort | uniq -c | sort -rn | head -n 10

如果你还需要更多的示例,你可以查看 Bro 提供的官方练习【传送门】。

你可以使用 tail 命令输出 DNS 日志并在客户端生成一些 DNS 流量:

tail -f dns.log | awk  '{print $3, $7, $9}'

请注意,只有 conn.log 才可以启用 MaxMind GEOIP 查询。

pi@foxhound:$ bro-cut resp_cc < conn.log| sort | uniq -c | sort -rn | head -n 10

   755 US

   524 RO

   123 GB

    49 NL

     28 EU

    25 IE

    10 DE

     7 ES

     6 CA

我还没有制作仪表盘(Dashboard),如果你不想自己做的话,你可以直接使用 VPS ELK 实例在云端存储所有的日志,或者你也可以使用 ELK/Splunk/Graylog 将日志存储在本地。

基础维护

开启 / 停止 netsniff-ng:

pi@foxhound:~ $ sudo service netsniff-ngstop

pi@foxhound:~ $ sudo service netsniff-ngstart

pi@foxhound:~ $ sudo service netsniff-ngstatus

开启 / 停止 bro(网络统计):

pi@foxhound:~ $ sudo -i broctl stop

pi@foxhound:~ $ sudo -i broctl start

pi@foxhound:~ $ sudo -i broctl netstats

pi@foxhound:~ $ sudo -i broctl status

手动运行 Loki:

root@foxhound:~ $ Python /nsm/Loki/loki.py--noprocscan --dontwait --onlyrelevant -p /nsm/bro/extracted -l /nsm/Loki/log

检测 CriticalStack 的入侵威胁指标(IoCs),可通过 cron 脚本实现定期检查:

root@foxhound:~ $ sudo -u critical-stackcritical-stack-intel list

使用树莓派部署网络监视器

参考资料

1.      部署网络安全监控:http://www.appliednsm.com/

2.      网络安全监控实践:https://www.nostarch.com/nsm

3.      Laika BOSS- 对象扫描系统:https://github.com/lmco/laikaboss

4.      PassiveDNS:https://github.com/gamelinux/passivedns

5.      D3js(图形化工具):https://d3js.org/

6.      Graylog:https://www.graylog.org/

使用树莓派部署网络监视器

* 参考来源:sneakymonkey

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-11/148294.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计7210字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中