All Projects → williamfhe → godivert

williamfhe / godivert

Licence: LGPL-3.0 License
Bindings for WinDivert in Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to godivert

WinDivertSharp
A minimal .NET binding over WinDivert
Stars: ✭ 91 (+145.95%)
Mutual labels:  windivert, packet-capture, divert-sockets
DivertPInvoke
PInvoke wrapper for WinDivert
Stars: ✭ 22 (-40.54%)
Mutual labels:  windivert, packet-capture
Divert.Net
.NET Wrapper for WinDivert
Stars: ✭ 51 (+37.84%)
Mutual labels:  windivert, packet-capture
Airpwn Ng
airpwn-ng
Stars: ✭ 231 (+524.32%)
Mutual labels:  packet-capture
sharppcap
Official repository - Fully managed, cross platform (Windows, Mac, Linux) .NET library for capturing packets
Stars: ✭ 1,054 (+2748.65%)
Mutual labels:  windivert
zk-sniffer
sniffer and parse zookeeper packet
Stars: ✭ 38 (+2.7%)
Mutual labels:  packet-capture
BoringSSLKeys
Grab your libboringssl keys to decrypt traffic (pcaps)
Stars: ✭ 46 (+24.32%)
Mutual labels:  packet-capture
Cuishark
A protocol analyzer like a wireshark on CUI. cuishark is using libwireshark to analyze packets. https://cuishark.slankdev.net
Stars: ✭ 208 (+462.16%)
Mutual labels:  packet-capture
tcpslice
tcpslice concatenates multiple pcap files together, or extracts time slices from one or more pcap files.
Stars: ✭ 48 (+29.73%)
Mutual labels:  packet-capture
dsc
DNS Statistics Collector
Stars: ✭ 94 (+154.05%)
Mutual labels:  packet-capture
Tanji
Habbo Packet Sniffer/Analyzer
Stars: ✭ 33 (-10.81%)
Mutual labels:  packet-capture
rvi capture
rvictl for Linux and Windows: capture packets sent/received by iOS devices
Stars: ✭ 124 (+235.14%)
Mutual labels:  packet-capture
xpcap
Cross-platform Packet Capture which supports Linux and macOS(BSD) in 1000 LOC without depending on libpcap.
Stars: ✭ 19 (-48.65%)
Mutual labels:  packet-capture
Dnscap
Network capture utility designed specifically for DNS traffic
Stars: ✭ 234 (+532.43%)
Mutual labels:  packet-capture
sockdump
Dump unix domain socket traffic with bpf
Stars: ✭ 160 (+332.43%)
Mutual labels:  packet-capture
Etl2pcapng
Utility that converts an .etl file containing a Windows network packet capture into .pcapng format.
Stars: ✭ 228 (+516.22%)
Mutual labels:  packet-capture
Netvisix
Netvisix visualizes the network packet flow between hosts
Stars: ✭ 65 (+75.68%)
Mutual labels:  packet-capture
SnifferUI
基于MFC和WinPcap库开发的网络抓包和协议分析软件
Stars: ✭ 86 (+132.43%)
Mutual labels:  packet-capture
pf-diverters
A collection of (OpenBSD) PF divert socket daemons
Stars: ✭ 42 (+13.51%)
Mutual labels:  divert-sockets
arp-dns-attacks
ARP spoofing, HTTP redirection, DNS spoofing and DNS forging using pcap library
Stars: ✭ 25 (-32.43%)
Mutual labels:  packet-capture

GoDivert

Go bindings for WinDivert.

WinDivert is a user-mode packet capture-and-divert package for Windows.

Installation

go get github.com/williamfhe/godivert

Introduction

The binding's documentation can be found Here.

If you don't have the WinDivert dll installed on your System or you want to load a specific WinDivert dll you should do :

godivert.LoadDLL("PathToThe64bitDLL", "PathToThe32bitDLL")

The path can be a relative path to the .exe current directory or an absolute path.

Note that the driver must be in the same directory as the dll. LoadDLL will then load the dll depending on your OS architecture.

