Skip to contentSkip to conversation

snort hex strings and libipt_string.c (revisited)

PATCH In review Michael Rash 2 participants 2 messages Feb 18, 2003 – Feb 26, 2003 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 snort hex strings and libipt_string.c (revisited)

Ok, after thinking about it a bit more I removed the --hex-string option in favor of a more flexible strategy. Snort signatures can have content fields that mix both ascii and hex strings like so:

|3200000006000000|Drives|2400|

The attached patch extends the original parse_string() function to handle hex data embedded between two "|" characters. It also handles escaping (necessary if you actually want to use the "|" in ascii data now since it has a special meaning). With any luck we should now just be able to take the content fields right out of snort and iptables should be able to handle them.

BTW, snort2iptables was the inspiration for this patch. Thanks for the great original idea!

(Hopefully the code is in the correct format now, but let me know if anything needs to be changed.)

--Mike

Michael Rash
http://www.cipherdyne.com
Key fingerprint = 4EE3 816C 413A 41AA 72C8 4463 70C3 783C 1B91 F85E

libipt_string.c renamed from libipt_string.c.old +65 -2
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <getopt.h>
+#include <ctype.h>
 
 #include <iptables.h>
 #include <linux/netfilter_ipv4/ipt_string.h>
@@ -44,8 +45,70 @@
 static void
 parse_string(const unsigned char *s, struct ipt_string_info *info)
 {	
-        if (strlen(s) <= BM_MAX_NLEN) strcpy(info->string, s);
-	else exit_error(PARAMETER_PROBLEM, "STRING too long `%s'", s);
+	int i=0, slen, sindex=0, schar;
+	short hex_f = 0, literal_f = 0;
+	char hextmp[3];
+
+	slen = strlen(s);
+
+	if (slen == 0) {
+		exit_error(PARAMETER_PROBLEM,
+			"STRING must contain at least one char");
+	}
+
+	while (i < slen) {
+		if (s[i] == '\\' && !hex_f) {
+			literal_f = 1;
+		} else if (s[i] == '\\') {
+			exit_error(PARAMETER_PROBLEM,
+				"Cannot include literals in hex data");
+		} else if (s[i] == '|') {
+			if (hex_f)
+				hex_f = 0;
+			else
+				hex_f = 1;
+			if (i+1 >= slen)
+				break;
+			else
+				i++;  /* advance to the next character */
+		}
+
+		if (literal_f) {
+			if (i+1 >= slen) {
+				exit_error(PARAMETER_PROBLEM,
+					"Bad literal placement at end of string");
+			}
+			info->string[sindex] = s[i+1];
+			i += 2;  /* skip over literal char */
+			literal_f = 0;
+		} else if (hex_f) {
+			if (i+1 >= slen) {
+				exit_error(PARAMETER_PROBLEM,
+					"Odd number of hex digits");
+			}
+			if (i+2 >= slen) {
+				/* must end with a "|" */
+				exit_error(PARAMETER_PROBLEM, "Invalid hex block");
+			}
+			hextmp[0] = s[i];
+			hextmp[1] = s[i+1];
+			hextmp[2] = '\0';
+			if (! sscanf(hextmp, "%x", &schar))
+				exit_error(PARAMETER_PROBLEM,
+					"Invalid hex char `%c'", s[i]);
+			info->string[sindex] = (char) schar;
+			if (s[i+2] == ' ')
+				i += 3;  /* spaces included in the hex block */
+			else
+				i += 2;
+		} else {  /* the char is not part of hex data, so just copy */
+			info->string[sindex] = s[i];
+			i++;
+		}
+		if (sindex > BM_MAX_NLEN)
+			exit_error(PARAMETER_PROBLEM, "STRING too long `%s'", s);
+		sindex++;
+	}
 }
 
 /* Function which parses command options; returns true if it
#1 lore raw

On Tue, Feb 18, 2003 at 06:03:44PM -0500, Michael Rash wrote:

Ok, just forget about my last mail. I should read netfilter-devel more often ;)

Quoted 3 lines

Ok, after thinking about it a bit more I removed the --hex-string option in favor of a more flexible strategy. Snort signatures can have content fields that mix both ascii and hex strings like so:

great. I'll apply your patch to cvs right now.

Quoted 2 lines

BTW, snort2iptables was the inspiration for this patch. Thanks for the great original idea!

Of course this all is a big hack. What you should be doing if you really want to do massive string/pattern searching in the kernel is something like libqsearch for the kernel

(http://www.cartel-securite.fr/pbiondi/libqsearch.html)

Quoted 1 line

Michael Rash

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie