-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
fakessh.go
81 lines (70 loc) · 1.88 KB
/
fakessh.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
package main
import (
"crypto/rand"
"crypto/rsa"
"errors"
"fmt"
"log"
"net"
"os"
"time"
"golang.org/x/crypto/ssh"
)
var (
errBadPassword = errors.New("permission denied")
serverVersions = []string{
"SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.3",
"SSH-2.0-OpenSSH_6.7p1 Debian-5+deb8u3",
"SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.10",
"SSH-2.0-OpenSSH_7.4",
"SSH-2.0-OpenSSH_8.0",
"SSH-2.0-OpenSSH_8.4p1 Debian-2~bpo10+1",
"SSH-2.0-OpenSSH_8.4p1 Debian-5+deb11u1",
"SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.6",
}
)
func main() {
if len(os.Args) > 1 {
logPath := fmt.Sprintf("%s/fakessh-%s.log", os.Args[1], time.Now().Format("2006-01-02-15-04-05-000"))
logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
log.Println("Failed to open log file:", logPath, err)
return
}
defer logFile.Close()
log.SetOutput(logFile)
}
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
serverConfig := &ssh.ServerConfig{
MaxAuthTries: 6,
PasswordCallback: passwordCallback,
ServerVersion: serverVersions[0],
}
privateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
signer, _ := ssh.NewSignerFromSigner(privateKey)
serverConfig.AddHostKey(signer)
listener, err := net.Listen("tcp", ":22")
if err != nil {
log.Println("Failed to listen:", err)
return
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Failed to accept:", err)
break
}
go handleConn(conn, serverConfig)
}
}
func passwordCallback(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
log.Println(conn.RemoteAddr(), string(conn.ClientVersion()), conn.User(), string(password))
time.Sleep(100 * time.Millisecond)
return nil, errBadPassword
}
func handleConn(conn net.Conn, serverConfig *ssh.ServerConfig) {
defer conn.Close()
log.Println(conn.RemoteAddr())
ssh.NewServerConn(conn, serverConfig)
}