2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
9 /* lex-func-prefix: yy */
18 #include "trafgen_parser.tab.h"
21 extern void yyerror(const char *);
23 static char *try_convert_shellcode(char *sstr)
26 char *bstr, *ostr = sstr, *hay;
27 size_t blen, slen = strlen(sstr), tot = 0;
28 const char *needle = "\\x";
38 while ((hay = strstr(hay, needle)) != NULL ) {
39 hay += strlen(needle) + 2;
44 printf("Warning: mixed shellcode with strings, "
50 bstr = xzmalloc(blen);
54 bstr[j++] = (uint8_t) strtoul(sstr + 2, &sstr, 16);
72 number_oct ([0][0-9]+)
73 number_hex ([0][x][a-fA-F0-9]+)
74 number_bin ([0][b][0-1]+)
75 number_dec (([0])|([-+]?[1-9][0-9]*))
76 number_ascii ([a-zA-Z])
80 "cpu" { return K_CPU; }
81 "fill" { return K_FILL; }
82 "rnd" { return K_RND; }
83 "csumip" { return K_CSUMIP; }
84 "csumip4" { return K_CSUMIP; }
85 "csumudp" { return K_CSUMUDP; }
86 "csumtcp" { return K_CSUMTCP; }
87 "drnd" { return K_DRND; }
88 "dinc" { return K_DINC; }
89 "ddec" { return K_DDEC; }
90 "seqinc" { return K_SEQINC; }
91 "seqdec" { return K_SEQDEC; }
92 "const8"|"c8" { return K_CONST8; }
93 "const16"|"c16" { return K_CONST16; }
94 "const32"|"c32" { return K_CONST32; }
95 "const64"|"c64" { return K_CONST64; }
97 [ ]*"-"[ ]* { return '-'; }
98 [ ]*"+"[ ]* { return '+'; }
99 [ ]*"*"[ ]* { return '*'; }
100 [ ]*"/"[ ]* { return '/'; }
101 [ ]*"%"[ ]* { return '%'; }
102 [ ]*"&"[ ]* { return '&'; }
103 [ ]*"|"[ ]* { return '|'; }
104 [ ]*"<"[ ]* { return '<'; }
105 [ ]*">"[ ]* { return '>'; }
106 [ ]*"^"[ ]* { return '^'; }
118 "\""[^\"]+"\"" { yylval.str = try_convert_shellcode(xstrdup(yytext));
121 ([ \t\r\n]+)? { return K_WHITE; }
123 "/*"([^\*]|\*[^/])*"*/" { return K_COMMENT; }
125 "#"[^\n]* { return K_COMMENT; }
127 {number_hex} { yylval.number = strtoul(yytext, NULL, 16);
130 {number_dec} { yylval.number = strtol(yytext, NULL, 10);
133 {number_oct} { yylval.number = strtol(yytext + 1, NULL, 8);
136 {number_bin} { yylval.number = strtol(yytext + 2, NULL, 2);
139 {number_ascii} { yylval.number = (uint8_t) (*yytext);
142 "'\\x"[a-fA-F0-9]{2}"'" { yylval.number = strtol(yytext + 3, NULL, 16);
145 "'"."'" { yylval.number = (uint8_t) (*(yytext + 1));
148 ";"[^\n]* {/* NOP */}
149 . { printf("Unknown character '%s'", yytext);
150 yyerror("lex Unknown character"); }