To start create a new instance of WinDivertHandle by calling NewWinDivertHandle and passing the filter as a parameter.

Documentation of the filter can be found Here.

winDivert, err := godivert.NewWinDivertHandle("Your filter here")

WinDivertHandle is struct that you can use to call WinDivert's function like Recv or Send.

You can divert a packet from the network stack by using winDivert.Recv() where winDivert is an instance of WinDivertHandle.

packet, err := winDivert.Recv()

You can then choose to send the packet or modify it.

packet.SetDstPort(1234) // Sets the destination port
packet.Send(winDivert) // Sends the packet back on the network stack

You can get and set values from the packet's header by using the header package. Documentation on this package can be found Here .

As the packet has been modified the checksums have to be recalculated before sending it back on the network stack.

It is done automatically if the packet has been modified when calling packet.Send but you can do it manually by calling packet.CalcNewChecksum.

To receive packets you can also use winDivert.Packets.

packetChan, err := winDivert.Packets()

Here packetChan is a channel of *godivert.Packet coming directly from the network stack.

Note that all packets diverted are guaranteed to match the filter given in godivert.NewWinDivertHandle("You filter here")

Examples

Capturing and Printing a Packet

package main

import (
    "fmt"
    "github.com/williamfhe/godivert"
)

func main() {
    winDivert, err := godivert.NewWinDivertHandle("true")
    if err != nil {
        panic(err)
    }

    packet, err := winDivert.Recv()
    if err != nil {
        panic(err)
    }
    defer winDivert.Close()

    fmt.Println(packet)

    packet.Send(winDivert)

}

Wait for a packet and print it.

Blocking Protocol by IP

package main

import (
    "net"
    "time"
    "github.com/williamfhe/godivert"
)

var cloudflareDNS = net.ParseIP("1.1.1.1")

func checkPacket(wd *godivert.WinDivertHandle, packetChan <-chan *godivert.Packet) {
    for packet := range packetChan {
        if !packet.DstIP().Equal(cloudflareDNS) {
            packet.Send(wd)
        }
    }
}

func main() {
    winDivert, err := godivert.NewWinDivertHandle("icmp")
    if err != nil {
        panic(err)
    }
    defer winDivert.Close()

    packetChan, err := winDivert.Packets()
    if err != nil {
        panic(err)
    }

    go checkPacket(winDivert, packetChan)

    time.Sleep(1 * time.Minute)
}

Forbid all ICMP packets to reach 1.1.1.1 for 1 minute.

Try it :

ping 1.1.1.1

Packet Count

package main

import (
    "fmt"
    "time"
    "github.com/williamfhe/godivert"
    "github.com/williamfhe/godivert/header"
)

var icmpv4, icmpv6, udp, tcp, unknown, served uint

func checkPacket(wd *godivert.WinDivertHandle, packetChan  <- chan *godivert.Packet) {
    for packet := range packetChan {
        countPacket(packet)
        wd.Send(packet)
    }
}

func countPacket(packet *godivert.Packet) {
    served++
    switch packet.NextHeaderType() {
    case header.ICMPv4:
        icmpv4++
    case header.ICMPv6:
        icmpv6++
    case header.TCP:
        tcp++
    case header.UDP:
        udp++
    default:
        unknown++
    }
}


func main() {
    winDivert, err := godivert.NewWinDivertHandle("true")
    if err != nil {
        panic(err)
    }

    fmt.Println("Starting")
    defer winDivert.Close()

    packetChan, err := winDivert.Packets()
    if err != nil {
        panic(err)
    }

    n := 50
    for i := 0; i < n; i++ {
        go checkPacket(winDivert, packetChan)
    }

    time.Sleep(15 * time.Second)

    fmt.Println("Stopping...")

    fmt.Printf("Served: %d packets\n", served)

    fmt.Printf("ICMPv4=%d ICMPv6=%d UDP=%d TCP=%d Unknown=%d", icmpv4, icmpv6, udp, tcp, unknown)
}

Count all protocols passing by for 15 seconds.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].