Changeset 1582

Show
Ignore:
Timestamp:
02/29/08 20:03:17 (6 months ago)
Author:
till
Message:

nebula
- real time signals for adjusting certain parameters
- write instead of printf in signal handlers

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nebula/trunk/src/classify.c

    r1581 r1582  
    8383                ((hash*)t->data)->cnt++; 
    8484                 
    85                 if (verbose) printf("    md5sum is %s (%u instances)\n", ((hash*)t->data)->md5sum, ((hash*)t->data)->cnt); 
    86                 if (verbose) printf("    absolute match found.\n"); 
     85                if (verbose > 1) printf("    MD5 hash is %s (%u instances)\n", ((hash*)t->data)->md5sum, ((hash*)t->data)->cnt); 
     86                if (verbose) printf("    Absolute MD5 match found.\n"); 
    8787                return(0); 
    8888        } else { 
     
    111111                        exit(EXIT_FAILURE); 
    112112                } 
    113                 if (verbose) printf("    md5sum is %s (%u instances)\n", ((hash*)t->data)->md5sum, ((hash*)t->data)->cnt); 
     113                if (verbose) printf("    MD5 hash is %s (%u instances)\n", ((hash*)t->data)->md5sum, ((hash*)t->data)->cnt); 
    114114 
    115115 
     
    122122                                        if (!((hash*)t->data)->cl) { 
    123123                                                add_entry_to_cluster((cluster *)cur_cqelem->data, (hash*)t->data); 
    124                                                 if (verbose) printf("  cluster has now %u elements.\n", ((hash*)t->data)->cl->cnt); 
     124                                                if (verbose) printf("    Cluster has now %u elements (threshold is %lu).\n", 
     125                                                        ((hash*)t->data)->cl->cnt, ((hash*)t->data)->cl->threshold); 
    125126                                                break; 
    126127                                        } else { 
     
    159160 
    160161 
    161                                         } else if (verbose) printf("  cluster has now %u elements.\n", ((hash*)t->data)->cl->cnt); 
     162                                        } else if (verbose) printf("    Cluster has now %u elements (threshold is %lu).\n", 
     163                                                ((hash*)t->data)->cl->cnt, ((hash*)t->data)->cl->threshold); 
    162164                                } else { 
    163165                                        // create new cluster of two outliers 
     
    186188                                        ((cluster *) clusterq->head->data)->parent = clusterq->head; 
    187189 
    188                                         if (verbose) printf("  cluster created.\n"); 
     190                                        if (verbose) printf("    New Cluster created.\n"); 
    189191                                } 
    190192                        } 
     
    197199 
    198200                         
    199                 if (verbose) printf("    spamsum is %s (%u instances)\n", ((hash*)t->data)->spamsum, ((hash*)t->data)->cnt); 
     201                if (verbose > 1) printf("    Spamsum is %s (%u instances)\n", ((hash*)t->data)->spamsum, ((hash*)t->data)->cnt); 
    200202 
    201203                if (!((hash*)t->data)->cl) { 
     
    216218                        queue_ins(outlierq, t->data, outlierq_max); 
    217219 
    218                         if (verbose) printf("  input added to outlier queue.\n"); 
    219                 } 
    220         } 
    221  
    222         // check for signature generation criteria here 
     220                        if (verbose) printf("    Input added to outlier queue (queue size: %lu).\n", outlierq->size); 
     221                } 
     222        } 
     223 
     224        // check for signature generation criterion here 
    223225        if ((((hash*)t->data)->cl) && ((hash*)t->data)->cl->cnt >= ((hash*)t->data)->cl->threshold) { 
    224                 printf("[=] cluster size (%u) hit threshold (%lu), generating signature.\n", 
     226                printf("[=] Cluster size (%u) hit threshold (%lu), generating signature.\n", 
    225227                        ((hash*)t->data)->cl->cnt, ((hash*)t->data)->cl->threshold); 
    226228 
  • nebula/trunk/src/nebula.c

    r1570 r1582  
    5555                " -a <filename>\t append new snort signatures to this file\n" 
    5656                "\t\t -C <size>\t cluster queue size\n" 
    57                 "\t\t -c <similarity> cluster criteria (a similarity measure in percent)\n" 
     57                "\t\t -c <similarity> cluster criterion (a similarity measure in percent)\n" 
    5858                "\t\t -d\t\t daemonize\n" 
    5959                "\t\t -h\t\t this help\n" 
     
    100100        rules_file              = NULL;         // a: NULL 
    101101        clusterq_max            = 5000;         // C 
    102         cluster_radius          = 95.0;         // c: 95% similarity as cluster criteria 
     102        cluster_radius          = 95.0;         // c: 95% similarity as cluster criterion 
    103103        daemonize               = 0;            // d: 0 
    104104        clusterhashq_max        = 500000;       // E 
     
    236236 
    237237        if (verbose) { 
    238                 printf("  Initial snort signature ID: %u\n\n", global_sid); 
    239                 printf("  Initial cluster size threshold: %lu\n\n", initial_threshold); 
    240         } 
     238                printf("  Initial snort signature ID: %u\n", global_sid); 
     239                printf("  Initial cluster size threshold: %lu\n", initial_threshold); 
     240                printf("  Cluster criterion (minimal similarity): %.1f percent\n", cluster_radius); 
     241        } 
     242        printf("\n"); 
    241243 
    242244 
     
    283285                                                        pthread_mutex_unlock(&sessions_mutex); 
    284286 
    285                                                         if (verbose) printf("[>] Connection accepted.\n"); 
     287                                                        if (verbose > 1) printf("[>] Connection accepted.\n"); 
    286288                                                        break; 
    287289                                                } 
  • nebula/trunk/src/session.c

    r1568 r1582  
    4949        if (pfd) { 
    5050                if (pfd->fd >= 0) close(pfd->fd); 
    51                 if (verbose) printf("[<] Connection terminated.\n"); 
     51                if (verbose > 1) printf("[<] Connection terminated.\n"); 
    5252 
    5353                memset(pfd, 0, sizeof(struct pollfd)); 
     
    9999 
    100100        if ((bytes_read = read(fd, buf + s->bytes_read, len - s->bytes_read)) == -1) { 
    101                 fprintf(stderr, "Error while reading from socket: %s.\n", strerror(errno)); 
     101                fprintf(stderr, "Error - Unable to read from socket: %s.\n", strerror(errno)); 
    102102                return(-1); 
    103103        } 
     
    160160 
    161161                        if (!strncmp(sha512sum, s->secret_hash, 128)) { 
    162                                 if (verbose > 1) printf("  valid secret hash read, session authenticated.\n"); 
     162                                if (verbose > 1) printf("    Valid secret hash read, session authenticated.\n"); 
    163163                                s->state        = AUTHENTICATED; 
    164164                                s->bytes_read   = 0; 
     
    168168 
    169169                        if (s->state != AUTHENTICATED) { 
    170                                 if (verbose > 1) printf("  secret mismatch, dropping session.\n"); 
     170                                if (verbose > 1) printf("    Secret mismatch, dropping session.\n"); 
    171171                                return(0); 
    172172                        } 
     
    185185                } else if (rv == 32) { 
    186186                        s->md5sum[32] = 0; 
    187                         if (verbose > 1) printf("  attack md5sum: %s\n", s->md5sum); 
     187                        if (verbose > 1) printf("    Attack MD5 hash: %s\n", s->md5sum); 
    188188 
    189189                        pthread_rwlock_rdlock(&md5sum_trie_lock); 
     
    197197                                        return(-1); 
    198198                                } 
    199                                 printf("[x] Rejecting known attack\n"); 
    200  
     199 
     200                                // increase instance counter 
     201                                ((hash*)t->data)->cnt++; 
     202                                if (verbose > 1) printf("    MD5 hash is %s (%u instances)\n", ((hash*)t->data)->md5sum, ((hash*)t->data)->cnt); 
     203 
     204                                if (verbose) printf("[x] Absolute MD5 match found, Rejecting known attack\n"); 
    201205                                return(0); 
    202206                        } else { 
     
    208212                        } 
    209213 
    210                         if (verbose > 1) printf("  unknown attack, submission requested.\n"); 
     214                        if (verbose > 1) printf("    Unknown attack, submission requested.\n"); 
    211215                        s->state        = REQUEST_SENT; 
    212216                        s->bytes_read   = 0; 
     
    220224                        switch (s->protocol) { 
    221225                        case 6: 
    222                                 if (verbose > 1) printf("  protocol: TCP\n"); 
     226                                if (verbose > 1) printf("    Protocol: TCP\n"); 
    223227                                break; 
    224228                        case 17: 
    225                                 if (verbose > 1) printf("  protocol: UDP\n"); 
     229                                if (verbose > 1) printf("    Protocol: UDP\n"); 
    226230                                break; 
    227231                        default: 
    228                                 if (verbose > 1) printf("  protocol: unknown\n"); 
     232                                if (verbose > 1) printf("    Protocol: unknown\n"); 
    229233                                return(0); 
    230234                                break; 
     
    240244                } else if (rv == 2) { 
    241245                        s->port = ntohs(s->port); 
    242                         if (verbose > 1) printf("  port: %u\n", s->port); 
     246                        if (verbose > 1) printf("    Port: %u\n", s->port); 
    243247                        s->state        = PORT_READ; 
    244248                        s->bytes_read   = 0; 
     
    251255                } else if (rv == 4) { 
    252256                        s->attack_len = ntohl(s->attack_len); 
    253                         if (verbose > 1) printf("  bytes of attacks: %lu\n", s->attack_len); 
     257                        if (verbose > 1) printf("    Bytes of attack string: %lu\n", s->attack_len); 
    254258                        s->state        = ATTACK_LEN_READ; 
    255259                        s->bytes_read   = 0; 
     
    262266                } else if (rv == 4) { 
    263267                        s->cattack_len = ntohl(s->cattack_len); 
    264                         if (verbose > 1) printf("  bytes of compressed attacks: %u\n", s->cattack_len); 
     268                        if (verbose > 1) printf("    Bytes of compressed attack string: %u\n", s->cattack_len); 
    265269                        s->state        = CATTACK_LEN_READ; 
    266270                        s->bytes_read   = 0; 
     
    276280                        return(-1); 
    277281                } else if (rv == s->cattack_len) { 
    278                         if (verbose > 1) printf("  attack read.\n"); 
     282                        if (verbose > 1) printf("    Attack read.\n"); 
    279283                        s->state        = CATTACK_READ; 
    280284                        s->bytes_read   = 0; 
     
    287291                } else if (rv == 2) { 
    288292                        s->hmac_len = ntohs(s->hmac_len); 
    289                         if (verbose > 1) printf("  length of HMAC: %u\n", s->hmac_len); 
     293                        if (verbose > 1) printf("    Length of HMAC: %u\n", s->hmac_len); 
    290294                        s->state        = HMAC_LEN_READ; 
    291295                        s->bytes_read   = 0; 
     
    300304                        return(-1); 
    301305                } else if (rv == s->hmac_len) { 
    302                         if (verbose > 1) printf("  HMAC read: %s\n", s->hmac); 
     306                        if (verbose > 1) printf("    HMAC read: %s\n", s->hmac); 
    303307                        hmac_port = htons(s->port); 
    304308 
     
    308312                                return(-1); 
    309313                        } 
    310                         if (verbose > 1) printf("  response sent.\n"); 
     314                        if (verbose > 1) printf("    Response sent.\n"); 
    311315 
    312316 
     
    338342 
    339343                        free(sha512sum); 
    340                         if (verbose > 1) printf("  received HMAC is valid.\n"); 
     344                        if (verbose > 1) printf("  HMAC is valid.\n"); 
    341345 
    342346 
     
    361365                                exit(EXIT_FAILURE); 
    362366                        } 
    363                         if (verbose > 1) printf("  successfully uncompressed %lu bytes.\n", s->attack_len); 
     367                        if (verbose > 1) printf("    Successfully uncompressed %lu bytes.\n", s->attack_len); 
    364368 
    365369                        if (s->protocol == 6) 
     
    371375                        md5sum = mem_md5sum(s->attack, s->attack_len); 
    372376                        if (md5sum && strncmp(md5sum, s->md5sum, 32)) { 
    373                                 fprintf(stderr, " Attack checksum does not match, discarding submission.\n"); 
     377                                fprintf(stderr, "[x] Attack checksum does not match, discarding submission.\n"); 
    374378                                return(-1); 
    375379                        } 
  • nebula/trunk/src/signals.c

    r1566 r1582  
    3131 
    3232 
    33 void handle_usr_signal(int sig) { 
    34         switch (sig) { 
    35         case SIGUSR1: 
     33                                        // SIGRTMIN+[0..2] are used for thread control 
     34#define SIGINCV         (SIGRTMIN+3)    // signal to increase verbosity level 
     35#define SIGDECV         (SIGRTMIN+4)    // signal to decrease verbosity level 
     36#define SIGINCC         (SIGRTMIN+5)    // signal to increase similarity threshold by 5 
     37#define SIGDECC         (SIGRTMIN+6)    // signal to decrease similarity threshold by 5 
     38 
     39 
     40void handle_ctrl_signal(int sig) { 
     41        if (sig == SIGINCV) { 
    3642                verbose++; 
    37                 printf("Verbosity level increased to %u.\n", verbose); 
    38                 break; 
    39         case SIGUSR2: 
    40                 if (!verbose) break; 
     43                write(STDOUT_FILENO, "[*] Verbosity level increased.\n", 31); 
     44                return; 
     45        } 
     46        if (sig == SIGDECV) { 
     47                if (!verbose) return; 
    4148                verbose--; 
    42                 printf("Verbosity level decreased to %u.\n", verbose); 
    43                 break; 
    44         default: 
    45                 break; 
     49                write(STDOUT_FILENO, "[*] Verbosity level decreased.\n", 31); 
     50                return; 
     51        } 
     52        if (sig == SIGINCC) { 
     53                if (cluster_radius == 100.0) return; 
     54                if (cluster_radius < 95.0) { 
     55                        cluster_radius += 5.0; 
     56                        write(STDOUT_FILENO, "[*] Cluster criterion increased by 5 percent\n", 44); 
     57                } else { 
     58                        cluster_radius = 100.0; 
     59                        write(STDOUT_FILENO, "[*] Cluster criterion increased to 100 percent\n", 46); 
     60                } 
     61                return; 
     62        } 
     63        if (sig == SIGDECC) { 
     64                if (cluster_radius == 0.0) return; 
     65                if (cluster_radius > 5.0) { 
     66                        cluster_radius -= 5.0; 
     67                        write(STDOUT_FILENO, "[*] Cluster criterion decreased by 5 percent\n", 44); 
     68                } else { 
     69                        cluster_radius = 0.0; 
     70                        write(STDOUT_FILENO, "[*] Cluster criterion decreased to 0 percent\n", 44); 
     71                } 
     72                return; 
    4673        } 
    4774        return; 
     
    4976 
    5077void handle_term_signal(int sig) { 
    51         if (verbose) printf("\nPremature termination forced (signal %d caught).\n", sig); 
     78        if (sig == SIGINT) { 
     79                // free memory and quit 
     80                if (verbose) write(STDOUT_FILENO, "[*] Termination forced, cleaning up.\n", 37); 
     81                cleanup(); 
     82                write(STDOUT_FILENO, "[*] Done.\n", 10); 
     83                exit(EXIT_SUCCESS); 
     84        } 
    5285 
    53         cleanup(); 
    54  
    55         printf("[*] Done.\n"); 
    56         exit(EXIT_SUCCESS); 
    57         return; 
     86        // signal indicates an error 
     87        write(STDOUT_FILENO, "\n[*] Premature termination forced.\n", 35); 
     88        exit(EXIT_FAILURE); 
    5889} 
    59  
    60  
    61 #ifdef PROFILE 
    62 void handle_alarm_signal(int sig) { 
    63         if (show_progress) printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\n"); 
    64         printf("%*u | files: %*u\tcluster: %*u\toutlier: %*u\tduplicates: %*u \tfiles/second: %*.0f\tbytes/second: %*u\n", 
    65                 6, checkpoint, 
    66                 8, (unsigned int) num_of_files, 
    67                 5, (unsigned int) clusterq->size, 
    68                 7, (unsigned int) outlierq->size, 
    69                 7, num_of_duplicates, 
    70                 6, files_in_interval/alarm_time, 
    71                 9, (unsigned int) bytes_in_interval/alarm_time); 
    72         fflush(stdout); 
    73  
    74         checkpoint++; 
    75         files_in_interval = 0; 
    76         bytes_in_interval = 0; 
    77         alarm(alarm_time); 
    78  
    79         return; 
    80 } 
    81 #endif 
    8290 
    8391 
     
    96104                SIGTERM 
    97105        }; 
    98         // install handler for signals to toggle verbosity 
     106 
     107 
     108        // install handler for control realtime signals 
    99109        memset(&s_action, 0, sizeof(struct sigaction)); 
    100         s_action.sa_handler     = handle_usr_signal; 
     110        s_action.sa_handler     = handle_ctrl_signal; 
    101111#ifdef SA_RESTART 
    102112        s_action.sa_flags       |= SA_RESTART; 
    103113#endif 
    104         if (sigaction(SIGUSR1, &s_action, NULL) == -1) { 
    105                 fprintf(stdout, "  Error - Unable to install handler for signal %d: %m.\n", SIGUSR1); 
    106                 exit(EXIT_FAILURE); 
     114        for (i = 3; i < 8; i++) { 
     115                if (sigaction(SIGRTMIN+i, &s_action, NULL) == -1) { 
     116                        fprintf(stdout, "  Error - Unable to install handler for signal %d: %m.\n", SIGRTMIN+i); 
     117                        exit(EXIT_FAILURE); 
     118                } 
    107119        } 
    108         if (sigaction(SIGUSR2, &s_action, NULL) == -1) { 
    109                 fprintf(stdout, "  Error - Unable to install handler for signal %d: %m.\n", SIGUSR2); 
    110                 exit(EXIT_FAILURE); 
    111         } 
     120 
    112121 
    113122        // install handler for signals which terminate the process  
     
    124133        } 
    125134 
    126 #ifdef PROFILE 
    127         // install handler for SIGALARM 
    128         memset(&s_action, 0, sizeof(struct sigaction)); 
    129         s_action.sa_handler     = handle_alarm_signal; 
    130 #ifdef SA_RESTART 
    131         s_action.sa_flags       |= SA_RESTART; 
    132 #endif 
    133         if (sigaction(SIGALRM, &s_action, NULL) == -1) { 
    134                 fprintf(stdout, "  Error - Unable to install handler for signal %d: %m.\n", SIGALRM); 
    135                 exit(EXIT_FAILURE); 
    136         } 
    137 #endif 
    138  
    139135        return; 
    140136} 
  • nebula/trunk/src/signals.h

    r1566 r1582  
    2929void handle_usr_signal(int sig); 
    3030void handle_term_signal(int sig); 
    31 #ifdef PROFILE 
    32 void handle_alarm_signal(int sig); 
    33 #endif 
    3431void set_signal_handlers(void); 
    3532 
  • nebula/trunk/src/util.c

    r1566 r1582  
    108108        lock_mutex = 0; 
    109109 
    110         printf("[*] Cleaning up...\n"); 
    111110        // close all open sessions 
    112111        for (i = 0; i+1 < POLLFD_SET_SIZE; i++)