-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
197 lines (168 loc) · 4.89 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
package main
import (
"fmt"
"os/exec"
"sync"
"gitlab.com/grey_scale/packetpacman/tests-and-analysis/clientsidetest.git/models"
"github.com/google/gopacket/layers"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"gitlab.com/grey_scale/packetpacman/tests-and-analysis/clientsidetest.git/MLInterface"
"gitlab.com/grey_scale/packetpacman/tests-and-analysis/clientsidetest.git/controllers"
)
func main() {
BUFFER_SIZE := 100
var wg sync.WaitGroup
wg.Add(1)
processPacketJobs := make(chan gopacket.Packet, BUFFER_SIZE)
processPacketlevel1Results := make(chan models.ProcessedAndRawPacketData, BUFFER_SIZE)
processPacketlevel2Results := make(chan []byte, BUFFER_SIZE)
level1Jobs := make(chan models.ProcessedAndRawPacketData, BUFFER_SIZE)
level1Results := make(chan MLInterface.PacketAnalysisResult, BUFFER_SIZE)
//networkJobs := make(chan []byte)
//networkResults := make(chan models.MLServerResponse)
go processWorker(processPacketJobs, processPacketlevel1Results, processPacketlevel2Results)
go processWorker(processPacketJobs, processPacketlevel1Results, processPacketlevel2Results)
go levelOneInterface(level1Jobs, level1Results)
go levelOneInterface(level1Jobs, level1Results)
//go networkWorker(networkJobs, networkResults)
//go networkWorker(networkJobs, networkResults)
handle, err := pcap.OpenLive("wlp2s0", 1600, true, pcap.BlockForever)
if err != nil {
panic(err)
}
go func() {
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
processPacketJobs <- packet
}
close(processPacketJobs)
}()
go func() {
for packetData := range processPacketlevel1Results {
level1Jobs <- packetData
}
close(level1Jobs)
}()
go func() {
for results := range level1Results {
fmt.Println(results)
}
}()
/* go func() {
for json := range processPacketlevel2Results {
networkJobs <- json
}
close(networkJobs)
}() */
/* go func() {
for resp := range networkResults {
//fmt.Println(resp)
processResponse(resp)
}
}() */
wg.Wait()
}
func processWorker(jobs <-chan gopacket.Packet, processPacketlevel1Results chan<- models.ProcessedAndRawPacketData, processPacketlevel2Results chan<- []byte) {
for packet := range jobs {
data := processPacket(packet)
processPacketlevel1Results <- data
//dataJSON, _ := json.Marshal(data)
//processPacketlevel2Results <- dataJSON
}
close(processPacketlevel1Results)
//close(processPacketlevel2Results)
}
func levelOneInterface(jobs <-chan models.ProcessedAndRawPacketData, results chan<- MLInterface.PacketAnalysisResult) {
for data := range jobs {
temp := MLInterface.PacketAnalysisResult{
SrcIP: data.ProcessedPacket.IPLayer.SrcIP,
SrcPrt: data.ProcessedPacket.TCPLayer.SrcPort,
DstPrt: data.ProcessedPacket.TCPLayer.DstPort,
Flag: 0,
}
results <- temp
}
close(results)
}
func networkWorker(jobs <-chan []byte, results chan<- models.MLServerResponse) {
for bytes := range jobs {
results <- controllers.SendPackets(bytes)
}
close(results)
}
func processPacket(packet gopacket.Packet) models.ProcessedAndRawPacketData {
var iplayerinterface models.IPLayer
var tcplayerinterface models.TCPLayer
iplayer := packet.Layer(layers.LayerTypeIPv4)
if iplayer != nil {
ip, _ := iplayer.(*layers.IPv4)
iplayerinterface = models.IPLayer{
SrcIP: ip.SrcIP.String(),
DstIP: ip.DstIP.String(),
Version: ip.Version,
IHL: ip.IHL,
TOS: ip.TOS,
Length: ip.Length,
ID: ip.Id,
Flags: ip.Flags.String(),
FragOffset: ip.FragOffset,
TTL: ip.TTL,
}
}
tcplayer := packet.Layer(layers.LayerTypeTCP)
if tcplayer != nil {
tcp, _ := tcplayer.(*layers.TCP)
tcplayerinterface = models.TCPLayer{
SrcPort: tcp.SrcPort.String(),
DstPort: tcp.DstPort.String(),
Seq: tcp.Seq,
Ack: tcp.Ack,
DataOffset: tcp.DataOffset,
Window: tcp.Window,
Checksum: tcp.Checksum,
Urgent: tcp.Urgent,
HeaderLength: len(tcp.Contents),
FlagFIN: tcp.FIN,
FlagSYN: tcp.SYN,
FlagRST: tcp.RST,
FlagPSH: tcp.PSH,
FlagACK: tcp.ACK,
FlagURG: tcp.URG,
FlagECE: tcp.ECE,
FlagCWR: tcp.CWR,
FlagNS: tcp.NS,
}
}
data := models.ProcessedAndRawPacketData{
ProcessedPacket: models.Packet{
IPLayer: iplayerinterface,
TCPLayer: tcplayerinterface,
},
RawPacket: packet.String(),
}
return data
}
func blockIP(IP string) {
app := "iptables"
arg0 := "-A"
arg1 := "INPUT"
arg2 := "-s"
arg3 := IP
arg4 := "-j"
arg5 := "DROP"
cmd := exec.Command(app, arg0, arg1, arg2, arg3, arg4, arg5)
err := cmd.Run()
if err != nil {
fmt.Println("command err", err)
}
}
func processResponse(resp models.MLServerResponse) {
//check for error
//check flag
//check ip
//if flag = 3 call blockIP
//else if flag = 2 inform user
//else if flag = 1 all cool
//log message from the server
}