-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshtool.c
554 lines (484 loc) · 12.3 KB
/
sshtool.c
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
* sshtool
*
* Copyright (c) 2015 xerub
* Copyright (c) 1998 Pavel Machek
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#define enable_interrupt_key()
#define disable_interrupt_key()
#define got_interrupt() 0
static int sockr, sockw;
size_t
g_strlcpy(char *dest, const char *src, size_t dest_size)
{
register char *d = dest;
register const char *s = src;
register size_t n = dest_size;
if (dest == NULL || src == NULL) return 0;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0)
do
{
register char c = *s++;
*d++ = c;
if (c == 0)
break;
}
while (--n != 0);
/* If not enough room in dest, add NUL and traverse rest of src */
if (n == 0)
{
if (dest_size != 0)
*d = 0;
while (*s++)
;
}
return s - src - 1; /* count does not include NUL */
}
void __attribute__((noreturn))
vfs_die(const char *message)
{
fprintf(stderr, "%s", message);
exit(1);
}
int
vfs_s_get_line (int sock, char *buf, int buf_len, char term)
{
int i;
char c;
for (i = 0; i < buf_len - 1; i++, buf++){
if (read (sock, buf, sizeof(char)) <= 0)
return 0;
if (*buf == term){
*buf = 0;
return 1;
}
}
/* Line is too long - terminate buffer and discard the rest of line */
*buf = 0;
while (read (sock, &c, sizeof (c)) > 0) {
if (c == '\n')
return 1;
}
return 0;
}
int
vfs_s_get_line_interruptible (char *buffer, int size, int fd)
{
int n;
int i;
enable_interrupt_key ();
for (i = 0; i < size-1; i++){
n = read (fd, buffer+i, 1);
disable_interrupt_key ();
if (n == -1 && errno == EINTR){
buffer [i] = 0;
return EINTR;
}
if (n == 0){
buffer [i] = 0;
return 0;
}
if (buffer [i] == '\n'){
buffer [i] = 0;
return 1;
}
}
buffer [size-1] = 0;
return 0;
}
/*
* Reply codes.
*/
#define PRELIM 1 /* positive preliminary */
#define COMPLETE 2 /* positive completion */
#define CONTINUE 3 /* positive intermediate */
#define TRANSIENT 4 /* transient negative completion */
#define ERROR 5 /* permanent negative completion */
/* command wait_flag: */
#define NONE 0x00
#define WAIT_REPLY 0x01
#define WANT_STRING 0x02
static char reply_str [80];
static int fish_decode_reply (char *s, int was_garbage)
{
int code;
if (!sscanf(s, "%d", &code)) {
code = 500;
return 5;
}
if (code<100) return was_garbage ? ERROR : (!code ? COMPLETE : PRELIM);
return code / 100;
}
/* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
static int fish_get_reply (int sock, char *string_buf, int string_len)
{
char answer[1024];
int was_garbage = 0;
for (;;) {
if (!vfs_s_get_line(sock, answer, sizeof(answer), '\n')) {
if (string_buf)
*string_buf = 0;
return 4;
}
if (strncmp(answer, "### ", 4)) {
was_garbage = 1;
if (string_buf)
g_strlcpy(string_buf, answer, string_len);
} else return fish_decode_reply(answer+4, was_garbage);
}
}
static int
fish_command (
int wait_reply, const char *fmt, ...)
{
va_list ap;
char *str;
int status;
va_start (ap, fmt);
status = vasprintf(&str, fmt, ap);
va_end (ap);
if (status < 0)
return TRANSIENT;
enable_interrupt_key ();
status = write (sockw, str, strlen (str));
free (str);
disable_interrupt_key ();
if (status < 0)
return TRANSIENT;
if (wait_reply)
return fish_get_reply (sockr,
(wait_reply & WANT_STRING) ? reply_str :
NULL, sizeof (reply_str) - 1);
return COMPLETE;
}
static void
fish_pipeopen(const char *argv[])
{
int fileset1[2], fileset2[2];
int res;
if ((pipe(fileset1)<0) || (pipe(fileset2)<0))
vfs_die("Cannot pipe(): %m.");
if ((res = fork())) {
if (res<0) vfs_die("Cannot fork(): %m.");
/* We are the parent */
close(fileset1[0]);
sockw = fileset1[1];
close(fileset2[1]);
sockr = fileset2[0];
} else {
close(0);
dup(fileset1[0]);
close(fileset1[0]); close(fileset1[1]);
close(1);
dup(fileset2[1]);
close(fileset2[0]); close(fileset2[1]);
execvp(*argv, (char **)argv);
_exit(3);
}
}
#define ERRNOR(ecode, code) do { fprintf(stderr, #ecode "\n"); return code; } while (0)
static char *
fish_retra(const char *remote_path, unsigned *sz)
{
int rv;
char *ptr, *buf;
unsigned total = 0, got = 0;
const char *temporary_path = "tmp/retra.tar";
const char *quoted_path = remote_path;
*sz = 0;
/* Set up remote locale to C, otherwise dates cannot be recognized */
if (fish_command(WAIT_REPLY, "LANG=C; LC_ALL=C; LC_TIME=C\nexport LANG; export LC_ALL; export LC_TIME\necho '### 200'\n") != COMPLETE)
ERRNOR (E_PROTO, NULL);
rv = fish_command(WANT_STRING,
"#RETRA /%s\n"
"if ls /%s >/dev/null 2>&1; then\n"
"if tar -cf /%s /%s 2>/dev/null; then\n"
"ls -ln /%s 2>/dev/null | (\n"
"read p l u g s r\n"
"echo \"$s\"\n"
")\n"
"echo '### 100'\n"
"cat /%s\n"
"rm /%s >/dev/null 2>&1\n"
"echo '### 200'\n"
"else\n"
"echo '### 500'\n"
"fi\n"
"else\n"
"echo 0\n"
"echo '### 100'\n"
"echo '### 200'\n"
"fi\n",
remote_path, quoted_path, temporary_path, quoted_path, temporary_path, temporary_path, temporary_path);
if (rv != PRELIM)
ERRNOR (E_REMOTE, NULL);
if (sscanf( reply_str, "%u", &total )!=1)
ERRNOR (E_REMOTE, NULL);
ptr = buf = malloc(total);
if (buf) {
for (;;) {
int n = 0;
int len = total - got;
disable_interrupt_key();
while (len && ((n = read (sockr, ptr, len))<0)) {
if ((errno == EINTR) && !got_interrupt())
continue;
break;
}
enable_interrupt_key();
if (n>0) got += n;
if (n<0) vfs_die("ABORT\n");
if ((!n) && ((fish_get_reply (sockr, NULL, 0) != COMPLETE)))
ERRNOR (E_REMOTE, NULL);
if (!n) break;
ptr += n;
}
*sz = total;
}
return buf;
}
static int
fish_stor(const char *remote_path, char *buf, unsigned sz)
{
int rv;
unsigned total;
const char *quoted_name = remote_path;
printf("fish: store %s: sending command...\n", remote_path);
rv = fish_command (WAIT_REPLY,
"#STOR %lu /%s\n"
"echo '### 001'\n"
"file=/%s\n"
"res=`exec 3>&1\n"
"(\n"
"head -c %lu -q - || echo DD >&3\n"
") 2>/dev/null | (\n"
"cat > \"$file\"\n"
"cat > /dev/null\n"
")`; [ \"$res\" = DD ] && {\n"
"> \"$file\"\n"
"rest=%lu\n"
"while [ $rest -gt 0 ]\n"
"do\n"
" cnt=`expr \\( $rest + 255 \\) / 256`\n"
" n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
" rest=`expr $rest - $n`\n"
"done\n"
"}; echo '### 200'\n",
(unsigned long)sz, remote_path,
quoted_name, (unsigned long)sz,
(unsigned long)sz);
if (rv != PRELIM) {
ERRNOR(E_REMOTE, -1);
}
total = 0;
while (1) {
int t, n = 8192;
if (total + n > sz) n = sz - total;
if (n == 0)
break;
if ((t = write (sockw, buf, n)) != n) {
goto error_return;
}
disable_interrupt_key();
total += n;
buf += n;
printf("fish: storing file %d (%lu)\n", total, (unsigned long) sz);
}
if (fish_get_reply (sockr, NULL, 0) != COMPLETE)
ERRNOR (E_REMOTE, -1);
return 0;
error_return:
fish_get_reply(sockr, NULL, 0);
return -1;
}
static int
fish_chmod (const char *path, int mode)
{
int r;
char cmd[8192];
snprintf(cmd, sizeof(cmd), "#CHMOD %4.4o /%s\n"
"chmod %4.4o \"/%s\" 2>/dev/null\n"
"echo '### 000'\n",
mode & 07777, path,
mode & 07777, path);
r = fish_command (WAIT_REPLY, "%s", cmd);
if (r != COMPLETE) ERRNOR (E_REMOTE, -1);
return 0;
}
static int
fish_exec2 (const char *path, const char *arg)
{
int res;
char cmd[8192];
snprintf(cmd, sizeof(cmd), "#EXEC2 /%s %s\n"
"/%s %s 2>/dev/null\n"
"echo '### 000'\n",
path, arg, path, arg);
res = fish_command (NONE, "%s", cmd);
if (res != COMPLETE)
ERRNOR (E_REMOTE, -1);
while (1) {
res = vfs_s_get_line_interruptible (cmd, sizeof (cmd), sockr);
if (!res || res == EINTR)
ERRNOR(E_CONNRESET, -1);
if (!strncmp(cmd, "### ", 4))
break;
printf("%s\n", cmd);
}
if (fish_decode_reply(cmd+4, 0) != COMPLETE)
ERRNOR (E_REMOTE, -1);
return 0;
}
static char *
read_file(const char *path, unsigned *size)
{
FILE *f;
size_t rv;
size_t sz;
void *buf;
f = fopen(path, "rb");
if (!f) {
return NULL;
}
fseek(f, 0, SEEK_END);
sz = ftell(f);
fseek(f, 0, SEEK_SET);
buf = malloc(sz);
if (!buf) {
fclose(f);
return NULL;
}
rv = fread(buf, 1, sz, f);
fclose(f);
if (sz - rv) {
free(buf);
return NULL;
}
*size = sz;
return buf;
}
int
main(int argc, char **argv)
{
int status;
char *savepath = NULL;
char *kloader = NULL;
char *ibsspath = NULL;
const char *args[] = {
"ssh",
"-p", "2222",
"-l", "root",
"localhost",
"echo FISH:; /bin/sh",
NULL
};
char answer[2048];
int opt;
while ((opt = getopt(argc, argv, "hb:k:s:p:")) != -1) {
switch (opt) {
case 'p':
args[2] = optarg;
break;
case 's':
savepath = optarg;
break;
case 'k':
kloader = optarg;
break;
case 'b':
ibsspath = optarg;
break;
case 'h':
default:
fprintf(stderr, "usage: %s [-s baseband.tar] [-k kloader -b ibss] [-p PORT] [host]\n", argv[0]);
return 1;
}
}
if (optind < argc) {
args[5] = argv[optind];
}
printf("fish: Connecting to %s:%s\n", args[5], args[2]);
fish_pipeopen(args);
printf("fish: Waiting for initial line...\n");
if (!vfs_s_get_line(sockr, answer, sizeof (answer), ':'))
ERRNOR(E_PROTO, -1);
printf("fish: Sending initial line...\n");
if (fish_command(WAIT_REPLY, "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
ERRNOR (E_PROTO, -1);
printf("fish: Handshaking version...\n");
if (fish_command(WAIT_REPLY, "#VER 0.0.0\necho '### 000'\n") != COMPLETE)
ERRNOR (E_PROTO, -1);
/* ... */
if (savepath) {
char *buf;
unsigned sz;
printf("fish: Starting linear transfer...\n");
buf = fish_retra("usr/local/standalone/*", &sz);
if (sz == 0) {
free(buf);
buf = fish_retra("usr/standalone/*", &sz);
}
if (buf) {
FILE *f = fopen(savepath, "wb");
if (f) {
fwrite(buf, 1, sz, f);
fclose(f);
} else {
printf("error: cannot write %s\n", savepath);
}
free(buf);
}
printf("fish: Transferred %u bytes\n", sz);
} else if (kloader && ibsspath) {
int rv = -1;
unsigned sz1, sz2;
char *buf1 = read_file(kloader, &sz1);
char *buf2 = read_file(ibsspath, &sz2);
if (buf1 && buf2) {
rv = fish_stor("tmp/kloader", buf1, sz1);
if (rv == 0) {
rv = fish_chmod("tmp/kloader", 0755);
if (rv == 0) {
rv = fish_stor("tmp/pwnediBSS", buf2, sz2);
}
}
}
free(buf2);
free(buf1);
if (rv == 0) {
fish_exec2("tmp/kloader", "/tmp/pwnediBSS");
} else {
printf("error: cannot send %s and %s\n", kloader, ibsspath);
}
}
/* ... */
printf("fish: Disconnecting from %s\n", args[5]);
fish_command(NONE, "#BYE\nexit\n");
close(sockw);
close(sockr);
wait(&status);
return 0;
}