suricata
app-layer-smb.c
Go to the documentation of this file.
1 /* Copyright (C) 2017-2020 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 #include "suricata-common.h"
19 #include "suricata.h"
20 
21 #include "app-layer-protos.h"
22 #include "app-layer-detect-proto.h"
23 #include "app-layer-parser.h"
24 
25 #include "util-unittest.h"
26 
27 #include "rust.h"
28 #include "app-layer-smb.h"
29 #include "util-misc.h"
30 
31 
33 static SuricataFileContext sfc = { &sbcfg };
34 
35 #define SMB_CONFIG_DEFAULT_STREAM_DEPTH 0
36 
37 #ifdef UNITTESTS
38 static void SMBParserRegisterTests(void);
39 #endif
40 
42 {
43  rs_smb_init(&sfc);
44  rs_smb_register_parser();
45 
46 #ifdef UNITTESTS
47  AppLayerParserRegisterProtocolUnittests(IPPROTO_TCP, ALPROTO_SMB, SMBParserRegisterTests);
48 #endif
49 
50  return;
51 }
52 
53 #ifdef UNITTESTS
54 #include "stream-tcp.h"
55 #include "util-unittest-helper.h"
56 
57 /** \test multi transactions and cleanup */
58 static int SMBParserTxCleanupTest(void)
59 {
60  uint64_t ret[4];
63 
64  StreamTcpInitConfig(true);
65  TcpSession ssn;
66  memset(&ssn, 0, sizeof(ssn));
67 
68  Flow *f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 445);
69  FAIL_IF_NULL(f);
70  f->protoctx = &ssn;
71  f->proto = IPPROTO_TCP;
72  f->alproto = ALPROTO_SMB;
73 
74  char req_str[] ="\x00\x00\x00\x79\xfe\x53\x4d\x42\x40\x00\x01\x00\x00\x00\x00\x00" \
75  "\x05\x00\xe0\x1e\x10\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00" \
76  "\x00\x00\x00\x00\x00\x00\x00\x00\x10\x72\xd2\x9f\x36\xc2\x08\x14" \
77  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
78  "\x00\x00\x00\x00\x39\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00" \
79  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00" \
80  "\x00\x00\x00\x00\x07\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00" \
81  "\x78\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
82  req_str[28] = 0x01;
83  int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_SMB,
84  STREAM_TOSERVER | STREAM_START, (uint8_t *)req_str, sizeof(req_str));
85  FAIL_IF_NOT(r == 0);
86  req_str[28]++;
88  STREAM_TOSERVER, (uint8_t *)req_str, sizeof(req_str));
89  FAIL_IF_NOT(r == 0);
90  req_str[28]++;
92  STREAM_TOSERVER, (uint8_t *)req_str, sizeof(req_str));
93  FAIL_IF_NOT(r == 0);
94  req_str[28]++;
96  STREAM_TOSERVER, (uint8_t *)req_str, sizeof(req_str));
97  FAIL_IF_NOT(r == 0);
98  req_str[28]++;
100  STREAM_TOSERVER, (uint8_t *)req_str, sizeof(req_str));
101  FAIL_IF_NOT(r == 0);
102  req_str[28]++;
104  STREAM_TOSERVER, (uint8_t *)req_str, sizeof(req_str));
105  FAIL_IF_NOT(r == 0);
106  req_str[28]++;
108  STREAM_TOSERVER, (uint8_t *)req_str, sizeof(req_str));
109  FAIL_IF_NOT(r == 0);
110  req_str[28]++;
112  STREAM_TOSERVER, (uint8_t *)req_str, sizeof(req_str));
113  FAIL_IF_NOT(r == 0);
114  req_str[28]++;
115 
117  UTHAppLayerParserStateGetIds(f->alparser, &ret[0], &ret[1], &ret[2], &ret[3]);
118  FAIL_IF_NOT(ret[0] == 0); // inspect_id[0]
119  FAIL_IF_NOT(ret[1] == 0); // inspect_id[1]
120  FAIL_IF_NOT(ret[2] == 0); // log_id
121  FAIL_IF_NOT(ret[3] == 0); // min_id
122 
123  char resp_str[] = "\x00\x00\x00\x98\xfe\x53\x4d\x42\x40\x00\x01\x00\x00\x00\x00\x00" \
124  "\x05\x00\x21\x00\x11\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00" \
125  "\x00\x00\x00\x00\x00\x00\x00\x00\x10\x72\xd2\x9f\x36\xc2\x08\x14" \
126  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
127  "\x00\x00\x00\x00\x59\x00\x00\x00\x01\x00\x00\x00\x48\x38\x40\xb3" \
128  "\x0f\xa8\xd3\x01\x84\x9a\x2b\x46\xf7\xa8\xd3\x01\x48\x38\x40\xb3" \
129  "\x0f\xa8\xd3\x01\x48\x38\x40\xb3\x0f\xa8\xd3\x01\x00\x00\x00\x00" \
130  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00" \
131  "\x00\x00\x00\x00\x9e\x8f\xb8\x91\x00\x00\x00\x00\x01\x5b\x11\xbb" \
132  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
133 
134  resp_str[28] = 0x01;
136  STREAM_TOCLIENT | STREAM_START, (uint8_t *)resp_str, sizeof(resp_str));
137  FAIL_IF_NOT(r == 0);
138  resp_str[28] = 0x04;
140  STREAM_TOCLIENT, (uint8_t *)resp_str, sizeof(resp_str));
141  FAIL_IF_NOT(r == 0);
142  resp_str[28] = 0x05;
144  STREAM_TOCLIENT, (uint8_t *)resp_str, sizeof(resp_str));
145  FAIL_IF_NOT(r == 0);
146  resp_str[28] = 0x06;
148  STREAM_TOCLIENT, (uint8_t *)resp_str, sizeof(resp_str));
149  FAIL_IF_NOT(r == 0);
150  resp_str[28] = 0x08;
152  STREAM_TOCLIENT, (uint8_t *)resp_str, sizeof(resp_str));
153  FAIL_IF_NOT(r == 0);
154  resp_str[28] = 0x02;
156  STREAM_TOCLIENT, (uint8_t *)resp_str, sizeof(resp_str));
157  FAIL_IF_NOT(r == 0);
158  resp_str[28] = 0x07;
160  STREAM_TOCLIENT, (uint8_t *)resp_str, sizeof(resp_str));
161  FAIL_IF_NOT(r == 0);
163 
164  UTHAppLayerParserStateGetIds(f->alparser, &ret[0], &ret[1], &ret[2], &ret[3]);
165  FAIL_IF_NOT(ret[0] == 2); // inspect_id[0]
166  FAIL_IF_NOT(ret[1] == 2); // inspect_id[1]
167  FAIL_IF_NOT(ret[2] == 2); // log_id
168  FAIL_IF_NOT(ret[3] == 2); // min_id
169 
170  resp_str[28] = 0x03;
172  STREAM_TOCLIENT, (uint8_t *)resp_str, sizeof(resp_str));
173  FAIL_IF_NOT(r == 0);
175 
176  UTHAppLayerParserStateGetIds(f->alparser, &ret[0], &ret[1], &ret[2], &ret[3]);
177  FAIL_IF_NOT(ret[0] == 8); // inspect_id[0]
178  FAIL_IF_NOT(ret[1] == 8); // inspect_id[1]
179  FAIL_IF_NOT(ret[2] == 8); // log_id
180  FAIL_IF_NOT(ret[3] == 8); // min_id
181 
182  req_str[28] = 0x09;
184  STREAM_TOSERVER | STREAM_EOF, (uint8_t *)req_str, sizeof(req_str));
185  FAIL_IF_NOT(r == 0);
187 
188  UTHAppLayerParserStateGetIds(f->alparser, &ret[0], &ret[1], &ret[2], &ret[3]);
189  FAIL_IF_NOT(ret[0] == 8); // inspect_id[0] not updated by ..Cleanup() until full tx is done
190  FAIL_IF_NOT(ret[1] == 8); // inspect_id[1]
191  FAIL_IF_NOT(ret[2] == 8); // log_id
192  FAIL_IF_NOT(ret[3] == 8); // min_id
193 
194  resp_str[28] = 0x09;
196  STREAM_TOCLIENT | STREAM_EOF, (uint8_t *)resp_str, sizeof(resp_str));
197  FAIL_IF_NOT(r == 0);
199 
200  UTHAppLayerParserStateGetIds(f->alparser, &ret[0], &ret[1], &ret[2], &ret[3]);
201  FAIL_IF_NOT(ret[0] == 9); // inspect_id[0]
202  FAIL_IF_NOT(ret[1] == 9); // inspect_id[1]
203  FAIL_IF_NOT(ret[2] == 9); // log_id
204  FAIL_IF_NOT(ret[3] == 9); // min_id
205 
207  StreamTcpFreeConfig(true);
208  UTHFreeFlow(f);
209 
210  PASS;
211 }
212 
213 static void SMBParserRegisterTests(void)
214 {
215  UtRegisterTest("SMBParserTxCleanupTest", SMBParserTxCleanupTest);
216 }
217 
218 #endif /* UNITTESTS */
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
RegisterSMBParsers
void RegisterSMBParsers(void)
Definition: app-layer-smb.c:41
stream-tcp.h
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Flow_::proto
uint8_t proto
Definition: flow.h:375
STREAMING_BUFFER_CONFIG_INITIALIZER
#define STREAMING_BUFFER_CONFIG_INITIALIZER
Definition: util-streaming-buffer.h:77
Flow_
Flow data structure.
Definition: flow.h:353
AppLayerParserThreadCtxFree
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
Definition: app-layer-parser.c:277
rust.h
Flow_::protoctx
void * protoctx
Definition: flow.h:451
util-unittest.h
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression to true.
Definition: util-unittest.h:82
STREAM_START
#define STREAM_START
Definition: stream.h:29
Flow_::alparser
AppLayerParserState * alparser
Definition: flow.h:485
app-layer-detect-proto.h
StreamTcpInitConfig
void StreamTcpInitConfig(bool)
To initialize the stream global configuration data.
Definition: stream-tcp.c:365
UTHBuildFlow
Flow * UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
Definition: util-unittest-helper.c:521
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
alp_tctx
AppLayerParserThreadCtx * alp_tctx
Definition: fuzz_applayerparserparse.c:20
app-layer-parser.h
AppLayerParserRegisterProtocolUnittests
void AppLayerParserRegisterProtocolUnittests(uint8_t ipproto, AppProto alproto, void(*RegisterUnittests)(void))
Definition: app-layer-parser.c:1752
UTHFreeFlow
void UTHFreeFlow(Flow *flow)
Definition: util-unittest-helper.c:526
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:251
AppLayerParserTransactionsCleanup
void AppLayerParserTransactionsCleanup(Flow *f)
remove obsolete (inspected and logged) transactions
Definition: app-layer-parser.c:885
StreamTcpFreeConfig
void StreamTcpFreeConfig(bool quiet)
Definition: stream-tcp.c:670
AppLayerParserParse
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
Definition: app-layer-parser.c:1211
suricata-common.h
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
StreamingBufferConfig_
Definition: util-streaming-buffer.h:67
SuricataFileContext_
Definition: rust-context.h:58
UTHAppLayerParserStateGetIds
void UTHAppLayerParserStateGetIds(void *ptr, uint64_t *i1, uint64_t *i2, uint64_t *log, uint64_t *min)
Definition: app-layer-parser.c:172
STREAM_EOF
#define STREAM_EOF
Definition: stream.h:30
app-layer-protos.h
suricata.h
app-layer-smb.h
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:37
AppLayerParserThreadCtx_
Definition: app-layer-parser.c:85
TcpSession_
Definition: stream-tcp-private.h:260
util-misc.h
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:460