Changeset 423

Show
Ignore:
Timestamp:
03/25/06 20:52:18 (3 years ago)
Author:
dp
Message:

shellcode-signatures:

  • add proper error-handling for the parser
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nepenthes/trunk/modules/shellcode-signatures/parser.y

    r422 r423  
    55        #include <stdio.h> 
    66        #include <memory.h> 
     7        #include <errno.h> 
    78         
    89        #include "parser.h" 
     
    2223        static char *get_mapping_by_numeric(int num); 
    2324         
     25        static char error_buffer[0xff]; 
    2426%} 
    2527 
     
    284286        int yyerror(char* s) 
    285287        { 
    286                 printf(" %s at '%s' on line %d\n", s, yytext, line_number ); 
     288                snprintf(error_buffer, sizeof(error_buffer), 
     289                        "%s at '%s' on line %d", s, yytext, line_number); 
    287290                return 0; 
    288291        } 
     
    296299        struct shellcode *sc_parse_file(const char *filename) 
    297300        { 
     301                yyin = fopen(filename, "r"); 
     302                 
     303                if( yyin == NULL ) 
     304                { 
     305                        snprintf(error_buffer, sizeof(error_buffer), "%s", strerror(errno)); 
     306                        return NULL; 
     307                } 
     308 
    298309                init_shellcode(); 
    299                  
    300                 yyin = fopen(filename, "r"); 
    301                  
    302                 if( yyin == NULL ) 
     310                if( yyparse() != 0 ) 
     311                { 
     312                       fclose(yyin); 
     313                       /* TODO free partially alloc'd shellcodes */ 
    303314                        return NULL; 
    304  
    305                 yyparse(); 
     315                } 
    306316                fclose(yyin); 
    307317                 
     
    311321        char *sc_get_error() 
    312322        { 
    313                 return "no idea"
    314         } 
     323                return error_buffer
     324        } 
  • nepenthes/trunk/modules/shellcode-signatures/y.tab.c

    r422 r423  
    133133        #include <stdio.h> 
    134134        #include <memory.h> 
     135        #include <errno.h> 
    135136         
    136137        #include "parser.h" 
     
    150151        static char *get_mapping_by_numeric(int num); 
    151152         
     153        static char error_buffer[0xff]; 
    152154 
    153155 
     
    178180 
    179181/* Line 214 of yacc.c.  */ 
    180 #line 181 "y.tab.c" 
     182#line 183 "y.tab.c" 
    181183 
    182184#if ! defined (yyoverflow) || YYERROR_VERBOSE 
     
    366368static const unsigned char yyrline[] = 
    367369{ 
    368        0,    44,    44,    46,    50,    75,    83,    88,    93,    98
    369      103,   108,   113,   118,   123,   128,   133,   138,   144,   146
    370      150,   154,   155,   156,   160,   167,   171,   174,   176,   180
    371      184,   188,   192,   196,   200,   204,   211,   219,   221 
     370       0,    46,    46,    48,    52,    77,    85,    90,    95,   100
     371     105,   110,   115,   120,   125,   130,   135,   140,   146,   148
     372     152,   156,   157,   158,   162,   169,   173,   176,   178,   182
     373     186,   190,   194,   198,   202,   206,   213,   221,   223 
    372374}; 
    373375#endif 
     
    11061108    { 
    11071109        case 4: 
    1108 #line 51 "parser.y" 
     1110#line 53 "parser.y" 
    11091111    { 
    11101112                int i; 
     
    11311133 
    11321134  case 5: 
    1133 #line 76 "parser.y" 
     1135#line 78 "parser.y" 
    11341136    { 
    11351137                shellcodes->name = strndup(string_get_buffer(), string_get_len()); 
     
    11391141 
    11401142  case 6: 
    1141 #line 84 "parser.y" 
     1143#line 86 "parser.y" 
    11421144    { 
    11431145                shellcodes->nspace = sc_xor; 
     
    11461148 
    11471149  case 7: 
    1148 #line 89 "parser.y" 
     1150#line 91 "parser.y" 
    11491151    { 
    11501152                shellcodes->nspace = sc_linkxor; 
     
    11531155 
    11541156  case 8: 
    1155 #line 94 "parser.y" 
     1157#line 96 "parser.y" 
    11561158    { 
    11571159                shellcodes->nspace = sc_konstanzxor; 
     
    11601162 
    11611163  case 9: 
    1162 #line 99 "parser.y" 
     1164#line 101 "parser.y" 
    11631165    { 
    11641166                shellcodes->nspace = sc_leimbachxor; 
     
    11671169 
    11681170  case 10: 
    1169 #line 104 "parser.y" 
     1171#line 106 "parser.y" 
    11701172    { 
    11711173                shellcodes->nspace = sc_bindshell; 
     
    11741176 
    11751177  case 11: 
    1176 #line 109 "parser.y" 
     1178#line 111 "parser.y" 
    11771179    { 
    11781180                shellcodes->nspace = sc_connectbackshell; 
     
    11811183 
    11821184  case 12: 
    1183 #line 114 "parser.y" 
     1185#line 116 "parser.y" 
    11841186    { 
    11851187                 shellcodes->nspace = sc_connectbackfiletransfer; 
     
    11881190 
    11891191  case 13: 
    1190 #line 119 "parser.y" 
     1192#line 121 "parser.y" 
    11911193    { 
    11921194                shellcodes->nspace = sc_execute; 
     
    11951197 
    11961198  case 14: 
    1197 #line 124 "parser.y" 
     1199#line 126 "parser.y" 
    11981200    { 
    11991201                shellcodes->nspace = sc_download; 
     
    12021204 
    12031205  case 15: 
    1204 #line 129 "parser.y" 
     1206#line 131 "parser.y" 
    12051207    { 
    12061208                shellcodes->nspace = sc_url; 
     
    12091211 
    12101212  case 16: 
    1211 #line 134 "parser.y" 
     1213#line 136 "parser.y" 
    12121214    { 
    12131215                shellcodes->nspace = sc_link; 
     
    12161218 
    12171219  case 17: 
    1218 #line 139 "parser.y" 
     1220#line 141 "parser.y" 
    12191221    { 
    12201222                shellcodes->nspace = sc_blink; 
     
    12231225 
    12241226  case 24: 
    1225 #line 161 "parser.y" 
     1227#line 163 "parser.y" 
    12261228    { 
    12271229                printf("flags none...\n"); 
     
    12301232 
    12311233  case 29: 
    1232 #line 181 "parser.y" 
     1234#line 183 "parser.y" 
    12331235    { 
    12341236                shellcodes->map[shellcodes->map_items++] = key; 
     
    12371239 
    12381240  case 30: 
    1239 #line 185 "parser.y" 
     1241#line 187 "parser.y" 
    12401242    { 
    12411243                shellcodes->map[shellcodes->map_items++] = size; 
     
    12441246 
    12451247  case 31: 
    1246 #line 189 "parser.y" 
     1248#line 191 "parser.y" 
    12471249    { 
    12481250                shellcodes->map[shellcodes->map_items++] = sizeinvert; 
     
    12511253 
    12521254  case 32: 
    1253 #line 193 "parser.y" 
     1255#line 195 "parser.y" 
    12541256    {    
    12551257                 shellcodes->map[shellcodes->map_items++] = port; 
     
    12581260 
    12591261  case 33: 
    1260 #line 197 "parser.y" 
     1262#line 199 "parser.y" 
    12611263    { 
    12621264                shellcodes->map[shellcodes->map_items++] = host; 
     
    12651267 
    12661268  case 34: 
    1267 #line 201 "parser.y" 
     1269#line 203 "parser.y" 
    12681270    { 
    12691271                shellcodes->map[shellcodes->map_items++] = command; 
     
    12721274 
    12731275  case 35: 
    1274 #line 205 "parser.y" 
     1276#line 207 "parser.y" 
    12751277    { 
    12761278        shellcodes->map[shellcodes->map_items++] = uri; 
     
    12791281 
    12801282  case 36: 
    1281 #line 212 "parser.y" 
     1283#line 214 "parser.y" 
    12821284    { 
    12831285                shellcodes->pattern = strndup(string_get_buffer(), string_get_len()); 
     
    12911293 
    12921294/* Line 1010 of yacc.c.  */ 
    1293 #line 1294 "y.tab.c" 
     1295#line 1296 "y.tab.c" 
    12941296  
    12951297  yyvsp -= yylen; 
     
    15161518 
    15171519 
    1518 #line 224 "parser.y" 
     1520#line 226 "parser.y" 
    15191521 
    15201522 
     
    15791581        int yyerror(char* s) 
    15801582        { 
    1581                 printf(" %s at '%s' on line %d\n", s, yytext, line_number ); 
     1583                snprintf(error_buffer, sizeof(error_buffer), 
     1584                        "%s at '%s' on line %d", s, yytext, line_number); 
    15821585                return 0; 
    15831586        } 
     
    15911594        struct shellcode *sc_parse_file(const char *filename) 
    15921595        { 
    1593                 init_shellcode(); 
    1594                  
    15951596                yyin = fopen(filename, "r"); 
    15961597                 
    15971598                if( yyin == NULL ) 
     1599                { 
     1600                        snprintf(error_buffer, sizeof(error_buffer), "%s", strerror(errno)); 
    15981601                        return NULL; 
    1599  
    1600                 yyparse(); 
     1602                } 
     1603 
     1604                init_shellcode(); 
     1605                if( yyparse() != 0 ) 
     1606                { 
     1607                        fclose(yyin); 
     1608                        /* TODO free partially alloc'd shellcodes */ 
     1609                        return NULL; 
     1610                } 
    16011611                fclose(yyin); 
    16021612                 
     
    16061616        char *sc_get_error() 
    16071617        { 
    1608                 return "no idea"
    1609         } 
    1610  
     1618                return error_buffer
     1619        } 
     1620