-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
257 lines (213 loc) · 5.48 KB
/
main.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
package main
import (
"encoding/binary"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"sync"
"github.com/creack/pty"
"github.com/mdlayher/vsock"
)
var (
Version = "dev"
)
const (
FrameTypeData = 0x01
FrameTypeWindowSize = 0x02
)
func main() {
fmt.Printf(`slicer-ssh-agent (version: %s)
Copyright (c) 2025 Alex Ellis, OpenFaaS Ltd
`, Version)
port := uint32(514)
l, err := vsock.Listen(port, nil)
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Connection accepted from socket\n")
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
var err error
// Try multiple shells in order of preference
shells := []string{"/bin/bash", "/bin/sh", "/usr/bin/sh"}
var ptty *os.File
var shellCmd *exec.Cmd
for _, shell := range shells {
log.Printf("Trying to start shell: %s", shell)
shellCmd = exec.Command(shell, "--login") // Add -l flag for login shell
// Set environment variables to make it look like SSH
shellCmd.Env = append(os.Environ(),
"SSH_TTY=/dev/pts/0",
"TERM=xterm-256color",
"HOME=/root/")
shellCmd.Dir = "/root/"
if _, err := os.Stat("/etc/update-motd.d/"); err == nil {
shellCmd.Args = append(shellCmd.Args, "-c", "/usr/bin/run-parts /etc/update-motd.d/; exec bash")
}
log.Printf("Starting shell: %v", shellCmd.Args)
// Don't set Setpgid as it might be causing permission issues
ptty, err = pty.Start(shellCmd)
if err == nil {
log.Printf("Successfully started shell: %s", shell)
break
}
log.Printf("Failed to start shell %s: %v", shell, err)
}
if err != nil {
log.Printf("Could not start any shell: %v", err)
sendErrorMessage(conn, fmt.Sprintf("Failed to start any command: %v", err))
return
}
defer ptty.Close()
// Set initial window size
var ws pty.Winsize
ws.Cols = 80
ws.Rows = 24
pty.Setsize(ptty, &ws)
// Create a mutex to protect writes to the connection
var connMutex sync.Mutex
// Function to send a framed message
sendFrame := func(frameType byte, payload []byte) error {
connMutex.Lock()
defer connMutex.Unlock()
// Write frame header
header := make([]byte, 5)
header[0] = frameType
binary.BigEndian.PutUint32(header[1:5], uint32(len(payload)))
if _, err := conn.Write(header); err != nil {
return err
}
// Write payload
if len(payload) > 0 {
if _, err := conn.Write(payload); err != nil {
return err
}
}
return nil
}
// Create a WaitGroup to track active goroutines
var wg sync.WaitGroup
wg.Add(2)
// Create a channel to signal process exit
processDone := make(chan struct{})
// Start a goroutine to wait for the process to exit
go func() {
shellCmd.Wait()
close(processDone)
conn.Close()
log.Printf("Shell process exited")
}()
// Start a goroutine to read from the PTY and send to the connection
go func() {
defer wg.Done()
buf := make([]byte, 32*1024)
for {
select {
case <-processDone:
//close the connection
log.Printf("Process exited, stopping PTY reader")
return
default:
n, err := ptty.Read(buf)
if err != nil {
if err != io.EOF {
log.Printf("Error reading from PTY: %v", err)
}
return
}
// Send data frame
if err := sendFrame(FrameTypeData, buf[:n]); err != nil {
log.Printf("Error sending data frame: %v", err)
return
}
}
}
}()
// Start a goroutine to read framed messages from the connection
go func() {
defer wg.Done()
frameHeader := make([]byte, 5)
for {
select {
case <-processDone:
log.Printf("Process exited, stopping connection reader")
return
default:
// Read frame header
if _, err := io.ReadFull(conn, frameHeader); err != nil {
if err != io.EOF {
log.Printf("Error reading frame header: %v", err)
}
// Kill the process if the connection is closed
if shellCmd.Process != nil {
shellCmd.Process.Kill()
}
return
}
frameType := frameHeader[0]
payloadLen := binary.BigEndian.Uint32(frameHeader[1:5])
// Read payload
payload := make([]byte, payloadLen)
if payloadLen > 0 {
if _, err := io.ReadFull(conn, payload); err != nil {
log.Printf("Error reading frame payload: %v", err)
// Kill the process if the connection is closed
if shellCmd.Process != nil {
shellCmd.Process.Kill()
}
return
}
}
// Process frame
switch frameType {
case FrameTypeData:
// Write data to PTY
if _, err := ptty.Write(payload); err != nil {
log.Printf("Error writing to PTY: %v", err)
return
}
case FrameTypeWindowSize:
// Resize PTY window
if len(payload) >= 8 {
width := binary.BigEndian.Uint32(payload[0:4])
height := binary.BigEndian.Uint32(payload[4:8])
var ws pty.Winsize
ws.Cols = uint16(width)
ws.Rows = uint16(height)
pty.Setsize(ptty, &ws)
log.Printf("Resized PTY to %dx%d", width, height)
}
default:
log.Printf("Unknown frame type: %d", frameType)
}
}
}
}()
// Wait for all goroutines to complete
wg.Wait()
// Ensure the process is terminated
if shellCmd.Process != nil {
shellCmd.Process.Kill()
}
log.Printf("Connection handler completed")
}
func sendErrorMessage(conn net.Conn, message string) {
log.Printf("Sending error message: %s", message)
header := make([]byte, 5)
header[0] = FrameTypeData
binary.BigEndian.PutUint32(header[1:5], uint32(len(message)))
conn.Write(header)
conn.Write([]byte(message))
}