forked from macronut/godivert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.go
203 lines (163 loc) · 4.91 KB
/
packet.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
package godivert
import (
"fmt"
"github.com/williamfhe/godivert/header"
"net"
)
// Represents a packet
type Packet struct {
Raw []byte
Addr *WinDivertAddress
PacketLen uint
IpHdr header.IPHeader
NextHeader header.ProtocolHeader
ipVersion int
hdrLen int
nextHeaderType uint8
parsed bool
}
// Parse the packet's headers
func (p *Packet) ParseHeaders() {
p.ipVersion = int(p.Raw[0] >> 4)
if p.ipVersion == 4 {
p.hdrLen = int((p.Raw[0] & 0xf) << 2)
p.nextHeaderType = p.Raw[9]
p.IpHdr = header.NewIPv4Header(p.Raw)
} else {
p.hdrLen = 40
p.nextHeaderType = p.Raw[6]
p.IpHdr = header.NewIPv6Header(p.Raw)
}
switch p.nextHeaderType {
case header.ICMPv4:
p.NextHeader = header.NewICMPv4Header(p.Raw[p.hdrLen : p.hdrLen+header.ICMPv4HeaderLen])
case header.TCP:
p.NextHeader = header.NewTCPHeader(p.Raw[p.hdrLen:])
case header.UDP:
p.NextHeader = header.NewUDPHeader(p.Raw[p.hdrLen : p.hdrLen+header.UDPHeaderLen])
case header.ICMPv6:
p.NextHeader = header.NewICMPv6Header(p.Raw[p.hdrLen : p.hdrLen+header.ICMPv6HeaderLen])
default:
// Protocol not implemented
p.NextHeader = nil
}
p.parsed = true
}
func (p *Packet) String() string {
p.VerifyParsed()
nextHeaderType := p.NextHeaderType()
return fmt.Sprintf("Packet {\n"+
"\tIPHeader=%v\n"+
"\tNextHeaderType=(%d)->%s\n"+
"\tNextHeader: %v\n"+
"\tWinDivertAddr=%v\n"+
"\tRawData=%v\n"+
"}",
p.IpHdr, nextHeaderType, header.ProtocolName(nextHeaderType), p.NextHeader, p.Addr, p.Raw)
}
// Returns the version of the IP protocol
// Shortcut for ipHdr.Version()
func (p *Packet) IpVersion() int {
return p.ipVersion
}
// Returns the IP Protocol number of the next Header
// https://en.wikipedia.org/wiki/List_of_IP_protocol_numbers
func (p *Packet) NextHeaderType() uint8 {
p.VerifyParsed()
return p.nextHeaderType
}
// Returns the source IP of the packet
// Shortcut for IpHdr.SrcIP()
func (p *Packet) SrcIP() net.IP {
p.VerifyParsed()
return p.IpHdr.SrcIP()
}
// Sets the source IP of the packet
// Shortcut for IpHdr.SetSrcIP()
func (p *Packet) SetSrcIP(ip net.IP) {
p.VerifyParsed()
p.IpHdr.SetSrcIP(ip)
}
// Returns the destination IP of the packet
// Shortcut for IpHdr.DstIP()
func (p *Packet) DstIP() net.IP {
p.VerifyParsed()
return p.IpHdr.DstIP()
}
// Sets the destination IP of the packet
// Shortcut for IpHdr.SetDstIP()
func (p *Packet) SetDstIP(ip net.IP) {
p.VerifyParsed()
p.IpHdr.SetDstIP(ip)
}
// Returns the source port of the packet
// Shortcut for NextHeader.SrcPort()
func (p *Packet) SrcPort() (uint16, error) {
p.VerifyParsed()
if p.NextHeader == nil {
return 0, fmt.Errorf("cannot get source port on protocolID=%d, protocol not implemented", p.nextHeaderType)
}
return p.NextHeader.SrcPort()
}
// Sets the source port of the packet
// Shortcut for NextHeader.SetSrcPort()
func (p *Packet) SetSrcPort(port uint16) error {
p.VerifyParsed()
if p.NextHeader == nil {
return fmt.Errorf("cannot change source port on protocolID=%d, protocol not implemented", p.nextHeaderType)
}
return p.NextHeader.SetSrcPort(port)
}
// Returns the destination port of the packet
// Shortcut for NextHeader.DstPort()
func (p *Packet) DstPort() (uint16, error) {
p.VerifyParsed()
if p.NextHeader == nil {
return 0, fmt.Errorf("cannot change get port on protocolID=%d, protocol not implemented", p.nextHeaderType)
}
return p.NextHeader.DstPort()
}
// Sets the destination port of the packet
// Shortcut for NextHeader.SetDstPort()
func (p *Packet) SetDstPort(port uint16) error {
p.VerifyParsed()
if p.NextHeader == nil {
return fmt.Errorf("cannot change destination port on protocolID=%d, protocol not implemented", p.nextHeaderType)
}
return p.NextHeader.SetDstPort(port)
}
// Returns the name of the protocol
func (p *Packet) NextHeaderProtocolName() string {
return header.ProtocolName(p.NextHeaderType())
}
// Inject the packet on the Network Stack
// If the packet has been modified calls WinDivertHelperCalcChecksum to get a new checksum
func (p *Packet) Send(wd *WinDivertHandle) (uint, error) {
if p.parsed && (p.IpHdr.NeedNewChecksum() || p.NextHeader != nil && p.NextHeader.NeedNewChecksum()) {
wd.HelperCalcChecksum(p)
}
return wd.Send(p)
}
// Recalculate the packet's checksum
// Shortcut for WinDivertHelperCalcChecksum
func (p *Packet) CalcNewChecksum(wd *WinDivertHandle) {
wd.HelperCalcChecksum(p)
}
// Check if the headers have already been parsed and call ParseHeaders() if not
func (p *Packet) VerifyParsed() {
if !p.parsed {
p.ParseHeaders()
}
}
// Returns the Direction of the packet
// WinDivertDirectionInbound (true) for inbound Packets
// WinDivertDirectionOutbound (false) for outbound packets
// Shortcut for Addr.Direction()
func (p *Packet) Direction() Direction {
return p.Addr.Direction()
}
// Check the packet with the filter
// Returns true if the packet matches the filter
func (p *Packet) EvalFilter(filter string) (bool, error) {
return HelperEvalFilter(p, filter)
}