LoginSignup
0
1

nexmonをビルド・インストールしてラズパイ4でパケットキャプチャをしよう

Last updated at Posted at 2024-01-29

無線LANのパケットキャプチャがしたかったが手元に手軽にモニターモードになるドングルがない。
ググったらnexmonという便利そうなツールがあったがビルドして使うまでに結構ハマったので私的なメモとして残しておく。

■キャプチャするために必要な前提
・Wiresharkを使用
・ラズベリーパイ4モデルBを使用

■キャプチャしようと思ったときに上がる選択肢
・windowsでairpcapやomnipeekなどをお金を払って使用する
 →めっちゃお金がかかるので個人利用には向かない
・モニターモードに設定できるWLANドングルを使用する
 →Linuxだとモニターモードで使用できるドングルがあるが、キャプチャーしたいチャンネルの指定などがよくわからなかったので除外した
 ※誰か知ってたら教えてください。

■ビルド方法
ラズパイのOSはnexmonの公式の対応カーネルを見ると5.4系のカーネルが比較的サポートされてそうなので5.4系の最後のimageを使用した。
https://downloads.raspberrypi.org/raspios_full_armhf/images/raspios_full_armhf-2021-01-12/ 2021-01-11-raspios-buster-armhf-full.zip
OSのインストール方法は適当に探してインストールしてください。

基本的に公式の「Steps to create your own firmware patches」に従いますが、参考にさせていただいたサイトの手順も混じっています。
https://github.com/seemoo-lab/nexmon
用語など間違ってる可能性がありますが、指摘貰えれば直します。

①カーネルの固定

$ wget http://archive.raspberrypi.org/debian/pool/main/r/raspberrypi-firmware/raspberrypi-kernel-headers_1.20210108-1_armhf.deb
$ dpkg -i raspberrypi-kernel-headers_1.20210108-1_armhf.deb
$ sudo apt-mark hold libraspberrypi-bin libraspberrypi0 raspberrypi-bootloader raspberrypi-kernel raspberrypi-kernel-headers

②パッケージ更新
※--allow-releaseinfo-changeをつけないと③でインストールできないライブラリーが出てくる

$ sudo apt-get update --allow-releaseinfo-change
$ sudo apt-get upgrade

③必要なライブラリーやソフトをインストール
※raspberrypi-kernel-headersいらんかも

$ sudo apt install git libgmp3-dev gawk qpdf bison flex make autoconf libtool texinfo

④nexmonをgitからcloneしてくる

$ git clone https://github.com/seemoo-lab/nexmon.git

⑤libislビルド
※libgmp3-devをインストールしてないとビルドに失敗する。gmp.hがないとかで怒られる

$ cd buildtools/isl-0.10
$ ./configure
$ make
$ sudo make install
$ sudo ln -s /usr/local/lib/libisl.so /usr/lib/arm-linux-gnueabihf/libisl.so.10

⑥ibmpfrビルド

$ cd buildtools/mpfr-3.1.4
$ autoreconf -f -i
$ ./configure
$ make
$ sudo make install
$ sudo ln -s /usr/local/lib/libmpfr.so /usr/lib/arm-linux-gnueabihf/libmpfr.so.4

⑦ビルドの環境変数設定

$ cd ~/nexmon
$ sudo -s
$ source setup_env.sh
$ make

→/lib/modules/5.4.83-v7+/buildがないと怒られた。
→③でraspberrypi-kernel-headersに入れるとカーネルヘッダーが更新されて上記のフォルダーがなくなる
→raspberrypi-kernel-headersは更新しちゃダメ

⑧nexmonのbcm43455c0のkernel moduleをmake

$ cd patches/bcm43455c0/7_45_206/nexmon
$ make
 BUILDING DRIVER for kernel 5.4 brcmfmac_5.4.y-nexmon/brcmfmac.ko (details: log/driver.log)
