-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshd.go
196 lines (168 loc) · 4.14 KB
/
sshd.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
package main
import (
"fmt"
"io/ioutil"
"log"
"golang.org/x/crypto/ssh"
"net"
"errors"
"strings"
"os/exec"
"bytes"
"encoding/binary"
)
const (
ENABLE_PASS_AUTH = false
)
var (
hostKeySigner ssh.Signer
publicKeys [][]byte
)
func init() {
pemBytes, err := ioutil.ReadFile("id_rsa")
if err != nil {
log.Fatal("Failed to load private key:", err)
}
hostKeySigner, err = ssh.ParsePrivateKey(pemBytes)
if err != nil {
log.Fatal("Failed to parse private key:", err)
}
pemBytes, err = ioutil.ReadFile("authorized_keys")
if err != nil {
log.Fatal("Failed to load authorized_key", err)
}
publicKeys = make([][]byte, 0)
for {
pubKey, _, _, rest, err := ssh.ParseAuthorizedKey(pemBytes)
if err != nil {
break
}
publicKeys = append(publicKeys, pubKey.Marshal())
pemBytes = rest
}
}
func passAuth(conn ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
fmt.Println("passwordcallback", pass)
if string(pass) == "tiger" {
return &ssh.Permissions{}, nil
}
return nil, errors.New("fuck you")
}
func pubkeyAuth(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
for _, publicKey := range publicKeys {
if bytes.Equal(key.Marshal(), publicKey) {
return nil, nil
}
}
return nil, errors.New("pubkey rejected")
}
func parseArgs(str string) (command string, args []string) {
commandSlice := strings.SplitN(str, " ", 2)
command = commandSlice[0]
argsEscapedSlice := strings.Split(commandSlice[1], "'")
parity := false
fmt.Println(argsEscapedSlice, strings.Join(argsEscapedSlice, ","))
for _, elem := range argsEscapedSlice {
if parity {
args = append(args, elem)
} else {
args = append(args, strings.Split(strings.TrimSpace(elem), " ")...)
}
parity = !parity
}
return
}
// Payload: int: command size, string: command
func handleExec(ch ssh.Channel, req *ssh.Request) {
length := binary.BigEndian.Uint32(req.Payload)
fmt.Println("command length", length)
//todo use length subslice and range check
command := string(req.Payload[4:])
if strings.HasPrefix(command, "git") {
fmt.Println("running", command)
command, args := parseArgs(command)
fmt.Printf("cmd:%s args:%v\n", command, strings.Join(args, ","))
cmd := exec.Command(command, args...)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
if err != nil {
fmt.Println("there was an error running cmd:", err.Error())
}
fmt.Println("out", out.Bytes())
ch.Write(out.Bytes())
ch.Close()
} else {
ch.Write([]byte("command is not a GIT command\r\n"))
ch.Close()
return
}
}
func handleChan(ch ssh.Channel, reqs <-chan *ssh.Request) {
for req := range reqs {
if req.Type == "env" {
ptr := req.Payload
//todo range check
length := binary.BigEndian.Uint32(ptr)
name := string(ptr[4:4+length])
ptr = ptr[4+length:]
length = binary.BigEndian.Uint32(ptr)
value := string(ptr[4:4+length])
fmt.Println("ENV", name, value)
continue
}
if req.Type != "exec" {
fmt.Println("skipping request", req)
continue
}
handleExec(ch, req)
}
}
func chanReq(chanReq ssh.NewChannel) {
if chanReq.ChannelType() != "session" {
chanReq.Reject(ssh.Prohibited, "channel type is not a session")
return
}
ch, reqs, err := chanReq.Accept()
if err != nil {
log.Fatal(err)
}
go handleChan(ch, reqs)
}
func handleSshClientConnection(sshConn *ssh.ServerConn, inChans <-chan ssh.NewChannel) {
defer sshConn.Close()
for req := range inChans {
go chanReq(req)
}
}
func main() {
config := &ssh.ServerConfig{
PublicKeyCallback: pubkeyAuth,
}
if ENABLE_PASS_AUTH {
config.PasswordCallback = passAuth
}
config.AddHostKey(hostKeySigner)
address := ":2022"
serverConn, err := net.Listen("tcp", address)
if err != nil {
log.Fatal("Could not listen on address:", address)
panic(err)
}
for {
log.Println("Accepting")
clientConn, err := serverConn.Accept()
if err != nil {
panic(err)
}
log.Println("Client connected from", clientConn.RemoteAddr())
sshConn, inChans, _, err := ssh.NewServerConn(clientConn, config)
if err != nil {
log.Println("Failed to create ssh connection")
clientConn.Close()
continue
}
go handleSshClientConnection(sshConn, inChans)
}
}