File: dce2_http.c

package info (click to toggle)
snort 2.9.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 53,752 kB
  • sloc: ansic: 214,625; sh: 13,872; makefile: 2,574; yacc: 505; perl: 496; lex: 260; sql: 213; sed: 14
file content (282 lines) | stat: -rw-r--r-- 8,242 bytes parent folder | download
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
/****************************************************************************
 * Copyright (C) 2008-2012 Sourcefire, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************
 * Provides session handling of an RPC over HTTP transport.
 *
 * 8/17/2008 - Initial implementation ... Todd Wease <twease@sourcefire.com>
 *
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "sf_types.h"
#include "dce2_http.h"
#include "snort_dce2.h"
#include "dce2_co.h"
#include "dce2_memory.h"
#include "dce2_stats.h"
#include "sf_snort_packet.h"
#include "sf_dynamic_preprocessor.h"

/********************************************************************
 * Extern variables
 ********************************************************************/
extern DCE2_Stats dce2_stats;

/********************************************************************
 * Private function prototypes
 ********************************************************************/
static DCE2_HttpSsnData * DCE2_HttpSsnInit(void);
static void DCE2_HttpProcess(DCE2_HttpSsnData *);

/********************************************************************
 * Function: DCE2_HttpSsnInit()
 *
 * Creates and initializes an rpc over http session data structure.
 *
 * Arguments: None
 *
 * Returns:
 *  DCE2_HttpSsnData *
 *      Valid pointer to an rpc over http session data structure.
 *      NULL if unable to allocate memory.
 *
 ********************************************************************/
static DCE2_HttpSsnData * DCE2_HttpSsnInit(void)
{
    DCE2_HttpSsnData *hsd = DCE2_Alloc(sizeof(DCE2_HttpSsnData), DCE2_MEM_TYPE__HTTP_SSN);

    if (hsd == NULL)
        return NULL;

    hsd->state = DCE2_HTTP_STATE__NONE;
    DCE2_CoInitTracker(&hsd->co_tracker);

    DCE2_ResetRopts(&hsd->sd.ropts);

    return hsd;
}

/********************************************************************
 * Function: DCE2_HttpProxySsnInit()
 *
 * Wrapper around main session data initialization.  Adds
 * statistical info for a proxy specific rpc over http session.
 *
 * Arguments: None
 *
 * Returns:
 *  DCE2_HttpSsnData *
 *      Valid pointer to an rpc over http session data structure.
 *      NULL if unable to allocate memory.
 *
 ********************************************************************/
DCE2_HttpSsnData * DCE2_HttpProxySsnInit(void)
{
    DCE2_HttpSsnData *hsd = DCE2_HttpSsnInit();

    if (hsd == NULL)
        return NULL;

    dce2_stats.http_proxy_sessions++;

    return hsd;
}

/********************************************************************
 * Function: DCE2_HttpServerSsnInit()
 *
 * Wrapper around main session data initialization.  Adds
 * statistical info for a server specific rpc over http session.
 *
 * Arguments: None
 *
 * Returns:
 *  DCE2_HttpSsnData *
 *      Valid pointer to an rpc over http session data structure.
 *      NULL if unable to allocate memory.
 *
 ********************************************************************/
DCE2_HttpSsnData * DCE2_HttpServerSsnInit(void)
{
    DCE2_HttpSsnData *hsd = DCE2_HttpSsnInit();

    if (hsd == NULL)
        return NULL;

    dce2_stats.http_server_sessions++;

    return hsd;
}

/********************************************************************
 * Function: DCE2_HttpProcessProxy()
 *
 * Wrapper arount main processing point for an RPC over HTTP
 * session.  Checks and sets session setup state for a proxy.
 *
 * Arguments:
 *  DCE2_HttpSsnData *
 *      Pointer to an RPC over HTTP session data structure.
 *
 * Returns: None
 *
 ********************************************************************/
