Moloch安裝與使用

0X01 moloch簡介

Moloch是一款由 AOL 開源的,能夠大規模的捕獲IPv4數據包(PCAP)、索引和數據庫系統。所以我的Capture Machines和Elasticsearch Machines都放在一臺上面, 有條件的強烈推薦把這2個組件分離開來。

根據官方介紹,需要留意一下事情:

  • 1.Moloch不支持32位
  • 2.內核4.X 有助於抓包性能提升

0x02 Moloch安裝

1.環境配置

[admin@localhost ~]$ uname -a
Linux localhost.localdomain 4.18.0-151.el8.x86_64 #1 SMP Wed Dec 4 17:04:30 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

2.Moloch下載

官網即可下載根據自己的配置環境下載對應的版本的安裝包。

3.Moloch安裝

安裝程序之前需要先安裝三個依賴包

[root@localhost admin]# yum install -y perl-libwww-perl perl-JSON libyaml-devel

cnetos8在安裝libyaml-devel時會提示找不到依賴庫,建議採用以下方法安裝。

[root@192 admin]# wget http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/libyaml-devel-0.1.7-5.el8.x86_64.rpm
[root@192 admin]# rpm -ivh libyaml-devel-0.1.7-5.el8.x86_64.rpm

依賴包安裝成功後,可以繼續安裝moloch。依賴包libyaml-devel下載地址

[root@192 admin]# rpm -ivh moloch-2.2.2-1.x86_64.rpm

4.安裝pfring

moloch的Capture默認使用libpcap後面我們可以用pfring 提升抓包性能。

[root@192 admin]#  cd /etc/yum.repos.d/
[root@192 yum.repos.d]# wget http://packages.ntop.org/centos-stable/ntop.repo -O ntop.repo• wget http://packages.ntop.org/centos-stable/epel-7.repo -O epel.repo•   yum erase zeromq3 #添加pf_ring官方的yum源,系統自帶的yum源沒有pf_ring的yum包。
[root@192 yum.repos.d]# yum clean all
[root@192 yum.repos.d]# yum update
[root@192 yum.repos.d]# yum install pfring

0x03.elasticsearch安裝配置

es可以選擇在配置moloch時候安裝 也可以自己單獨安裝 我選擇自己單獨安裝。

Es的下載地址:https://www.elastic.co/downloads/elasticsearch

這裏使用的是:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.1.rpm

[root@192 aaa]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.1.rpm
[root@192 aaa]# rpm -ivh elasticsearch-6.4.1.rpm #需要提前預置java環境

安裝完成之後,修改相關配置

[root@192 /]# cd etc/
[root@192 etc]# cd elasticsearch/

1.設置虛擬內存

[root@192 elasticsearch]# vim jvm.options
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms1g #設置一下虛擬內存,如果內存夠大就多設置,小就少設置。
-Xmx1g

2.設置存儲告警

[root@192 elasticsearch]# vim elasticsearch.yml

cluster.routing.allocation.disk.threshold_enabled: falsenetwork.host: 0.0.0.0 #抓包經常會把硬盤用完,當硬盤使用空間到80%。es就開始報警,直接把報警關掉。

#
#network.host: 0.0.0.0 #這裏建議 0.0.0.0
#
# Set a custom port for HTTP:
#
#http.port: 9200

3.系統設置修改

#第一步:修改/etc/security/limits.conf文件,增加配置,用戶退出後重新登錄生效
*                soft    nproc           65535
*                hard    nproc           65535
*                soft    nofile          65535
*                hard    nofile          65535

#第二步:因爲上方是針對所有用戶的設置,這樣會受系統限制,咱們還需要更改一下系統限制。
#修改/etc/security/limits.d/20-nproc.conf,沒有這個文件的可以直接創建
*          soft    nproc     65535
root       soft    nproc     unlimited

#第三步:還需要修改虛擬內存大小,修改/etc/sysctl.conf文件在最後面追加下面內容 會永久生效
vm.max_map_count=262144
kernel.pid_max = 65535  #這個可填可不填

#第四步:使用 如下命令 第一個查看修改後的結果,第二個可多開文件數
sysctl -p
ulimit -n 65536

#第五步:防火牆永久增加端口讓其他機器可以訪問
#添加
[root@192 /]# firewall-cmd --zone=public --add-port=9200/tcp --permanent
success
[root@192 /]# firewall-cmd --zone=public --add-port=8005/tcp --permanent
success
[root@192 /]# firewall-cmd --reload #重新載入
success

4.查看es狀態

[root@192 /]# systemctl start elasticsearch.service  #啓動ES

[root@192 /]# systemctl status elasticsearch.service #查看ES啓動狀態
● elasticsearch.service - Elasticsearch #啓動成功,前邊爲綠色
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; ven>
   Active: active (running) since Wed 2020-04-08 06:09:13 EDT; 6s ago
     Docs: http://www.elastic.co
 Main PID: 1625 (java)
    Tasks: 4
   Memory: 356.8M
   CGroup: /system.slice/elasticsearch.service
           └─1625 /bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitia>

Apr 08 06:09:13 192.168.126.155 systemd[1]: Started Elasticsearch.

最後訪問一下:http://127.0.0.1:9200,出現以下信息證明安裝沒有問題。

