-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcp.go
122 lines (101 loc) · 3.39 KB
/
gcp.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
package main
import (
"fmt"
"time"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/layers"
"github.com/google/gopacket"
"bufio"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
)
func main() {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"SNO", "Device Name", "Device IP", "Device Netmask"})
var devs []pcap.Interface
devs, _ = pcap.FindAllDevs()
for temp, dev := range devs {
dev_idx := temp/2
if len(dev.Addresses) == 0 {
continue
}
table.Append([]string{strconv.Itoa(dev_idx), string(dev.Name), dev.Addresses[0].IP.String(), dev.Addresses[0].Netmask.String()})
}
table.Render()
reader := bufio.NewReader(os.Stdin)
fmt.Println("\nPlease Enter Ethernet Device SNO: ")
input, _ := reader.ReadString('\n')
input = strings.TrimSuffix(input,"\n")
udev_idx, _ := strconv.ParseInt(input, 10, 64)
udev_idx = 2*udev_idx
udev := devs[int(udev_idx)]
fmt.Println("Chosen device is: ", udev.Name)
var device string = string(udev.Name)
var snapshot int32 = 65535
var promiscuous bool = false
var timeout time.Duration = -1 * time.Second
var handle *pcap.Handle
handle, _ = pcap.OpenLive(device,snapshot, promiscuous, timeout)
defer handle.Close()
reader = bufio.NewReader(os.Stdin)
fmt.Println("\nDo you wish to see L3 packets, L4 packets, both L3/L4 packets simultaneously or set a filter? Please enter your choice/SNO: ")
tableMenu := tablewriter.NewWriter(os.Stdout)
tableMenu.SetHeader([]string{"SNO", "Option"})
tableMenu.Append([]string{"0", "Only Layer 3"})
tableMenu.Append([]string{"1", "Only Layer 4"})
tableMenu.Append([]string{"2", "Both Layer 3 and Layer 4"})
tableMenu.Append([]string{"3", "Apply Filter (Per packet output)"})
tableMenu.Render()
fmt.Println("Enter your choice: ")
choice_input, _ := reader.ReadString('\n')
choice_input = strings.TrimSuffix(choice_input,"\n")
choice, _ := strconv.ParseInt(choice_input, 10, 64)
ip_bool, tcp_bool, filter_bool := 0,0,0
switch choice {
case 0:
ip_bool = 1
case 1:
tcp_bool = 1
case 2:
ip_bool = 1
tcp_bool = 1
case 3:
filter_bool = 1
reader := bufio.NewReader(os.Stdin)
fmt.Println("\nPlease enter filter: ")
filter, _ := reader.ReadString('\n')
filter = strings.TrimSuffix(filter, "\n")
err := handle.SetBPFFilter(filter)
if err == nil {
fmt.Println("\n[INFO] Filter applied successfully!")
}
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
if filter_bool == 1 {
fmt.Println(packet)
}
tableIP := tablewriter.NewWriter(os.Stdout)
tableIP.SetHeader([]string{"IP Protocol", "Source IP", "Destination IP"})
tableTCP := tablewriter.NewWriter(os.Stdout)
tableTCP.SetHeader([]string{"TCP ACK", "TCP SYN", "Source Port", "Destination Port"})
ipv4Layer := packet.Layer(layers.LayerTypeIPv4)
if ipv4Layer != nil {
ip, _ := ipv4Layer.(*layers.IPv4)
tableIP.Append([]string{ip.Protocol.String(), ip.SrcIP.String(), ip.DstIP.String()})
}
tcpLayer := packet.Layer(layers.LayerTypeTCP)
if tcpLayer != nil {
tcp, _ := tcpLayer.(*layers.TCP)
tableTCP.Append([]string{strconv.FormatBool(tcp.ACK), strconv.FormatBool(tcp.SYN), tcp.SrcPort.String(), tcp.DstPort.String()})
}
if ip_bool == 1 && ipv4Layer != nil {
tableIP.Render()
}
if tcp_bool == 1 && tcpLayer != nil {
tableTCP.Render()
}
}
}