-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (87 loc) · 2.56 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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/chifflier/nfqueue-go/nfqueue"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func real_callback(payload *nfqueue.Payload) int {
fmt.Println("Real callback")
fmt.Printf(" 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 TCP layer from this packet
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
fmt.Println("This is a TCP packet!")
// Get actual TCP data from this layer
tcp, _ := tcpLayer.(*layers.TCP)
fmt.Printf("From src port %d to dst port %d\n", tcp.SrcPort, tcp.DstPort)
}
if packet.ApplicationLayer() != nil {
body := packet.ApplicationLayer().Payload()
if len(body) > 0 {
bodyStr := string(body)
fmt.Println(bodyStr)
// modify payload of application layer
*packet.ApplicationLayer().(*gopacket.Payload) = []byte("GET / HTTP/1.1")
// if its tcp we need to tell it which network layer is being used
// to be able to handle multiple protocols we can add a if clause around this
packet.TransportLayer().(*layers.TCP).SetNetworkLayerForChecksum(packet.NetworkLayer())
buffer := gopacket.NewSerializeBuffer()
options := gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
// Serialize Packet to get raw bytes
if err := gopacket.SerializePacket(buffer, options, packet); err != nil {
log.Fatalln(err)
}
packetBytes := buffer.Bytes()
fmt.Println(packetBytes)
fmt.Println("-- ")
payload.SetVerdictModified(nfqueue.NF_ACCEPT, packetBytes)
return 0
}
}
// Iterate over all layers, printing out each layer type
// for _, layer := range packet.Layers() {
// fmt.Println("PACKET LAYER:", layer.LayerType())
// body := string(layer.LayerPayload())
// fmt.Println(body)
// if strings.HasPrefix(body, "GET") {
// fmt.Print("Modify Packet!!!!!")
// fmt.Println(hex.Dump(payload.Data))
// }
// }
fmt.Println("-- ")
payload.SetVerdictModified(nfqueue.NF_ACCEPT, payload.Data)
// payload.SetVerdict(nfqueue.NF_ACCEPT)
return 0
}
func main() {
q := new(nfqueue.Queue)
q.SetCallback(real_callback)
q.Init()
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.StopLoop()
}
}()
// XXX Drop privileges here
q.Loop()
q.DestroyQueue()
q.Close()
os.Exit(0)
}