forked from icodeface/grdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.go
119 lines (102 loc) · 2.27 KB
/
socket.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
package core
import (
"encoding/hex"
"fmt"
"github.com/icodeface/grdp/glog"
"github.com/icodeface/grdp/protocol/nla"
"github.com/icodeface/tls"
"net"
)
type SocketLayer struct {
conn net.Conn
tlsConn *tls.Conn
ntlm *nla.NTLMv2
}
func NewSocketLayer(conn net.Conn, ntlm *nla.NTLMv2) *SocketLayer {
l := &SocketLayer{
conn: conn,
tlsConn: nil,
ntlm: ntlm,
}
return l
}
func (s *SocketLayer) Read(b []byte) (n int, err error) {
if s.tlsConn != nil {
return s.tlsConn.Read(b)
}
return s.conn.Read(b)
}
func (s *SocketLayer) Write(b []byte) (n int, err error) {
if s.tlsConn != nil {
return s.tlsConn.Write(b)
}
return s.conn.Write(b)
}
func (s *SocketLayer) Close() error {
if s.tlsConn != nil {
err := s.tlsConn.Close()
if err != nil {
return err
}
}
return s.conn.Close()
}
func (s *SocketLayer) StartTLS() error {
glog.Info("StartTLS")
config := &tls.Config{
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS10,
MaxVersion: tls.VersionTLS13,
PreferServerCipherSuites: true,
}
s.tlsConn = tls.Client(s.conn, config)
return s.tlsConn.Handshake()
}
func (s *SocketLayer) StartNLA() error {
glog.Info("StartNLA")
err := s.StartTLS()
if err != nil {
glog.Info("start tls failed", err)
return err
}
req := nla.EncodeDERTRequest([]nla.Message{s.ntlm.GetNegotiateMessage()}, "", "")
_, err = s.Write(req)
if err != nil {
glog.Info("send NegotiateMessage", err)
return err
}
resp := make([]byte, 1024)
n, err := s.Read(resp)
if err != nil {
return fmt.Errorf("read %s", err)
}
return s.recvChallenge(resp[:n])
}
func (s *SocketLayer) recvChallenge(data []byte) error {
glog.Debug("recvChallenge", hex.EncodeToString(data))
tsreq, err := nla.DecodeDERTRequest(data)
if err != nil {
return err
}
// get pubkey
pubkey := ""
msg := s.ntlm.GetAuthenticateMessage(tsreq.NegoTokens[0].Data)
req := nla.EncodeDERTRequest([]nla.Message{msg}, "", pubkey)
_, err = s.Write(req)
if err != nil {
glog.Info("send AuthenticateMessage", err)
return err
}
//resp := make([]byte, 1024)
//_, err = s.Read(resp)
//if err != nil {
// return err
//}
//return s.recvPubKeyInc(resp)
fmt.Println("todo")
return nil
}
func (s *SocketLayer) recvPubKeyInc(data []byte) error {
// todo
return nil
}