censor2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Packet-censoring code for SSH-2, used to identify sensitive fields
  3. * like passwords so that the logging system can avoid writing them
  4. * into log files.
  5. */
  6. #include <assert.h>
  7. #include "putty.h"
  8. #include "ssh.h"
  9. int ssh2_censor_packet(
  10. const PacketLogSettings *pls, int type, bool sender_is_client,
  11. ptrlen pkt, logblank_t *blanks)
  12. {
  13. int nblanks = 0;
  14. ptrlen str;
  15. BinarySource src[1];
  16. BinarySource_BARE_INIT_PL(src, pkt);
  17. if (pls->omit_data &&
  18. (type == SSH2_MSG_CHANNEL_DATA ||
  19. type == SSH2_MSG_CHANNEL_EXTENDED_DATA)) {
  20. /* "Session data" packets - omit the data string. */
  21. get_uint32(src); /* skip channel id */
  22. if (type == SSH2_MSG_CHANNEL_EXTENDED_DATA)
  23. get_uint32(src); /* skip extended data type */
  24. str = get_string(src);
  25. if (!get_err(src)) {
  26. assert(nblanks < MAX_BLANKS);
  27. blanks[nblanks].offset = src->pos - str.len;
  28. blanks[nblanks].type = PKTLOG_OMIT;
  29. blanks[nblanks].len = str.len;
  30. nblanks++;
  31. }
  32. }
  33. if (sender_is_client && pls->omit_passwords) {
  34. if (type == SSH2_MSG_USERAUTH_REQUEST) {
  35. /* If this is a password packet, blank the password(s). */
  36. get_string(src); /* username */
  37. get_string(src); /* service name */
  38. str = get_string(src); /* auth method */
  39. if (ptrlen_eq_string(str, "password")) {
  40. get_bool(src);
  41. /* Blank the password field. */
  42. str = get_string(src);
  43. if (!get_err(src)) {
  44. assert(nblanks < MAX_BLANKS);
  45. blanks[nblanks].offset = src->pos - str.len;
  46. blanks[nblanks].type = PKTLOG_BLANK;
  47. blanks[nblanks].len = str.len;
  48. nblanks++;
  49. /* If there's another password field beyond it
  50. * (change of password), blank that too. */
  51. str = get_string(src);
  52. if (!get_err(src))
  53. blanks[nblanks-1].len =
  54. src->pos - blanks[nblanks].offset;
  55. }
  56. }
  57. } else if (pls->actx == SSH2_PKTCTX_KBDINTER &&
  58. type == SSH2_MSG_USERAUTH_INFO_RESPONSE) {
  59. /* If this is a keyboard-interactive response packet,
  60. * blank the responses. */
  61. get_uint32(src);
  62. assert(nblanks < MAX_BLANKS);
  63. blanks[nblanks].offset = src->pos;
  64. blanks[nblanks].type = PKTLOG_BLANK;
  65. do {
  66. str = get_string(src);
  67. } while (!get_err(src));
  68. blanks[nblanks].len = src->pos - blanks[nblanks].offset;
  69. nblanks++;
  70. } else if (type == SSH2_MSG_CHANNEL_REQUEST) {
  71. /*
  72. * If this is an X forwarding request packet, blank the
  73. * fake auth data.
  74. *
  75. * Note that while we blank the X authentication data
  76. * here, we don't take any special action to blank the
  77. * start of an X11 channel, so using MIT-MAGIC-COOKIE-1
  78. * and actually opening an X connection without having
  79. * session blanking enabled is likely to leak your cookie
  80. * into the log.
  81. */
  82. get_uint32(src);
  83. str = get_string(src);
  84. if (ptrlen_eq_string(str, "x11-req")) {
  85. get_bool(src);
  86. get_bool(src);
  87. get_string(src);
  88. str = get_string(src);
  89. if (!get_err(src)) {
  90. assert(nblanks < MAX_BLANKS);
  91. blanks[nblanks].offset = src->pos - str.len;
  92. blanks[nblanks].type = PKTLOG_BLANK;
  93. blanks[nblanks].len = str.len;
  94. nblanks++;
  95. }
  96. }
  97. }
  98. }
  99. return nblanks;
  100. }