Skip to content

gon-sui/nmap-blocker

Repository files navigation

Nmap Blocker

eBPFを使用したポートスキャン検出・ブロックプログラムです。このプログラムは、XDP(eXpress Data Path)を使用してネットワークインターフェースでパケットを監視し、ポートスキャンを検出してブロックします。

機能

  • ポートスキャンの検出(SYNパケットの監視)
  • スキャン元IPアドレスの追跡
  • スキャン検出時の自動ブロック
  • SSHポート(22番)への通信は常に許可
  • 詳細なログ出力

必要条件

  • Linux カーネル 5.4以上
  • Rust 1.70以上
  • BPFコンパイラコレクション(BCC)
  • 管理者権限(root)

インストール

  1. リポジトリのクローン:
git clone https://github.com/yourusername/nmap-blocker.git
cd nmap-blocker
  1. 依存関係のインストール:
cargo build --release

使用方法

  1. プログラムの実行:
sudo ./target/release/nmap-blocker -i <インターフェース名>

例:

sudo ./target/release/nmap-blocker -i ens18
  1. プログラムの停止:
  • Ctrl+Cを押してプログラムを停止します。

動作の仕組み

  1. パケットの監視

    • ネットワークインターフェースで受信するすべてのパケットを監視
    • IPv4パケットのみを処理
    • TCPのSYNパケットを検出
  2. スキャン検出

    • 同一IPアドレスからのSYNパケットをカウント
    • 10回以上のSYNパケットを検出した場合、そのIPからの通信をブロック
  3. ホワイトリスト

    • 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

詳細は各ライセンスファイルを参照してください。

Prerequisites

  1. stable rust toolchains: rustup toolchain install stable
  2. nightly rust toolchains: rustup toolchain install nightly --component rust-src
  3. (if cross-compiling) rustup target: rustup target add ${ARCH}-unknown-linux-musl
  4. (if cross-compiling) LLVM: (e.g.) brew install llvm (on macOS)
  5. (if cross-compiling) C toolchain: (e.g.) brew install filosottile/musl-cross/musl-cross (on macOS)
  6. bpf-linker: cargo install bpf-linker (--no-default-features on macOS)

Build & Run

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-compiling on macOS

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.

nmap-blocker

About

No description, website, or topics provided.

Resources

License

Apache-2.0 and 2 other licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
GPL-2.0
LICENSE-GPL2
MIT
LICENSE-MIT

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages