Live Nexmon CSI feed from phone to PC

huangapple go评论84阅读模式
英文:

Live Nexmon CSI feed from phone to PC

问题

我正在致力于一个项目,使用手机上捕获的Wi-Fi信道状态信息(CSI)来检测手势。

我正在寻找一种通过USB连接到手机的PC上获取Nexus 5上使用Nexmon CSI拍摄的实时网络捕获的方法。

我目前正在使用tcpdump将数据包捕获到文件中,如下所示(非实时),然后使用名为csiread的Python包将CSI幅度和相位信息提取到Numpy中,以便使用Matplotlib进行可视化。

adb shell "su -c 'tcpdump -i wlan0 -v dst port 5500 -c 50 -w /sdcard/capture.pcap'"

简要说明:由于Wireshark能够读取Nexmon捕获,也能读取任何tcpdump捕获,我将使用它来演示兼容性。使用上述命令生成的文件可以使用Wireshark打开。

我的计划是使用adb shell "su -c 'tcpdump -i wlan0 -v dst port 5500 -c 50 -w -'" > wireshark -k -i -来测试实时数据包是否可读,但我收到了这个错误:“Unrecognized libpcap format or not libpcap data”。

以下是有效的内容:

# 在本地机器上对tcpdump进行实时捕获并导入Wireshark
# '-w -'将捕获写入标准输出
sudo tcpdump -i any -w - | wireshark -k -i -
# (如上所述)通过ADB在手机上进行Nexmon tcpdump捕获,将文件保存到手机上
# 文件可以由wireshark和csiread打开
adb shell "su -c 'tcpdump -i wlan0 -vv dst port 5500 -w /sdcard/capture.pcap -c 50'"
adb pull /sdcard/capture.pcap ~/Desktop/capture.pcap

以下是无效的内容:

以下代码受到了此教程的启发,该教程介绍了如何从SSH服务器获取实时的tcpdump数据流。

#(失败)尝试将手机上tcpdump的实时捕获传输到wireshark
adb shell "su -c 'tcpdump -i wlan0 -v dst port 5500 -w -'" | wireshark -k -i -
#(失败)尝试将实时数据直接写入PC上的文件
# 对Wireshark和csiread均无效
# -c是数据包数量
adb shell "su -c 'tcpdump -i wlan0 -v dst port 5500 -c 2 -w -'" > ~/Desktop/adb_direct_capture.pcap

这是Nexmon CSI的文档。它似乎未涵盖这种情况。

有趣的是,在检查通过ADB直接传输的数据时,tcpdump的正常输出(例如“tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes”)也被传递了。我尝试删除这样的行,但生成的文件仍然无效。我收到诸如“Not a pcap capture file (bad magic: b'\xef\xbf\xbd\xc3’)"之类的错误。我使用十六进制编辑器检查“有效”和“无效”文件时发现,“有效”文件似乎要短得多,尽管在正常编辑器中它们看起来长度相同。我猜想所有标准输出数据在通过ADB传输时会混乱。

英文:

I am working on a project to detect hand gestures using Wi-Fi Channel State Information (CSI) captured on a phone.

I'm looking for a way to get a live network capture taken using Nexmon CSI on a Nexus 5 to a PC that is attached to the phone over USB.

I'm currently using tcpdump to capture the packages into a file as seen below (not live), and I use a Python package called csiread to extract CSI magnitude and phase information into Numpy for visualization using Matplotlib.

adb shell "su -c 'tcpdump -i wlan0 -v dst port 5500 -c 50 -w /sdcard/capture.pcap'"

Quick note: Since Wireshark is able to read Nexmon captures in addition to any tcpdump capture, I'll be using that to demonstrate compatibility. The file generated with the above command can be opened using Wireshark.

My plan was to use adb shell "su -c 'tcpdump -i wlan0 -v dst port 5500 -c 50 -w -'" > wireshark -k -i - to test if the live packages are readable, but I receive this error: "Unrecognized libpcap format or not libpcap data."

Here are the things that work:

# tcpdump live feed on local maching piped to Wireshark
# '-w -' writes the captures to the standard output
sudo tcpdump -i any -w - | wireshark -k -i -
# (mentioned above) Nexmon tcpdump capture on phone through ADB to a file on phone
# file can be opened by wireshark and csiread
adb shell "su -c 'tcpdump -i wlan0 -vv dst port 5500 -w /sdcard/capture.pcap -c 50'"
adb pull /sdcard/capture.pcap ~/Desktop/capture.pcap

And here are the things that DON'T work:

The following code was inspired by this tutorial on how to get a live tcpdump feed from an SSH server.

# (fail) attempted to pipe live capture from phone tcpdump to wireshark
adb shell "su -c 'tcpdump -i wlan0 -v dst port 5500 -w -'" | wireshark -k -i -
# (fail) attemped to write live feed directly to a file on pc
# works with neither Wireshark nor csiread
# -c is the package count
adb shell "su -c 'tcpdump -i wlan0 -v dst port 5500 -c 2 -w -'" > ~/Desktop/adb_direct_capture.pcap

Here's Nexmon CSI's documentation. It doesn't seem to cover such cases.

What's curious is that upon inspecting the data piped directly over ADB, tcpdump's normal outputs such as "tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes" are also passed over. I tried removing such lines but the resulting file was still invalid. I get errors such as "Not a pcap capture file (bad magic: b'\xef\xbf\xbd\xc3')". When I inspected both "valid" and "invalid" files using a hex editor, the "valid" fiels seems to be much shorter even though they look the same length in a normal editor. My guess is that all the standard output data get messed up during their trip through ADB??

答案1

得分: 0

I simply had to use adb exec-out instead of adb shell, while dumping the stderr:

adb exec-out "su -c 'tcpdump --flags-go-here -w - 2>/dev/null'" > path/filename.pcap

Let me explain starting from the beginning of the line:

  • adb exec-out directly transmits the binary output of any command passed to it, so it doesn't corrupt such files (which was my issue).
  • su -c '' runs the command as root.
  • tcpdump simply captures packets.
  • The -w - flag tells tcpdump to write directly to the standard output.
  • 2>/dev/null discards the standard error (stderr), which includes messages such as "captured xx packages."
  • > filename simply writes the output to a file on the receiving computer.

I hope this helps someone!

英文:

I simply had to use adb exec-out instead of adb shell, while dumping the stderr:

adb exec-out "su -c 'tcpdump --flags-go-here -w - 2>/dev/null'" > path/filename.pcap

Let me explain starting from the beginning of the line:

  • adb exec-out "" directly transmits the binary output of any command passed to it, so it doesn't corrupt such files (which was my issue).
  • su -c '' runs the command as root.
  • tcpdump simply captures packets.
  • The -w - flag tells tcpdump to write directly to the standard output.
  • 2>/dev/null discards the standard error (stderr), which includes messages such as "captured xx packages.".
  • > filename simply writes the output to a file on the receiving computer.

I hope this helps someone!

huangapple
  • 本文由 发表于 2023年6月19日 08:16:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定