forked from iamacarpet/ssh-bastion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforward.go
executable file
·249 lines (219 loc) · 8.68 KB
/
forward.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
package main
import (
"io"
"fmt"
"log"
"net"
"sync"
"time"
"bytes"
"io/ioutil"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/terminal"
)
type rw struct {
io.Reader
io.Writer
}
func (s *SSHServer) SessionForward(startTime time.Time, sshConn *ssh.ServerConn, newChannel ssh.NewChannel, chans <-chan ssh.NewChannel) {
rawsesschan, sessReqs, err := newChannel.Accept()
if err != nil {
log.Printf("Unable to Accept Session, closing connection...")
sshConn.Close()
return
}
defer sshConn.Close()
sesschan := NewLogChannel(startTime, rawsesschan, sshConn.User())
// Handle all incoming channel requests
go func() {
for newChannel = range chans {
if newChannel == nil {
return
}
newChannel.Reject(ssh.Prohibited, "remote server denied channel request")
continue
}
}()
// Proxy the channel and its requests
var agentForwarding bool = false
maskedReqs := make(chan *ssh.Request, 5)
go func() {
// For the pty-req and shell request types, we have to reply to those right away.
// This is for PuTTy compatibility - if we don't, it won't allow any input.
// We also have to change them to WantReply = false,
// or a double reply will cause a fatal error client side.
for req := range sessReqs {
sesschan.LogRequest(req)
if req.Type == "[email protected]" {
agentForwarding = true
if req.WantReply {
req.Reply(true, []byte{})
}
continue
} else if (req.Type == "pty-req") && (req.WantReply) {
req.Reply(true, []byte{})
req.WantReply = false
} else if (req.Type == "shell") && (req.WantReply) {
req.Reply(true, []byte{})
req.WantReply = false
}
maskedReqs <- req
}
}()
// Set the window header to SSH Relay login.
fmt.Fprintf(sesschan, "%s]0;SSH Bastion Relay Login%s", []byte{27}, []byte{7})
fmt.Fprintf(sesschan, "%s\r\n", GetMOTD())
var remote SSHConfigServer
var remote_name string
if user, ok := config.Users[sshConn.User()]; ! ok {
fmt.Fprintf(sesschan, "User has no permitted remote hosts.\r\n")
sesschan.Close()
return
} else {
if acl, ok := config.ACLs[user.ACL]; ! ok {
fmt.Fprintf(sesschan, "Error processing server selection (Invalid ACL).\r\n")
log.Printf("Invalid ACL detected for user %s.", sshConn.User())
sesschan.Close()
return
} else {
svr, err := InteractiveSelection(sesschan, "Please choose from the following servers:", acl.AllowedServers)
if err != nil {
fmt.Fprintf(sesschan, "Error processing server selection.\r\n")
sesschan.Close()
return
}
if server, ok := config.Servers[svr]; ! ok {
fmt.Fprintf(sesschan, "Incorrectly Configured Server Selected.\r\n")
sesschan.Close()
return
} else {
remote_name = svr
remote = server
}
}
}
err = sesschan.SyncToFile(remote_name)
if err != nil {
fmt.Fprintf(sesschan, "Failed to Initialize Session.\r\n")
sesschan.Close()
return
}
WriteAuthLog("Connecting to remote for relay (%s) by %s from %s.", remote.ConnectPath, sshConn.User(), sshConn.RemoteAddr())
fmt.Fprintf(sesschan, "Connecting to %s\r\n", remote_name)
var clientConfig *ssh.ClientConfig
clientConfig = &ssh.ClientConfig{
User: sshConn.User(),
Auth: []ssh.AuthMethod{
ssh.PasswordCallback(func() (secret string, err error) {
if secret, ok := sshConn.Permissions.Extensions["password"]; ok && config.Global.PassPassword {
return secret, nil
} else {
//log.Printf("Prompting for password for remote...")
t := terminal.NewTerminal(sesschan, "")
s, err := t.ReadPassword(fmt.Sprintf("%s@%s password: ", clientConfig.User, remote_name))
//log.Printf("Got password for remote auth, err: %s", err)
return s, err
}
}),
},
HostKeyCallback: func(hostname string, remote_addr net.Addr, key ssh.PublicKey) error {
for _, keyFileName := range remote.HostPubKeyFiles {
hostKeyData, err := ioutil.ReadFile(keyFileName)
if err != nil {
log.Printf("Error reading host key file (%s) for remote (%s): %s", keyFileName, remote_name, err)
continue
}
hostKey, _, _, _, err := ssh.ParseAuthorizedKey(hostKeyData)
if err != nil {
log.Printf("Error parsing host key file (%s) for remote (%s): %s", keyFileName, remote_name, err)
continue
}
if ( key.Type() == hostKey.Type() ) && ( bytes.Compare(key.Marshal(), hostKey.Marshal()) == 0 ) {
log.Printf("Accepting host public key from file (%s) for remote (%s).", keyFileName, remote_name)
return nil
}
}
WriteAuthLog("Host key validation failed for remote %s by user %s from %s.", remote.ConnectPath, sshConn.User(), remote_addr)
return fmt.Errorf("HOST KEY VALIDATION FAILED - POSSIBLE MITM BETWEEN RELAY AND REMOTE")
},
}
if len(remote.LoginUser) > 0 {
clientConfig.User = remote.LoginUser
}
// Set up the agent
if agentForwarding {
agentChan, agentReqs, err := sshConn.OpenChannel("[email protected]", nil)
if err == nil {
defer agentChan.Close()
go ssh.DiscardRequests(agentReqs)
// Set up the client
ag := agent.NewClient(agentChan)
// Make sure PK is first in the list if supported.
clientConfig.Auth = append([]ssh.AuthMethod{ ssh.PublicKeysCallback(ag.Signers) }, clientConfig.Auth...)
}
}
log.Printf("Getting Ready to Dial Remote SSH %s", remote_name)
client, err := ssh.Dial("tcp", remote.ConnectPath, clientConfig)
if err != nil {
fmt.Fprintf(sesschan, "Connect failed: %v\r\n", err)
sesschan.Close()
return
}
defer client.Close()
log.Printf("Dialled Remote SSH Successfully...")
// Forward the session channel
log.Printf("Setting up channel to remote %s", remote_name)
channel2, reqs2, err := client.OpenChannel("session", []byte{})
if err != nil {
fmt.Fprintf(sesschan, "Remote session setup failed: %v\r\n", err)
sesschan.Close()
return
}
WriteAuthLog("Connected to remote for relay (%s) by %s from %s.", remote.ConnectPath, sshConn.User(), sshConn.RemoteAddr())
defer WriteAuthLog("Disconnected from remote for relay (%s) by %s from %s.", remote.ConnectPath, sshConn.User(), sshConn.RemoteAddr())
log.Printf("Starting session proxy...")
proxy(maskedReqs, reqs2, sesschan, channel2)
}
func proxy(reqs1, reqs2 <-chan *ssh.Request, channel1 *LogChannel, channel2 ssh.Channel) {
var closer sync.Once
closeFunc := func() {
channel1.Close()
channel2.Close()
}
defer closer.Do(closeFunc)
closerChan := make(chan bool, 1)
// From remote, to client.
go func() {
io.Copy(channel1, channel2)
closerChan <- true
}()
go func() {
io.Copy(channel2, channel1)
closerChan <- true
}()
for {
select {
case req := <-reqs1:
if req == nil {
return
}
b, err := channel2.SendRequest(req.Type, req.WantReply, req.Payload)
if err != nil {
return
}
req.Reply(b, nil)
case req := <-reqs2:
if req == nil {
return
}
b, err := channel1.SendRequest(req.Type, req.WantReply, req.Payload)
if err != nil {
return
}
req.Reply(b, nil)
case <-closerChan:
return
}
}
}