nexmon/patches/driver/brcmfmac_5.4.y-nexmon/debug.c:17:5: error: redefinition of ‘brcmf_debug_create_memdump’
 int brcmf_debug_create_memdump(struct brcmf_bus *bus, const void *data,
     ^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from nexmon/patches/driver/brcmfmac_5.4.y-nexmon/bus.h:9,
                 from nexmon/patches/driver/brcmfmac_5.4.y-nexmon/debug.c:13:
nexmon/patches/driver/brcmfmac_5.4.y-nexmon/debug.h:135:5: note: previous definition of ‘brcmf_debug_create_memdump’ was here
 int brcmf_debug_create_memdump(struct brcmf_bus *bus, const void *data,
     ^~~~~~~~~~~~~~~~~~~~~~~~~~
nexmon/patches/driver/brcmfmac_5.4.y-nexmon/debug.c:45:16: error: redefinition of ‘brcmf_debugfs_get_devdir’
 struct dentry *brcmf_debugfs_get_devdir(struct brcmf_pub *drvr)
                ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from nexmon/patches/driver/brcmfmac_5.4.y-nexmon/bus.h:9,
                 from nexmon/patches/driver/brcmfmac_5.4.y-nexmon/debug.c:13:
nexmon/patches/driver/brcmfmac_5.4.y-nexmon/debug.h:124:30: note: previous definition of ‘brcmf_debugfs_get_devdir’ was here
 static inline struct dentry *brcmf_debugfs_get_devdir(struct brcmf_pub *drvr)
                              ^~~~~~~~~~~~~~~~~~~~~~~~
nexmon/patches/driver/brcmfmac_5.4.y-nexmon/debug.c:50:5: error: redefinition of ‘brcmf_debugfs_add_entry’
 int brcmf_debugfs_add_entry(struct brcmf_pub *drvr, const char *fn,
     ^~~~~~~~~~~~~~~~~~~~~~~
In file included from nexmon/patches/driver/brcmfmac_5.4.y-nexmon/bus.h:9,
                 from nexmon/patches/driver/brcmfmac_5.4.y-nexmon/debug.c:13:
nexmon/patches/driver/brcmfmac_5.4.y-nexmon/debug.h:129:5: note: previous definition of ‘brcmf_debugfs_add_entry’ was here
 int brcmf_debugfs_add_entry(struct brcmf_pub *drvr, const char *fn,
     ^~~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [scripts/Makefile.build:266: nexmon/patches/driver/brcmfmac_5.4.y-nexmon/debug.o] エラー 1
make[2]: *** 未完了のジョブを待っています....
make[1]: *** [Makefile:1732: nexmon/patches/driver/brcmfmac_5.4.y-nexmon] エラー 2
make: *** [Makefile:51: brcmfmac.ko] エラー 2

→ビルドエラーがおきる。
→イシューで上がってるっぽいが治ったのかはよくわからなかった。
 https://github.com/seemoo-lab/nexmon/issues/483
→makefileに-DDEBUG入れるのが正しいかも。
→とりあえず該当箇所のdebug.h DEBUGのdefineを有効にしてビルド

⑨現在のファームウェアをバックアップ

$ make backup-firmware
$ cp /lib/firmware/brcm/brcmfmac43455-sdio.bin brcmfmac43455-sdio.bin.orig

⑩ビルドしたファームウェアを差し替え

$ make install
$ make install-firmware

          ###########   ###########   ##########    ##########           
         ############  ############  ############  ############          
         ##            ##            ##   ##   ##  ##        ##          
         ##            ##            ##   ##   ##  ##        ##          
         ###########   ####  ######  ##   ##   ##  ##    ######          
          ###########  ####  #       ##   ##   ##  ##    #    #          
                   ##  ##    ######  ##   ##   ##  ##    #    #          
                   ##  ##    #       ##   ##   ##  ##    #    #          
         ############  ##### ######  ##   ##   ##  ##### ######          
         ###########    ###########  ##   ##   ##   ##########           

            S E C U R E   M O B I L E   N E T W O R K I N G               


                               presents:                                  

              # ###   ###  #   # # ###  ###   ###  # ###                  
              ##   # #   #  # #  ##   ##   # #   # ##   #                 
              #    # #####   #   #    #    # #   # #    #                 
              #    # #      # #  #    #    # #   # #    #                 
              #    #  #### #   # #    #    #  ###  #    #                 

                The C-based Firmware Patching Framework                   


                           !!! WARNING !!!                                
    Our software may damage your hardware and may void your hardware’s    
     warranty! You use our tools at your own risk and responsibility      
  COLLECTING STATISTICS read nexmon/STATISTICS.md for more information
  COMPILING src/version.c => obj/version.o (details: log/compiler.log)
  GENERATING LINKER FILE gen/nexmon.pre => gen/nexmon.ld
  GENERATING LINKER FILE gen/nexmon.pre => gen/flashpatches.ld
  LINKING OBJECTS => gen/patch.elf (details: log/linker.log, log/linker.err)
  GENERATING MAKE FILE gen/nexmon.pre => gen/nexmon.mk
  GENERATING MAKE FILE gen/nexmon.pre => gen/flashpatches.mk
  APPLYING FLASHPATCHES gen/flashpatches.mk => brcmfmac43455-sdio.bin (details: log/flashpatches.log)
  APPLYING PATCHES gen/nexmon.mk => brcmfmac43455-sdio.bin (details: log/patches.log)
  BUILDING DRIVER for kernel 5.4 brcmfmac_5.4.y-nexmon/brcmfmac.ko (details: log/driver.log)
  COPYING brcmfmac43455-sdio.bin => /lib/firmware/brcm/brcmfmac43455-sdio.bin
  UNLOADING brcmfmac
  RELOADING brcmfmac

⑪nexutil(ユーティリティ)をmakeしてインストール
この後のチャンネル変更などに使用する。

$ cd utilities/nexutil/
$ make
$ make install

⑫モニター用のif追加

$ iw phy `iw dev wlan0 info | gawk '/wiphy/ {printf "phy" $2}'` interface add mon0 type monitor
$ iwconfig
mon0: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        unspec 00-00-00-00-00-00-00-00  txqueuelen 1000  (不明なネット)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

mon0      IEEE 802.11  Mode:Monitor  Frequency:5.18 GHz  
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Power Management:on

⑬wlan0をダウン

$ sudo ifconfig wlan0 down

⑭mon0のチャンネル1に設定

$ nexutil -k1
$ iwconfig
mon0      IEEE 802.11  Mode:Monitor  Frequency:2.412 GHz  Tx-Power=31 dBm   
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Power Management:on

⑮mon0のifを有効化

$sudo ifconfig mon0 up

⑯Wiresharkでmon0をキャプチャー
Wiresharkを起動して表示を選ぶと「無線ツールバー」にチェックが入ってた。
ここでチャンネルが表示されているがここで選べるかはやってないのでわからない。
※ch36になっていますが、ch1で設定した時はch1が表示されていました。
画像は5Ghzのキャプチャーができるかch36で設定した時のモノです。

$sudo wireshark

Wireshark_cap.png

⑰取得した結果
指定したチャンネルでパケットがキャプチャー出来ている。よかったですね。
Wireshark_cap1.png

Wireshark_cap32.png

■まだよくわかってないこと
・長時間キャプチャーができるのかはわかってない。キャプチャーしたのは2分くらいで長時間耐えられるのかは不明
・安定性が上がる設定もあるようなのでもしかしたら長時間キャプチャすると落ちるかもしれない
・電源OFF→ONしたあとにおそらくロードしたkernel moduleを差し替えないとキャプチャーできない状態に戻ると思われるので差し替えが必要

■参考にしたサイト
https://github.com/seemoo-lab/nexmon
https://www.beatcraft.com/labs/2021/10/raspi4wifimonitor.html
https://qiita.com/marumen/items/5bef667338a96a4a6c7d
https://qiita.com/24601_cold/items/7b2538f1d4f09f6a5f89
https://qiita.com/god19/items/f21d274b45688d9679fa

0
1
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
1