#filter #packet #adapter #driver #mac-address #set #networking

已删除 ndisapi-rs

Rust crate用于与Windows数据包过滤器驱动程序(NDISAPI)交互

4个版本

0.4.6 2023年6月14日
0.4.5 2023年6月7日
0.4.1 2023年5月28日
0.3.3 2023年5月27日

#26#mac-address

Download history • Rust 包仓库 36/week @ 2024-03-28 • Rust 包仓库 18/week @ 2024-04-04 • Rust 包仓库

83 每月下载量

Apache-2.0

295KB
4K SLoC

NDISAPI-RS

Crates.io Documentation License

NDISAPI-RS是一个用于与Windows数据包过滤器驱动程序交互的Rust crate。它提供了一个易于使用、安全且高效的接口,可以在网络堆栈的NDIS级别上高效地过滤(检查和修改)原始网络数据包,对网络活动的影响最小。

Windows数据包过滤器(WinpkFilter)是一个高性能、轻量级的Windows数据包过滤框架,允许开发人员在NDIS级别上高效地检查、修改和控制原始网络数据包。它具有友好的API和支持各种Windows版本,简化了网络数据包操作,无需内核模式编程知识。

功能

  • 枚举网络适配器
  • 查询和设置网络适配器属性
  • 捕获和分析数据包
  • 过滤和修改数据包
  • 发送原始数据包

依赖项

安装

将以下内容添加到您的Cargo.toml文件中

[dependencies]
ndisapi-rs = "0.4.6"

用法

以下是如何枚举网络适配器并打印其信息的示例

use ndisapi_rs::{MacAddress, Ndisapi};

fn main() {
    let ndis = Ndisapi::new("NDISRD").expect("Failed to create NdisApi instance");

    let adapters = ndis
        .get_tcpip_bound_adapters_info()
        .expect("Failed to enumerate adapters");

    for adapter in adapters {
        println!("Adapter: {:?}", adapter.get_name());
        println!(
            "Description: {:?}",
            Ndisapi::get_friendly_adapter_name(adapter.get_name()).unwrap_or("Unknown".to_string())
        );
        println!(
            "MAC Address: {:?}",
            MacAddress::from_slice(adapter.get_hw_address()).unwrap_or_default()
        );
        println!("-------------------------------");
    }
}

有关更多示例和深入使用,请参阅文档

演示

以下是如何运行listadapters示例的示例

PS D:\firezone\ndisapi> cargo run --example listadapters
   Compiling ndisapi-rs v0.4.5 (D:\firezone\ndisapi)
    Finished dev [unoptimized + debuginfo] target(s) in 3.22s
     Running `target\debug\examples\listadapters.exe`
Detected Windows Packet Filter version 3.4.3
1. Local Area Connection* 10
        \DEVICE\{EDEE8C42-F604-4A7B-BFAA-6B110923217E}
         Medium: 0
         MAC: 9A:47:3D:60:26:9D
         MTU: 1500
         FilterFlags: FilterFlags(0x0)
Getting OID_GEN_CURRENT_PACKET_FILTER Error: Data error (cyclic redundancy check).
         OID_802_3_CURRENT_ADDRESS: 9A:47:3D:60:26:9D
2. vEthernet (Default Switch)
        \DEVICE\{6FE04972-B2B5-4F5C-97E6-B8518A017192}
         Medium: 0
         MAC: 00:15:5D:91:A3:15
         MTU: 1500
         FilterFlags: FilterFlags(0x0)
         OID_GEN_CURRENT_PACKET_FILTER: 0x0000000B
         OID_802_3_CURRENT_ADDRESS: 00:15:5D:91:A3:15
...

12. vEthernet (WLAN Virtual Switch)
        \DEVICE\{05F9267C-C548-4822-8535-9A57F1A99DB7}
         Medium: 0
         MAC: 18:47:3D:60:26:9D
         MTU: 1500
         FilterFlags: FilterFlags(0x0)
         OID_GEN_CURRENT_PACKET_FILTER: 0x0000000B
         OID_802_3_CURRENT_ADDRESS: 18:47:3D:60:26:9D

以下是异步-pass-through示例的演示。对于此场景,我们将假设vEthernet (WLAN 虚拟交换机)是默认的互联网连接

PS D:\firezone\ndisapi> cargo run --example async-packthru -- --interface-index 12
    Finished dev [unoptimized + debuginfo] target(s) in 0.11s
     Running `target\debug\examples\async-packthru.exe --interface-index 12`
Detected Windows Packet Filter version 3.4.3
Using interface \DEVICE\{05F9267C-C548-4822-8535-9A57F1A99DB7}
Press ENTER to exit
=======================================================================================================

Interface --> MSTCP (93 bytes)

  Ipv4 Address([142, 250, 102, 108]) => Address([192, 168, 3, 126])
   TCP 993 -> 54163
=======================================================================================================

MSTCP --> Interface (89 bytes)

  Ipv4 Address([192, 168, 3, 126]) => Address([142, 250, 102, 108])
   TCP 54163 -> 993
=======================================================================================================

Interface --> MSTCP (60 bytes)

  Ipv4 Address([142, 250, 102, 108]) => Address([192, 168, 3, 126])
   TCP 993 -> 54163
=======================================================================================================

Interface --> MSTCP (202 bytes)

  Ipv4 Address([192, 168, 3, 105]) => Address([224, 0, 0, 251])
   UDP 5353 -> 5353

Interface --> MSTCP (222 bytes)

  Ipv6 Address([254, 128, 0, 0, 0, 0, 0, 0, 18, 44, 107, 255, 254, 84, 37, 126]) => Address([255, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 251])
   UDP 5353 -> 5353
=======================================================================================================

Interface --> MSTCP (118 bytes)

  Ipv4 Address([142, 250, 102, 108]) => Address([192, 168, 3, 126])
   TCP 993 -> 54163
=======================================================================================================

MSTCP --> Interface (115 bytes)

  Ipv4 Address([192, 168, 3, 126]) => Address([142, 250, 102, 108])
   TCP 54163 -> 993
=======================================================================================================

Interface --> MSTCP (60 bytes)

  Ipv4 Address([142, 250, 102, 108]) => Address([192, 168, 3, 126])
   TCP 993 -> 54163
=======================================================================================================

MSTCP --> Interface (74 bytes)

  Ipv4 Address([192, 168, 3, 126]) => Address([158, 255, 51, 217])
   UDP 63616 -> 59999
=======================================================================================================

Interface --> MSTCP (80 bytes)

  Ipv4 Address([192, 168, 3, 105]) => Address([224, 0, 0, 251])
   UDP 5353 -> 5353

Interface --> MSTCP (100 bytes)

  Ipv6 Address([254, 128, 0, 0, 0, 0, 0, 0, 18, 44, 107, 255, 254, 84, 37, 126]) => Address([255, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 251])
   UDP 5353 -> 5353

Shutting down...

许可证

本项目采用Apache License 2.0许可。有关详细信息,请参阅LICENSE

依赖项

~131MB
~2M SLoC