| ︙ | | |
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
|
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
|
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
av = strtoul(ae, &ae, 10);
bv = strtoul(be, &be, 10);
if (av != bv) return (av < bv ? -1 : +1);
return 0;
}
static int do_ssh_init(void) {
char c;
char c, *vsp;
char version[10];
char vstring[40];
char vstring[80];
char vlog[sizeof(vstring)+20];
int i;
#ifdef FWHACK
i = 0;
while (s_read(&c, 1) == 1) {
if (c == 'S' && i < 2) i++;
else if (c == 'S' && i == 2) i = 2;
else if (c == 'H' && i == 2) break;
else i = 0;
}
#else
if (s_read(&c,1) != 1 || c != 'S') return 0;
if (s_read(&c,1) != 1 || c != 'S') return 0;
if (s_read(&c,1) != 1 || c != 'H') return 0;
#endif
strcpy(vstring, "SSH-");
vsp = vstring+4;
if (s_read(&c,1) != 1 || c != '-') return 0;
i = 0;
while (1) {
if (s_read(&c,1) != 1)
return 0;
if (vsp < vstring+sizeof(vstring)-1)
*vsp++ = c;
if (i >= 0) {
if (c == '-') {
version[i] = '\0';
i = -1;
} else if (i < sizeof(version)-1)
version[i++] = c;
}
else if (c == '\n')
break;
}
*vsp = 0;
sprintf(vlog, "Server version: %s", vstring);
vlog[strcspn(vlog, "\r\n")] = '\0';
logevent(vlog);
sprintf(vstring, "SSH-%s-PuTTY\n",
(ssh_versioncmp(version, "1.5") <= 0 ? version : "1.5"));
sprintf(vlog, "We claim version: %s", vstring);
vlog[strcspn(vlog, "\r\n")] = '\0';
logevent(vlog);
s_write(vstring, strlen(vstring));
return 1;
}
static void ssh_protocol(unsigned char *in, int inlen, int ispkt) {
int i, j, len;
unsigned char session_id[16];
|
| ︙ | | |
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
|
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
|
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
while (!ispkt)
crReturnV;
if (pktin.type != SSH_SMSG_PUBLIC_KEY)
fatalbox("Public key packet not received");
memcpy(cookie, pktin.body, 8);
logevent("Received public keys");
MD5Init(&md5c);
memcpy(cookie, pktin.body, 8);
i = makekey(pktin.body+8, &servkey, &keystr1);
j = makekey(pktin.body+8+i, &hostkey, &keystr2);
/*
* Hash the host key and print the hash in the log box. Just as
* a last resort in case the registry's host key checking is
* compromised, we'll allow the user some ability to verify
* host keys by eye.
*/
MD5Init(&md5c);
MD5Update(&md5c, keystr2, hostkey.bytes);
MD5Final(session_id, &md5c);
{
char logmsg[80];
int i;
logevent("Host key MD5 is:");
strcpy(logmsg, " ");
for (i = 0; i < 16; i++)
sprintf(logmsg+strlen(logmsg), "%02x", session_id[i]);
logevent(logmsg);
}
supported_ciphers_mask = ((pktin.body[12+i+j] << 24) |
(pktin.body[13+i+j] << 16) |
(pktin.body[14+i+j] << 8) |
(pktin.body[15+i+j]));
supported_auths_mask = ((pktin.body[16+i+j] << 24) |
(pktin.body[17+i+j] << 16) |
(pktin.body[18+i+j] << 8) |
(pktin.body[19+i+j]));
MD5Init(&md5c);
MD5Update(&md5c, keystr2, hostkey.bytes);
MD5Update(&md5c, keystr1, servkey.bytes);
MD5Update(&md5c, pktin.body, 8);
MD5Final(session_id, &md5c);
|
| ︙ | | |
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
|
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
|
+
+
+
+
+
+
+
+
+
+
|
if (hostkey.bytes > servkey.bytes) {
rsaencrypt(rsabuf, 32, &servkey);
rsaencrypt(rsabuf, servkey.bytes, &hostkey);
} else {
rsaencrypt(rsabuf, 32, &hostkey);
rsaencrypt(rsabuf, hostkey.bytes, &servkey);
}
logevent("Encrypted session key");
cipher_type = cfg.cipher == CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH :
cfg.cipher == CIPHER_DES ? SSH_CIPHER_DES :
SSH_CIPHER_3DES;
if ((supported_ciphers_mask & (1 << cipher_type)) == 0) {
c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
cipher_type = SSH_CIPHER_3DES;
}
switch (cipher_type) {
case SSH_CIPHER_3DES: logevent("Using 3DES encryption"); break;
case SSH_CIPHER_DES: logevent("Using single-DES encryption"); break;
case SSH_CIPHER_BLOWFISH: logevent("Using Blowfish encryption"); break;
}
s_wrpkt_start(SSH_CMSG_SESSION_KEY, len+15);
pktout.body[0] = cipher_type;
memcpy(pktout.body+1, cookie, 8);
pktout.body[9] = (len*8) >> 8;
pktout.body[10] = (len*8) & 0xFF;
memcpy(pktout.body+11, rsabuf, len);
pktout.body[len+11] = pktout.body[len+12] = 0; /* protocol flags */
pktout.body[len+13] = pktout.body[len+14] = 0;
s_wrpkt();
logevent("Trying to enable encryption...");
free(rsabuf);
cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish :
cipher_type == SSH_CIPHER_DES ? &ssh_des :
&ssh_3des;
cipher->sesskey(session_key);
do { crReturnV; } while (!ispkt);
if (pktin.type != SSH_SMSG_SUCCESS)
fatalbox("Encryption not successfully enabled");
logevent("Successfully started encryption");
fflush(stdout);
{
static char username[100];
static int pos = 0;
static char c;
if (!*cfg.username) {
|
| ︙ | | |
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
+
+
+
+
+
|
char stuff[200];
strncpy(username, cfg.username, 99);
username[99] = '\0';
sprintf(stuff, "Sent username \"%s\".\r\n", username);
c_write(stuff, strlen(stuff));
}
s_wrpkt_start(SSH_CMSG_USER, 4+strlen(username));
{
char userlog[20+sizeof(username)];
sprintf(userlog, "Sent username \"%s\"", username);
logevent(userlog);
}
pktout.body[0] = pktout.body[1] = pktout.body[2] = 0;
pktout.body[3] = strlen(username);
memcpy(pktout.body+4, username, strlen(username));
s_wrpkt();
}
do { crReturnV; } while (!ispkt);
|
| ︙ | | |
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
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
|
+
+
+
|
* exchange if we're doing TIS authentication.
*/
pwpkt_type = SSH_CMSG_AUTH_PASSWORD;
if (pktin.type == SSH_SMSG_FAILURE &&
cfg.try_tis_auth &&
(supported_auths_mask & (1<<SSH_AUTH_TIS))) {
pwpkt_type = SSH_CMSG_AUTH_TIS_RESPONSE;
logevent("Requested TIS authentication");
s_wrpkt_start(SSH_CMSG_AUTH_TIS, 0);
s_wrpkt();
do { crReturnV; } while (!ispkt);
if (pktin.type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
logevent("TIS authentication declined");
c_write("TIS authentication refused.\r\n", 29);
} else {
int challengelen = ((pktin.body[0] << 24) |
(pktin.body[1] << 16) |
(pktin.body[2] << 8) |
(pktin.body[3]));
logevent("Received TIS challenge");
c_write(pktin.body+4, challengelen);
}
}
if (pwpkt_type == SSH_CMSG_AUTH_PASSWORD)
c_write("password: ", 10);
pos = 0;
|
| ︙ | | |
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
+
+
+
+
|
}
c_write("\r\n", 2);
s_wrpkt_start(pwpkt_type, 4+strlen(password));
pktout.body[0] = pktout.body[1] = pktout.body[2] = 0;
pktout.body[3] = strlen(password);
memcpy(pktout.body+4, password, strlen(password));
s_wrpkt();
logevent("Sent password");
memset(password, 0, strlen(password));
do { crReturnV; } while (!ispkt);
if (pktin.type == 15) {
c_write("Access denied\r\n", 15);
logevent("Authentication refused");
} else if (pktin.type != 14) {
fatalbox("Strange packet received, type %d", pktin.type);
}
}
logevent("Authentication successful");
if (!cfg.nopty) {
i = strlen(cfg.termtype);
s_wrpkt_start(SSH_CMSG_REQUEST_PTY, i+5*4+1);
pktout.body[0] = (i >> 24) & 0xFF;
pktout.body[1] = (i >> 16) & 0xFF;
pktout.body[2] = (i >> 8) & 0xFF;
|
| ︙ | | |
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
+
+
+
|
ssh_state = SSH_STATE_INTERMED;
do { crReturnV; } while (!ispkt);
if (pktin.type != SSH_SMSG_SUCCESS && pktin.type != SSH_SMSG_FAILURE) {
fatalbox("Protocol confusion");
} else if (pktin.type == SSH_SMSG_FAILURE) {
c_write("Server refused to allocate pty\r\n", 32);
}
logevent("Allocated pty");
}
s_wrpkt_start(SSH_CMSG_EXEC_SHELL, 0);
s_wrpkt();
logevent("Started session");
ssh_state = SSH_STATE_SESSION;
if (size_needed)
ssh_size();
while (1) {
crReturnV;
if (ispkt) {
if (pktin.type == SSH_SMSG_STDOUT_DATA ||
pktin.type == SSH_SMSG_STDERR_DATA) {
long len = 0;
for (i = 0; i < 4; i++)
len = (len << 8) + pktin.body[i];
c_write(pktin.body+4, len);
} else if (pktin.type == SSH_MSG_DISCONNECT) {
ssh_state = SSH_STATE_CLOSED;
logevent("Received disconnect request");
} else if (pktin.type == SSH_SMSG_SUCCESS) {
/* may be from EXEC_SHELL on some servers */
} else if (pktin.type == SSH_SMSG_FAILURE) {
/* may be from EXEC_SHELL on some servers
* if no pty is available or in other odd cases. Ignore */
} else if (pktin.type == SSH_SMSG_EXITSTATUS) {
s_wrpkt_start(SSH_CMSG_EXIT_CONFIRMATION, 0);
|
| ︙ | | |