-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.go
397 lines (330 loc) · 10.4 KB
/
tcp.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package main
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"log"
"math/rand"
"net"
"os"
"strconv"
"strings"
)
const (
FlagFIN = iota // 0 0000 0001
FlagSYN // 0 0000 0010
FlagRST // 0 0000 0100
FlagPSH // 0 0000 1000
FlagACK // 0 0001 0000
FlagURG // 0 0010 0000
FlagECE // 0 0100 0000
FlagCWR // 0 1000 0000
FlagNS // 1 0000 0000
)
//TCPPacket structure
type TCPPacket struct {
SrcPort uint16
DestPort uint16
SeqNum uint32
AckNum uint32
DataOffset uint8 // 4 bits
Flags uint16 // 3 bits (reserved) + 9 bits
WindowSize uint16
Checksum uint16
Urgent uint16
Options []TCPOption
Data []byte
}
type TCPOption struct {
Kind uint8
Length uint8
Data []byte
}
func NewTCPPacket(data []byte) *TCPPacket {
var packet TCPPacket
reader := bytes.NewReader(data)
binary.Read(reader, binary.BigEndian, &packet.SrcPort)
binary.Read(reader, binary.BigEndian, &packet.DestPort)
binary.Read(reader, binary.BigEndian, &packet.SeqNum)
binary.Read(reader, binary.BigEndian, &packet.AckNum)
var field uint16
binary.Read(reader, binary.BigEndian, &field)
fmt.Printf("decode flags %x %08b %08b\n", field, uint8(field>>8), uint8(field))
packet.DataOffset = uint8(field >> 12)
packet.Flags = field & 0xfff
binary.Read(reader, binary.BigEndian, &packet.WindowSize)
binary.Read(reader, binary.BigEndian, &packet.Checksum)
binary.Read(reader, binary.BigEndian, &packet.Urgent)
if packet.DataOffset > 5 {
}
headerLen := packet.DataOffset * 4
if len(data) > int(headerLen) {
packet.Data = data[headerLen:]
}
return &packet
}
func (packet *TCPPacket) SetFlag(flag uint8) {
packet.Flags |= (1 << flag)
}
func (packet *TCPPacket) ClearFlag(flag uint8) {
packet.Flags &^= (1 << flag)
}
func (packet *TCPPacket) HasFlag(flag uint8) bool {
return (packet.Flags>>flag)&0x1 != 0
}
func (packet *TCPPacket) Marshall(srcIP, dstIP string) []byte {
packet.Checksum = 0
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, packet.SrcPort)
binary.Write(buffer, binary.BigEndian, packet.DestPort)
binary.Write(buffer, binary.BigEndian, packet.SeqNum)
binary.Write(buffer, binary.BigEndian, packet.AckNum)
binary.Write(buffer, binary.BigEndian, uint16(packet.DataOffset)<<12|uint16(packet.Flags))
binary.Write(buffer, binary.BigEndian, packet.WindowSize)
binary.Write(buffer, binary.BigEndian, packet.Checksum)
binary.Write(buffer, binary.BigEndian, packet.Urgent)
for _, option := range packet.Options {
binary.Write(buffer, binary.BigEndian, option.Kind)
if option.Length > 1 {
binary.Write(buffer, binary.BigEndian, option.Length)
binary.Write(buffer, binary.BigEndian, option.Data)
}
}
if packet.Data != nil {
binary.Write(buffer, binary.BigEndian, packet.Data)
}
output := buffer.Bytes()
checksum := checksum(output, srcIP, dstIP)
fmt.Printf("Checksum %d / 0x%x\n", checksum, checksum)
output[16] = byte(checksum >> 8)
output[17] = byte(checksum)
return output
}
func (t *TCPPacket) String() string {
return strings.Join([]string{
"Source port: " + strconv.Itoa(int(t.SrcPort)),
"Dest port:" + strconv.Itoa(int(t.DestPort)),
"Sequence:" + strconv.Itoa(int(t.SeqNum)),
"Acknowledgement:" + strconv.Itoa(int(t.AckNum)),
"Header len:" + strconv.Itoa(int(t.DataOffset)),
"Flags:" + fmt.Sprintf("%b", uint8(t.Flags)),
"Flag ns:" + strconv.FormatBool(t.HasFlag(FlagNS)),
"Flag crw:" + strconv.FormatBool(t.HasFlag(FlagCWR)),
"Flag ece:" + strconv.FormatBool(t.HasFlag(FlagECE)),
"Flag urg:" + strconv.FormatBool(t.HasFlag(FlagURG)),
"Flag ack:" + strconv.FormatBool(t.HasFlag(FlagACK)),
"Flag psh:" + strconv.FormatBool(t.HasFlag(FlagPSH)),
"Flag rst:" + strconv.FormatBool(t.HasFlag(FlagRST)),
"Flag syn:" + strconv.FormatBool(t.HasFlag(FlagSYN)),
"Flag fin:" + strconv.FormatBool(t.HasFlag(FlagFIN)),
"Window size:" + strconv.Itoa(int(t.WindowSize)),
"Checksum:" + strconv.Itoa(int(t.Checksum)),
"Urgent:" + strconv.Itoa(int(t.Urgent)),
"Data:" + string(t.Data),
}, "\n")
}
func checksum(tcpData []byte, srcIP, dstIP string) uint16 {
length := uint16(len(tcpData))
srcIPBytes := ipToBytes(srcIP)
dstIPBytes := ipToBytes(dstIP)
// pseudoHeader := []byte{
// srcIPBytes[0], srcIPBytes[1], srcIPBytes[2], srcIPBytes[3],
// dstIPBytes[0], dstIPBytes[1], dstIPBytes[2], dstIPBytes[3],
// 0, // zero
// 6, // protocol number (6 == TCP)
// byte(length >> 8), byte(length), // TCP length (16 bits), not inc pseudo header
// }
// fullOutput := make([]byte, 0, len(pseudoHeader)+len(tcpData))
// fullOutput = append(fullOutput, pseudoHeader...)
// fullOutput = append(fullOutput, tcpData...)
// dump(fullOutput)
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, srcIPBytes)
binary.Write(buffer, binary.BigEndian, dstIPBytes)
binary.Write(buffer, binary.BigEndian, byte(0))
binary.Write(buffer, binary.BigEndian, byte(6))
binary.Write(buffer, binary.BigEndian, length)
binary.Write(buffer, binary.BigEndian, tcpData)
fullOutput := buffer.Bytes()
fullLength := len(fullOutput)
var word uint16
var sum uint32
for i := 0; i+1 < fullLength; i += 2 {
word = uint16(fullOutput[i])<<8 | uint16(fullOutput[i+1])
sum += uint32(word)
}
if fullLength%2 != 0 {
sum += uint32(fullOutput[fullLength-1])
}
for (sum >> 16) > 0 {
sum = (sum & 0xffff) + (sum >> 16)
}
// Bitwise complement
return uint16(^sum)
}
func ipToBytes(ip string) [4]byte {
parts := strings.Split(ip, ".")
b0, _ := strconv.Atoi(parts[0])
b1, _ := strconv.Atoi(parts[1])
b2, _ := strconv.Atoi(parts[2])
b3, _ := strconv.Atoi(parts[3])
return [4]byte{byte(b0), byte(b1), byte(b2), byte(b3)}
}
func Flag(set uint8, flag uint8, value bool) uint8 {
if value {
return set | (1 << flag)
}
return set &^ (1 << flag)
}
func main() {
if len(os.Args) < 2 {
log.Fatalln("Usage : sudo TeaCP <dest address> <port>")
}
destIP := os.Args[1]
port, _ := strconv.Atoi(os.Args[2])
destPort := uint16(port)
fmt.Println("Use", destIP, destPort)
srcIP, _ := net.ResolveIPAddr("ip4", "10.12.0.1")
dstIP, _ := net.ResolveIPAddr("ip4", destIP)
_, err := DialTeaCP(srcIP, dstIP, 8080)
if err != nil {
log.Fatalln("Error while dialing TeaCP connection ", err)
}
}
func main2() {
if len(os.Args) < 2 {
log.Fatalln("Usage : sudo TeaCP <dest address> <port>")
}
destIP := os.Args[1]
port, _ := strconv.Atoi(os.Args[2])
destPort := uint16(port)
fmt.Println("Use", destIP, destPort)
srcIP, _ := net.ResolveIPAddr("ip4", "10.12.0.1")
dstIP, _ := net.ResolveIPAddr("ip4", destIP)
sourcePort := uint16(55897)
conn := NewTunIPConn(srcIP, dstIP)
err := conn.Open()
if err != nil {
log.Fatalln("Error while opening opening TunIPConn", err)
}
defer conn.Close()
fmt.Println("Interface opened. Pause while setup.")
fmt.Println("Try: sudo ifconfig tun11 10.12.0.2 10.12.0.1")
fmt.Print("Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
fmt.Println("GO !")
packet := &TCPPacket{}
packet.SrcPort = sourcePort
packet.DestPort = destPort
packet.DataOffset = uint8(5)
packet.SetFlag(FlagSYN)
packet.SeqNum = uint32(rand.Int())
packet.AckNum = 0
packet.WindowSize = uint16(4096 * 8)
bytes := packet.Marshall("10.12.0.1", dstIP.IP.String())
fmt.Println("TCP Output len:", len(bytes))
length, err := conn.Write(bytes)
if err != nil {
log.Fatalln("Error while sending SYN TCP Packet:", err)
}
fmt.Printf("%d bytes sent (TCP Seq:%d, Ack:%d)\n", length, packet.SeqNum, packet.AckNum)
//Receive SYN+ACK
buffer := make([]byte, 4096*16)
length, err = conn.Read(buffer)
if err != nil {
log.Fatalln("Error while reading tun interface", err)
}
fmt.Println(length, "bytes read")
//dump(buffer[:length])
responseTcp := NewTCPPacket(buffer[:length])
fmt.Println("")
fmt.Println("TCP Packet")
fmt.Println(responseTcp.String())
//SEND ACK
packet = &TCPPacket{}
packet.SrcPort = sourcePort
packet.DestPort = destPort
packet.DataOffset = uint8(5)
packet.SetFlag(FlagACK)
packet.SeqNum = responseTcp.AckNum
packet.AckNum = responseTcp.SeqNum + 1
packet.WindowSize = uint16(4096 * 8)
bytes = packet.Marshall("10.12.0.1", dstIP.IP.String())
fmt.Println("TCP Output len:", len(bytes))
length, err = conn.Write(bytes)
if err != nil {
log.Fatalln("Error while sending SYN TCP Packet:", err)
}
fmt.Printf("%d bytes sent (TCP Seq:%d, Ack:%d)\n", length, packet.SeqNum, packet.AckNum)
lastAck := packet.AckNum
//Send REQUEST
packet = &TCPPacket{}
packet.SrcPort = sourcePort
packet.DestPort = destPort
packet.DataOffset = uint8(5)
packet.SetFlag(FlagACK)
packet.SetFlag(FlagPSH)
packet.SeqNum = responseTcp.AckNum
packet.AckNum = responseTcp.SeqNum + 1
packet.WindowSize = uint16(4096 * 8)
packet.Data = []byte("GET /hello HTTP/1.1\r\nHost: 192.168.10.170:8080\r\n\r\n")
bytes = packet.Marshall("10.12.0.1", dstIP.IP.String())
fmt.Println("TCP Output len:", len(bytes))
length, err = conn.Write(bytes)
if err != nil {
log.Fatalln("Error while sending SYN TCP Packet:", err)
}
fmt.Printf("%d bytes sent (TCP Seq:%d, Ack:%d)\n", length, packet.SeqNum, packet.AckNum)
//Receive response
for {
buffer = make([]byte, 4096*16)
length, err = conn.Read(buffer)
if err != nil {
log.Fatalln("Error while reading tun interface", err)
}
fmt.Println(length, "bytes read")
//dump(buffer[:length])
responseTcp = NewTCPPacket(buffer[:length])
fmt.Println("")
fmt.Println("TCP Packet")
fmt.Println(responseTcp.String())
if lastAck == responseTcp.SeqNum+uint32(len(responseTcp.Data)) {
continue
}
//SEND ACK
packet = &TCPPacket{}
packet.SrcPort = sourcePort
packet.DestPort = destPort
packet.DataOffset = uint8(5)
packet.SetFlag(FlagACK)
packet.SeqNum = responseTcp.AckNum
packet.AckNum = responseTcp.SeqNum + uint32(len(responseTcp.Data))
packet.WindowSize = uint16(4096 * 8)
bytes = packet.Marshall("10.12.0.1", dstIP.IP.String())
fmt.Println("TCP Output len:", len(bytes))
length, err = conn.Write(bytes)
if err != nil {
log.Fatalln("Error while sending SYN TCP Packet:", err)
}
fmt.Printf("%d bytes sent (TCP Seq:%d, Ack:%d)\n", length, packet.SeqNum, packet.AckNum)
lastAck = packet.AckNum
}
fmt.Print("Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
func dump(bytes []byte) {
linesCount := 1
for i := 0; i < len(bytes); i++ {
if (i % 4) == 0 {
fmt.Printf("%2d - ", linesCount)
linesCount++
}
fmt.Printf("%08b", bytes[i])
if (i+1)%4 == 0 {
fmt.Printf("\n")
} else {
fmt.Printf(" ")
}
}
}