-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
185 lines (152 loc) · 3.83 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
// started with code from: https://github.com/chifflier/nfqueue-go/blob/master/nfqueue/test_nfqueue_gopacket/test_nfqueue.go
// how to install : go install . # use it inside mane_go_nfqueue folder
// net config : sudo ./netconfig.sh
// how to run : sudo $GOPATH/bin/mane_go_nfqueue
package main
import (
// "encoding/hex"
"fmt"
"mane_go_nfqueue/nfqueue"
"os"
"os/signal"
"syscall"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"net"
// "reflect"
"bufio"
"bytes"
"net/http"
// "strings"
)
func real_callback(payload *nfqueue.Payload) int {
// global variables
var srcIP net.IP
var dstIP net.IP
// var srcPort layers.TCPPort
// var dstPort layers.TCPPort
fmt.Printf("PKT [%03d] ", payload.Id)
// fmt.Printf("------------------------------------------------------------------------\n id: %d\n", payload.Id)
// fmt.Println(hex.Dump(payload.Data))
// Decode a packet
packet := gopacket.NewPacket(payload.Data, layers.LayerTypeIPv4, gopacket.Default)
// Get the IP layer from this packet
if ipLayer := packet.Layer(layers.LayerTypeIPv4); ipLayer != nil {
// Get actual IP data from this layer
ip, _ := ipLayer.(*layers.IPv4)
srcIP = ip.SrcIP
dstIP = ip.DstIP
fmt.Printf("%15s > %-15s ", srcIP, dstIP)
}
if icmpLayer := packet.Layer(layers.LayerTypeICMPv4); icmpLayer != nil {
fmt.Print("\t√\tICMP\n")
payload.SetVerdict(nfqueue.NF_ACCEPT)
return 0
}
if arpLayer := packet.Layer(layers.LayerTypeARP); arpLayer != nil {
fmt.Print("\t√\tARP\n")
payload.SetVerdict(nfqueue.NF_ACCEPT)
return 0
}
if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer != nil {
fmt.Print("\t√\tUDP\n")
payload.SetVerdict(nfqueue.NF_ACCEPT)
return 0
}
// Get the TCP layer from this packet
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
// Get actual TCP data from this layer
tcp, _ := tcpLayer.(*layers.TCP)
// srcPort = tcp.SrcPort
// dstPort = tcp.DstPort
// fmt.Printf("%s:%d > %s:%d ", srcIP, srcPort, dstIP, dstPort)
// fmt.Printf("%15s > %-15s ", srcIP, dstIP)
//
// if there is payload...
// try to read it and see if it is an http request
//
if len(tcp.Payload) != 0 {
reader := bufio.NewReader(bytes.NewReader(tcp.Payload))
httpReq, err := http.ReadRequest(reader)
if err != nil {
//
// if the Payload could not be parsed as an HTTP request
// forward the packet...
//
//fmt.Print(err)
fmt.Print("\t√\tTCP ")
payload.SetVerdict(nfqueue.NF_ACCEPT)
if tcp.SYN {
fmt.Print("[SYN] ")
}
if tcp.ACK {
fmt.Print("[ACK] ")
}
if tcp.FIN {
fmt.Print("[FIN] ")
}
} else {
//
// if the payload contained http request
//
fmt.Print("\t√\tTCP ")
payload.SetVerdict(nfqueue.NF_ACCEPT)
if tcp.SYN {
fmt.Print("[SYN] ")
}
if tcp.ACK {
fmt.Print("[ACK] ")
}
if tcp.FIN {
fmt.Print("[FIN] ")
}
fmt.Printf("[HTTP] REQUEST %s", httpReq.URL.String())
}
} else {
//
// if there was no payload
// forward the packet...
//
fmt.Print("\t√\tTCP ")
if tcp.SYN {
fmt.Print("[SYN] ")
}
if tcp.ACK {
fmt.Print("[ACK] ")
}
if tcp.FIN {
fmt.Print("[FIN] ")
}
payload.SetVerdict(nfqueue.NF_ACCEPT)
}
fmt.Print("\n")
return 0
}
fmt.Printf("\t√\tOTHER\n")
payload.SetVerdict(nfqueue.NF_ACCEPT)
return 0
}
func main() {
q := new(nfqueue.Queue)
q.SetCallback(real_callback)
q.Init()
defer q.Close()
q.Unbind(syscall.AF_INET)
q.Bind(syscall.AF_INET)
q.CreateQueue(0)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
// sig is a ^C, handle it
_ = sig
q.Close()
os.Exit(0)
// XXX we should break gracefully from loop
}
}()
// XXX Drop privileges here
// XXX this should be the loop
q.TryRun()
fmt.Printf("exit...\n")
}