blob: 7c6366cbbf1b39b27d11472814865660836b6c8b [file] [log] [blame]
Emmanuel Roger30719132000-10-04 15:19:31 +00001/* Shared library add-on to iptables to add string matching support.
2 *
3 * Copyright (C) 2000 Emmanuel Roger <winfield@freegates.be>
András Kis-Szabó764316a2001-02-26 17:31:20 +00004 *
Pablo Neirac6fbf412005-08-28 08:09:44 +00005 * 2005-08-05 Pablo Neira Ayuso <pablo@eurodev.net>
6 * - reimplemented to use new string matching iptables match
7 * - add functionality to match packets by using window offsets
8 * - add functionality to select the string matching algorithm
9 *
András Kis-Szabó764316a2001-02-26 17:31:20 +000010 * ChangeLog
Michael Rashb807fb32004-01-05 09:50:12 +000011 * 29.12.2003: Michael Rash <mbr@cipherdyne.org>
12 * Fixed iptables save/restore for ascii strings
13 * that contain space chars, and hex strings that
14 * contain embedded NULL chars. Updated to print
15 * strings in hex mode if any non-printable char
16 * is contained within the string.
17 *
András Kis-Szabó764316a2001-02-26 17:31:20 +000018 * 27.01.2001: Gianni Tedesco <gianni@ecsc.co.uk>
19 * Changed --tos to --string in save(). Also
20 * updated to work with slightly modified
21 * ipt_string_info.
Emmanuel Roger30719132000-10-04 15:19:31 +000022 */
Jan Engelhardt67156c02011-08-01 20:08:42 +020023#define _GNU_SOURCE 1 /* strnlen for older glibcs */
Emmanuel Roger30719132000-10-04 15:19:31 +000024#include <stdio.h>
Emmanuel Roger30719132000-10-04 15:19:31 +000025#include <string.h>
26#include <stdlib.h>
Michael Rashf8ac3292003-02-26 17:34:13 +000027#include <ctype.h>
Yasuyuki KOZAKAI6ac58e32007-07-24 06:50:03 +000028#include <xtables.h>
Yasuyuki KOZAKAI6ac58e32007-07-24 06:50:03 +000029#include <linux/netfilter/xt_string.h>
Emmanuel Roger30719132000-10-04 15:19:31 +000030
Jan Engelhardtc618a0b2011-03-06 18:12:04 +010031enum {
32 O_FROM = 0,
33 O_TO,
34 O_ALGO,
35 O_ICASE,
36 O_STRING,
37 O_HEX_STRING,
38 F_STRING = 1 << O_STRING,
39 F_HEX_STRING = 1 << O_HEX_STRING,
40 F_OP_ANY = F_STRING | F_HEX_STRING,
41};
42
Jan Engelhardt181dead2007-10-04 16:27:07 +000043static void string_help(void)
Emmanuel Roger30719132000-10-04 15:19:31 +000044{
45 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020046"string match options:\n"
Pablo Neirac6fbf412005-08-28 08:09:44 +000047"--from Offset to start searching from\n"
48"--to Offset to stop searching\n"
Joonwoo Park78d2d142008-07-07 13:32:25 +020049"--algo Algorithm\n"
50"--icase Ignore case (default: 0)\n"
Jan Engelhardt9b488b92008-06-08 19:11:51 +020051"[!] --string string Match a string in a packet\n"
52"[!] --hex-string string Match a hex string in a packet\n");
Emmanuel Roger30719132000-10-04 15:19:31 +000053}
54
Jan Engelhardtc618a0b2011-03-06 18:12:04 +010055#define s struct xt_string_info
56static const struct xt_option_entry string_opts[] = {
57 {.name = "from", .id = O_FROM, .type = XTTYPE_UINT16,
58 .flags = XTOPT_PUT, XTOPT_POINTER(s, from_offset)},
59 {.name = "to", .id = O_TO, .type = XTTYPE_UINT16,
60 .flags = XTOPT_PUT, XTOPT_POINTER(s, to_offset)},
61 {.name = "algo", .id = O_ALGO, .type = XTTYPE_STRING,
62 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, algo)},
63 {.name = "string", .id = O_STRING, .type = XTTYPE_STRING,
64 .flags = XTOPT_INVERT, .excl = F_HEX_STRING},
65 {.name = "hex-string", .id = O_HEX_STRING, .type = XTTYPE_STRING,
66 .flags = XTOPT_INVERT, .excl = F_STRING},
67 {.name = "icase", .id = O_ICASE, .type = XTTYPE_NONE},
68 XTOPT_TABLEEND,
Emmanuel Roger30719132000-10-04 15:19:31 +000069};
Jan Engelhardtc618a0b2011-03-06 18:12:04 +010070#undef s
Emmanuel Roger30719132000-10-04 15:19:31 +000071
Jan Engelhardt181dead2007-10-04 16:27:07 +000072static void string_init(struct xt_entry_match *m)
Pablo Neirac6fbf412005-08-28 08:09:44 +000073{
Yasuyuki KOZAKAI6ac58e32007-07-24 06:50:03 +000074 struct xt_string_info *i = (struct xt_string_info *) m->data;
Pablo Neirac6fbf412005-08-28 08:09:44 +000075
Jan Engelhardte88a7c22011-02-18 02:00:33 +010076 i->to_offset = UINT16_MAX;
Pablo Neirac6fbf412005-08-28 08:09:44 +000077}
78
79static void
Yasuyuki KOZAKAI6ac58e32007-07-24 06:50:03 +000080parse_string(const char *s, struct xt_string_info *info)
Emmanuel Roger30719132000-10-04 15:19:31 +000081{
Pablo Neira Ayuso409f2a82009-03-02 11:46:55 +010082 /* xt_string does not need \0 at the end of the pattern */
Yasuyuki KOZAKAI6ac58e32007-07-24 06:50:03 +000083 if (strlen(s) <= XT_STRING_MAX_PATTERN_SIZE) {
84 strncpy(info->pattern, s, XT_STRING_MAX_PATTERN_SIZE);
Pablo Neira Ayuso409f2a82009-03-02 11:46:55 +010085 info->patlen = strnlen(s, XT_STRING_MAX_PATTERN_SIZE);
Pablo Neirac6fbf412005-08-28 08:09:44 +000086 return;
87 }
Jan Engelhardt1829ed42009-02-21 03:29:44 +010088 xtables_error(PARAMETER_PROBLEM, "STRING too long \"%s\"", s);
Michael Rash96d85932003-04-21 07:27:03 +000089}
90
Pablo Neirac6fbf412005-08-28 08:09:44 +000091static void
Yasuyuki KOZAKAI6ac58e32007-07-24 06:50:03 +000092parse_hex_string(const char *s, struct xt_string_info *info)
Michael Rash96d85932003-04-21 07:27:03 +000093{
Michael Rashf8ac3292003-02-26 17:34:13 +000094 int i=0, slen, sindex=0, schar;
95 short hex_f = 0, literal_f = 0;
96 char hextmp[3];
97
98 slen = strlen(s);
99
100 if (slen == 0) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100101 xtables_error(PARAMETER_PROBLEM,
Michael Rashf8ac3292003-02-26 17:34:13 +0000102 "STRING must contain at least one char");
103 }
104
105 while (i < slen) {
Phil Sutter6fc77622018-09-17 13:38:33 +0200106 if (sindex >= XT_STRING_MAX_PATTERN_SIZE)
107 xtables_error(PARAMETER_PROBLEM,
108 "STRING too long \"%s\"", s);
Michael Rashf8ac3292003-02-26 17:34:13 +0000109 if (s[i] == '\\' && !hex_f) {
110 literal_f = 1;
111 } else if (s[i] == '\\') {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100112 xtables_error(PARAMETER_PROBLEM,
Michael Rashf8ac3292003-02-26 17:34:13 +0000113 "Cannot include literals in hex data");
114 } else if (s[i] == '|') {
115 if (hex_f)
116 hex_f = 0;
Michael Rashb807fb32004-01-05 09:50:12 +0000117 else {
Michael Rashf8ac3292003-02-26 17:34:13 +0000118 hex_f = 1;
Michael Rashb807fb32004-01-05 09:50:12 +0000119 /* get past any initial whitespace just after the '|' */
120 while (s[i+1] == ' ')
121 i++;
122 }
Michael Rashf8ac3292003-02-26 17:34:13 +0000123 if (i+1 >= slen)
124 break;
125 else
126 i++; /* advance to the next character */
127 }
128
129 if (literal_f) {
130 if (i+1 >= slen) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100131 xtables_error(PARAMETER_PROBLEM,
Michael Rashf8ac3292003-02-26 17:34:13 +0000132 "Bad literal placement at end of string");
133 }
Pablo Neirac6fbf412005-08-28 08:09:44 +0000134 info->pattern[sindex] = s[i+1];
Michael Rashf8ac3292003-02-26 17:34:13 +0000135 i += 2; /* skip over literal char */
136 literal_f = 0;
137 } else if (hex_f) {
138 if (i+1 >= slen) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100139 xtables_error(PARAMETER_PROBLEM,
Michael Rashf8ac3292003-02-26 17:34:13 +0000140 "Odd number of hex digits");
141 }
142 if (i+2 >= slen) {
143 /* must end with a "|" */
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100144 xtables_error(PARAMETER_PROBLEM, "Invalid hex block");
Michael Rashf8ac3292003-02-26 17:34:13 +0000145 }
Michael Rash96d85932003-04-21 07:27:03 +0000146 if (! isxdigit(s[i])) /* check for valid hex char */
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100147 xtables_error(PARAMETER_PROBLEM, "Invalid hex char '%c'", s[i]);
Michael Rash96d85932003-04-21 07:27:03 +0000148 if (! isxdigit(s[i+1])) /* check for valid hex char */
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100149 xtables_error(PARAMETER_PROBLEM, "Invalid hex char '%c'", s[i+1]);
Michael Rashf8ac3292003-02-26 17:34:13 +0000150 hextmp[0] = s[i];
151 hextmp[1] = s[i+1];
152 hextmp[2] = '\0';
153 if (! sscanf(hextmp, "%x", &schar))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100154 xtables_error(PARAMETER_PROBLEM,
Michael Rashf8ac3292003-02-26 17:34:13 +0000155 "Invalid hex char `%c'", s[i]);
Pablo Neirac6fbf412005-08-28 08:09:44 +0000156 info->pattern[sindex] = (char) schar;
Michael Rashf8ac3292003-02-26 17:34:13 +0000157 if (s[i+2] == ' ')
158 i += 3; /* spaces included in the hex block */
159 else
160 i += 2;
161 } else { /* the char is not part of hex data, so just copy */
Pablo Neirac6fbf412005-08-28 08:09:44 +0000162 info->pattern[sindex] = s[i];
Michael Rashf8ac3292003-02-26 17:34:13 +0000163 i++;
164 }
Phil Sutter6fc77622018-09-17 13:38:33 +0200165 sindex++;
Michael Rashf8ac3292003-02-26 17:34:13 +0000166 }
Pablo Neirac6fbf412005-08-28 08:09:44 +0000167 info->patlen = sindex;
Emmanuel Roger30719132000-10-04 15:19:31 +0000168}
169
Jan Engelhardtc618a0b2011-03-06 18:12:04 +0100170static void string_parse(struct xt_option_call *cb)
Emmanuel Roger30719132000-10-04 15:19:31 +0000171{
Jan Engelhardtc618a0b2011-03-06 18:12:04 +0100172 struct xt_string_info *stringinfo = cb->data;
173 const unsigned int revision = (*cb->match)->u.user.revision;
Emmanuel Roger30719132000-10-04 15:19:31 +0000174
Jan Engelhardtc618a0b2011-03-06 18:12:04 +0100175 xtables_option_parse(cb);
176 switch (cb->entry->id) {
177 case O_STRING:
178 parse_string(cb->arg, stringinfo);
179 if (cb->invert) {
Joonwoo Park78d2d142008-07-07 13:32:25 +0200180 if (revision == 0)
181 stringinfo->u.v0.invert = 1;
182 else
183 stringinfo->u.v1.flags |= XT_STRING_FLAG_INVERT;
184 }
Michael Rash96d85932003-04-21 07:27:03 +0000185 break;
Jan Engelhardtc618a0b2011-03-06 18:12:04 +0100186 case O_HEX_STRING:
187 parse_hex_string(cb->arg, stringinfo); /* sets length */
188 if (cb->invert) {
Joonwoo Park78d2d142008-07-07 13:32:25 +0200189 if (revision == 0)
190 stringinfo->u.v0.invert = 1;
191 else
192 stringinfo->u.v1.flags |= XT_STRING_FLAG_INVERT;
193 }
Emmanuel Roger30719132000-10-04 15:19:31 +0000194 break;
Jan Engelhardtc618a0b2011-03-06 18:12:04 +0100195 case O_ICASE:
Joonwoo Park78d2d142008-07-07 13:32:25 +0200196 if (revision == 0)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100197 xtables_error(VERSION_PROBLEM,
Joonwoo Park78d2d142008-07-07 13:32:25 +0200198 "Kernel doesn't support --icase");
199
200 stringinfo->u.v1.flags |= XT_STRING_FLAG_IGNORECASE;
Joonwoo Park78d2d142008-07-07 13:32:25 +0200201 break;
Emmanuel Roger30719132000-10-04 15:19:31 +0000202 }
Emmanuel Roger30719132000-10-04 15:19:31 +0000203}
204
Jan Engelhardtc618a0b2011-03-06 18:12:04 +0100205static void string_check(struct xt_fcheck_call *cb)
Emmanuel Roger30719132000-10-04 15:19:31 +0000206{
Jan Engelhardtc618a0b2011-03-06 18:12:04 +0100207 if (!(cb->xflags & F_OP_ANY))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100208 xtables_error(PARAMETER_PROBLEM,
Pablo Neirac6fbf412005-08-28 08:09:44 +0000209 "STRING match: You must specify `--string' or "
210 "`--hex-string'");
Emmanuel Roger30719132000-10-04 15:19:31 +0000211}
212
Michael Rashb807fb32004-01-05 09:50:12 +0000213/* Test to see if the string contains non-printable chars or quotes */
214static unsigned short int
215is_hex_string(const char *str, const unsigned short int len)
216{
217 unsigned int i;
218 for (i=0; i < len; i++)
219 if (! isprint(str[i]))
220 return 1; /* string contains at least one non-printable char */
221 /* use hex output if the last char is a "\" */
Jan Engelhardtf4daf542011-08-25 12:11:20 +0200222 if (str[len-1] == '\\')
Michael Rashb807fb32004-01-05 09:50:12 +0000223 return 1;
224 return 0;
225}
226
227/* Print string with "|" chars included as one would pass to --hex-string */
228static void
229print_hex_string(const char *str, const unsigned short int len)
230{
231 unsigned int i;
232 /* start hex block */
Dwight Davis3716dfd2011-08-12 17:02:09 -0400233 printf(" \"|");
Jan Engelhardt131d4fb2011-08-21 13:16:16 +0200234 for (i=0; i < len; i++)
235 printf("%02x", (unsigned char)str[i]);
Michael Rashb807fb32004-01-05 09:50:12 +0000236 /* close hex block */
Dwight Davis3716dfd2011-08-12 17:02:09 -0400237 printf("|\"");
Michael Rashb807fb32004-01-05 09:50:12 +0000238}
239
240static void
241print_string(const char *str, const unsigned short int len)
242{
243 unsigned int i;
Jan Engelhardt73866352010-12-18 02:04:59 +0100244 printf(" \"");
Michael Rashb807fb32004-01-05 09:50:12 +0000245 for (i=0; i < len; i++) {
Jan Engelhardtd51a97b2011-08-26 14:46:40 +0200246 if (str[i] == '\"' || str[i] == '\\')
Jan Engelhardtf4daf542011-08-25 12:11:20 +0200247 putchar('\\');
Michael Rashb807fb32004-01-05 09:50:12 +0000248 printf("%c", (unsigned char) str[i]);
249 }
Jan Engelhardt73866352010-12-18 02:04:59 +0100250 printf("\""); /* closing quote */
Michael Rashb807fb32004-01-05 09:50:12 +0000251}
Stephane Ouellette451f3ea2003-04-27 12:59:00 +0000252
Emmanuel Roger30719132000-10-04 15:19:31 +0000253static void
Jan Engelhardt181dead2007-10-04 16:27:07 +0000254string_print(const void *ip, const struct xt_entry_match *match, int numeric)
Emmanuel Roger30719132000-10-04 15:19:31 +0000255{
Yasuyuki KOZAKAI6ac58e32007-07-24 06:50:03 +0000256 const struct xt_string_info *info =
257 (const struct xt_string_info*) match->data;
Joonwoo Park78d2d142008-07-07 13:32:25 +0200258 const int revision = match->u.user.revision;
259 int invert = (revision == 0 ? info->u.v0.invert :
260 info->u.v1.flags & XT_STRING_FLAG_INVERT);
Stephane Ouellette451f3ea2003-04-27 12:59:00 +0000261
Pablo Neirac6fbf412005-08-28 08:09:44 +0000262 if (is_hex_string(info->pattern, info->patlen)) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100263 printf(" STRING match %s", invert ? "!" : "");
Pablo Neirac6fbf412005-08-28 08:09:44 +0000264 print_hex_string(info->pattern, info->patlen);
Michael Rashb807fb32004-01-05 09:50:12 +0000265 } else {
Jan Engelhardt73866352010-12-18 02:04:59 +0100266 printf(" STRING match %s", invert ? "!" : "");
Pablo Neirac6fbf412005-08-28 08:09:44 +0000267 print_string(info->pattern, info->patlen);
Michael Rashb807fb32004-01-05 09:50:12 +0000268 }
Jan Engelhardt73866352010-12-18 02:04:59 +0100269 printf(" ALGO name %s", info->algo);
Pablo Neirac6fbf412005-08-28 08:09:44 +0000270 if (info->from_offset != 0)
Jan Engelhardt73866352010-12-18 02:04:59 +0100271 printf(" FROM %u", info->from_offset);
Pablo Neirac6fbf412005-08-28 08:09:44 +0000272 if (info->to_offset != 0)
Jan Engelhardt73866352010-12-18 02:04:59 +0100273 printf(" TO %u", info->to_offset);
Joonwoo Park78d2d142008-07-07 13:32:25 +0200274 if (revision > 0 && info->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
Jan Engelhardt73866352010-12-18 02:04:59 +0100275 printf(" ICASE");
Emmanuel Roger30719132000-10-04 15:19:31 +0000276}
277
Jan Engelhardt181dead2007-10-04 16:27:07 +0000278static void string_save(const void *ip, const struct xt_entry_match *match)
Emmanuel Roger30719132000-10-04 15:19:31 +0000279{
Yasuyuki KOZAKAI6ac58e32007-07-24 06:50:03 +0000280 const struct xt_string_info *info =
281 (const struct xt_string_info*) match->data;
Joonwoo Park78d2d142008-07-07 13:32:25 +0200282 const int revision = match->u.user.revision;
283 int invert = (revision == 0 ? info->u.v0.invert :
284 info->u.v1.flags & XT_STRING_FLAG_INVERT);
Stephane Ouellette451f3ea2003-04-27 12:59:00 +0000285
Pablo Neirac6fbf412005-08-28 08:09:44 +0000286 if (is_hex_string(info->pattern, info->patlen)) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100287 printf("%s --hex-string", (invert) ? " !" : "");
Pablo Neirac6fbf412005-08-28 08:09:44 +0000288 print_hex_string(info->pattern, info->patlen);
Michael Rashb807fb32004-01-05 09:50:12 +0000289 } else {
Jan Engelhardt73866352010-12-18 02:04:59 +0100290 printf("%s --string", (invert) ? " !": "");
Pablo Neirac6fbf412005-08-28 08:09:44 +0000291 print_string(info->pattern, info->patlen);
Michael Rashb807fb32004-01-05 09:50:12 +0000292 }
Jan Engelhardt73866352010-12-18 02:04:59 +0100293 printf(" --algo %s", info->algo);
Pablo Neirac6fbf412005-08-28 08:09:44 +0000294 if (info->from_offset != 0)
Jan Engelhardt73866352010-12-18 02:04:59 +0100295 printf(" --from %u", info->from_offset);
Pablo Neirac6fbf412005-08-28 08:09:44 +0000296 if (info->to_offset != 0)
Jan Engelhardt73866352010-12-18 02:04:59 +0100297 printf(" --to %u", info->to_offset);
Joonwoo Park78d2d142008-07-07 13:32:25 +0200298 if (revision > 0 && info->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
Jan Engelhardt73866352010-12-18 02:04:59 +0100299 printf(" --icase");
Emmanuel Roger30719132000-10-04 15:19:31 +0000300}
301
Stephane Ouellette451f3ea2003-04-27 12:59:00 +0000302
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200303static struct xtables_match string_mt_reg[] = {
304 {
305 .name = "string",
306 .revision = 0,
307 .family = NFPROTO_UNSPEC,
308 .version = XTABLES_VERSION,
309 .size = XT_ALIGN(sizeof(struct xt_string_info)),
310 .userspacesize = offsetof(struct xt_string_info, config),
311 .help = string_help,
312 .init = string_init,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200313 .print = string_print,
314 .save = string_save,
Jan Engelhardtc618a0b2011-03-06 18:12:04 +0100315 .x6_parse = string_parse,
316 .x6_fcheck = string_check,
317 .x6_options = string_opts,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200318 },
319 {
320 .name = "string",
321 .revision = 1,
322 .family = NFPROTO_UNSPEC,
323 .version = XTABLES_VERSION,
324 .size = XT_ALIGN(sizeof(struct xt_string_info)),
325 .userspacesize = offsetof(struct xt_string_info, config),
326 .help = string_help,
327 .init = string_init,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200328 .print = string_print,
329 .save = string_save,
Jan Engelhardtc618a0b2011-03-06 18:12:04 +0100330 .x6_parse = string_parse,
331 .x6_fcheck = string_check,
332 .x6_options = string_opts,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200333 },
Yasuyuki KOZAKAI4ccb6f52007-07-24 06:52:16 +0000334};
335
Emmanuel Roger30719132000-10-04 15:19:31 +0000336void _init(void)
337{
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200338 xtables_register_matches(string_mt_reg, ARRAY_SIZE(string_mt_reg));
Emmanuel Roger30719132000-10-04 15:19:31 +0000339}