Changeset 1641

Show
Ignore:
Timestamp:
06/30/08 23:31:07 (2 months ago)
Author:
till
Message:

honeytrap
- configurable sensor id
- length restriction for sql statements removed

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • honeytrap/trunk/src/modules/htm_submitPostgres.c

    r1636 r1641  
    4343 
    4444const char      module_name[]           = "submitPostgres"; 
    45 const char      module_version[]        = "0.1.0"; 
     45const char      module_version[]        = "0.1.1"; 
    4646 
    4747static const char *config_keywords[] = { 
     48        "sensor_id", 
    4849        "db_host", 
    4950        "db_port", 
     
    5455 
    5556struct pg_conn  *db_connection; 
    56 char            *db_host = NULL, 
     57char            *sensor_id = NULL, 
     58                *db_host = NULL, 
    5759                *db_port = NULL, 
    5860                *db_name = NULL, 
     
    6163                *db_info = NULL; 
    6264 
    63 #define MAX_SQL_BUFFER  10485760                // 10 MB 
    64 #define MAX_URI_SIZE    2048 
    65  
    6665 
    6766void plugin_init(void) { 
    68         /* TODO: register sensor in db, if not existent */ 
    6967        plugin_register_hooks(); 
    7068        register_plugin_confopts(module_name, config_keywords, sizeof(config_keywords)/sizeof(char *)); 
     
    7371                exit(EXIT_FAILURE); 
    7472        } 
     73 
     74        // check if all needed options are given 
     75        if (db_host == NULL) { 
     76                fprintf(stderr, "  SubmitPostgres Error - Incomplete configuration: Database host missing.\n"); 
     77                exit(EXIT_FAILURE); 
     78        } 
     79        if (db_name == NULL) { 
     80                fprintf(stderr, "  SubmitPostgres Error - Incomplete configuration: Database name missing.\n"); 
     81                exit(EXIT_FAILURE); 
     82        } 
     83        if (db_user == NULL) { 
     84                fprintf(stderr, "  SubmitPostgres Error - Incomplete configuration: Database user missing.\n"); 
     85                exit(EXIT_FAILURE); 
     86        } 
     87        if (db_pass == NULL) { 
     88                fprintf(stderr, "  SubmitPostgres Error - Incomplete configuration: Database password missing.\n"); 
     89                exit(EXIT_FAILURE); 
     90        } 
     91        if (sensor_id == NULL) { 
     92                fprintf(stderr, "  SubmitPostgres Error - Incomplete configuration: Sensor ID missing.\n"); 
     93                exit(EXIT_FAILURE); 
     94        } 
     95 
    7596        return; 
    7697} 
     
    95116        conf_node       *confopt = NULL; 
    96117 
    97         if ((confopt = check_keyword(tree, node->keyword)) == NULL) return(NULL)
     118        if ((confopt = check_keyword(tree, node->keyword)) == NULL) return NULL
    98119 
    99120        while (node->val) { 
    100121                if ((value = malloc(node->val->size+1)) == NULL) { 
    101                         perror("  Error - Unable to allocate memory"); 
     122                        fprintf(stderr, "  SubmitPostgres Error - Unable to allocate memory: %s.", strerror(errno)); 
    102123                        exit(EXIT_FAILURE); 
    103124                } 
     
    117138                } else if OPT_IS("db_pass") { 
    118139                        db_pass = value; 
     140                } else if OPT_IS("sensor_id") { 
     141                        sensor_id = value; 
    119142                } else { 
    120                         fprintf(stderr, "  Error - Invalid configuration option for plugin %s: %s\n", module_name, node->keyword); 
     143                        fprintf(stderr, "  SubmitPostgres Error - Invalid configuration option for plugin %s: %s\n", module_name, node->keyword); 
    121144                        exit(EXIT_FAILURE); 
    122145                } 
    123146        } 
    124         return(node); 
     147 
     148        return node; 
    125149} 
    126150 
     
    134158                } 
    135159        } 
    136         if (db_host == NULL) { 
    137                 logmsg(LOG_ERR, 1, "SubmitPostgres Error - Database connection info is incomplete: Host missing.\n"); 
    138                 return(-1); 
    139         } 
    140         if (db_name == NULL) { 
    141                 logmsg(LOG_ERR, 1, "SubmitPostgres Error - Database connection info is incomplete: Database name missing.\n"); 
    142                 return(-1); 
    143         } 
    144         if (db_user == NULL) { 
    145                 logmsg(LOG_ERR, 1, "SubmitPostgres Error - Database connection info is incomplete: User missing.\n"); 
    146                 return(-1); 
    147         } 
    148         if (db_pass == NULL) { 
    149                 logmsg(LOG_ERR, 1, "SubmitPostgres Error - Database connection info is incomplete: Password missing.\n"); 
    150                 return(-1); 
    151         } 
    152160 
    153161        if (asprintf(&db_info, "port=%s host=%s user=%s password=%s dbname=%s", db_port, db_host, db_user, db_pass, db_name) == -1) { 
    154162                logmsg(LOG_ERR, 1, "SubmitPostgres Error - Unable to allocate memory: %s.\n", strerror(errno)); 
    155                 return(-1)
     163                return -1
    156164        } 
    157165 
     
    160168                logmsg(LOG_ERR, 1, "SubmitPostgres Error - Could not connect to database: %s.\n", PQerrorMessage(db_connection)); 
    161169                PQfinish(db_connection); 
    162                 return(-1)
     170                return -1
    163171        } 
    164172        logmsg(LOG_DEBUG, 1, "SubmitPostgres - Database connection established.\n"); 
     
    166174                logmsg(LOG_ERR, 1, "SubmitPostgres Error - Could not set database character encoding to UTF8: %s.\n", PQerrorMessage(db_connection)); 
    167175                PQfinish(db_connection); 
    168                 return(-1)
    169         } 
    170         return(0)
     176                return -1
     177        } 
     178        return 0
    171179} 
    172180 
    173181 
    174182void db_disconnect(void) { 
    175         /* disconnect from database */ 
     183        // disconnect from database 
    176184        PQfinish(db_connection); 
    177185        free(db_info); 
     
    188196        if (download->dl_type == NULL) { 
    189197                logmsg(LOG_WARN, 1, "SubmitPostgres Warning - Could not build URI: Unknown protocol type.\n"); 
    190                 return(NULL)
     198                return NULL
    191199        } 
    192200 
     
    202210        } 
    203211 
    204         return(uri)
     212        return uri
    205213} 
    206214 
     
    214222        char            *locationID = NULL; 
    215223 
    216         // no data - nothing todo 
     224        // no data - nothing to do 
    217225        if ((attack->a_conn.payload.size == 0) || (attack->a_conn.payload.data == NULL)) { 
    218226                logmsg(LOG_DEBUG, 1, "SubmitPostgres - No data received, nothing to save.\n"); 
    219                 return(0)
     227                return 0
    220228        } 
    221229 
     
    225233        if (db_connect() != 0) { 
    226234                logmsg(LOG_ERR, 1, "SubmitPostgres Error - Unable to connect to database: %s.\n", PQerrorMessage(db_connection)); 
    227                 return(-1)
     235                return -1
    228236        } 
    229237        logmsg(LOG_DEBUG, 1, "SubmitPostgres - Connection to database established.\n"); 
     
    235243                PQclear(res); 
    236244                db_disconnect(); 
    237                 return(-1)
     245                return -1
    238246        } 
    239247        PQclear(res); 
     
    243251        if (attack->a_conn.payload.size > 0 && attack->a_conn.payload.data != NULL) { 
    244252          logmsg(LOG_DEBUG, 1, "SubmitPostgres - Submitting attack string.\n"); 
    245                 if ((query = malloc(MAX_SQL_BUFFER + 1)) == NULL) { 
    246                         logmsg(LOG_ERR, 1, "SubmitPostgres Error - Unable to allocate memory: %s.\n", strerror(errno)); 
    247                         return(-1); 
    248                 } 
    249                 memset(query, 0, MAX_SQL_BUFFER + 1); 
    250  
    251253                // escape attack string 
    252254                if ((esc_bytea = PQescapeByteaConn(db_connection, attack->a_conn.payload.data, attack->a_conn.payload.size, &length)) == NULL) { 
    253255                        logmsg(LOG_ERR, 1, "Database error - Could not escape attack string: %s.\n", PQerrorMessage(db_connection)); 
    254256                        db_disconnect(); 
    255                         return(-1)
     257                        return -1
    256258                } 
    257259 
     
    259261                        logmsg(LOG_ERR, 1, "SubmitPostgres Error - Unable to allocate memory: %s.\n", strerror(errno)); 
    260262                        db_disconnect(); 
    261                         return(-1)
     263                        return -1
    262264                } 
    263265                if ((strftime(starttime, 40, "%Y-%m-%d %T %Z", localtime(&attack->start_time)) == 0) ||  
     
    265267                        logmsg(LOG_ERR, 1, "SubmitPostgres Error - Unable to convert timestamps.\n"); 
    266268                        db_disconnect(); 
    267                         return(-1)
     269                        return -1
    268270                } 
    269271 
     
    275277                        db_disconnect(); 
    276278                        free(query); 
    277                         return(-1)
     279                        return -1
    278280                } 
    279281                 
    280                 if (snprintf(query, MAX_SQL_BUFFER
     282                if (asprintf(&query
    281283                             "SELECT attacks.honeytrap_add_attack_string('%s'::varchar, %d::integer, '%s'::timestamptz, '%s'::timestamptz, '%s'::inet, %s::integer, %d::integer, '%s'::inet, %d::integer, %d, %d::smallint, '%s'::inet, %d::integer, E'%s'::bytea)", 
    282284                             attack->a_conn.payload.md5sum, 
     
    293295                             fwd_ip, 
    294296                             attack->p_conn.r_port, 
    295                              esc_bytea) >= MAX_SQL_BUFFER) { 
    296                   logmsg(LOG_ERR, 1, "Error - Could not save attack: SQL query exceeds maximum size (increase MAX_SQL_BUFFER and recompile).\n"); 
    297                   free(query); 
    298                   return(-1); 
    299                   } 
     297                             esc_bytea) == -1) { 
     298                        logmsg(LOG_ERR, 1, "SubmitPostgres Error - Could not create SQL query: %s.\n", strerror(errno)); 
     299                        return -1; 
     300                } 
    300301 
    301302                if (PQresultStatus(res = PQexec(db_connection, query)) != PGRES_TUPLES_OK) { 
     
    304305                        db_disconnect(); 
    305306                        free(query); 
    306                         return(-1)
     307                        return -1
    307308                } 
    308309                 
     
    326327 
    327328                        for(i=0;i<attack->dl_count;i++) { 
    328                                 if ((query = calloc(1, MAX_SQL_BUFFER + 1)) == NULL) { 
    329                                         logmsg(LOG_ERR, 1, "Error - Unable to allocate memory: %s.\n", strerror(errno)); 
    330                                         return(-1); 
    331                                 } 
    332  
    333329                                // escape data 
    334330                                if ((esc_bytea = PQescapeByteaConn(db_connection, attack->download[i].dl_payload.data, attack->download[i].dl_payload.size, &length)) == NULL) { 
     
    337333                                        db_disconnect(); 
    338334                                        free(query); 
    339                                         return(-1)
     335                                        return -1
    340336                                } 
    341337 
     
    349345                                        logmsg(LOG_ERR, 1, "SubmitPostgres Error - Unable to allocate memory: %s.\n", strerror(errno)); 
    350346                                        free(url); 
    351                                         return(-1)
     347                                        return -1
    352348                                } 
    353                                 if (snprintf(query, MAX_SQL_BUFFER, "SELECT malware.sensor_honeytrap_add_sample('%s', '%s', %d, '%s', '%s', '%s', %d, %d, E'%s')", 
    354                                     attack->download[i].dl_payload.sha512sum, 
    355                                     "UBN_HT"
    356                                     attack_inst, 
    357                                     url, 
    358                                     l_ip, 
    359                                     r_ip, 
    360                                     attack->a_conn.l_port, 
    361                                     attack->download->r_port, 
    362                                     esc_bytea) >= MAX_SQL_BUFFER) { 
     349                                if (asprintf(&query, "SELECT malware.sensor_honeytrap_add_sample('%s', '%s', %d, '%s', '%s', '%s', %d, %d, E'%s')", 
     350                                            attack->download[i].dl_payload.sha512sum, 
     351                                            sensor_id
     352                                            attack_inst, 
     353                                            url, 
     354                                            l_ip, 
     355                                            r_ip, 
     356                                            attack->a_conn.l_port, 
     357                                            attack->download->r_port, 
     358                                            esc_bytea) == -1) { 
    363359                                        logmsg(LOG_ERR, 1, 
    364                                                 "SubmitPostgres Error - SQL query exceeds maximum size (increase MAX_SQL_BUFFER and recompile plugin).\n"); 
     360                                                "SubmitPostgres Error - Could not create SQL query: %s.\n", strerror(errno)); 
    365361                                        free(url); 
    366                                         free(query); 
    367                                         return(-1); 
     362                                        return -1; 
    368363                                } 
    369364                                free(url); 
     
    374369                                        db_disconnect(); 
    375370                                        free(query); 
    376                                         return(-1)
     371                                        return -1
    377372                                } 
    378373                                free(query); 
     
    385380        } 
    386381 
    387         /* end transaction and disconnect */ 
     382        // end transaction and disconnect 
    388383        if (PQresultStatus(res = PQexec(db_connection, "END")) != PGRES_COMMAND_OK) { 
    389384                logmsg(LOG_ERR, 1, "SubmitPostgres Error - END command failed: %s.\n", PQerrorMessage(db_connection)); 
    390385                PQclear(res); 
    391386                db_disconnect(); 
    392                 return(-1)
     387                return -1
    393388        } 
    394389 
    395390        PQclear(res); 
    396391        db_disconnect(); 
    397         return(0)
    398 } 
     392        return 0
     393}