{
  "name" : "JskrJLn",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "CUduIznDSPiIhyF9fssK7g",
  "version" : {
    "number" : "6.4.1",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "e36acdb",
    "build_date" : "2018-09-13T22:18:07.696808Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

0x04.moloch設置

進行安裝後的設置目錄。運行./configure


[root@192 /]# cd /data/moloch/bin/
[root@192 bin]# ./Configure
Found interfaces: ens33;lo;virbr0
Semicolon ';' seperated list of interfaces to monitor [eth1] ens33 #輸入你自己對應的網卡
Install Elasticsearch server locally for demo, must have at least 3G of memory,                                                                                                                                NOT recommended for production use (yes or no) [no] no #輸入no即可
Elasticsearch server URL [http://localhost:9200] http://127.0.0.1:9200 #elasticsearch服務器地址  一般默認本機或者你指定的地址  必須以http://開頭
Password to encrypt S2S and other things [no-default] root #默認的登陸密碼,可自行設置
Moloch - Creating configuration files
Installing systemd start files, use systemctl
Moloch - Installing /etc/logrotate.d/moloch to rotate files after 7 days
Moloch - Installing /etc/security/limits.d/99-moloch.conf to make core and memlo                                                                                                                               ck unlimited
Download GEO files? (yes or no) [yes] yes #下載一些文件。直接“yes”即可

具體關於moloch設置信息詳情參考官網,也可參考https://woj.app/3872.html

1.升級初始化

/data/moloch/db/db.pl  http://localhost:9200  init    ##第一次安裝初始化、或者想刪除所有數據
/data/moloch/db/db.pl  http://localhost:9200 upgrade  ##升級moloch 數據包
#記得輸入UPGRADE
/data/moloch/bin/moloch_add_user.sh admin "Admin User" moloch --admin  ##新增admin賬戶,密碼是moloch
systemctl enable molochcapture.service ##開機啓動Capture
systemctl start molochcapture.service  ##啓動Capture
systemctl enable molochviewer.service  ##開機啓動Viewer
systemctl start molochviewer.service   ##啓動Viewer

如果不是特別講究的話,操作到這裏就可以了。

2.數據刪除

由於是虛擬機部署的,所以需要及時清理數據,否則虛擬機很快會因爲數據過大,卡頓。

# 關於pcap的數據包,可使用moloch來控制刪除
[root@moloch ~]# vim /data/moloch/etc/config.ini #不同版本的moloch,路徑也不同
# moloch 默認是freeSpaceG = 5%,也就是磁盤空間會保留5%。
freeSpaceG = 5%
# es使用moloch自帶的腳本來控制刪除
[root@moloch db]# vim daily.sh
#!/bin/sh
# This script is only needed for Moloch deployments that monitor live traffic.
# It drops the old index and optimizes yesterdays index.
# It should be run once a day during non peak time.
# CONFIG
ESHOSTPORT=10.100.10.7:9200
RETAINNUMDAYS=1
/data/moloch/db/db.pl $ESHOSTPORT expire daily $RETAINNUMDAYS
# 再做個定時任務 每天晚上跑一次
[root@moloch ~]# crontab -e
01 04 * * * /data/moloch/db/daily.sh >> /var/log/moloch/daily.log 2>&1

3.網卡優化

# Set ring buf size, see max with ethool -g eth0
ethtool -G eth0 rx 4096 tx 4096
# Turn off feature, see available features with ethtool -k eth0
ethtool -K eth0 rx off tx off gs off tso off gso off

4.High Performance Settings 高性能設置

# MOST IMPORTANT, use basic magicMode, libfile kills performance
magicMode=basic

# 官方稱pfring效果更好
# pfring/snf might be better
pcapReadMethod=tpacketv3

# Increase by 1 if still getting Input Drops
tpacketv3NumThreads=2

# Defaults
pcapWriteMethod=simple
pcapWriteSize = 2560000

# Start with 5 packet threads, increase by 1 if getting thread drops
packetThreads=5

# Set to number of packets a second
maxPacketsInQueue = 200000

5.pfring 配置

[root@moloch ~]# vim /data/moloch/etc/config.ini
rootPlugins=reader-pfring.so
pcapReadMethod=pfring

0x05.moloch使用

作爲二線分析人員,大部分情況下不需要進行實時抓包分析流量的,那麼我們只需要導入數據包進行分析即可。需要把moloch抓包功能關閉掉。

#導入離線pcap文件,如果遇到提示GEO存在問題的,建議重新初始化即可
cd /data/moloch/bin
./capture/moloch-capture –R /offline-pcap-dir <--recursive> <--copy>

./moloch-capture -r /data/moloch/raw/infile.pcap

-r #pcapfile脫機pcap文件
-R #pcapdir脫機pcap目錄,將處理所有*.pcap文件
--recursive #可以遍歷子目錄
--copy #可以將pcap文件拷貝到config.ini中指定的pcapDir


#刪除pcap文件
./moloch-capture --copy --delete /home/jx/test.pcapng #路徑
#網頁查看導入數據
file == "/home/admin/http.pcap" #導入數據路徑

0x06.總結

記錄一下本文中提到的服務啓動命令吧。

systemctl start elasticsearch.service  #es啓動命令,訪問127.0.0.1:9200即可
systemctl start molochviewer.service molochcapture.service #moloch啓動命令、訪問IP:8005即可,密碼是自己設置的。

添加用戶

/data/moloch/bin/moloch_add_user.sh admin "Admin User" moloch --admin  ##新增admin賬戶,密碼是moloch

0x07.參考推薦

Moloch快速安裝部署:https://www.cnblogs.com/yaoyaojcy/p/11727122.html

Moloch優化及安裝部署:https://woj.app/3872.html/comment-page-1

Moloch配置及漢化說明:https://woj.app/4008.html

Moloch-zh:https://github.com/xazlsec/Moloch-zh

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章