-
Notifications
You must be signed in to change notification settings - Fork 50
/
ssh.go
174 lines (149 loc) · 4.1 KB
/
ssh.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
package flamingo
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"net"
"strings"
"sync"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)
// ConfSSH describes the options for a ssh service
type ConfSSH struct {
PrivateKey string
BindPort uint16
BindHost string
RecordWriter *RecordWriter
ServerConfig *ssh.ServerConfig
shutdown bool
listener net.Listener
m sync.Mutex
}
// IsShutdown checks to see if the service is shutting down
func (c *ConfSSH) IsShutdown() bool {
c.m.Lock()
defer c.m.Unlock()
return c.shutdown
}
// Shutdown flags the service to shut down
func (c *ConfSSH) Shutdown() {
c.m.Lock()
defer c.m.Unlock()
c.shutdown = true
c.listener.Close()
}
// NewConfSSH creates a default configuration for the SSH capture server
func NewConfSSH() *ConfSSH {
return &ConfSSH{
BindPort: 22,
BindHost: "[::]",
ServerConfig: &ssh.ServerConfig{
ServerVersion: "SSH-2.0-OpenSSH_7.6p1",
},
}
}
func getSSHHandlePassword(c *ConfSSH) func(ssh.ConnMetadata, []byte) (*ssh.Permissions, error) {
return func(sshConn ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
c.RecordWriter.Record(
"ssh",
sshConn.RemoteAddr().String(),
map[string]string{
"username": sshConn.User(),
"password": string(pass),
"version": string(sshConn.ClientVersion()),
"method": "password",
"_server": fmt.Sprintf("%s:%d", c.BindHost, c.BindPort),
},
)
return nil, fmt.Errorf("password collected for %q", sshConn.User())
}
}
func getSSHHandlePublic(c *ConfSSH) func(ssh.ConnMetadata, ssh.PublicKey) (*ssh.Permissions, error) {
return func(sshConn ssh.ConnMetadata, pubkey ssh.PublicKey) (*ssh.Permissions, error) {
c.RecordWriter.Record(
"ssh",
sshConn.RemoteAddr().String(),
map[string]string{
"username": sshConn.User(),
"pubkey-sha256": ssh.FingerprintSHA256(pubkey),
"pubkey": strings.TrimSpace(string(ssh.MarshalAuthorizedKey(pubkey))),
"version": string(sshConn.ClientVersion()),
"method": "pubkey",
"_server": fmt.Sprintf("%s:%d", c.BindHost, c.BindPort),
},
)
return nil, fmt.Errorf("pubkey collected for %q", sshConn.User())
}
}
// SpawnSSH starts a logging SSH server
func SpawnSSH(c *ConfSSH) error {
if c.PrivateKey == "" {
return fmt.Errorf("no host key has been set")
}
// Configure the ssh server
pk, err := ssh.ParsePrivateKey([]byte(c.PrivateKey))
if err != nil {
return fmt.Errorf("failed to parse private key")
}
c.ServerConfig.AddHostKey(pk)
c.ServerConfig.PasswordCallback = getSSHHandlePassword(c)
c.ServerConfig.PublicKeyCallback = getSSHHandlePublic(c)
// Create the TCP listener
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", c.BindHost, c.BindPort))
if err != nil {
return fmt.Errorf("failed to listen on %s:%d (%s)", c.BindHost, c.BindPort, err)
}
c.listener = listener
// Start the ssh handler
go sshStart(c)
return nil
}
func sshStart(c *ConfSSH) {
log.Debugf("ssh is listening on %s:%d", c.BindHost, c.BindPort)
for {
if c.IsShutdown() {
log.Debugf("ssh server on %s:%d is shutting down", c.BindHost, c.BindPort)
break
}
tcpConn, err := c.listener.Accept()
if err != nil {
// Triggers on shutdown, will break on reiteration of the loop
continue
}
go sshHandleConn(tcpConn, c)
}
}
func sshHandleConn(tcpConn net.Conn, c *ConfSSH) {
// Ensure the socket is closed
defer tcpConn.Close()
// Negotiate the session
sshConn, _, _, err := ssh.NewServerConn(tcpConn, c.ServerConfig)
if err != nil {
return
}
// Until we support authenticated sessions, this is unused
// Ensure the ssh session is closed
defer sshConn.Close()
}
// SSHGenerateRSAKey generates a new SSH host key
func SSHGenerateRSAKey(bits int) ([]byte, error) {
pkey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, err
}
err = pkey.Validate()
if err != nil {
return nil, err
}
pkeyDER := x509.MarshalPKCS1PrivateKey(pkey)
pkeyBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: pkeyDER,
}
pkeyPEM := pem.EncodeToMemory(&pkeyBlock)
return pkeyPEM, nil
}