1. 程式人生 > 其它 >如何使用樹莓派自制網路監視器

如何使用樹莓派自制網路監視器

本文所介紹的工具適合家庭環境下的“黑盒測試”,它可以幫助你記錄網路中發生的所有事情。你可以用它來檢測網路威脅,或將資料提供給相關專家來進行網路取證分析。

如果你需要的是企業環境下的解決方案,你可以點選閱讀原文參考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

[ 4]   0.00-1.00   sec 8.86 MBytes  74.3 Mbits/sec    0  89.1 KBytes      

...

...

...     

[ 4]  19.00-20.00  sec 9.26 MBytes  77.7 Mbits/sec    0  1.23 MBytes      

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

[ ID] Interval           Transfer     Bandwidth       Retr

[ 4]   0.00-20.00  sec  185 MBytes  77.5 Mbits/sec  139             sender

[ 4]   0.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/