eBPFを使用したポートスキャン検出・ブロックプログラムです。このプログラムは、XDP(eXpress Data Path)を使用してネットワークインターフェースでパケットを監視し、ポートスキャンを検出してブロックします。
- ポートスキャンの検出(SYNパケットの監視)
- スキャン元IPアドレスの追跡
- スキャン検出時の自動ブロック
- SSHポート(22番)への通信は常に許可
- 詳細なログ出力
- Linux カーネル 5.4以上
- Rust 1.70以上
- BPFコンパイラコレクション(BCC)
- 管理者権限(root)
- リポジトリのクローン:
git clone https://github.com/yourusername/nmap-blocker.git
cd nmap-blocker- 依存関係のインストール:
cargo build --release- プログラムの実行:
sudo ./target/release/nmap-blocker -i <インターフェース名>例:
sudo ./target/release/nmap-blocker -i ens18- プログラムの停止:
- Ctrl+Cを押してプログラムを停止します。
-
パケットの監視
- ネットワークインターフェースで受信するすべてのパケットを監視
- IPv4パケットのみを処理
- TCPのSYNパケットを検出
-
スキャン検出
- 同一IPアドレスからのSYNパケットをカウント
- 10回以上のSYNパケットを検出した場合、そのIPからの通信をブロック
-
ホワイトリスト
- SSHポート(22番)への通信は常に許可
- その他のポートへの通信はスキャン検出ロジックに従って処理
プログラムは以下のようなログを出力します:
- スキャン検出時:
Detected potential scan from IP: <IPアドレス> (count: <カウント>) - ブロック時:
Blocking scan from IP: <IPアドレス> (count: <カウント>) - 通常のSYNパケット:
Received SYN packet from IP: <IPアドレス>
このプロジェクトは以下のライセンスの下で公開されています:
- MIT License
- Apache License 2.0
- GPL-2.0 License
詳細は各ライセンスファイルを参照してください。
- stable rust toolchains:
rustup toolchain install stable - nightly rust toolchains:
rustup toolchain install nightly --component rust-src - (if cross-compiling) rustup target:
rustup target add ${ARCH}-unknown-linux-musl - (if cross-compiling) LLVM: (e.g.)
brew install llvm(on macOS) - (if cross-compiling) C toolchain: (e.g.)
brew install filosottile/musl-cross/musl-cross(on macOS) - bpf-linker:
cargo install bpf-linker(--no-default-featureson macOS)
Use cargo build, cargo check, etc. as normal. Run your program with:
cargo run --release --config 'target."cfg(all())".runner="sudo -E"'Cargo build scripts are used to automatically build the eBPF correctly and include it in the program.
Cross compilation should work on both Intel and Apple Silicon Macs.
CC=${ARCH}-linux-musl-gcc cargo build --package nmap-blocker --release \
--target=${ARCH}-unknown-linux-musl \
--config=target.${ARCH}-unknown-linux-musl.linker=\"${ARCH}-linux-musl-gcc\"The cross-compiled program target/${ARCH}-unknown-linux-musl/release/nmap-blocker can be
copied to a Linux server or VM and run there.