void DCE2_HttpProcessProxy(DCE2_HttpSsnData *hsd)
{
    const SFSnortPacket *p = hsd->sd.wire_pkt;

    DEBUG_WRAP(DCE2_DebugMsg(DCE2_DEBUG__MAIN, "Processing RPC over HTTP proxy packet.\n"));
    dce2_stats.http_proxy_pkts++;

    if (hsd->state == DCE2_HTTP_STATE__NONE)
    {
        if (DCE2_SsnFromClient(p))
            hsd->state = DCE2_HTTP_STATE__INIT_CLIENT;
    }

    DCE2_HttpProcess(hsd);
}

/********************************************************************
 * Function: DCE2_HttpProcessServer()
 *
 * Wrapper arount main processing point for an RPC over HTTP
 * session.  Checks and sets session setup state for a server.
 *
 * Arguments:
 *  DCE2_HttpSsnData *
 *      Pointer to an RPC over HTTP session data structure.
 *
 * Returns: None
 *
 ********************************************************************/
void DCE2_HttpProcessServer(DCE2_HttpSsnData *hsd)
{
    const SFSnortPacket *p = hsd->sd.wire_pkt;

    DEBUG_WRAP(DCE2_DebugMsg(DCE2_DEBUG__MAIN, "Processing RPC over HTTP server packet.\n"));
    dce2_stats.http_server_pkts++;

    if (hsd->state == DCE2_HTTP_STATE__NONE)
    {
        if (DCE2_SsnFromServer(p))
            hsd->state = DCE2_HTTP_STATE__INIT_SERVER;
    }

    DCE2_HttpProcess(hsd);
}

/********************************************************************
 * Function: DCE2_HttpProcess()
 *
 * Main processing point for an RPC over HTTP session.
 *
 * Arguments:
 *  DCE2_HttpSsnData *
 *      Pointer to an RPC over HTTP session data structure.
 *
 * Returns: None
 *
 ********************************************************************/
static void DCE2_HttpProcess(DCE2_HttpSsnData *hsd)
{
    const SFSnortPacket *p = hsd->sd.wire_pkt;
    const uint8_t *data_ptr = p->payload;
    uint16_t data_len = p->payload_size;
    uint16_t overlap_bytes = DCE2_SsnGetOverlap(&hsd->sd);

    switch (hsd->state)
    {
        case DCE2_HTTP_STATE__INIT_CLIENT:
            hsd->state = DCE2_HTTP_STATE__INIT_SERVER;
            break;

        case DCE2_HTTP_STATE__INIT_SERVER:
            /* Don't really need to look at server response, since if the client
             * RPC_CONNECT request was bad, the TCP session is terminated by
             * the server */
            hsd->state = DCE2_HTTP_STATE__RPC_DATA;
            break;

        case DCE2_HTTP_STATE__RPC_DATA:
            if (overlap_bytes != 0)
            {
                if (overlap_bytes >= data_len)
                    return;

                DCE2_MOVE(data_ptr, data_len, overlap_bytes);
            }

            DCE2_CoProcess(&hsd->sd, &hsd->co_tracker, data_ptr, data_len);

            break;

        default:
            break;
    }
}

/********************************************************************
 * Function: DCE2_HttpDataFree()
 *
 * Frees dynamically allocated data within the RPC over HTTP
 * session data structure.
 *
 * Arguments:
 *  DCE2_HttpSsnData *
 *      Pointer to an RPC over HTTP session data structure.
 *
 * Returns: None
 *
 ********************************************************************/
void DCE2_HttpDataFree(DCE2_HttpSsnData *hsd)
{
    if (hsd == NULL)
        return;

    DCE2_CoCleanTracker(&hsd->co_tracker);
}

/********************************************************************
 * Function: DCE2_HttpSsnFree()
 *
 * Frees the session data structure and any dynamically allocated
 * data within it.
 *
 * Arguments:
 *  void *
 *      Pointer to an RPC over HTTP session data structure.
 *
 * Returns: None
 *
 ********************************************************************/
void DCE2_HttpSsnFree(void *ssn)
{
    DCE2_HttpSsnData *hsd = (DCE2_HttpSsnData *)ssn;

    if (hsd == NULL)
        return;

    DCE2_HttpDataFree(hsd);
    DCE2_Free((void *)hsd, sizeof(DCE2_HttpSsnData), DCE2_MEM_TYPE__HTTP_SSN);
}