aboutsummaryrefslogtreecommitdiff
path: root/src/lib/protocols/rsh.c
blob: 6048e2f543b26cc44f520cbe30f6d0e7a6aab1ec (plain)
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
/*
 * rsh.c
 *
 * Copyright (C) 2022-23 - ntop.org
 *
 * This file is part of nDPI, an open source deep packet inspection
 * library based on the OpenDPI and PACE technology by ipoque GmbH
 *
 * nDPI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * nDPI 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ndpi_protocol_ids.h"

#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_RSH

#include "ndpi_api.h"
#include "ndpi_private.h"

#define RSH_DEFAULT_PORT 514

static void ndpi_int_rsh_add_connection(struct ndpi_detection_module_struct * ndpi_struct,
                                        struct ndpi_flow_struct * flow)
{
  ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_UNKNOWN, NDPI_PROTOCOL_RSH,
                             NDPI_CONFIDENCE_DPI);
}

static void ndpi_search_rsh(struct ndpi_detection_module_struct * ndpi_struct,
                            struct ndpi_flow_struct *flow)
{
  struct ndpi_packet_struct *packet = &ndpi_struct->packet;

  /* Use a port based approach for midstream detection. */
  if (packet->tcp->dest == RSH_DEFAULT_PORT ||
      packet->tcp->source == RSH_DEFAULT_PORT)
  {
    if (packet->payload[packet->payload_packet_len - 1] == '\n')
    {
      if (flow->packet_counter > 5)
      {
        ndpi_int_rsh_add_connection(ndpi_struct, flow);
        flow->protos.rsh.client_username[0] = '\0';
        flow->protos.rsh.server_username[0] = '\0';
        flow->protos.rsh.command[0] = '\0';
      }
      return;
    }
  }

  switch (flow->packet_counter)
  {
    case 1:
      if (packet->payload_packet_len >= 2 &&
          packet->payload_packet_len <= 6)
      {
        int i;

        for (i = 0; i < packet->payload_packet_len - 1; ++i)
        {
          if (ndpi_isdigit(packet->payload[i]) == 0)
          {
            NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
            return;
          }
        }
      } else {
        NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
      }
      return;

    case 2:
      if (packet->payload_packet_len < 3 ||
          packet->payload[packet->payload_packet_len - 1] != '\0')
      {
        NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
        return;
      }

      {
	char str[64];
        char const * dissected_info[] = { (char const *)packet->payload,
                                          NULL, NULL };
        size_t i;

        for (i = 1; i < NDPI_ARRAY_LENGTH(dissected_info); ++i) {
          dissected_info[i] = memchr(dissected_info[i - 1], '\0',
                                     packet->payload_packet_len -
                                     (dissected_info[i - 1] - dissected_info[0]));

          if (dissected_info[i] == NULL ||
              ndpi_is_printable_buffer((uint8_t const *)dissected_info[i - 1],
                                       (dissected_info[i] - dissected_info[i - 1])) == 0)
          {
            NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
            return;
          }

          if (dissected_info[i] - dissected_info[0] >= packet->payload_packet_len - 1)
          {
            if (dissected_info[NDPI_ARRAY_LENGTH(dissected_info) - 1] == NULL)
            {
              NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
              return;
            }
            break;
          }

          dissected_info[i]++;
        }

        ndpi_int_rsh_add_connection(ndpi_struct, flow);

        strncpy(flow->protos.rsh.client_username, dissected_info[0],
                ndpi_min(NDPI_ARRAY_LENGTH(flow->protos.rsh.client_username),
                         (unsigned long)(dissected_info[1] - dissected_info[0])));
        strncpy(flow->protos.rsh.server_username, dissected_info[1],
                ndpi_min(NDPI_ARRAY_LENGTH(flow->protos.rsh.server_username),
                         (unsigned long)(dissected_info[2] - dissected_info[1])));
        strncpy(flow->protos.rsh.command, dissected_info[2],
                ndpi_min(NDPI_ARRAY_LENGTH(flow->protos.rsh.command),
                         (unsigned long)packet->payload_packet_len -
                         (unsigned long)(dissected_info[2] - dissected_info[0])));

	
        if (snprintf(str, NDPI_ARRAY_LENGTH(str), "User '%s' executing '%s'",
                     flow->protos.rsh.server_username,
                     flow->protos.rsh.command) < 0)
          str[0] = '\0';
        
        ndpi_set_risk(flow, NDPI_CLEAR_TEXT_CREDENTIALS, str);
      }
      return;

    default:
      NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
      return;
  }
}


void init_rsh_dissector(struct ndpi_detection_module_struct * ndpi_struct,
                        u_int32_t * id)
{
  ndpi_set_bitmask_protocol_detection("RSH", ndpi_struct, *id,
                                      NDPI_PROTOCOL_RSH, ndpi_search_rsh,
                                      NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
                                      SAVE_DETECTION_BITMASK_AS_UNKNOWN,
                                      ADD_TO_DETECTION_BITMASK);

  *id += 1;
}