-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_https_get_after_tcp3way_tlshandshake.go
327 lines (259 loc) · 11.5 KB
/
send_https_get_after_tcp3way_tlshandshake.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
package debugging
import (
"bytes"
"log"
p "github.com/ddddddO/packemon"
"golang.org/x/sys/unix"
)
// SendTCP3wayAndTLShandshake とほぼ同じ。違いは client から送る Application Data が http のフォーマット
func (dnw *debugNetworkInterface) SendHTTPSGetAfterTCP3wayAndTLShandshake(firsthopMACAddr [6]byte) error {
var srcPort uint16 = 0xa32f
var dstPort uint16 = 0x28cb // 10443
// var dstPort uint16 = 0x01bb // 443
// var srcIPAddr uint32 = 0xac184fcf // 172.23.242.78 / 旧PC
var srcIPAddr uint32 = 0xac163718 // 172.22.55.24 / 新PC
var dstIPAddr uint32 = 0xc0a80a6e // raspberry pi . raspberry pi 上で https-server 起動で、うまくいった
dstMACAddr := p.HardwareAddr(firsthopMACAddr)
srcMACAddr := p.HardwareAddr(dnw.Intf.HardwareAddr)
tcp := p.NewTCPSyn(srcPort, dstPort)
ipv4 := p.NewIPv4(p.IPv4_PROTO_TCP, srcIPAddr, dstIPAddr)
tcp.CalculateChecksum(ipv4)
ipv4.Data = tcp.Bytes()
ipv4.CalculateTotalLength()
ipv4.CalculateChecksum()
ethernetFrame := p.NewEthernetFrame(dstMACAddr, srcMACAddr, p.ETHER_TYPE_IPv4, ipv4.Bytes())
if err := dnw.Send(ethernetFrame); err != nil {
return err
}
tlsClientHello := p.NewTLSClientHello()
var tlsServerHello *p.TLSServerHello
var tlsClientKeyExchange *p.TLSClientKeyExchange
var tlsClientFinished []byte
var keyblock *p.KeyBlock
var clientSequence int
var master []byte
for {
log.Println("in loop")
recieved := make([]byte, 1500)
n, _, err := unix.Recvfrom(dnw.Socket, recieved, 0)
if err != nil {
if n == -1 {
continue
}
return err
}
ethernetFrame := p.ParsedEthernetFrame(recieved)
switch ethernetFrame.Header.Typ {
case p.ETHER_TYPE_IPv4:
ipv4 := p.ParsedIPv4(ethernetFrame.Data)
switch ipv4.Protocol {
case p.IPv4_PROTO_TCP:
tcp := p.ParsedTCP(ipv4.Data)
switch tcp.DstPort {
case srcPort: // synパケットの送信元ポート
if tcp.Flags == p.TCP_FLAGS_SYN_ACK {
log.Println("passive TCP_FLAGS_SYN_ACK")
// syn/ackを受け取ったのでack送信
tcp := p.NewTCPAck(srcPort, dstPort, tcp.Sequence, tcp.Acknowledgment)
ipv4 := p.NewIPv4(p.IPv4_PROTO_TCP, srcIPAddr, dstIPAddr)
tcp.CalculateChecksum(ipv4)
ipv4.Data = tcp.Bytes()
ipv4.CalculateTotalLength()
ipv4.CalculateChecksum()
ethernetFrame := p.NewEthernetFrame(dstMACAddr, srcMACAddr, p.ETHER_TYPE_IPv4, ipv4.Bytes())
if err := dnw.Send(ethernetFrame); err != nil {
return err
}
// ここで TLS Client Helloを送る
if err := dnw.SendTLSClientHello(tlsClientHello, srcPort, dstPort, srcIPAddr, dstIPAddr, firsthopMACAddr, tcp.Sequence, tcp.Acknowledgment); err != nil {
return err
}
continue
}
// ここでのServer Hello(ack)受信後の処理はうまくいった
// 関連: https://github.com/ddddddO/packemon/issues/64
// Wireshark 見るに、このackのパケットを後続のpsh/ackのパケット2つのtcp data部分をつなげて、ServerHello/Certificate/ServerHelloDone みたい
// 上記リンクの、1パケットパターンのパケットと2パケットパターンのパケット見比べて確認した
if tcp.Flags == p.TCP_FLAGS_ACK {
log.Println("passive TCP_FLAGS_ACK")
tlsHandshakeType := []byte{tcp.Data[5]}
tlsContentType := []byte{tcp.Data[0]}
// ServerHello/Certificate/ServerHelloDone がセグメント分割されたパケットで届くことが多々あるため、このブロック内で連続して受信している
// TODO: (10)443ポートがdstで絞った方がいいかも
if bytes.Equal(tlsHandshakeType, []byte{0x02}) && bytes.Equal(tlsContentType, []byte{p.TLS_CONTENT_TYPE_HANDSHAKE}) {
log.Println("[ACK] passive TLS ServerHello(with Certificate/ServerHelloDone ?)")
log.Printf("\tTCP data: %x\n", tcp.Data)
for {
recieved := make([]byte, 1500)
n, _, err := unix.Recvfrom(dnw.Socket, recieved, 0)
if err != nil {
if n == -1 {
continue
}
return err
}
eth := p.ParsedEthernetFrame(recieved)
ip := p.ParsedIPv4(eth.Data)
t := p.ParsedTCP(ip.Data)
if t.Flags == p.TCP_FLAGS_PSH_ACK {
log.Println("[PSH_ACK] passive TLS ServerHello(with Certificate/ServerHelloDone !!!???)")
// tcp data の末尾の0パディングを取り除く
tmp1 := tcp.Data
for offset := len(tcp.Data) - 2; bytes.Equal(tcp.Data[offset:offset+2], []byte{00, 00}); offset -= 2 {
tmp1 = tmp1[:len(tmp1)-2]
}
tmp2 := t.Data
for offset := len(t.Data) - 4; bytes.Equal(t.Data[offset:offset+4], []byte{00, 00, 00, 00}); offset -= 4 {
tmp2 = tmp2[:len(tmp2)-4]
}
mergedTCPData := append(tmp1, tmp2...)
tlsServerHello = p.ParsedTLSServerHello(mergedTCPData)
if err := tlsServerHello.Certificate.Validate(); err != nil {
return err
}
// ackを返し
tcp := p.NewTCPAck(srcPort, dstPort, t.Sequence, t.Acknowledgment)
ipv4 := p.NewIPv4(p.IPv4_PROTO_TCP, srcIPAddr, dstIPAddr)
tcp.CalculateChecksum(ipv4)
ipv4.Data = tcp.Bytes()
ipv4.CalculateTotalLength()
ipv4.CalculateChecksum()
ethernetFrame := p.NewEthernetFrame(dstMACAddr, srcMACAddr, p.ETHER_TYPE_IPv4, ipv4.Bytes())
if err := dnw.Send(ethernetFrame); err != nil {
return err
}
// さらに ClientKeyExchange や Finished などを返す
tlsClientKeyExchange, keyblock, clientSequence, master, tlsClientFinished = p.NewTLSClientKeyExchangeAndChangeCipherSpecAndFinished(
tlsClientHello,
tlsServerHello,
)
tcp = p.NewTCPWithData(srcPort, dstPort, tlsClientKeyExchange.Bytes(), tcp.Sequence, tcp.Acknowledgment)
ipv4 = p.NewIPv4(p.IPv4_PROTO_TCP, srcIPAddr, dstIPAddr)
tcp.CalculateChecksum(ipv4)
ipv4.Data = tcp.Bytes()
ipv4.CalculateTotalLength()
ipv4.CalculateChecksum()
ethernetFrame = p.NewEthernetFrame(dstMACAddr, srcMACAddr, p.ETHER_TYPE_IPv4, ipv4.Bytes())
if err := dnw.Send(ethernetFrame); err != nil {
return err
}
break
}
}
continue
}
continue
}
if tcp.Flags == p.TCP_FLAGS_PSH_ACK {
log.Println("passive TCP_FLAGS_PSH_ACK")
tlsHandshakeType := []byte{tcp.Data[5]}
tlsContentType := []byte{tcp.Data[0]}
log.Printf("\ttcp data: %x\n\n", tcp.Data[0:10])
log.Printf("\ttlsHandshakeType: %x\n", tlsHandshakeType)
log.Printf("\ttlsContentType: %x\n", tlsContentType)
// ServerHelloを受信
// TODO: (10)443ポートがdstで絞った方がいいかも
// SeverHello(0x02)
if bytes.Equal(tlsHandshakeType, []byte{0x02}) && bytes.Equal(tlsContentType, []byte{p.TLS_CONTENT_TYPE_HANDSHAKE}) {
log.Printf("tlsHandshakeType: %x\n", tlsHandshakeType)
log.Println("passive TLS ServerHello")
// TODO: server から、ServerHello/Certificate/ServerHelloDone でひとまとまりで返ってくればパースできるが、ServerHello と Certificate/ServerHelloDone がわかれて返ってくることがある。それで失敗してるよう?
// 分かれてるとき、ServerHell はフラグが ACK だけど、分かれてないとき PSH/ACK
tlsServerHello = p.ParsedTLSServerHello(tcp.Data)
if err := tlsServerHello.Certificate.Validate(); err != nil {
return err
}
// ackを返し
tcp := p.NewTCPAck(srcPort, dstPort, tcp.Sequence, tcp.Acknowledgment)
ipv4 := p.NewIPv4(p.IPv4_PROTO_TCP, srcIPAddr, dstIPAddr)
tcp.CalculateChecksum(ipv4)
ipv4.Data = tcp.Bytes()
ipv4.CalculateTotalLength()
ipv4.CalculateChecksum()
ethernetFrame := p.NewEthernetFrame(dstMACAddr, srcMACAddr, p.ETHER_TYPE_IPv4, ipv4.Bytes())
if err := dnw.Send(ethernetFrame); err != nil {
return err
}
// さらに ClientKeyExchange や Finished などを返す
tlsClientKeyExchange, keyblock, clientSequence, master, tlsClientFinished = p.NewTLSClientKeyExchangeAndChangeCipherSpecAndFinished(
tlsClientHello,
tlsServerHello,
)
tcp = p.NewTCPWithData(srcPort, dstPort, tlsClientKeyExchange.Bytes(), tcp.Sequence, tcp.Acknowledgment)
ipv4 = p.NewIPv4(p.IPv4_PROTO_TCP, srcIPAddr, dstIPAddr)
tcp.CalculateChecksum(ipv4)
ipv4.Data = tcp.Bytes()
ipv4.CalculateTotalLength()
ipv4.CalculateChecksum()
ethernetFrame = p.NewEthernetFrame(dstMACAddr, srcMACAddr, p.ETHER_TYPE_IPv4, ipv4.Bytes())
if err := dnw.Send(ethernetFrame); err != nil {
return err
}
continue
}
// ChangeCipherSpec/Finishedを受信
// TODO: (10)443ポートがdstとかもっと絞った方がいいかも
if bytes.Equal(tlsContentType, []byte{p.TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC}) {
log.Println("recieved ChangeCipherSpec/Finished !!")
verifingData := &p.ForVerifing{
Master: master,
ClientHello: tlsClientHello,
ServerHello: tlsServerHello,
ClientKeyExchange: tlsClientKeyExchange.ClientKeyExchange,
ClientFinished: tlsClientFinished,
}
tlsChangeCiperSpecAndFinished := p.ParsedTLSChangeCipherSpecAndFinished(tcp.Data, keyblock, clientSequence, verifingData)
log.Printf("\tChangeCipherSpecProtocol.RecordLayer.ContentType: %x\n", tlsChangeCiperSpecAndFinished.ChangeCipherSpecProtocol.RecordLayer.ContentType)
log.Printf("\tFinished.RawEncrypted:\n%x\n", tlsChangeCiperSpecAndFinished.Finished.RawEncrypted)
// TODO: 上のParsed内でserverからきたFinishedの検証してるけど、この辺りに持ってきた方がいいかも
// Finishedの検証が成功したので、以降からApplicationDataをやりとり
log.Println("Send TLS Application DATA")
clientSequence++
httpGetReq := p.NewHTTP()
tlsApplicationData := p.NewTLSApplicationData(httpGetReq.Bytes(), keyblock, clientSequence)
tcp = p.NewTCPWithData(srcPort, dstPort, tlsApplicationData, tcp.Acknowledgment, tcp.Sequence)
ipv4 = p.NewIPv4(p.IPv4_PROTO_TCP, srcIPAddr, dstIPAddr)
tcp.CalculateChecksum(ipv4)
ipv4.Data = tcp.Bytes()
ipv4.CalculateTotalLength()
ipv4.CalculateChecksum()
ethernetFrame = p.NewEthernetFrame(dstMACAddr, srcMACAddr, p.ETHER_TYPE_IPv4, ipv4.Bytes())
if err := dnw.Send(ethernetFrame); err != nil {
return err
}
continue
}
}
if tcp.Flags == p.TCP_FLAGS_FIN_ACK {
log.Println("passive TCP_FLAGS_FIN_ACK")
// それにack
tcp := p.NewTCPAck(srcPort, dstPort, tcp.Sequence, tcp.Acknowledgment)
ipv4 := p.NewIPv4(p.IPv4_PROTO_TCP, srcIPAddr, dstIPAddr)
tcp.CalculateChecksum(ipv4)
ipv4.Data = tcp.Bytes()
ipv4.CalculateTotalLength()
ipv4.CalculateChecksum()
ethernetFrame := p.NewEthernetFrame(dstMACAddr, srcMACAddr, p.ETHER_TYPE_IPv4, ipv4.Bytes())
if err := dnw.Send(ethernetFrame); err != nil {
return err
}
return nil
}
// dnw.PassiveCh <- &p.Passive{
// EthernetFrame: ethernetFrame,
// IPv4: ipv4,
// TCP: tcp,
// }
continue
default:
// dnw.PassiveCh <- &p.Passive{
// EthernetFrame: ethernetFrame,
// IPv4: ipv4,
// TCP: tcp,
// }
}
}
}
}
return nil
}