Contents
Suricata インストール
SURICATA IDS/IPSはネットワーク上の通信を監視し、不審なトラフィックを検知するオープンソースのIDSです。基本的な仕組みはシグネチャ型であるため、あらかじめ設定した不正な通信を検知できます。また、Suricataは検知だけでなく防御も行えることが特徴です。
1.Suricata のインストールと設定
①Suricata のインストール
| 1 | #  apt install suricata | 
バージョンの確認
| 1 2 | # suricata -V This is Suricata version 6.0.10 RELEASE | 
サービスに登録し有効化する
| 1 2 3 | #  systemctl enable suricata.service Synchronizing state of suricata.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable suricata | 
②Suricataがネットワークパケットを検査するインターフェースとIPアドレスを決定
| 1 2 3 | # ip --brief add lo               UNKNOWN        127.0.0.1/8 ::1/128 ens33            UP             192.168.11.83/24 fe80::20c:29ff:fef9:86e2/64 | 
③設定ファイルを編集
| 1 2 3 4 5 6 7 8 9 | #  vi /etc/suricata/suricata.yaml # 15行目 : varsセクションで、ネットワークを定義する HOME_NET: "[192.168.11.0/24]" EXTRNAL_NET: "!$HOME_NET" # 589行目 : af-packetセクションのインターフェース名を設定 af-packet:     - interface: ens33 | 
| 1 2 3 4 | # vi  /etc/default/suricata 16行目 # Interface to listen on (for pcap mode) IFACE=ens33 | 
④Suricataのルール更新
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # suricata-update -o /etc/suricata/rules 19/6/2023 -- 16:52:51 - <Info> -- Using data-directory /var/lib/suricata. 19/6/2023 -- 16:52:51 - <Info> -- Using Suricata configuration /etc/suricata/suricata.yaml 19/6/2023 -- 16:52:51 - <Info> -- Using /etc/suricata/rules for Suricata provided rules. 19/6/2023 -- 16:52:51 - <Info> -- Found Suricata version 6.0.10 at /usr/bin/suricata. 19/6/2023 -- 16:52:51 - <Info> -- Loading /etc/suricata/suricata.yaml 19/6/2023 -- 16:52:51 - <Info> -- Disabling rules for protocol http2 19/6/2023 -- 16:52:51 - <Info> -- Disabling rules for protocol modbus 19/6/2023 -- 16:52:51 - <Info> -- Disabling rules for protocol dnp3 19/6/2023 -- 16:52:51 - <Info> -- Disabling rules for protocol enip 19/6/2023 -- 16:52:51 - <Info> -- No sources configured, will use Emerging Threats Open 19/6/2023 -- 16:52:51 - <Info> -- Fetching https://rules.emergingthreats.net/open/suricata-6.0.10/emerging.rules.tar.gz.  100% - 3937754/3937754 19/6/2023 -- 16:52:53 - <Info> -- Done. 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/app-layer-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/decoder-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/dhcp-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/dnp3-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/dns-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/files.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/http-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/ipsec-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/kerberos-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/modbus-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/nfs-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/ntp-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/smb-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/smtp-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/stream-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Loading distribution rule file /etc/suricata/rules/tls-events.rules 19/6/2023 -- 16:52:53 - <Info> -- Ignoring file rules/emerging-deleted.rules 19/6/2023 -- 16:52:55 - <Info> -- Loaded 43002 rules. 19/6/2023 -- 16:52:55 - <Info> -- Disabled 14 rules. 19/6/2023 -- 16:52:55 - <Info> -- Enabled 0 rules. 19/6/2023 -- 16:52:55 - <Info> -- Modified 0 rules. 19/6/2023 -- 16:52:55 - <Info> -- Dropped 0 rules. 19/6/2023 -- 16:52:55 - <Info> -- Enabled 131 rules for flowbit dependencies. 19/6/2023 -- 16:52:55 - <Info> -- Backing up current rules. 19/6/2023 -- 16:52:55 - <Info> -- Writing rules to /var/lib/suricata/rules/suricata.rules: total: 43002; enabled: 34210; added: 43002; removed 0; modified: 0 19/6/2023 -- 16:52:56 - <Info> -- Writing /var/lib/suricata/rules/classification.config 19/6/2023 -- 16:52:56 - <Info> -- Testing with suricata -T. 19/6/2023 -- 16:53:17 - <Info> -- Done | 
⑤Suricataの起動確認
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # systemctl status suricata ● suricata.service - Suricata IDS/IDP daemon      Loaded: loaded (/lib/systemd/system/suricata.service; enabled; preset: ena>      Active: active (running) since Mon 2023-06-19 16:54:35 JST; 42s ago        Docs: man:suricata(8)              man:suricatasc(8)              https://suricata-ids.org/docs/     Process: 1941 ExecStart=/usr/bin/suricata -D --af-packet -c /etc/suricata/s>    Main PID: 1942 (Suricata-Main)       Tasks: 10 (limit: 2265)      Memory: 70.8M         CPU: 437ms      CGroup: /system.slice/suricata.service              mq1942 /usr/bin/suricata -D --af-packet -c /etc/suricata/suricata.> Jun 19 16:54:34 Lepard systemd[1]: Starting suricata.service - Suricata IDS/IDP> Jun 19 16:54:35 Lepard suricata[1941]: 19/6/2023 -- 16:54:35 - <Notice> - This > Jun 19 16:54:35 Lepard systemd[1]: Started suricata.service - Suricata IDS/IDP | 
ログを確認
| 1 2 3 4 5 6 7 8 9 10 11 | # tail /var/log/suricata/suricata.log 19/6/2023 -- 16:54:35 - <Info> - eve-log output device (regular) initialized: eve.json 19/6/2023 -- 16:54:35 - <Info> - stats output device (regular) initialized: stats.log 19/6/2023 -- 16:54:35 - <Warning> - [ERRCODE: SC_ERR_NO_RULES(42)] - No rule files match the pattern /etc/suricata/rules/suricata.rules 19/6/2023 -- 16:54:35 - <Warning> - [ERRCODE: SC_ERR_NO_RULES_LOADED(43)] - 1 rule files specified, but no rules were loaded! 19/6/2023 -- 16:54:35 - <Info> - Threshold config parsed: 0 rule(s) found 19/6/2023 -- 16:54:35 - <Info> - 0 signatures processed. 0 are IP-only rules, 0 are inspecting packet payload, 0 inspect application layer, 0 are decoder event only 19/6/2023 -- 16:54:35 - <Info> - Going to use 4 thread(s) 19/6/2023 -- 16:54:35 - <Info> - Using unix socket file '/var/run/suricata-command.socket' 19/6/2023 -- 16:54:35 - <Notice> - all 4 packet processing threads, 4 management threads initialized, engine started. 19/6/2023 -- 16:54:35 - <Info> - All AFP capture threads are running. | 
統計情報を確認するには、stats.log ファイルを確認します(デフォルトで8秒ごとに更新)
| 1 | # tail -f /var/log/suricata/stats.log | 
より高度な出力であるEVE JSONは、以下のコマンドで生成することができる
| 1 | # tail -f /var/log/suricata/eve.json | 
2.Suricata のテスト
①curl ユーティリティで ping テストを実行
| 1 2 | # curl http://testmynids.org/uid/index.html uid=0(root) gid=0(root) groups=0(root) | 
②ログに記録されたかどうかを調べるため、アラートログを確認
| 1 2 3 4 | # cat /var/log/suricata/fast.log 06/19/2023-21:30:28.268725  [**] [1:2027863:5] ET INFO Observed DNS Query to .biz TLD [**] [Classification: Potentially Bad Traffic] [Priority: 2] {UDP} 192.168.11.22:50307 -> 192.168.11.1:53 06/19/2023-21:35:03.490358  [**] [1:2013028:7] ET POLICY curl User-Agent Outbound [**] [Classification: Attempted Information Leak] [Priority: 2] {TCP} 192.168.11.83:41924 -> 18.65.159.60:80 06/19/2023-21:35:03.502956  [**] [1:2100498:7] GPL ATTACK_RESPONSE id check returned root [**] [Classification: Potentially Bad Traffic] [Priority: 2] {TCP} 18.65.159.60:80 -> 192.168.11.83:41924 | 
3.Suricata Rulesの設定
①Suricataにパッケージされているルールセットの表示
| 1 2 3 4 5 6 | # ls -al /var/lib/suricata/rules/ total 24816 drwxr-xr-x 2 root root     4096 Jun 19 16:52 . drwxr-xr-x 4 root root     4096 Jun 19 16:52 .. -rw-r--r-- 1 root root     3228 Jun 19 16:52 classification.config -rw-r--r-- 1 root root 25397078 Jun 19 16:52 suricata.rules | 
②ルールセットを提供するソースのインデックス一覧
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # suricata-update list-sources Name: et/open   Vendor: Proofpoint   Summary: Emerging Threats Open Ruleset   License: MIT Name: et/pro   Vendor: Proofpoint   Summary: Emerging Threats Pro Ruleset   License: Commercial   Replaces: et/open   Parameters: secret-code   Subscription: https://www.proofpoint.com/us/threat-insight/et-pro-ruleset Name: oisf/trafficid   Vendor: OISF   Summary: Suricata Traffic ID ruleset   License: MIT Name: scwx/enhanced   Vendor: Secureworks   Summary: Secureworks suricata-enhanced ruleset   License: Commercial   Parameters: secret-code   Subscription: https://www.secureworks.com/contact/ (Please reference CTU Countermeasures) Name: scwx/malware   Vendor: Secureworks   Summary: Secureworks suricata-malware ruleset   License: Commercial   Parameters: secret-code   Subscription: https://www.secureworks.com/contact/ (Please reference CTU Countermeasures) Name: scwx/security   Vendor: Secureworks   Summary: Secureworks suricata-security ruleset   License: Commercial   Parameters: secret-code   Subscription: https://www.secureworks.com/contact/ (Please reference CTU Countermeasures) Name: sslbl/ssl-fp-blacklist   Vendor: Abuse.ch   Summary: Abuse.ch SSL Blacklist   License: Non-Commercial Name: sslbl/ja3-fingerprints   Vendor: Abuse.ch   Summary: Abuse.ch Suricata JA3 Fingerprint Ruleset   License: Non-Commercial Name: etnetera/aggressive   Vendor: Etnetera a.s.   Summary: Etnetera aggressive IP blacklist   License: MIT Name: tgreen/hunting   Vendor: tgreen   Summary: Threat hunting rules   License: GPLv3 Name: malsilo/win-malware   Vendor: malsilo   Summary: Commodity malware rules   License: MIT Name: stamus/lateral   Vendor: Stamus Networks   Summary: Lateral movement rules   License: GPL-3.0-only | 
③ソースを有効にする(et/openを有効にする場合)
| 1 2 3 4 5 6 7 | # suricata-update enable-source et/open 19/6/2023 -- 17:45:10 - <Info> -- Using data-directory /var/lib/suricata. 19/6/2023 -- 17:45:10 - <Info> -- Using Suricata configuration /etc/suricata/suricata.yaml 19/6/2023 -- 17:45:10 - <Info> -- Using /etc/suricata/rules for Suricata provided rules. 19/6/2023 -- 17:45:10 - <Info> -- Found Suricata version 6.0.10 at /usr/bin/suricata. 19/6/2023 -- 17:45:10 - <Info> -- Creating directory /var/lib/suricata/update/sources 19/6/2023 -- 17:45:10 - <Info> -- Source et/open enabled | 
アップデートを実行
| 1 | # suricata-update -o /etc/suricata/rules | 
Suricata service再起動
| 1 | # systemctl restart suricata | 
4.Suricata Custom Rulesの作成
①カスタマールールを含むファイルを作成
| 1 2 3 | # vi /etc/suricata/rules/local.rules 下記内容を記載 alert icmp any any -> $HOME_NET any (msg:"ICMP Ping"; sid:1; rev:1;) | 
②設定ファイルを編集(新しいルールのパスを定義)
| 1 2 3 4 5 6 7 8 | # vi /etc/suricata/suricata.yaml # 1924行目あたりに追記 default-rule-path: /var/lib/suricata/rules rule-files:   - suricata.rules   - /etc/suricata/rules/local.rules | 
③設定ファイルのテスト
| 1 2 3 4 5 6 7 8 9 10 11 12 | # suricata -T -c /etc/suricata/suricata.yaml -v 19/6/2023 -- 19:03:41 - <Info> - Running suricata under test mode 19/6/2023 -- 19:03:41 - <Notice> - This is Suricata version 6.0.10 RELEASE running in SYSTEM mode 19/6/2023 -- 19:03:41 - <Info> - CPUs/cores online: 4 19/6/2023 -- 19:03:41 - <Info> - fast output device (regular) initialized: fast.log 19/6/2023 -- 19:03:41 - <Info> - eve-log output device (regular) initialized: eve.json 19/6/2023 -- 19:03:41 - <Info> - stats output device (regular) initialized: stats.log 19/6/2023 -- 19:03:52 - <Info> - 2 rule files processed. 34211 rules successfully loaded, 0 rules failed 19/6/2023 -- 19:03:52 - <Info> - Threshold config parsed: 0 rule(s) found 19/6/2023 -- 19:03:53 - <Info> - 34214 signatures processed. 1281 are IP-only rules, 5222 are inspecting packet payload, 27501 inspect application layer, 108 are decoder event only 19/6/2023 -- 19:04:00 - <Notice> - Configuration provided was successfully loaded. Exiting. 19/6/2023 -- 19:04:00 - <Info> - cleaning up signature grouping structure... complete | 
Suricat service再起動
| 1 | # systemctl restart suricata | 
④Custom Rulesの適用テスト
同一ローカルネットワーク上の別のデバイスでpingを実行し、ログに記録されたかどうかを確認する
| 1 2 3 | # cat /var/log/suricata/fast.log 06/19/2023-19:05:36.647601  [**] [1:1:1] ICMP Ping [**] [Classification: (null)] [Priority: 3] {ICMP} 192.168.11.22:8 -> 192.168.11.83:0 06/19/2023-19:05:36.647697  [**] [1:1:1] ICMP Ping [**] [Classification: (null)] [Priority: 3] {ICMP} 192.168.11.83:0 -> 192.168.11.22:0 | 
JSON形式のログを取得するには、システムにjqをインストールする
| 1 | # apt install jq | 
下記コマンドを実行し、同一ローカルネットワーク上の別のデバイスでpingを実行する
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # tail -f /var/log/suricata/eve.json | jq 'select(.event_type=="alert")' pingを実行するとコンソールに下記のように表示される {   "timestamp": "2023-06-19T19:13:06.550164+0900",   "flow_id": 1715006919042324,   "in_iface": "ens33",   "event_type": "alert",   "src_ip": "167.94.145.93",   "src_port": 51960,   "dest_ip": "192.168.11.83",   "dest_port": 25,   "proto": "TCP",   "metadata": {     "flowbits": [       "ET.Evil",       "ET.DshieldIP"     ]   },   "community_id": "1:z7XtW6Fg2zOPSQ1yy76yjw6Zijs=",   "alert": {     "action": "allowed",     "gid": 1,     "signature_id": 2402000,     "rev": 6673,     "signature": "ET DROP Dshield Block Listed Source group 1",     "category": "Misc Attack",     "severity": 2,     "metadata": {       "affected_product": [         "Any"       ],       "attack_target": [         "Any"       ],       "created_at": [         "2010_12_30"       ],       "deployment": [         "Perimeter"       ],       "signature_severity": [         "Major"       ],       "tag": [         "Dshield"       ],       "updated_at": [         "2023_06_16"       ]     }   },   "flow": {     "pkts_toserver": 1,     "pkts_toclient": 0,     "bytes_toserver": 60,     "bytes_toclient": 0,     "start": "2023-06-19T19:13:06.550164+0900"   } } | 
Logwatch
①インストール
| 1 | # apt -y install logwatch | 
②デフォルトの設定ファイルをコピー
| 1 | # cp /usr/share/logwatch/default.conf/logwatch.conf /etc/logwatch/conf/ | 
➂メールの送信先等を変更
| 1 2 3 4 5 6 7 8 9 | # vi /etc/logwatch/conf/logwatch.conf ●52 行目あたり 「MailTo = root」の行頭に「#」を挿入し、その下行に通知を受け取りたいメールアドレスを設定 #MailTo = root MailTo = [メールアドレス] ●85行目あたりログ通知の詳細度を設定 #Detail = Low Detail = High | 
④ディレクトリの作成
| 1 | # mkdir /var/cache/logwatch | 
⑤動作確認
logwatch をインストールするとデフォルトで cron 登録されるので、毎日レポートメールが届く
設定したアドレスにレポートが届くかテストを行います。
| 1 | # /etc/cron.daily/00logwatch | 
Chkrootkit
①chkrootkit をダウンロード、インストール
| 1 | # apt -y install chkrootkit | 
➁chkrootkit を確認
| 1 2 | # chkrootkit | grep INFECTED 何も表示されなければ問題ありません | 
④chkrootkit 定期実行スクリプトの作成と権限変更
自動で /usr/sbin/chkrootkit-dailyをもとに/etc/cron.daily/chkrtootkit が作成され、毎日自動で実行されるので、スクリプトの作成は不要。
これで、rootkit が仕掛けられていた場合に root 宛にメールが届くようになる。
ディスク使用率チェックスクリプト
1. スクリプト作成
| 1 2 | # cd /opt/script/ # vi disk_capacity_check.sh | 
disk_capacity_check.shの内容
| 1 2 3 4 5 6 7 8 9 10 | #!/bin/bash #通知先メールアドレス指定 MAIL="<your mailaddress> " DVAL=`/bin/df / | /usr/bin/tail -1 | /bin/sed 's/^.* \([0-9]*\)%.*$/\1/'` if [ $DVAL -gt 80 ]; then echo "Disk usage alert: $DVAL %" | mail -s "Disk Space Alert in `hostname`" $MAIL fi | 
| 1 | # chmod 700 disk_capacity_check.sh | 
2. 実行確認
①現在の使用率を確認
| 1 | # df -h | 
次のように表示される
| 1 2 3 4 5 6 7 | Filesystem      Size  Used Avail Use% Mounted on udev            1.5G     0  1.5G   0% /dev tmpfs           294M  776K  294M   1% /run /dev/sda1        19G  1.7G   16G  10% / tmpfs           1.5G     0  1.5G   0% /dev/shm tmpfs           5.0M     0  5.0M   0% /run/lock tmpfs           294M     0  294M   0% /run/user/1000 | 
②使用率80%以上になるようダミーファイルを作成(例ではdummyfile という名前で15G程度)
| 1 | # dd if=/dev/zero of=dummyfile bs=1M count=15000 | 
③再度確認
| 1 | # df -h | 
を実行して80%以上になっていることを確認
④ディスク容量チェックスクリプトを実行
| 1 | # /opt/script/disk_capacity_check.sh | 
設定したメールアドレスに本文の内容として「Disk usage alert : 93%」のように記載のメールが届きます
⑤作成した「dummyfile」を削除
| 1 | # rm dummyfile | 
⑥定期実行設定
| 1 2 | # crontab -e 30 2 * * * /opt/script/disk_capacity_check.sh | 

