-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (79 loc) · 2.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bytes"
"flag"
"fmt"
"log"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
func analysePacket(packet gopacket.Packet) {
if ip4Layer := packet.Layer(layers.LayerTypeIPv4); ip4Layer != nil {
ip4 := ip4Layer.(*layers.IPv4)
// tcp
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
tcp := tcpLayer.(*layers.TCP)
// is HTTP request or response
if len(tcp.Payload) > 4 {
if bytes.Equal(tcp.Payload[:4], []byte("HTTP")) {
fmt.Printf("%s Respond From %s:%d to %s:%d\n", tcp.Payload[:4], ip4.SrcIP, tcp.SrcPort, ip4.DstIP, tcp.DstPort)
} else if bytes.Equal(tcp.Payload[:4], []byte("GET ")) || bytes.Equal(tcp.Payload[:4], []byte("POST")) {
// find the path of the request
i1 := -1
i2 := -1
for index, value := range tcp.Payload {
if i1 == -1 {
if value == ' ' {
i1 = index + 1
}
} else if i2 == -1 {
if value == ' ' {
i2 = index
break
}
}
}
fmt.Printf("HTTP %s %s From %s:%d to %s:%d\n", tcp.Payload[:4], tcp.Payload[i1:i2], ip4.SrcIP, tcp.SrcPort, ip4.DstIP, tcp.DstPort)
}
}
// udp
} else if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer != nil {
udp := udpLayer.(*layers.UDP)
// dns
if dnsLayer := packet.Layer(layers.LayerTypeDNS); dnsLayer != nil {
fmt.Printf("DNS From %s:%d to %s:%d\n", ip4.SrcIP, udp.SrcPort, ip4.DstIP, udp.DstPort)
dns := dnsLayer.(*layers.DNS)
// is dns request or response
if dns.QR {
for _, dnsAnswer := range dns.Answers {
fmt.Printf("DNS Answer: %s\n", dnsAnswer.String())
}
} else {
for _, dnsQuestion := range dns.Questions {
fmt.Printf("DNS Question: %s\n", string(dnsQuestion.Name))
}
}
}
}
}
}
func main() {
var (
device string = "wlan0"
)
flag.StringVar(&device, "i", device, "Interface to use")
flag.Parse()
// Open a pcap handle for the provided interface
handle, err := pcap.OpenLive(device, 16384, true, pcap.BlockForever)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
packetSource.NoCopy = true
packetSource.Lazy = true
for packet := range packetSource.Packets() {
go analysePacket(packet)
}
}