-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (177 loc) · 7.23 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"encoding/hex"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
var (
// Default interface set to 'eth0'. iface indicates '-i' parameter.
iface string = "eth0"
// Default mode is live capture. pcapFile indicates '-r' parameter.
pcapFile string = ""
// Default mode is not any specific string for payload. string_payload indicates '-s' parameter.
string_payload string = ""
// Default mode is dumping all captured packets. expression indicates '<expression>' parameter.
// Expression parameter should be given in "" characters.
expression string = ""
snapshotLen int32 = 65535
promiscuous bool = true
err error
timeout time.Duration = -1 * time.Second
handle *pcap.Handle
)
func findMaxValue(s []int) int {
// This function used for properly split user parameters.
maxValue := 0
for _, element := range s {
if element > maxValue {
maxValue = element
}
}
return maxValue
}
func main() {
// Handle CLI arguments: (-i, -r, -s can be given in mixed order!)
arguments := os.Args
// To keep readed index in arguments, so can find 'expression' part properly.
var readed_arguments_index []int
for index, element := range arguments {
if element == "-i" {
iface = arguments[index + 1]
readed_arguments_index = append(readed_arguments_index, index + 1)
} else if element == "-r" {
pcapFile = arguments[index + 1]
readed_arguments_index = append(readed_arguments_index, index + 1)
} else if element == "-s" {
string_payload = arguments[index + 1]
readed_arguments_index = append(readed_arguments_index, index + 1)
} else if string([]rune(element)[0]) == "-" {
fmt.Println("UNKNOWN PARAMETER")
os.Exit(3)
}
}
if len(arguments) - 1 > findMaxValue(readed_arguments_index) {
for i := findMaxValue(readed_arguments_index) + 1; i < len(arguments); i++ {
expression += arguments[i] + " "
}
}
/*
// Debug line for control user parameters.
fmt.Println("-i -> ", iface)
fmt.Println("-r -> ", pcapFile)
fmt.Println("-s -> ", string_payload)
fmt.Println("expression -> ", expression)
*/
if len(readed_arguments_index) == 0 {
fmt.Println("Suggested Usage: sudo go run main.go -i <interface> <options> <expression>")
fmt.Println("Options:")
fmt.Println("\t -r: Read packets from <file> in tcpdump format")
fmt.Println("\t -s: Keep only packets that contain <string> in their payload (In quotation marks).")
fmt.Println("<expression> is a BPF filter that specifies which packets will be dumped. If no filter is given, packets seen on the interface (or contained in the trace) should be dumped. Otherwise, only packets matching <expression> should be dumped.")
os.Exit(3)
}
if pcapFile != "" {
// If '-r' parameter is specified, then read pcapFile.
handle, err = pcap.OpenOffline(pcapFile)
if err != nil { log.Fatal(err) }
defer handle.Close()
} else {
// If there is no '-r' parameter, then capture packets live.
handle, err = pcap.OpenLive(iface, snapshotLen, promiscuous, timeout)
if err != nil {log.Fatal(err) }
defer handle.Close()
}
if expression != "" {
// If some expression like "tcp and port 80" is specified, then set the BPFFilter:
err = handle.SetBPFFilter(expression)
if err != nil {
log.Fatal(err)
}
fmt.Println("BPF filter detected. Only capturing ", expression)
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
printPacketInfo(packet, string_payload)
}
}
func printPacketInfo(packet gopacket.Packet, payload_filter string) {
// This is the decider for printing the packet.
printThePacket := false
// Firstly check if '-s' parameter has entered or not.
if payload_filter != "" {
app := packet.ApplicationLayer()
if app != nil {
packet_payload := app.Payload()
if strings.Contains(string(packet_payload), payload_filter) {
// If there is '-s' parameter and payload has that, then print.
printThePacket = true
}
}
} else {
// If there is no '-s' parameter, then print all packets.
printThePacket = true
}
if printThePacket {
timestamp := packet.Metadata().Timestamp.Format("2006-01-02 15:04:05.999999")
fmt.Print(timestamp, " ")
ethernetLayer := packet.Layer(layers.LayerTypeEthernet)
if ethernetLayer != nil {
ethernetPacket, _ := ethernetLayer.(*layers.Ethernet)
fmt.Print(ethernetPacket.SrcMAC, " -> ", ethernetPacket.DstMAC)
fmt.Printf(" type 0x%x ", uint16(ethernetPacket.EthernetType))
fmt.Print("len ", packet.Metadata().Length)
fmt.Println()
}
ipLayer := packet.Layer(layers.LayerTypeIPv4)
tcpLayer := packet.Layer(layers.LayerTypeTCP) // It is 'nil' in case of packet is not TCP.
udpLayer := packet.Layer(layers.LayerTypeUDP) // It is 'nil' in case of packet is not UDP.
if ipLayer != nil {
ip, _ := ipLayer.(*layers.IPv4)
if tcpLayer != nil {
// If packet is TCP:
tcp, _ := tcpLayer.(*layers.TCP)
srcPort, dstPort := uint16(tcp.SrcPort), uint16(tcp.DstPort)
fmt.Print(ip.SrcIP, ":", srcPort, " -> ", ip.DstIP, ":", dstPort, " ")
fmt.Print(ip.Protocol, " ")
all_flags := [9]string{"FIN", "SYN", "RST", "PSH", "ACK", "URG", "ECE", "CWR", "NS"}
all_flags_value := [9]bool{tcp.FIN, tcp.SYN, tcp.RST, tcp.PSH, tcp.ACK, tcp.URG, tcp.ECE, tcp.CWR, tcp.NS}
for index, element := range all_flags_value {
if element {
fmt.Print(all_flags[index], " ")
}
}
fmt.Println()
} else if udpLayer != nil {
// If packet is UDP
udp, _ := udpLayer.(*layers.UDP)
srcPort, dstPort := uint16(udp.SrcPort), uint16(udp.DstPort)
fmt.Print(ip.SrcIP, ":", srcPort, " -> ", ip.DstIP, ":", dstPort, " ")
fmt.Print(ip.Protocol)
fmt.Println()
} else if uint8(ip.Protocol) == 1 {
// If packet is ICMP (ICMP's Protocol number is 1)
fmt.Print(ip.SrcIP, " -> ", ip.DstIP, " ")
fmt.Print(ip.Protocol)
fmt.Println()
}else {
fmt.Print(ip.SrcIP, " -> ", ip.DstIP, " ")
fmt.Print("OTHER")
fmt.Println()
}
}
app := packet.ApplicationLayer()
if app != nil {
packet_payload := app.Payload()
// Hex dump of byte[] type payload
fmt.Println(hex.Dump(packet_payload))
} else {
fmt.Println()
}
}
}