-
Notifications
You must be signed in to change notification settings - Fork 8
/
ssh.go
470 lines (438 loc) · 14.3 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package sshsyrup
import (
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"path"
"strconv"
"strings"
"time"
"github.com/spf13/viper"
netconn "github.com/mkishere/sshsyrup/net"
os "github.com/mkishere/sshsyrup/os"
"github.com/mkishere/sshsyrup/os/command"
"github.com/mkishere/sshsyrup/sftp"
"github.com/mkishere/sshsyrup/util/abuseipdb"
"github.com/mkishere/sshsyrup/util/termlogger"
"github.com/mkishere/sshsyrup/virtualfs"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"golang.org/x/crypto/ssh"
)
const (
logTimeFormat string = "20060102"
)
// SSHSession stores SSH session info
type SSHSession struct {
user string
src net.Addr
clientVersion string
sshChan <-chan ssh.NewChannel
log *log.Entry
sys *os.System
term string
fs afero.Fs
}
type envRequest struct {
Name string
Value string
}
type ptyRequest struct {
Term string
Width uint32
Height uint32
PWidth uint32
PHeight uint32
Modes string
}
type winChgRequest struct {
Width uint32
Height uint32
}
type tunnelRequest struct {
RemoteHost string
RemotePort uint32
LocalHost string
LocalPort uint32
}
type Server struct {
sshCfg *ssh.ServerConfig
vfs afero.Fs
}
var (
ipConnCnt *netconn.IPConnCount = netconn.NewIPConnCount()
)
// NewSSHSession create new SSH connection based on existing socket connection
func NewSSHSession(nConn net.Conn, sshConfig *ssh.ServerConfig, vfs afero.Fs) (*SSHSession, error) {
conn, chans, reqs, err := ssh.NewServerConn(nConn, sshConfig)
if err != nil {
return nil, err
}
clientIP, port, _ := net.SplitHostPort(conn.RemoteAddr().String())
logger := log.WithFields(log.Fields{
"user": conn.User(),
"srcIP": clientIP,
"port": port,
"clientStr": string(conn.ClientVersion()),
"sessionId": base64.StdEncoding.EncodeToString(conn.SessionID()),
})
logger.Infof("New SSH connection with client")
go ssh.DiscardRequests(reqs)
return &SSHSession{
user: conn.User(),
src: conn.RemoteAddr(),
clientVersion: string(conn.ClientVersion()),
sshChan: chans,
log: logger,
fs: vfs,
}, nil
}
func (s *SSHSession) handleNewSession(newChan ssh.NewChannel) {
channel, requests, err := newChan.Accept()
if err != nil {
s.log.WithError(err).Error("Could not accept channel")
return
}
var sh *os.Shell
go func(in <-chan *ssh.Request, channel ssh.Channel) {
quitSignal := make(chan int, 1)
for {
select {
case req := <-in:
if req == nil {
return
}
switch req.Type {
case "[email protected]", "[email protected]":
//Do nothing here
case "pty-req":
// Of coz we are not going to create a PTY here as we are honeypot.
// We are creating a pseudo-PTY
var ptyreq ptyRequest
if err := ssh.Unmarshal(req.Payload, &ptyreq); err != nil {
s.log.WithField("reqType", req.Type).WithError(err).Errorln("Cannot parse user request payload")
req.Reply(false, nil)
} else {
s.log.WithField("reqType", req.Type).Infof("User requesting pty(%v %vx%v)", ptyreq.Term, ptyreq.Width, ptyreq.Height)
s.sys = os.NewSystem(s.user, viper.GetString("server.hostname"), s.fs, channel, int(ptyreq.Width), int(ptyreq.Height), s.log)
s.term = ptyreq.Term
req.Reply(true, nil)
}
case "env":
var envReq envRequest
if err := ssh.Unmarshal(req.Payload, &envReq); err != nil {
req.Reply(false, nil)
} else {
s.log.WithFields(log.Fields{
"reqType": req.Type,
"envVarName": envReq.Name,
"envVarValue": envReq.Value,
}).Infof("User sends envvar:%v=%v", envReq.Name, envReq.Value)
req.Reply(true, nil)
}
case "shell":
s.log.WithField("reqType", req.Type).Info("User requesting shell access")
if s.sys == nil {
s.sys = os.NewSystem(s.user, viper.GetString("server.hostname"), s.fs, channel, 80, 24, s.log)
}
sh = os.NewShell(s.sys, s.src.String(), s.log.WithField("module", "shell"), quitSignal)
// Create delay function if exists
if viper.GetInt("server.processDelay") > 0 {
sh.DelayFunc = func() {
r := 500
sleepTime := viper.GetInt("server.processDelay") - r + rand.Intn(2*r)
time.Sleep(time.Millisecond * time.Duration(sleepTime))
}
}
// Create hook for session logger (For recording session to UML/asciinema)
var hook termlogger.LogHook
if viper.GetString("server.sessionLogFmt") == "asciinema" {
asciiLogParams := map[string]string{
"TERM": s.term,
"USER": s.user,
"SRC": s.src.String(),
}
hook, err = termlogger.NewAsciinemaHook(s.sys.Width(), s.sys.Height(),
viper.GetString("asciinema.apiEndpoint"), viper.GetString("asciinema.apiKey"), asciiLogParams,
fmt.Sprintf("logs/sessions/%v-%v.cast", s.user, termlogger.LogTimeFormat))
} else if viper.GetString("server.sessionLogFmt") == "uml" {
hook, err = termlogger.NewUMLHook(0, fmt.Sprintf("logs/sessions/%v-%v.ulm.log", s.user, time.Now().Format(logTimeFormat)))
} else {
log.Errorf("Session Log option %v not recognized", viper.GetString("server.sessionLogFmt"))
}
if err != nil {
log.Errorf("Cannot create %v log file", viper.GetString("server.sessionLogFmt"))
}
// The need of a goroutine here is that PuTTY will wait for reply before acknowledge it enters shell mode
go sh.HandleRequest(hook)
req.Reply(true, nil)
case "subsystem":
subsys := string(req.Payload[4:])
s.log.WithFields(log.Fields{
"reqType": req.Type,
"subSystem": subsys,
}).Infof("User requested subsystem %v", subsys)
if subsys == "sftp" {
sftpSrv := sftp.NewSftp(channel, s.fs,
s.user, s.log.WithField("module", "sftp"), quitSignal)
go sftpSrv.HandleRequest()
req.Reply(true, nil)
} else {
req.Reply(false, nil)
}
case "window-change":
s.log.WithField("reqType", req.Type).Info("User shell window size changed")
if sh != nil {
winChg := &winChgRequest{}
if err := ssh.Unmarshal(req.Payload, winChg); err != nil {
req.Reply(false, nil)
}
sh.SetSize(int(winChg.Width), int(winChg.Height))
}
case "exec":
cmd := string(req.Payload[4:])
s.log.WithFields(log.Fields{
"reqType": req.Type,
"cmd": cmd,
}).Info("User request remote exec")
args := strings.Split(cmd, " ")
var sys *os.System
if s.sys == nil {
sys = os.NewSystem(s.user, viper.GetString("server.hostname"), s.fs, channel, 80, 24, s.log)
} else {
sys = s.sys
}
if strings.HasPrefix(args[0], "scp") {
scp := command.NewSCP(channel, s.fs, s.log.WithField("module", "scp"))
go scp.Main(args[1:], quitSignal)
req.Reply(true, nil)
continue
}
n, err := sys.Exec(args[0], args[1:])
if err != nil {
channel.Write([]byte(fmt.Sprintf("%v: command not found\r\n", cmd)))
}
quitSignal <- n
req.Reply(true, nil)
default:
s.log.WithField("reqType", req.Type).Infof("Unknown channel request type %v", req.Type)
}
case ret := <-quitSignal:
s.log.Info("User closing channel")
defer closeChannel(channel, ret)
return
}
}
}(requests, channel)
}
func (s *SSHSession) handleNewConn() {
// Service the incoming Channel channel.
for newChannel := range s.sshChan {
s.log.WithField("chanType", newChannel.ChannelType()).Info("User created new session channel")
switch newChannel.ChannelType() {
case "direct-tcpip", "forwarded-tcpip":
var treq tunnelRequest
err := ssh.Unmarshal(newChannel.ExtraData(), &treq)
if err != nil {
s.log.WithError(err).Error("Cannot unmarshal port forwarding data")
newChannel.Reject(ssh.UnknownChannelType, "Corrupt payload")
}
s.log.WithFields(log.Fields{
"remoteHost": treq.RemoteHost,
"remotePort": treq.RemotePort,
"localHost": treq.LocalHost,
"localPort": treq.LocalPort,
"chanType": newChannel.ChannelType(),
}).Info("Trying to establish connection with port forwarding")
if newChannel.ChannelType() == "forwarded-tcpip" {
newChannel.Reject(ssh.Prohibited, "Port forwarding disabled")
continue
}
var host string
switch viper.GetString("server.portRedirection") {
case "disable":
newChannel.Reject(ssh.Prohibited, "Port forwarding disabled")
continue
case "map":
portMap := viper.GetStringMap("server.portRedirectionMap")
host = portMap[strconv.Itoa(int(treq.RemotePort))].(string)
case "direct":
host = fmt.Sprintf("%v:%v", treq.RemoteHost, treq.RemotePort)
}
if len(host) > 0 {
ch, req, err := newChannel.Accept()
if err != nil {
newChannel.Reject(ssh.ResourceShortage, "Cannot create new channel")
}
go ssh.DiscardRequests(req)
go func() {
s.log.WithFields(log.Fields{
"host": host,
}).Infoln("Creating connection to remote server")
conn, err := net.Dial("tcp", host)
if err != nil {
s.log.WithFields(log.Fields{
"host": host,
}).WithError(err).Error("Cannot create connection")
newChannel.Reject(ssh.ConnectionFailed, "Cannot establish connection")
return
}
go io.Copy(conn, ch)
go io.Copy(ch, conn)
}()
} else {
newChannel.Reject(ssh.ConnectionFailed, "Malformed channel request")
}
case "session":
go s.handleNewSession(newChannel)
default:
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
s.log.WithField("chanType", newChannel.ChannelType()).Infof("Unknown channel type %v", newChannel.ChannelType())
continue
}
}
}
func CreateSessionHandler(c <-chan net.Conn, sshConfig *ssh.ServerConfig, vfs afero.Fs) {
for conn := range c {
sshConfig.PasswordCallback = PasswordChallenge(viper.GetInt("server.maxTries"))
sshSession, err := NewSSHSession(conn, sshConfig, vfs)
clientIP, port, _ := net.SplitHostPort(conn.RemoteAddr().String())
abuseipdb.CreateProfile(clientIP)
abuseipdb.AddCategory(clientIP, abuseipdb.SSH, abuseipdb.Hacking)
if err != nil {
log.WithFields(log.Fields{
"srcIP": clientIP,
"port": port,
}).WithError(err).Error("Error establishing SSH connection")
} else {
sshSession.handleNewConn()
}
//conn.Close()
ipConnCnt.DecCount(clientIP)
abuseipdb.UploadReport(clientIP)
}
}
func closeChannel(ch ssh.Channel, signal int) {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(signal))
ch.SendRequest("exit-status", false, b)
ch.Close()
}
func NewServer(configPath string, hostKey []byte) (s Server) {
// Read banner
bannerFile, err := ioutil.ReadFile(path.Join(configPath, viper.GetString("server.banner")))
if err != nil {
bannerFile = []byte{}
}
// Initalize VFS
backupFS := afero.NewBasePathFs(afero.NewOsFs(), viper.GetString("virtualfs.savedFileDir"))
zipfs, err := virtualfs.NewVirtualFS(path.Join(configPath, viper.GetString("virtualfs.imageFile")))
if err != nil {
log.Error("Cannot create virtual filesystem")
}
vfs := afero.NewCopyOnWriteFs(zipfs, backupFS)
err = os.LoadUsers(path.Join(configPath, viper.GetString("virtualfs.uidMappingFile")))
if err != nil {
log.Errorf("Cannot load user mapping file %v", path.Join(configPath, viper.GetString("virtualfs.uidMappingFile")))
}
s = Server{
&ssh.ServerConfig{
PublicKeyCallback: func(c ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
clientIP, port, _ := net.SplitHostPort(c.RemoteAddr().String())
log.WithFields(log.Fields{
"user": c.User(),
"srcIP": clientIP,
"port": port,
"pubKeyType": key.Type(),
"pubKeyFingerprint": base64.StdEncoding.EncodeToString(key.Marshal()),
"authMethod": "publickey",
}).Info("User trying to login with key")
return nil, errors.New("Key rejected, revert to password login")
},
ServerVersion: viper.GetString("server.ident"),
MaxAuthTries: viper.GetInt("server.maxTries"),
BannerCallback: func(c ssh.ConnMetadata) string {
return string(bannerFile)
},
},
vfs,
}
private, err := ssh.ParsePrivateKey(hostKey)
if err != nil {
log.WithError(err).Fatal("Failed to parse private key")
}
s.sshCfg.AddHostKey(private)
return s
}
func PasswordChallenge(tries int) func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
triesLeft := tries
return func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
clientIP, port, _ := net.SplitHostPort(c.RemoteAddr().String())
log.WithFields(log.Fields{
"user": c.User(),
"srcIP": clientIP,
"port": port,
"authMethod": "password",
"password": string(pass),
}).Info("User trying to login with password")
successPerm := &ssh.Permissions{
Extensions: map[string]string{
"permit-agent-forwarding": "yes",
},
}
stpass, userExists := os.IsUserExist(c.User())
if userExists && stpass == string(pass) {
// Password match
return successPerm, nil
} else if userExists && (stpass != string(pass) || stpass == "*") || viper.GetBool("server.allowRandomUser") {
if viper.GetBool("server.allowRetryLogin") {
if triesLeft == 1 {
return successPerm, nil
}
triesLeft--
} else {
return successPerm, nil
}
}
time.Sleep(viper.GetDuration("server.retryDelay"))
return nil, fmt.Errorf("password rejected for %q", c.User())
}
}
func (sc Server) ListenAndServe() {
connChan := make(chan net.Conn)
// Create pool of workers to handle connections
for i := 0; i < viper.GetInt("server.maxConnections"); i++ {
go CreateSessionHandler(connChan, sc.sshCfg, sc.vfs)
}
listener, err := net.Listen("tcp", fmt.Sprintf("%v:%v", viper.GetString("server.addr"), viper.GetInt("server.port")))
if err != nil {
log.WithError(err).Fatal("Could not create listening socket")
}
defer listener.Close()
for {
nConn, err := listener.Accept()
host, port, _ := net.SplitHostPort(nConn.RemoteAddr().String())
log.WithFields(log.Fields{
"srcIP": host,
"port": port,
}).Info("Connection established")
if err != nil {
log.WithError(err).Error("Failed to accept incoming connection")
continue
}
cnt := ipConnCnt.Read(host)
if cnt >= viper.GetInt("server.maxConnPerHost") {
nConn.Close()
continue
} else {
ipConnCnt.IncCount(host)
}
tConn := netconn.NewThrottledConnection(nConn, viper.GetInt64("server.speed"), viper.GetDuration("server.timeout"))
connChan <- tConn
}
}