GithubHelp home page GithubHelp logo

l3ickey / lan_traffic Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 69 KB

Raspberry Piを使ってLAN内のトラフィックを監視する.

Python 34.20% Shell 0.33% PHP 65.47%
raspberry-pi network-analysis

lan_traffic's Introduction

lan_traffic

Raspberry Piを使ってLAN内のトラフィックを監視する.

必要なもの

  • ポートミラーリング機能のあるスイッチングハブ
  • Raspberry Pi
  • Raspberry Piでキャプチャした.pcapファイルを保存する外付けHDD

ネットワーク図

lan_network

ここで図中の青実線は有線LAN,青破線は無線LANを示す. スイッチングハブを経由して通信している端末のトラフィックを,スイッチングハブのポートミラーリング機能でRaspberry Piへミーラーリングする. Wi-Fiルータは二重ルータを防ぐためにアクセスポイントモードにする必要がある.

使い方

Wi-Fiルータをアクセスポイントモードで接続

モデムに接続したルータはルータモードに設定し,DHCPも有効にする. スイッチングハブに接続したWi-Fiルータはアクセスポイントモード(またはブリッジモード)に設定し,DHCPは無効にする.

スイッチングハブにポートミラーを設定

Raspberry Piと接続するポートをミラーリングポートとして設定する. Wi-Fiルータを接続しているポートなど,トラフィックをキャプチャしたい機器を接続しているポートをポートミラーの対象として設定する.

Raspberry PiのIPアドレスを固定

下記にRaspberry Pi OSの例を示す.

/etc/dhcpcd.confに設定を追記.

pi@raspberrypi:~ $ sudo vi /etc/dhcpcd.conf
(略)
interface wlan0
static ip_address=192.168.0.3/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

Raspberry Piを再起動して設定の反映を確認.

pi@raspberrypi:~ $ reboot
pi@raspberrypi:~ $ ifconfig
(略)
wlan0: flags=4163<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.0.3  netmask 255.255.255.0  broadcast 192.168.0.255
(略)

Raspberry Piに外付けHDDをマウント

下記にRaspberry Pi OSの例を示す.

HDDの認識を確認.

pi@raspberrypi:~ $ sudo fdisk -l | grep /dev/sda
Disk /dev/sda: 3.7 TiB, 4000752599040 bytes, 7813969920 sectors
/dev/sda1   2048 7813967871 7813965824  3.7T Microsoft basic data

HDDのUUIDとファイルシステムを確認.

pi@raspberrypi:~ $ sudo blkid /dev/sda1
/dev/sda1: LABEL="Elements" UUID="04F6232DF6231F04" TYPE="ntfs" PTTYPE="atari" PARTLABEL="Elements" PARTUUID="6d37ebe2-7815-4429-8743-f49e97b573c7"

/mnt/hddにHDDを自動マウントするように/etc/fstabに追記.

pi@raspberrypi:~ $ sudo mkdir /mnt/hdd
pi@raspberrypi:~ $ sudo vi /etc/fstab
(略)
UUID="04F6232DF6231F04" /mnt/hdd    ntfs    defaults,nofail 0   0

Raspberry Piを再起動して自動マウントしていることを確認.

pi@raspberrypi:~ $ reboot
pi@raspberrypi:~ $ df -h | grep /dev/sda
/dev/sda1        3.7T   24G  3.7T    1% /mnt/hdd

Raspberry Piでパケットをキャプチャ

下記にRaspberry Pi OSの例を示す.

必要なパッケージのインストール.

pi@raspberrypi:~ $ sudo apt update
pi@raspberrypi:~ $ sudo apt upgrade
pi@raspberrypi:~ $ sudo apt install tshark apache2 php libapache2-mod-php php-sqlite3

本リポジトリをクローン.

pi@raspberrypi:~ $ git clone https://github.com/FunabikiKeisuke/lan_traffic.git

データベースを保存するディレクトリを作成.

pi@raspberrypi:~ $ sudo mkdir /mnt/hdd/db

ミラーリング対象端末のMACアドレスとIPアドレスを登録.

pi@raspberrypi:~ $ python3 lan_traffic/register.py

.pcapファイルを保存するディレクトリを作成.

pi@raspberrypi:~ $ sudo mkdir /mnt/hdd/traffic_data

10分毎に.pcapファイルを保存し,.pcapファイルから通信量をデータベースに保存する設定を追記.

pi@raspberrypi:~ $ sudo crontab -e
(略)
*/10 * * * * bash /home/pi/lan_traffic/start_capture.sh
1,11,21,31,41,51 * * * * /usr/bin/python3 /home/pi/lan_traffic/traffic.py

Raspberry Piで通信量を表示するWebサーバを立てる

下記にRaspberry Pi OSの例を示す.

.htmlファイルをバックアップし,.phpファイルをコピー.

pi@raspberrypi:~ $ sudo mv /var/www/html/index.html /var/www/html/index.bak
pi@raspberrypi:~ $ sudo cp /home/pi/lan_traffic/*.php /var/www/html/

.cssファイルをダウンロードし,展開.

pi@raspberrypi:~ $ wget -P /var/tmp/ https://github.com/ysakasin/Umi/releases/download/v4.0.0/bootstrap-umi-4.0.0-dist.zip
pi@raspberrypi:~ $ sudo unzip /var/tmp/bootstrap-umi-4.0.0-dist.zip -d /var/www/html/

LAN内からWebブラウザでRaspberry PiのIPアドレスにアクセス.

http://192.168.0.3

参考文献

lan_traffic's People

Contributors

l3ickey avatar

Watchers

James Cloos avatar  avatar

lan_traffic's Issues

登録した端末毎にRGB値を指定しないとグラフが描画されない

register.py で端末を登録すると /mnt/hdd/db/device.db に保存される.
しかし,サイトに登録した端末のグラフを描画するためには device_color.php に登録名とRGB値をあらかじめ記述する必要があり,端末を追加した際に複数ファイルを編集する必要がある.
/mnt/hdd/db/device.db から登録した端末の数を取得し,端末の数だけグラフのRGB値を生成し,割り当てる実装にしたい.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.