/
quickdecoder.go
127 lines (110 loc) · 3.32 KB
/
quickdecoder.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"log"
"net"
"strings"
"time"
)
var (
device string = "eth0"
snapshot_len int32 = 1024
promiscuous bool = false
err error
timeout time.Duration = 2 * time.Second
handle *pcap.Handle
// Will reuse these for each packet
ethLayer layers.Ethernet
ipLayer layers.IPv4
tcpLayer layers.TCP
)
func checkLocalip(iface string) (string, error) {
ifaceobj, err := net.InterfaceByName(iface)
if err != nil {
return "", err
}
addrarry, err := ifaceobj.Addrs()
if err != nil {
return "", err
}
var localip = ""
fmt.Println(addrarry)
for _, ip := range addrarry {
IP := ip.String()
if strings.Contains(IP, "/24") {
localip = strings.TrimSuffix(IP, "/24")
}
}
return localip, nil
}
func main() {
//get local ip
localip, err := checkLocalip(device)
// Open device
handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
parser := gopacket.NewDecodingLayerParser(
layers.LayerTypeEthernet,
ðLayer,
&ipLayer,
&tcpLayer,
)
foundLayerTypes := []gopacket.LayerType{}
err := parser.DecodeLayers(packet.Data(), &foundLayerTypes)
if err != nil {
fmt.Println("Trouble decoding layers: ", err)
}
for _, layerType := range foundLayerTypes {
if layerType == layers.LayerTypeIPv4 {
fmt.Println("IPV4 found")
}
if ipLayer.DstIP.String() == localip || ipLayer.SrcIP.String() == localip {
fmt.Println("IPv4 go through this machine: ", ipLayer.SrcIP, "->", ipLayer.DstIP)
applicationLayer := packet.ApplicationLayer()
if strings.Contains(string(applicationLayer.Payload()), "HTTP") {
fmt.Println("HTTP found!")
fmt.Println("layer content", string(applicationLayer.LayerContents()))
fmt.Println("layer payload", string(applicationLayer.Payload()))
fmt.Println("layer type", string(applicationLayer.LayerType()))
}
}
}
}
}
/*
if layerType == layers.LayerTypeIPv4 {
fmt.Println("IPv4: ", ipLayer.SrcIP, "->", ipLayer.DstIP)
}
if ipLayer.DstIP.String() == localip {
if ipLayer.DstIP.String() == "10.10.103.131" {
if layerType == layers.LayerTypeIPv4 {
fmt.Println("IPv4: ", ipLayer.SrcIP, "->", ipLayer.DstIP)
applicationLayer := packet.ApplicationLayer()
if applicationLayer != nil {
fmt.Println("Application layer/Payload found.")
//fmt.Printf("%s\n", applicationLayer.Payload())
fmt.Printf("%v\n", string(applicationLayer.Payload()))
// Search for a string inside the payload
if strings.Contains(string(applicationLayer.Payload()), "HTTP") {
fmt.Println("HTTP found!")
fmt.Println("layer content", string(applicationLayer.LayerContents()))
fmt.Println("layer payload", string(applicationLayer.Payload()))
fmt.Println("layer type", string(applicationLayer.LayerType()))
}
}
}
if layerType == layers.LayerTypeTCP {
fmt.Println("TCP Port: ", tcpLayer.SrcPort, "->", tcpLayer.DstPort)
fmt.Println("TCP SYN:", tcpLayer.SYN, " | ACK:", tcpLayer.ACK)
fmt.Println("content ", string(tcpLayer.Contents))
fmt.Println("info", tcpLayer.ECE)
}
*/