root/fwsnort/trunk/patches/string_replace_iptables.patch

Revision 223, 5.6 KB (checked in by mbr, 5 years ago)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • extensions/libipt_string.c

    RCS file: /cvspublic/iptables/extensions/libipt_string.c,v
    retrieving revision 1.11
    diff -u -r1.11 libipt_string.c
     
    3232{ 
    3333        printf( 
    3434"STRING match v%s options:\n" 
    35 "--string [!] string          Match a string in a packet\n" 
    36 "--hex-string [!] string      Match a hex string in a packet\n", 
     35"--string [!] string          Match a string in a packet.\n" 
     36"--hex-string [!] string      Match a hex string in a packet.\n" 
     37"--replace-string             Replace matching string with a new string.\n" 
     38"--replace-hex-string         Replace matching string with a new hex string.\n", 
    3739IPTABLES_VERSION); 
    3840} 
    3941 
     
    4143static struct option opts[] = { 
    4244        { .name = "string",     .has_arg = 1, .flag = 0, .val = '1' }, 
    4345        { .name = "hex-string", .has_arg = 1, .flag = 0, .val = '2' }, 
     46        { .name = "replace-string", .has_arg = 1, .flag = 0, .val = '3' }, 
     47        { .name = "replace-hex-string", .has_arg = 1, .flag = 0, .val = '4' }, 
    4448        { .name = 0 } 
    4549}; 
    4650 
     
    5458 
    5559 
    5660static void 
    57 parse_string(const unsigned char *s, struct ipt_string_info *info) 
     61parse_string(const unsigned char *s, char *string) 
    5862{        
    59         if (strlen(s) <= BM_MAX_NLEN) strcpy(info->string, s); 
     63        if (strlen(s) <= BM_MAX_NLEN) strcpy(string, s); 
    6064        else exit_error(PARAMETER_PROBLEM, "STRING too long `%s'", s); 
    6165} 
    6266 
    6367 
    6468static void 
    65 parse_hex_string(const unsigned char *s, struct ipt_string_info *info) 
     69parse_hex_string(const unsigned char *s, char *string, u_int16_t *len) 
    6670{ 
    6771        int i=0, slen, sindex=0, schar; 
    6872        short hex_f = 0, literal_f = 0; 
     
    101105                                exit_error(PARAMETER_PROBLEM, 
    102106                                        "Bad literal placement at end of string"); 
    103107                        } 
    104                         info->string[sindex] = s[i+1]; 
     108                        string[sindex] = s[i+1]; 
    105109                        i += 2;  /* skip over literal char */ 
    106110                        literal_f = 0; 
    107111                } else if (hex_f) { 
     
    123127                        if (! sscanf(hextmp, "%x", &schar)) 
    124128                                exit_error(PARAMETER_PROBLEM, 
    125129                                        "Invalid hex char `%c'", s[i]); 
    126                         info->string[sindex] = (char) schar; 
     130                        string[sindex] = (char) schar; 
    127131                        if (s[i+2] == ' ') 
    128132                                i += 3;  /* spaces included in the hex block */ 
    129133                        else 
    130134                                i += 2; 
    131135                } else {  /* the char is not part of hex data, so just copy */ 
    132                         info->string[sindex] = s[i]; 
     136                        string[sindex] = s[i]; 
    133137                        i++; 
    134138                } 
    135139                if (sindex > BM_MAX_NLEN) 
    136140                        exit_error(PARAMETER_PROBLEM, "STRING too long `%s'", s); 
    137141                sindex++; 
    138142        } 
    139         info->len = sindex; 
     143        *len = sindex; 
    140144} 
    141145 
    142146 
     
    157161                                   "Can't specify multiple strings"); 
    158162 
    159163                check_inverse(optarg, &invert, &optind, 0); 
    160                 parse_string(argv[optind-1], stringinfo); 
     164                parse_string(argv[optind-1], stringinfo->string); 
    161165                if (invert) 
    162166                        stringinfo->invert = 1; 
    163167                stringinfo->len=strlen((char *)&stringinfo->string); 
     
    167171        case '2': 
    168172                if (*flags) 
    169173                        exit_error(PARAMETER_PROBLEM, 
    170                                    "Can't specify multiple strings"); 
     174                                   "Can't specify multiple hex strings"); 
    171175 
    172176                check_inverse(optarg, &invert, &optind, 0); 
    173                 parse_hex_string(argv[optind-1], stringinfo);  /* sets length */ 
     177                parse_hex_string(argv[optind-1], stringinfo->string, &stringinfo->len); 
    174178                if (invert) 
    175179                        stringinfo->invert = 1; 
    176180                *flags = 1; 
    177181                break; 
    178182 
     183        case '3': 
     184                if (! *flags) 
     185                        exit_error(PARAMETER_PROBLEM, 
     186                                "Must specify a string to replace with --string or --hex-string"); 
     187 
     188                check_inverse(optarg, &invert, &optind, 0); 
     189                if (invert) 
     190                        exit_error(PARAMETER_PROBLEM, 
     191                                "Can't negate --replace-string"); 
     192                parse_string(argv[optind-1], stringinfo->replace_str); 
     193                stringinfo->replace_len=strlen((char *)&stringinfo->replace_str); 
     194                /* make absolutely sure the replace string length is less than 
     195                 * or equal to the length of the string to be replaced */ 
     196                if (stringinfo->replace_len > stringinfo->len) 
     197                        exit_error(PARAMETER_PROBLEM, 
     198                                "Length of replace string must be <= length of string to be replaced"); 
     199                break; 
     200 
     201        case '4': 
     202                if (! *flags) 
     203                        exit_error(PARAMETER_PROBLEM, 
     204                                "Must specify a string to replace with --string or --hex-string"); 
     205 
     206                check_inverse(optarg, &invert, &optind, 0); 
     207                if (invert) 
     208                        exit_error(PARAMETER_PROBLEM, 
     209                                "Can't negate --replace-hex-string"); 
     210                parse_hex_string(argv[optind-1], stringinfo->replace_str, 
     211                        &stringinfo->replace_len); 
     212                /* make absolutely sure the replace string length is less than 
     213                 * or equal to the length of the string to be replaced */ 
     214                if (stringinfo->replace_len > stringinfo->len) 
     215                        exit_error(PARAMETER_PROBLEM, 
     216                                "Length of replace string must be <= length of string to be replaced"); 
     217                break; 
     218 
    179219        default: 
    180220                return 0; 
    181221        } 
     
    253293                printf("STRING match %s", (info->invert) ? "!" : ""); 
    254294                print_string(info->string, info->len); 
    255295        } 
     296        /* print replace string (if any) */ 
     297        if (info->replace_len > 0) { 
     298                if (is_hex_string(info->replace_str, info->replace_len)) { 
     299                        printf("REPLACE "); 
     300                        print_hex_string(info->replace_str, info->replace_len); 
     301                } else { 
     302                        printf("REPLACE "); 
     303                        print_string(info->replace_str, info->replace_len); 
     304                } 
     305        } 
    256306} 
    257307 
    258308 
     
    269319        } else { 
    270320                printf("--string %s", (info->invert) ? "! ": ""); 
    271321                print_string(info->string, info->len); 
     322        } 
     323        /* print out --replace-string args (if necessary) */ 
     324        if (info->replace_len > 0) { 
     325                if (is_hex_string(info->replace_str, info->replace_len)) { 
     326                        printf("--replace-hex-string "); 
     327                        print_hex_string(info->replace_str, info->replace_len); 
     328                } else { 
     329                        printf("--replace-string "); 
     330                        print_string(info->replace_str, info->replace_len); 
     331                } 
    272332        } 
    273333} 
    274334 
Note: See TracBrowser for help on using the browser.