/
scp.go
358 lines (283 loc) · 6.7 KB
/
scp.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
package handlers
import (
"bufio"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/NHAS/reverse_ssh/pkg/logger"
"golang.org/x/crypto/ssh"
)
func scpError(severity int, reason string, connection io.Writer) {
connection.Write([]byte{byte(severity)})
connection.Write([]byte(reason + "\n"))
}
func scp(commandParts []string, connection ssh.Channel, log logger.Logger) error {
//Find what the target file path is, essentially ignore anything that is a flag '-t'
loc := -1
mode := ""
for i := 0; i < len(commandParts); i++ {
if mode == "" && (commandParts[i] == "-t" || commandParts[i] == "-f") {
mode = commandParts[i]
continue
}
if len(commandParts[i]) > 0 && commandParts[i][0] != '-' {
loc = i
break
}
}
path := strings.Join(commandParts[loc:], " ")
log.Info("Mode: %s %s\n", mode, path)
switch mode {
case "-t":
err := to(path, connection)
if err != nil {
log.Warning("Error copying to: %s\n", err)
scpError(2, fmt.Sprintf("error: %s", err), connection)
return err
}
case "-f":
from(path, connection)
default:
log.Warning("Unknown mode.")
}
return nil
}
func readProtocolControl(connection ssh.Channel) (string, uint32, uint64, string, error) {
control, err := bufio.NewReader(connection).ReadString('\n')
if err != nil {
log.Println(err)
return "", 0, 0, "", err
}
_, err = connection.Write([]byte{0})
if err != nil {
return "", 0, 0, "", err
}
if len(control) > 0 && control[0] == 'E' {
return "exit", 0, 0, "", nil
}
parts := strings.SplitN(control, " ", 3)
if len(parts) != 3 {
return "", 0, 0, "", errors.New("Protocol error: " + control)
}
mode, _ := strconv.ParseInt(parts[0][1:], 8, 32)
size, _ := strconv.ParseInt(parts[1], 10, 64)
filename := parts[len(parts)-1]
filename = filename[:len(filename)-1]
switch parts[0][0] {
case 'D':
return "dir", uint32(mode), uint64(size), filename, nil
case 'C':
return "file", uint32(mode), uint64(size), filename, nil
default:
return "", 0, 0, "", errors.New(fmt.Sprintf("Unknown mode: %s", strings.TrimSpace(control)))
}
}
func readFile(connection ssh.Channel, path string, mode uint32, size uint64) error {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, fs.FileMode(mode))
if err != nil {
return err
}
defer f.Close()
b := make([]byte, 1024)
var nn uint64
for {
readsize := (size - nn) % 1024
if readsize == 0 {
readsize = 1024
}
n, err := connection.Read(b[:readsize])
if err != nil {
return err
}
nn += uint64(n)
nf, err := f.Write(b[:n])
if err != nil {
return err
}
if nf != n {
return err
}
if nn == size {
break
}
}
status, _ := readAck(connection)
if status != 0 {
return errors.New("ACK bad")
}
_, err = connection.Write([]byte{0})
return err
}
func readDirectory(connection ssh.Channel, path string, mode uint32) error {
err := os.Mkdir(path, os.FileMode(mode))
if err != nil && !os.IsExist(err) {
return err
}
for {
t, mode, size, filename, err := readProtocolControl(connection)
if err != nil {
return err
}
newPath := filepath.Join(path, filename)
switch t {
case "dir":
err = readDirectory(connection, newPath, mode)
if err != nil {
return err
}
case "file":
err = readFile(connection, newPath, mode, size)
if err != nil {
return err
}
case "exit":
return nil
}
}
}
func to(tocreate string, connection ssh.Channel) error {
_, err := connection.Write([]byte{0})
if err != nil {
return err
}
t, mode, size, filename, err := readProtocolControl(connection)
if err != nil {
return err
}
switch t {
case "dir":
return readDirectory(connection, tocreate, mode)
case "file":
pathinfo, err := os.Stat(tocreate)
if err != nil && !os.IsNotExist(err) {
return err
}
var path string = tocreate
if pathinfo != nil && pathinfo.IsDir() {
path = filepath.Join(tocreate, filename)
}
return readFile(connection, path, mode, size)
default:
return errors.New("Unknown file type")
}
}
func from(todownload string, connection ssh.Channel) {
clientReady, _ := readAck(connection)
if clientReady != 0 {
scpError(2, "client didnt acknowledge", connection)
return
}
fileinfo, err := os.Stat(todownload)
if err != nil {
scpError(1, err.Error(), connection)
return
}
if fileinfo.Mode().IsRegular() {
log.Println("Transfering single file")
err = scpTransferFile(todownload, fileinfo, connection)
}
if fileinfo.IsDir() {
log.Println("Transferring directory")
err = scpTransferDirectory(todownload, fileinfo, connection)
}
if err != nil {
scpError(1, err.Error(), connection)
}
connection.Write([]byte("E\n"))
success, _ := readAck(connection)
if success != 0 {
scpError(2, "failed to ack final write", connection)
return
}
return
}
func scpTransferDirectory(path string, mode fs.FileInfo, connection ssh.Channel) error {
_, err := connection.Write([]byte(fmt.Sprintf("D%#o 1 %s\n", mode.Mode().Perm(), filepath.Base(path))))
if err != nil {
return err
}
dir, err := os.Open(path)
if err != nil {
return err
}
files, err := dir.Readdir(-1)
dir.Close()
if err != nil {
return err
}
success, _ := readAck(connection)
if success != 0 {
return errors.New("Creating directory failed")
}
for _, file := range files {
if file.IsDir() {
err := scpTransferDirectory(filepath.Join(path, file.Name()), file, connection)
if err != nil {
scpError(1, err.Error(), connection)
}
continue
}
err := scpTransferFile(filepath.Join(path, file.Name()), file, connection)
if err != nil {
scpError(1, err.Error(), connection)
}
}
_, err = connection.Write([]byte("E\n"))
return err
}
func readAck(conn ssh.Channel) (int, error) {
buf := make([]byte, 1)
_, err := conn.Read(buf)
if err != nil {
return -1, err
}
return int(buf[0]), nil
}
func scpTransferFile(path string, fi fs.FileInfo, connection ssh.Channel) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = fmt.Fprintf(connection, "C%04o %d %s\n", fi.Mode().Perm(), fi.Size(), filepath.Base(path))
if err != nil {
return err
}
readyToAcceptFile, _ := readAck(connection)
if readyToAcceptFile != 0 {
return errors.New("Client unable to receive new file")
}
defer func() {
connection.Write([]byte{0})
failedToFinish, _ := readAck(connection)
if failedToFinish != 0 {
log.Println("Unable to finish up file")
}
}()
buf := make([]byte, 1024)
for {
n, err := file.Read(buf)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
nn, err := connection.Write(buf[:n])
if nn < n {
return errors.New("Not able to do full write, connection is bad")
}
if err != nil {
return err
}
if n < 1024 {
return nil
}
}
}