/
main.go
334 lines (287 loc) · 8.73 KB
/
main.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
// Based on server_complex.go at https://github.com/Scalingo/go-ssh-examples/
package main
import (
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"sync"
"syscall"
"unsafe"
"github.com/kr/pty"
"golang.org/x/crypto/ssh"
)
// Configuration variables
var (
defaultShell = "sh" // Shell used if the SHELL environment variable isn't set
// Public keys used for authentication. Equivalent of the SSH authorized_hosts files
authPublicKeys = map[string]string{
"user": "AAAAC3NzaC1lZDI1NTE5AAAAIADi9ZoVZstck6ELY0EIB863kD4qp5i6DYpQJHkwBiEo",
//"user2": "AAAAC3NzaC1lZDI1NTE5AAAAIADi9ZoVZstck6ELY0EIB863kD4qp5i6DYpQJHkwBiEo",
}
// SSH server host identification key
hostKeyBytes = []byte(`-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACCbwxBo/3QT+gE3R2U0m71gJvCeLY5wYzaaDBXd6J59HQAAAJDpU9P06VPT
9AAAAAtzc2gtZWQyNTUxOQAAACCbwxBo/3QT+gE3R2U0m71gJvCeLY5wYzaaDBXd6J59HQ
AAAEDJR51JvnXwYB6ZDMIHqtE1ke12AfQ/T0Fc5OZ5FOmiRpvDEGj/dBP6ATdHZTSbvWAm
8J4tjnBjNpoMFd3onn0dAAAACXJvb3RAa2FsaQECAwQ=
-----END OPENSSH PRIVATE KEY-----`)
sshServerConfig = &ssh.ServerConfig{
//ServerVersion: "SSH-2.0-OpenSSH_7.3p1 Debian-1",
ServerVersion: "",
PublicKeyCallback: publicKeyCallback,
}
)
// An SSH server is represented by a ServerConfig, which holds
// certificate details and handles authentication of ServerConns.
func main() {
//sshServerConfig =
// You can generate a keypair with 'ssh-keygen -t rsa -C "test@example.com"'
/*privateBytes, err := ioutil.ReadFile("./id_rsa")
if err != nil {
log.Fatal("Failed to load private key (./id_rsa)")
}
*/
var IPAddress, Port string
if len(os.Args) == 2 {
IPAddress = "localhost"
Port = os.Args[1]
} else if len(os.Args) == 3 {
IPAddress = os.Args[1]
Port = os.Args[2]
} else {
fmt.Println("syntax: ")
fmt.Println(" <IPAddress> <port> Binds to the specified IP and port")
fmt.Println(" <port> Binds to localhost and the specified port")
return
}
hostKey, err := ssh.ParsePrivateKey(hostKeyBytes)
if err != nil {
log.Fatal("Failed to parse host key")
}
sshServerConfig.AddHostKey(hostKey)
// Once a ServerConfig has been configured, connections can be accepted.
listener, err := net.Listen("tcp4", IPAddress+":"+Port)
if err != nil {
log.Fatalf("failed to listen on %s:%s", IPAddress, Port)
}
// Accept all connections
log.Printf("listening on %s:%s", IPAddress, Port)
for {
tcpConn, err := listener.Accept()
if err != nil {
log.Printf("failed to accept incoming connection (%s)", err)
continue
}
// Before use, a handshake must be performed on the incoming net.Conn.
sshConn, chans, reqs, err := ssh.NewServerConn(tcpConn, sshServerConfig)
if err != nil {
log.Printf("failed to handshake (%s)", err)
continue
}
// Check remote address
log.Printf("new connection from %s (%s)", sshConn.RemoteAddr(), sshConn.ClientVersion())
// Print incoming out-of-band Requests
go handleRequests(reqs)
// Accept all channels
go handleChannels(chans)
}
}
func handleRequests(reqs <-chan *ssh.Request) {
for req := range reqs {
log.Printf("recieved out-of-band request: %+v", req)
}
}
// Start assigns a pseudo-terminal tty os.File to c.Stdin, c.Stdout,
// and c.Stderr, calls c.Start, and returns the File of the tty's
// corresponding pty.
func PtyRun(c *exec.Cmd, tty *os.File) (err error) {
defer tty.Close()
c.Stdout = tty
c.Stdin = tty
c.Stderr = tty
c.SysProcAttr = &syscall.SysProcAttr{
Setctty: true,
Setsid: true,
}
return c.Start()
}
func handleChannels(chans <-chan ssh.NewChannel) {
// Service the incoming Channel channel.
for newChannel := range chans {
// Channels have a type, depending on the application level
// protocol intended. In the case of a shell, the type is
// "session" and ServerShell may be used to present a simple
// terminal interface.
if t := newChannel.ChannelType(); t != "session" {
newChannel.Reject(ssh.UnknownChannelType, fmt.Sprintf("unknown channel type: %s", t))
continue
}
channel, requests, err := newChannel.Accept()
if err != nil {
log.Printf("could not accept channel (%s)", err)
continue
}
// allocate a terminal for this channel
log.Print("creating pty...")
// Create new pty
f, tty, err := pty.Open()
if err != nil {
log.Printf("could not start pty (%s)", err)
continue
}
var shell string
shell = os.Getenv("SHELL")
if shell == "" {
shell = defaultShell
}
// Sessions have out-of-band requests such as "shell", "pty-req" and "env"
go func(in <-chan *ssh.Request) {
for req := range in {
//log.Printf("%v %s", req.Payload, req.Payload)
ok := false
switch req.Type {
case "exec":
ok = true
command := string(req.Payload[4 : req.Payload[3]+4])
cmd := exec.Command(shell, []string{"-c", command}...)
cmd.Stdout = channel
cmd.Stderr = channel
cmd.Stdin = channel
err := cmd.Start()
if err != nil {
log.Printf("could not start command (%s)", err)
continue
}
// teardown session
go func() {
_, err := cmd.Process.Wait()
if err != nil {
log.Printf("failed to exit bash (%s)", err)
}
channel.Close()
log.Printf("session closed")
}()
case "shell":
cmd := exec.Command(shell)
cmd.Env = []string{"TERM=xterm"}
err := PtyRun(cmd, tty)
if err != nil {
log.Printf("%s", err)
}
// Teardown session
var once sync.Once
close := func() {
channel.Close()
log.Printf("session closed")
}
// Pipe session to bash and visa-versa
go func() {
io.Copy(channel, f)
once.Do(close)
}()
go func() {
io.Copy(f, channel)
once.Do(close)
}()
// We don't accept any commands (Payload),
// only the default shell.
if len(req.Payload) == 0 {
ok = true
}
case "pty-req":
// Responding 'ok' here will let the client
// know we have a pty ready for input
ok = true
// Parse body...
termLen := req.Payload[3]
termEnv := string(req.Payload[4 : termLen+4])
w, h := parseDims(req.Payload[termLen+4:])
SetWinsize(f.Fd(), w, h)
log.Printf("pty-req '%s'", termEnv)
case "window-change":
w, h := parseDims(req.Payload)
SetWinsize(f.Fd(), w, h)
continue //no response
}
if !ok {
log.Printf("declining %s request...", req.Type)
}
req.Reply(ok, nil)
}
}(requests)
}
}
// =======================
// parseDims extracts two uint32s from the provided buffer.
func parseDims(b []byte) (uint32, uint32) {
w := binary.BigEndian.Uint32(b)
h := binary.BigEndian.Uint32(b[4:])
return w, h
}
// Winsize stores the Height and Width of a terminal.
type Winsize struct {
Height uint16
Width uint16
x uint16 // unused
y uint16 // unused
}
// SetWinsize sets the size of the given pty.
func SetWinsize(fd uintptr, w, h uint32) {
log.Printf("window resize %dx%d", w, h)
ws := &Winsize{Width: uint16(w), Height: uint16(h)}
syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCSWINSZ), uintptr(unsafe.Pointer(ws)))
}
// publicKeyCallback handles SSH key-based authentication
// This function is largely based off of the code in this post: https://lukevers.com/2016/05/01/ssh-as-authentication-for-web-applications
func publicKeyCallback(remoteConn ssh.ConnMetadata, remoteKey ssh.PublicKey) (*ssh.Permissions, error) {
fmt.Println("Trying to auth user " + remoteConn.User())
// Is it a valid user?
authPublicKey, User := authPublicKeys[remoteConn.User()]
if !User {
fmt.Println("User does not exist")
return nil, errors.New("User does not exist")
}
authPublicKeyBytes, err := base64.StdEncoding.DecodeString(authPublicKey)
if err != nil {
fmt.Println("Could not base64 decode key")
return nil, errors.New("Could not base64 decode key")
}
// Parse public key
parsedAuthPublicKey, err := ssh.ParsePublicKey([]byte(authPublicKeyBytes))
if err != nil {
fmt.Println("Could not parse public key")
return nil, err
}
// Make sure the key types match
if remoteKey.Type() != parsedAuthPublicKey.Type() {
fmt.Println("Key types don't match")
return nil, errors.New("Key types do not match")
}
remoteKeyBytes := remoteKey.Marshal()
authKeyBytes := parsedAuthPublicKey.Marshal()
// Make sure the key lengths match
if len(remoteKeyBytes) != len(authKeyBytes) {
fmt.Println("Key lengths don't match")
return nil, errors.New("Keys do not match")
}
// Make sure every byte of the key matches up
// TODO: This should be a constant time check
keysMatch := true
for i, b := range remoteKeyBytes {
if b != authKeyBytes[i] {
keysMatch = false
}
}
if keysMatch == false {
fmt.Println("Keys don't match")
return nil, errors.New("Keys do not match")
}
return nil, nil
}