0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

この記事について

Suricataを使いflow logを保存するshellscriptの自分用テンプレです。あまり役に立つことが無いよう願いながら使っています。

今回はこのコードでflow logを観察しました。

Suricata

「RaspberryPi 5BにSuricata7.0.3をインストール」で紹介した方法で実装しています。

使い道様々でしょうが、ラズパイ4BでSuricataを使う場合は4GBRAM以上の機種をお勧めします。

csv出力

flow_idを含む行だけ抽出します。

suricata_csv.sh
#!/bin/bash

# 最後に処理したタイムスタンプを保存するファイル
last_timestamp_file="/tmp/last_suricata_timestamp"

# CSVファイルのパス
datetime=$(date '+%Y%m%d-%H%M%S')
CSV_FILE="Suricata_logs_${datetime}.csv"

# ファイルが存在しない場合は作成
if [ ! -f "$last_timestamp_file" ]; then
    echo "1970-01-01T00:00:00.000000+0000" > "$last_timestamp_file"
fi

# CSVファイルにヘッダーを書き込む(必要なら)
echo "timestamp,flow_log" > $CSV_FILE

# /var/log/suricata/eve.json の変更を監視
inotifywait -m -e modify /var/log/suricata/eve.json |
while read path action file; do
    # "/var/log/suricata/eve.json" の最後の行を取得
    last_line=$(tail -n 1 /var/log/suricata/eve.json)
    last_line_timestamp=$(echo "$last_line" | jq -r '.timestamp')
  
    # 最後に処理したタイムスタンプを読み込む
    last_processed_timestamp=$(cat "$last_timestamp_file")
  
    # タイムスタンプが異なる場合にのみ処理
    if [ "$last_line_timestamp" != "$last_processed_timestamp" ]; then
        if echo "$last_line" | grep -q "flow_id"; then
            # CSV行を構築
            csv_row="$last_line_timestamp,\"$(echo "$last_line" | jq -c '.')\""

            # 重複行があれば除外
            if ! grep -Fxq "$csv_row" "$CSV_FILE"; then
                echo "$csv_row" >> "$CSV_FILE"
            fi

            # 最後に処理したタイムスタンプを更新
            echo "$last_line_timestamp" > "$last_timestamp_file"
        fi
    fi
    sleep 0.2
done

MQTTSのログが確認できました。

json出力

jsonでも出力させます。

#!/bin/bash

THING_NAME=$(uname -n)
# MQTTのパラメータ
MQTT_HOST="127.0.0.1"
MQTT_TOPIC=${THING_NAME}_mos/${THING_NAME}_logs/suricata
echo $MQTT_TOPIC

# 最後に処理したタイムスタンプを保存するファイル
last_timestamp_file="/tmp/last_suricata_timestamp"

# ファイルが存在しない場合は作成
if [ ! -f "$last_timestamp_file" ]; then
    echo "1970-01-01T00:00:00.000000+0000" > "$last_timestamp_file"
fi

# /var/log/suricata/eve.json の変更を監視
inotifywait -m -e modify /var/log/suricata/eve.json |
while read path action file; do
    # "/var/log/suricata/eve.json" の最後の行を取得
    last_line=$(tail -n 1 /var/log/suricata/eve.json)
    last_line_timestamp=$(echo "$last_line" | jq -r '.timestamp')
  
    # 最後に処理したタイムスタンプを読み込む
    last_processed_timestamp=$(cat "$last_timestamp_file")
  
    # タイムスタンプが異なる場合にのみ処理
    if [ "$last_line_timestamp" != "$last_processed_timestamp" ]; then
        if echo "$last_line" | grep -q "flow_id"; then
            # JSON 形式でログを発行
            formatted_json=$(echo "$last_line" | jq --arg datetime "$(date "+%F %T")" '. | {datetime: $datetime, flow_log: .}')
            "${THING_NAME}_mos/${THING_NAME}_logs/suricata" -m "$formatted_json"
            # 発行したログを表示
            echo "$formatted_json"
            mosquitto_pub -h "$MQTT_HOST" -t "$MQTT_TOPIC" -m "$formatted_json"
            # 最後に処理したタイムスタンプを更新
            echo "$last_line_timestamp" > "$last_timestamp_file"
        fi
    fi
    sleep 0.5
done

DEMO的にlocalhostでpubsubさせました。きれいに出力できています。

pub
.
.
{
  "datetime": "2024-07-10 21:51:39",
  "flow_log": {
    "timestamp": "2024-07-10T21:51:39.388678+0900",
    "flow_id": 898199686114866,
    "in_iface": "wlan0",
    "event_type": "tls",
    "src_ip": "192.168.88.88",
    "src_port": 56307,
    "dest_ip": "52.88.88.88",
    "dest_port": 8883,
    "proto": "TCP",
    "pkt_src": "wire/pcap",
    "tls": {
      "subject": "CN=*.iot.ap-northeast-1.amazonaws.com",
      "issuerdn": "C=US, O=Amazon, CN=Amazon RSA 2048 M01",
      "serial": "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx,
      "fingerprint": "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",
      "sni": "zzzzzzzzzzzzzzzzz-ats.iot.ap-northeast-1.amazonaws.com",
      "version": "TLS 1.2",
      "notbefore": "2023-12-14T00:00:00",
      "notafter": "2024-11-21T23:59:59"
    }
  }
}
.
.
sub
.
.
VNCcamServer1_mos/VNCcamServer1_logs/suricata {
  "datetime": "2024-07-10 21:51:39",
  "flow_log": {
    "timestamp": "2024-07-10T21:51:39.388678+0900",
    "flow_id": 898199686114866,
    "in_iface": "wlan0",
    "event_type": "tls",
    "src_ip": "192.168.88.88",
    "src_port": 56307,
    "dest_ip": "52.88.88.88",
    "dest_port": 8883,
    "proto": "TCP",
    "pkt_src": "wire/pcap",
    "tls": {
      "subject": "CN=*.iot.ap-northeast-1.amazonaws.com",
      "issuerdn": "C=US, O=Amazon, CN=Amazon RSA 2048 M01",
      "serial": "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx,
      "fingerprint": "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",
      "sni": "zzzzzzzzzzzzzzzzz-ats.iot.ap-northeast-1.amazonaws.com",
      "version": "TLS 1.2",
      "notbefore": "2023-12-14T00:00:00",
      "notafter": "2024-11-21T23:59:59"
    }
  }
}  
.
.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?