This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sshlowpot.go
308 lines (279 loc) · 6.39 KB
/
sshlowpot.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
//sshlowpot is a low-interaction ssh honeypot
package main
/*
* sshlowpot.go
* Low-interaction honeypot
* By J. Stuart McMurray
* Created 20160119
* Last Modified 20160119
*/
import (
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"time"
"golang.org/x/crypto/ssh"
)
var verbon = flag.Bool(
"v",
false,
"Enable verbose logging",
)
func main() {
var (
addr = flag.String(
"a",
"127.0.0.1:2222",
"Listen `address`",
)
sver = flag.String(
"ver",
"SSH-2.0-OpenSSH_7.0",
"SSH server `version` string",
)
privKey = flag.String(
"key",
"slp_id_rsa",
"SSH private key `file`, which will be created if it "+
"doesn't already exist",
)
hsto = flag.Duration(
"to",
time.Minute,
"SSH handshake `timeout`",
)
)
flag.Usage = func() {
fmt.Fprintf(
os.Stderr,
`Usage: %v [options]
Options are:
`,
os.Args[0],
)
flag.PrintDefaults()
}
flag.Parse()
/* High-resolution logging */
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
/* Server Config */
conf, err := serverConfig(*sver, *privKey)
if nil != err {
log.Fatalf("Unable to generate server config: %v", err)
}
/* Listen on the address */
l, err := net.Listen("tcp", *addr)
if nil != err {
log.Fatalf("Unable to listen on %v: %v", *addr, err)
}
log.Printf("Listening on %v", l.Addr())
/* Pop off connections, handle them */
for {
c, err := l.Accept()
if nil != err {
log.Fatalf("Unable to accept new connection: %v", err)
}
go handle(c, conf, *hsto)
}
}
/* Verbose logging */
func verbose(f string, a ...interface{}) {
if !*verbon {
return
}
log.Printf(f, a...)
}
/* serverConfig makes an SSH server config struct with server version string
sv and private key from file named pkf. */
func serverConfig(sv, pkf string) (*ssh.ServerConfig, error) {
/* Config to return */
c := &ssh.ServerConfig{
/* Log authentication */
PasswordCallback: logPass,
PublicKeyCallback: logPubKey,
KeyboardInteractiveCallback: logKeyInt,
/* Server Version string */
ServerVersion: sv,
}
/* Try to open the private key file */
privateKeyFile, err := os.OpenFile(pkf, os.O_RDWR|os.O_CREATE, 0600)
if nil != err {
return nil, err
}
defer privateKeyFile.Close()
/* Read the file's contents */
pkb, err := ioutil.ReadAll(privateKeyFile)
if nil != err {
return nil, err
}
/* If the file was empty, make a key, write the file */
if 0 == len(pkb) {
verbose(
"No private key in %v, making new key...",
privateKeyFile.Name(),
)
pkb, err = makeKeyInFile(privateKeyFile)
if nil != err {
return nil, err
}
verbose(
"Made SSH key and wrote it to %v",
privateKeyFile.Name(),
)
} else {
verbose("Read SSH key file %v", pkf)
}
/* Parse the key */
pk, err := ssh.ParsePrivateKey(pkb)
if nil != err {
return nil, err
}
/* Add it to the config */
c.AddHostKey(pk)
/* Return the config */
return c, nil
}
/* makeKeyInFile makes a private SSH key and writes it to the file f, and
* returns what it wrote to the file. */
func makeKeyInFile(f *os.File) ([]byte, error) {
/* Below code mooched from
* http://stackoverflow.com/questions/21151714/go-generate-an-ssh-public-key */
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return nil, err
}
privateKeyPEM := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
/* Encode the key */
pkb := pem.EncodeToMemory(privateKeyPEM)
/* Try to write it to the file */
if _, err := f.Write(pkb); nil != err {
return nil, err
}
/* Return the bytes */
return pkb, nil
}
/* logPass logs a password attempt (and returns failure) */
func logPass(
conn ssh.ConnMetadata,
password []byte,
) (*ssh.Permissions, error) {
log.Printf("%v Password:%q", ci(conn), password)
return nil, fmt.Errorf("invalid password")
}
/* logPubKey logs a public key attempt */
func logPubKey(
conn ssh.ConnMetadata,
key ssh.PublicKey,
) (*ssh.Permissions, error) {
log.Printf(
"%v Key(%v):%02X",
ci(conn),
key.Type(),
md5.Sum(key.Marshal()),
)
return nil, fmt.Errorf("invalid key")
}
/* logKeyInt logs a keyboard-interactive attempt */
func logKeyInt(
conn ssh.ConnMetadata,
client ssh.KeyboardInteractiveChallenge,
) (*ssh.Permissions, error) {
/* Send a welcome and the prompt */
a, err := client(
conn.User(),
"",
[]string{"Password: "},
[]bool{false},
)
/* Handle responses */
if nil != err { /* Something went wrong */
log.Printf(
"%v error sending keyboard-interactive prompt: %v",
ci(conn),
err,
)
} else if 0 == len(a) { /* Didn't get an answer */
log.Printf("%v no keyboard-interactive answer", ci(conn))
} else if 1 < len(a) { /* We mysteriously got back too many answers */
for i, v := range a {
log.Printf(
"%v Keyboard-Interactive-%v/%v: %q",
ci(conn),
i,
len(a),
v,
)
}
} else { /* Got just one answer */
log.Printf(
"%v Keyboard-Interactive:%q",
ci(conn),
a[0],
)
}
return nil, fmt.Errorf("invalid password")
}
/* ci returns a string containing info from an ssh.ConnMetadata */
func ci(m ssh.ConnMetadata) string {
return fmt.Sprintf(
"Address:%v Target:%v Version:%q User:%q",
m.RemoteAddr(),
victimName(m),
m.ClientVersion(),
m.User(),
)
}
/* handle handles a new connection */
func handle(c net.Conn, conf *ssh.ServerConfig, hsto time.Duration) {
defer c.Close()
verbose("Address:%v Connect", c.RemoteAddr())
ch := make(chan struct{}, 1)
/* Try to upgrade to an SSH connection */
go func(c net.Conn, conf *ssh.ServerConfig, ch chan<- struct{}) {
sc, _, _, err := ssh.NewServerConn(c, conf)
if nil == err { /* This should be the norm */
log.Printf(
"%v authenticated successfully, killing. "+
"This shouldn't happen.",
ci(sc),
)
}
ch <- struct{}{}
return
}(c, conf, ch)
/* Wait for the upgrade (and auth fails) or a timeout */
select {
case <-ch:
verbose("Address:%v Disconnect", c.RemoteAddr())
case <-time.After(hsto):
verbose("Address:%v Handshake timeout", c.RemoteAddr())
}
}
var vn string
/* victimName returns the name of the victim (honeypot) */
func victimName(c ssh.ConnMetadata) string {
/* Used a cached value */
if "" != vn {
return vn
}
/* Try the hostname first */
h, err := os.Hostname()
if nil != err {
verbose("Unable to determine hostname: %v", err)
/* Failing that, use the local address */
return c.LocalAddr().String()
}
vn = h
return vn
}