• Stars
    star
    134
  • Rank 270,967 (Top 6 %)
  • Language
    C#
  • License
    GNU General Publi...
  • Created about 2 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

面向对象的WinDivert的dotnet异步封装

WindivertDotnet

面向对象的WinDivertv2.2的dotnet异步封装,轻松实现网络数据拦截与修改。

1 nuget

WindivertDotnet

<PackageReference Include="WindivertDotnet" Version="1.*" /> 

2 功能介绍

  • 抓取网络数据包
  • 过滤或丢弃网络数据包
  • 嗅探网络数据包
  • 注入网络数据包
  • 修改网络数据包

3 如何使用

3.1 抓包改包

var filter = Filter.True
    .And(f => f.Network.Loopback)
    .And(f => f.Tcp.DstPort == 443)
    .And(f => f.Tcp.Ack == true);

using var divert = new WinDivert(filter, WinDivertLayer.Network);
using var packet = new WinDivertPacket();
using var addr = new WinDivertAddress();

while (true)
{
    // 读包
    await divert.RecvAsync(packet, addr);

    ProcessPacket(packet, addr); 

    // 修改后发出
    await divert.SendAsync(packet, addr);
}

static unsafe void ProcessPacket(WinDivertPacket packet, WinDivertAddress addr)
{
    // 解包
    var result = packet.GetParseResult();

    // 改包
    result.TcpHeader->DstPort = 443;

    // 重算checksums
    packet.CalcChecksums(addr);
}

3.2 注入数据包

private async Task SendEchoRequestAsync(IPAddress dstAddr)
{
    // 使用router计算将进行通讯的本机地址
    var router = new WinDivertRouter(dstAddr);
    using var addr = router.CreateAddress();
    using var packet = this.CreateIPV4EchoPacket(router.SrcAddress, router.DstAddress);

    packet.CalcChecksums(addr);     // 计算checksums,因为创建包时没有计算

    await this.divert.SendAsync(packet, addr);
}

/// <summary>
/// 创建icmp的echo包
/// </summary>
/// <param name="srcAddr"></param>
/// <param name="dstAddr"></param>
/// <returns></returns>
private unsafe WinDivertPacket CreateIPV4EchoPacket(IPAddress srcAddr, IPAddress dstAddr)
{
    // ipv4头
    var ipHeader = new IPV4Header
    {
        TTL = 128,
        Version =  IPVersion.V4,
        DstAddr = dstAddr,
        SrcAddr = srcAddr,
        Protocol = ProtocolType.Icmp,
        HdrLength = (byte)(sizeof(IPV4Header) / 4),
        Id = ++this.id,
        Length = (ushort)(sizeof(IPV4Header) + sizeof(IcmpV4Header))
    };

    // icmp头
    var icmpHeader = new IcmpV4Header
    {
        Type = IcmpV4MessageType.EchoRequest,
        Code = default,
        Identifier = ipHeader.Id,
        SequenceNumber = ++this.sequenceNumber,
    };

    // 将数据写到packet缓冲区
    var packet = new WinDivertPacket(ipHeader.Length);

    var writer = packet.GetWriter();
    writer.Write(ipHeader);
    writer.Write(icmpHeader);

    return packet;
}

More Repositories

1

NetworkSocket

NetworkSocket是一个以中间件(middleware)扩展通讯协议,以插件(plug)扩展服务器功能的支持SSL安全传输的通讯框架;目前支持http、websocket、fast、flex策略与silverlight策略协议。
C#
458
star
2

KestrelApp

基于Kestrel的网络编程应用示例
C#
241
star
3

CYarp

A reverse proxy toolkit to help you expose multiple local http servers behind a NAT or firewall to the internet
C#
120
star
4

ServiceSelf

为.NET 泛型主机的应用程序提供自安装为服务进程的能力
C#
112
star
5

WebApiClient.Extensions

WebApiClient项目的第三方扩展:Autofac、DependencyInjection、HttpClientFactory、SteeltoeOSS.Discovery、MessagePack、Protobuf、Json-Rpc
C#
71
star
6

Influxdb2.Client

Influxdb2的.net高性能客户端库,读写性能为官方客户端库的180%
C#
54
star
7

HttpMouse

基于yarp的http公网反向代理到内网的服务端与客户端库
C#
53
star
8

WebApiClient.Tools

WebApiClient项目的工具集,支持swagger解析生成WebApiClient的接口定义代码文件
C#
49
star
9

LuceneServer

Lucene.Net的完整服务器和客户端组件
C#
48
star
10

DependencyInjection.Annotation

基于注解的服务自动查找与注册的DI扩展
C#
47
star
11

PredicateLib

谓词筛选表达式Expression<Func<T, bool>>的扩展库
C#
45
star
12

Paho.MqttDotnet

A .Net wrapper for eclipse/paho.mqtt.c
C#
42
star
13

QMapper

快、准、狠的Mapper组件
C#
40
star
14

DebugViewer

用于捕获任意进程调用System.Diagnostics.Debugger.Log (level,category,message)输出的调试内容
C#
33
star
15

MemoryExtensions

High performance buffer types such as IArrayOwner<T>, RecyclableBufferWriter<T>, ResizableBufferWriter<T>, MemoryBufferWriter<T>, BufferReader
C#
28
star
16

AwaitableCompletionSource

AwaitableCompletionSource在多个场景下可替代TaskCompletionSource,更少的cpu时间和内存分配。
C#
27
star
17

PayProxy

提供一种支付结果回调参数调试的解决方案
C#
16
star
18

HookSln

这是提供给.Net使用的window hook和api hook托管和非托管混合项目
C#
13
star
19

HttpMessageHandlerFactory

具有生命周期管理和动态Web代理的HttpMessageHandler创建工厂
C#
12
star
20

SocketConnectionFactory

kestrel未公开的高性能的Socket客户端工厂
C#
12
star
21

WinFormHosting

WinForm for Microsoft.Extensions.Hosting
C#
6
star
22

BaiduAdBlock

百度搜索广告过滤的chrome插件
JavaScript
6
star
23

ValidBox

ValidBox4MVC是适用于Asp.Net Mvc前后端同时验证的组件,后端支持特性验证规则和Fluent Api验证规则两种风格,而前端的验证完全是异步验证的;其兄弟ValidBox4AspNet适用于Asp.Net WebForm控件和表单验证,支持前端和后端同时验证。
C#
6
star
24

AspnetcoreKong

aspnetcore微服务自动注册到kong的解决方案
C#
5
star
25

KingBrain

头脑王者小程序自动搜索答案服务+自动点击客户端+浏览器显示答案客户端
C#
5
star
26

SpiderEngineX

HttpClient+CsQuery爬虫引擎
C#
4
star
27

MemCached

这是.Net实现的MemCached二进制协议客户端
C#
4
star
28

ConfigMerge

一种Webconfig自动化升级方法
C#
2
star
29

FormattableLogging

C#
2
star
30

Logging.Debugger

Microsoft.Extensions.Logging统一日志的System.Diagnostics.Debugger日志提供者,可以使用DebugViewer工具监听日志内容
C#
2
star
31

Markdown

C#
1
star