Changeset 1238

Show
Ignore:
Timestamp:
04/25/07 23:08:54 (1 year ago)
Author:
till
Message:

udp confusions, fixed

Files:

Legend:

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

    r1221 r1238  
    2828#include <logging.h> 
    2929#include <honeytrap.h> 
     30#include <md5.h> 
    3031#include <attack.h> 
    3132#include <plughook.h> 
     
    5354        struct tm *file_time; 
    5455        time_t loc_time; 
    55         char *filename = NULL, *proto_str = NULL; 
    56         int dumpfile_fd; 
     56        char *filename, *mwfilename, *proto_str; 
     57        int i, dumpfile_fd; 
     58 
     59        filename        = NULL; 
     60        mwfilename      = NULL; 
     61        proto_str       = NULL; 
    5762 
    5863        logmsg(LOG_DEBUG, 1, "Dumping attack string into file.\n"); 
     
    105110        close(dumpfile_fd); 
    106111        logmsg(LOG_DEBUG, 1, "Plugin aSaveFile: Attack string saved as %s.\n", filename); 
     112 
     113        /* save malware */ 
     114        for (i=1; i<=attack->dl_count; i++) { 
     115                /* save file */ 
     116                /* we need the length of directory + "/" + filename plus md5 checksum */ 
     117                mwfilename = (char *) malloc(strlen(dlsave_dir)+strlen(filename)+35); 
     118                snprintf(mwfilename, strlen(dlsave_dir)+strlen(mwfilename) + 35, "%s/%s-%s", 
     119                        dlsave_dir, mem_md5sum(attack->download[i].dl_payload.data, attack->download[i].dl_payload.size), mwfilename); 
     120                logmsg(LOG_DEBUG, 1, "Malware sample dump - File name is %s\n", mwfilename); 
     121                if (((dumpfile_fd = open(mwfilename, O_WRONLY | O_CREAT | O_EXCL)) < 0) || 
     122                    (fchmod(dumpfile_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) != 0)) { 
     123                        logmsg(LOG_WARN, 1, "Malware sample dump - Unable to save %s: %s.\n", mwfilename, 
     124                                strerror(errno)); 
     125                        close(dumpfile_fd); 
     126                        return(-1); 
     127                } 
     128                if (write(dumpfile_fd, attack->download[i].dl_payload.data, attack->download[i].dl_payload.size) != attack->download[i].dl_payload.size) {  
     129                        logmsg(LOG_ERR, 1, "Malware sample dump error - Unable to write data to file: %s\n", 
     130                                strerror(errno)); 
     131                        close(dumpfile_fd); 
     132                        return(-1); 
     133                } 
     134                close(dumpfile_fd); 
     135                logmsg(LOG_NOTICE, 1, "Malware sample dump - %s saved.\n", attack->download[i].filename); 
     136        } 
     137 
    107138        return(0); 
    108139} 
  • honeytrap/trunk/src/modules/htm_aSavePostgres.c

    r1226 r1238  
    6262        /* connect to database */ 
    6363        if (PQstatus(db_connection = PQconnectdb(db_info)) != CONNECTION_OK) { 
    64                 logmsg(LOG_ERR, 1, "Error - Could not connect to database: %s.\n", PQerrorMessage(db_connection)); 
     64                logmsg(LOG_ERR, 1, "Postgres client error - Could not connect to database: %s.\n", PQerrorMessage(db_connection)); 
    6565                PQfinish(db_connection); 
    6666                return(-1); 
    6767        } 
    68         logmsg(LOG_NOISY, 1, "Attack database (Postgres) - Connection established.\n"); 
     68        logmsg(LOG_NOISY, 1, "Postgres client - Database connection established.\n"); 
    6969        if (PQsetClientEncoding(db_connection, "UTF8") != 0) { 
    70                 logmsg(LOG_ERR, 1, "Error - Could not set database character encoding to UTF8: %s.\n", PQerrorMessage(db_connection)); 
     70                logmsg(LOG_ERR, 1, "Postgres client error - Could not set database character encoding to UTF8: %s.\n", PQerrorMessage(db_connection)); 
    7171                PQfinish(db_connection); 
    7272                return(-1); 
     
    7979        /* disconnect from database */ 
    8080        PQfinish(db_connection); 
    81         logmsg(LOG_NOISY, 1, "Attack database (Postgres) - Connection closed.\n"); 
     81        logmsg(LOG_NOISY, 1, "Postgres client - Connection closed.\n"); 
    8282        return; 
    8383} 
    8484 
    8585 
    86 char *build_url(struct s_download *download) { 
    87         char            *url;          // generic malware URL format 'type://user:pass@path/to/file:port/protocol' 
    88  
    89         if ((url = malloc(MAX_URL_SIZE + 1)) == NULL) { 
    90                 logmsg(LOG_ERR, 1, "Error - Unable to allocate memory: %s.\n", strerror(errno)); 
     86char *build_uri(struct s_download *download) { 
     87        char            *uri;          // generic malware URI format 'type://user:pass@path/to/file:port/protocol' 
     88 
     89        if ((uri = malloc(MAX_URI_SIZE + 1)) == NULL) { 
     90                logmsg(LOG_ERR, 1, "Postgres client error - Unable to allocate memory: %s.\n", strerror(errno)); 
    9191                return(NULL); 
    9292        } 
    93         memset(url, 0, MAX_URL_SIZE+1); 
    94  
    95         logmsg(LOG_DEBUG, 1, "Building generic malware resource URL.\n"); 
     93        memset(uri, 0, MAX_URI_SIZE+1); 
     94 
     95        logmsg(LOG_DEBUG, 1, "Postgres client - Building generic malware resource URI.\n"); 
    9696 
    9797        /* should check for supported protocol types */ 
    9898        if (!strlen(download->dl_type)) { 
    99                 logmsg(LOG_WARN, 1, "Database warning - Could not build URL: Unknown protocol type.\n"); 
     99                logmsg(LOG_WARN, 1, "Postgres client - Warning: Could not build URI: Unknown protocol type.\n"); 
    100100                return(NULL); 
    101101        } 
    102         logmsg(LOG_DEBUG, 1, "(Build URL): Typ: %s.\n",download->dl_type); 
    103         snprintf(url + strlen(url), strlen(download->dl_type) + 4, "%s://", download->dl_type); 
     102        logmsg(LOG_DEBUG, 1, "Postgres client - Adding Type to URI: %s\n",download->dl_type); 
     103        snprintf(uri + strlen(uri), strlen(download->dl_type) + 4, "%s://", download->dl_type); 
    104104 
    105105        if(strlen(download->user)) { 
    106                 logmsg(LOG_NOISY,1,"(Build URL): User: %s Pass: %s.\n", download->user, download->pass); 
    107                 snprintf(url + strlen(url), strlen(download->user) + strlen(download->pass) + 3, "%s:%s@", download->user, download->pass); 
    108         } 
    109  
    110         logmsg(LOG_NOISY, 1, "(Build URL): URL: %s.\n", inet_ntoa(*(struct in_addr*)&download->r_addr)); 
    111         strncat(url, inet_ntoa(*(struct in_addr*)&download->r_addr), strlen(inet_ntoa(*(struct in_addr*)&download->r_addr))); 
     106                logmsg(LOG_NOISY,1,"Postgres client - Adding user and pass to URI: %s:%s\n", download->user, download->pass); 
     107                snprintf(uri + strlen(uri), strlen(download->user) + strlen(download->pass) + 3, "%s:%s@", download->user, download->pass); 
     108        } 
     109 
     110        logmsg(LOG_NOISY, 1, "Postgres client - Adding host to URI: %s\n", inet_ntoa(*(struct in_addr*)&download->r_addr)); 
     111        strncat(uri, inet_ntoa(*(struct in_addr*)&download->r_addr), strlen(inet_ntoa(*(struct in_addr*)&download->r_addr))); 
     112 
     113        if (download->filename) { 
     114                logmsg(LOG_NOISY, 1, "Postgres client - Adding filename to URI: %s\n", download->filename); 
     115                snprintf(uri + strlen(uri), strlen(download->filename) + 2, "/%s", download->filename); 
     116        } 
    112117 
    113118        if (download->r_port) { 
    114                 logmsg(LOG_NOISY, 1, "(Build URL): Port: %d.\n", download->r_port); 
    115                 snprintf(url + strlen(url), 7, ":%d/", download->r_port); 
    116                 strcat(url + strlen(url), PROTO(download->protocol)); 
    117         } 
    118  
    119         if (download->filename) { 
    120                 logmsg(LOG_NOISY, 1, "(Build URL): Filename: %s.\n", download->filename); 
    121                 snprintf(url + strlen(url), strlen(download->filename) + 2, "/%s", download->filename); 
    122         } 
    123  
    124         return(url); 
     119                logmsg(LOG_NOISY, 1, "Postgres client - Adding port to URI: %d\n", download->r_port); 
     120                snprintf(uri + strlen(uri), 7, ":%d/", download->r_port); 
     121                strcat(uri + strlen(uri), PROTO(download->protocol)); 
     122        } 
     123 
     124 
     125        return(uri); 
    125126} 
    126127 
     
    128129int db_submit(Attack *attack) { 
    129130        PGresult        *res; 
    130         char            *query, *starttime, *endtime, *url, *l_ip, *r_ip; 
     131        char            *query, *starttime, *endtime, *uri, *l_ip, *r_ip; 
    131132        u_char          *esc_bytea; 
    132133        int             mw_inst = -1; 
     
    136137        if ((!attack->a_conn.payload.size) && (!attack->dl_count)) return(0);  
    137138 
    138         logmsg(LOG_DEBUG, 1, "Attack database (Postgres) - Connecting to database.\n"); 
     139        logmsg(LOG_DEBUG, 1, "Postgres client - Connecting to database.\n"); 
    139140        if (db_connect() != 0) return(-1); 
    140141 
    141142        /* Start a transaction block */ 
    142143        if (PQresultStatus(res = PQexec(db_connection, "BEGIN")) != PGRES_COMMAND_OK) { 
    143                 logmsg(LOG_ERR, 1, "Database error - BEGIN command failed: %s.\n", PQerrorMessage(db_connection)); 
     144                logmsg(LOG_ERR, 1, "Postgres client error - BEGIN command failed: %s.\n", PQerrorMessage(db_connection)); 
    144145                PQclear(res); 
    145146                db_disconnect(); 
     
    152153        if (attack->dl_count) { 
    153154                if ((query = malloc(MAX_SQL_BUFFER + 1)) == NULL) { 
    154                         logmsg(LOG_ERR, 1, "Error - Unable to allocate memory: %s.\n", strerror(errno)); 
     155                        logmsg(LOG_ERR, 1, "Postgres client error - Unable to allocate memory: %s.\n", strerror(errno)); 
    155156                        return(-1); 
    156157                } 
     
    159160                /* escape byte data to prevent sql injection */ 
    160161                if ((esc_bytea = PQescapeByteaConn(db_connection, attack->download->dl_payload.data, attack->download->dl_payload.size, &length)) == NULL) { 
    161                         logmsg(LOG_ERR, 1, "Database error - Could not escape attack string: %s.\n", PQerrorMessage(db_connection)); 
     162                        logmsg(LOG_ERR, 1, "Postgres client error - Could not escape attack string: %s.\n", PQerrorMessage(db_connection)); 
    162163                        PQclear(res); 
    163164                        db_disconnect(); 
     
    168169                mem_md5sum(attack->download->dl_payload.data,attack->download->dl_payload.size); 
    169170 
    170                 if ((url = build_url(attack->download)) == NULL) { 
    171                         logmsg(LOG_WARN, 1, "Warning - Unable to build generic malware URL.\n"); 
    172                         free(url); 
    173                 } else logmsg(LOG_NOISY, 1, "Generic malware URL assembled: %s\n", url); 
     171                if ((uri = build_uri(attack->download)) == NULL) { 
     172                        logmsg(LOG_WARN, 1, "Postgres client warning - Unable to build generic malware URI.\n"); 
     173                        free(uri); 
     174                } else logmsg(LOG_NOISY, 1, "Postgres client - Generic malware URI assembled: %s\n", uri); 
    174175 
    175176                if (((l_ip = strdup(inet_ntoa(*(struct in_addr*)&(attack->a_conn.l_addr)))) == NULL) || 
    176177                    ((r_ip = strdup(inet_ntoa(*(struct in_addr*)&(attack->a_conn.r_addr)))) == NULL)) { 
    177                         logmsg(LOG_ERR, 1, "Error - Unable to allocate memory: %s.\n", strerror(errno)); 
    178                         free(url); 
     178                        logmsg(LOG_ERR, 1, "Postgres client error - Unable to allocate memory: %s.\n", strerror(errno)); 
     179                        free(uri); 
    179180                        return(-1); 
    180181                } 
     
    183184                        "honeytrap-default", 
    184185                        "dynamic-generic", 
    185                         url
     186                        uri
    186187                        inet_ntoa(*(struct in_addr*)&(attack->a_conn.l_addr)), 
    187188                        inet_ntoa(*(struct in_addr*)&(attack->a_conn.r_addr)), 
     
    189190                        attack->download->r_port, 
    190191                        esc_bytea) >= MAX_SQL_BUFFER) { 
    191                         logmsg(LOG_ERR, 1, "Error - Could not save attack: SQL query exceeds maximum size (increase MAX_SQL_BUFFER and recompile).\n"); 
    192                                 free(url); 
     192                        logmsg(LOG_ERR, 1, "Postgres client error - Could not save attack: SQL query exceeds maximum size (increase MAX_SQL_BUFFER and recompile).\n"); 
     193                                free(uri); 
    193194                                free(query); 
    194195                                return(-1); 
    195196                        } 
    196                 free(url); 
     197                free(uri); 
    197198 
    198199                if (PQresultStatus(res = PQexec(db_connection, query)) != PGRES_TUPLES_OK) { 
    199                         logmsg(LOG_ERR, 1, "Database error - Malware submission failed: %s.\n", PQerrorMessage(db_connection)); 
     200                        logmsg(LOG_ERR, 1, "Postgres client error - Malware submission failed: %s.\n", PQerrorMessage(db_connection)); 
    200201                        PQclear(res); 
    201202                        db_disconnect(); 
     
    205206                free(query); 
    206207                PQfreemem(esc_bytea); 
    207                 logmsg(LOG_NOISY, 1, "Attack database (Postgres) - Malware saved.\n"); 
     208                logmsg(LOG_NOISY, 1, "Postgres client - Malware saved.\n"); 
    208209 
    209210                /* get instance number for reference within attack_string record */ 
     
    217218        if (attack->a_conn.payload.size > 0) { 
    218219                if ((query = malloc(MAX_SQL_BUFFER + 1)) == NULL) { 
    219                         logmsg(LOG_ERR, 1, "Error - Unable to allocate memory: %s.\n", strerror(errno)); 
     220                        logmsg(LOG_ERR, 1, "Postgres client error - Unable to allocate memory: %s.\n", strerror(errno)); 
    220221                        return(-1); 
    221222                } 
     
    224225                /* escape byte data to prevent sql injection */     
    225226                if ((esc_bytea = PQescapeByteaConn(db_connection, attack->a_conn.payload.data, attack->a_conn.payload.size, &length)) == NULL) { 
    226                         logmsg(LOG_ERR, 1, "Database error - Could not escape malware binary string: %s.\n", PQerrorMessage(db_connection)); 
     227                        logmsg(LOG_ERR, 1, "Postgres client error - Could not escape malware binary string: %s.\n", PQerrorMessage(db_connection)); 
    227228                        db_disconnect(); 
    228229                        free(query); 
     
    256257                                attack->p_conn.r_port, 
    257258                                esc_bytea) >= MAX_SQL_BUFFER) { 
    258                                         logmsg(LOG_ERR, 1, "Error - Could not save attack: SQL query exceeds maximum size (increase MAX_SQL_BUFFER and recompile).\n"); 
     259                                        logmsg(LOG_ERR, 1, "Postgres client error - Could not save attack: SQL query exceeds maximum size (increase MAX_SQL_BUFFER and recompile).\n"); 
    259260                                        free(query); 
    260261                                        return(-1); 
     
    279280                                attack->p_conn.r_port, 
    280281                                esc_bytea) >= MAX_SQL_BUFFER) { 
    281                                         logmsg(LOG_ERR, 1, "Error - Could not save attack: SQL query exceeds maximum size (increase MAX_SQL_BUFFER and recompile).\n"); 
     282                                        logmsg(LOG_ERR, 1, "Postgres client error - Could not save attack: SQL query exceeds maximum size (increase MAX_SQL_BUFFER and recompile).\n"); 
    282283                                        free(query); 
    283284                                        return(-1); 
     
    286287 
    287288                if (PQresultStatus(res = PQexec(db_connection, query)) != PGRES_TUPLES_OK) { 
    288                         logmsg(LOG_ERR, 1, "Database error - Attack submission failed: %s.\n", PQerrorMessage(db_connection)); 
     289                        logmsg(LOG_ERR, 1, "Postgres client error - Attack submission failed: %s.\n", PQerrorMessage(db_connection)); 
    289290                        PQclear(res); 
    290291                        db_disconnect(); 
     
    293294                } 
    294295 
    295                 logmsg(LOG_NOISY, 1, "Attack database (Postgres) - Attack saved.\n"); 
     296                logmsg(LOG_NOISY, 1, "Postgres client - Attack saved.\n"); 
    296297                free(starttime); 
    297298                free(endtime); 
     
    304305        /* end transaction and disconnect */ 
    305306        if (PQresultStatus(res = PQexec(db_connection, "END")) != PGRES_COMMAND_OK) { 
    306                 logmsg(LOG_ERR, 1, "Database error - END command failed: %s.\n", PQerrorMessage(db_connection)); 
     307                logmsg(LOG_ERR, 1, "Postgres client error - END command failed: %s.\n", PQerrorMessage(db_connection)); 
    307308                PQclear(res); 
    308309                db_disconnect(); 
  • honeytrap/trunk/src/modules/htm_aSavePostgres.h

    r1201 r1238  
    2222 
    2323#define MAX_SQL_BUFFER  10485760                // 10 MB 
    24 #define MAX_URL_SIZE  2048 
     24#define MAX_URI_SIZE  2048 
    2525 
    2626const char      module_name[]           = "htm_aSavePostgres"; 
    27 const char      module_version[]        = "0.1"; 
     27const char      module_version[]        = "0.2"; 
    2828 
    2929struct pg_conn  *db_connection; 
  • honeytrap/trunk/src/modules/htm_b64Decode.c

    r1221 r1238  
    135135                        dec_attack.a_conn.payload.data = decoded->str; 
    136136                        dec_attack.a_conn.payload.size = decoded->len; 
    137 //                      plughook_process_attack(funclist_attack_preproc, dec_attack); 
    138                         plughook_process_attack(funclist_attack_analyze, dec_attack); 
    139                         plughook_process_attack(funclist_attack_savedata, dec_attack); 
    140                         plughook_process_attack(funclist_attack_postproc, dec_attack); 
     137//                      plughook_process_attack(funclist_attack_preproc, &dec_attack); 
     138                        plughook_process_attack(funclist_attack_analyze, &dec_attack); 
     139                        plughook_process_attack(funclist_attack_savedata, &dec_attack); 
     140                        plughook_process_attack(funclist_attack_postproc, &dec_attack); 
    141141 
    142142 
  • honeytrap/trunk/src/modules/htm_ftpDownload.c

    r1226 r1238  
    597597 
    598598                close(data_sock_fd); 
    599 printf("---> returning.\n"); 
    600599                return(0); 
    601600        } else logmsg(LOG_DEBUG, 1, "FTP download - Select on FTP data channel returned but socket is not set: %s\n", strerror(errno)); 
  • honeytrap/trunk/src/modules/htm_ftpDownload.h

    r1201 r1238  
    1919 
    2020const char module_name[]="htm_ftpDownload"; 
    21 const char module_version[]="0.3.1"; 
     21const char module_version[]="0.4.0"; 
    2222 
    2323void plugin_init(void); 
  • honeytrap/trunk/src/pcapmon.c

    r1117 r1238  
    8888                ip_hdr  = (struct ip_header *) ip; 
    8989                udp     = (struct udp_header *) (ip + (4 * ip_hdr->ip_hlen)); 
    90                 sport   = ntohs(udp->uh_dport); 
    91                 dport   = ntohs(udp->uh_sport); 
     90                sport   = ntohs(udp->uh_sport); 
     91                dport   = ntohs(udp->uh_dport); 
    9292                port_mode = port_flags[dport].udp; 
    9393        } else { 
     
    119119        } 
    120120 
    121         logmsg(LOG_INFO, 1, "Connection request on port %d/%s.\n", sport, PROTO(ip_hdr->ip_p)); 
    122         start_dynamic_server(ip_hdr->ip_dst, htons(dport), ip_hdr->ip_src, htons(sport), ip_hdr->ip_p); 
     121        if (ip_hdr->ip_p == UDP) { 
     122                logmsg(LOG_INFO, 1, "Connection request on port %d/udp.\n", dport); 
     123                start_dynamic_server(ip_hdr->ip_src, htons(sport), ip_hdr->ip_dst, htons(dport), ip_hdr->ip_p); 
     124        } else if (ip_hdr->ip_p == TCP) { 
     125                logmsg(LOG_INFO, 1, "Connection request on port %d/tcp.\n", sport); 
     126                start_dynamic_server(ip_hdr->ip_dst, htons(dport), ip_hdr->ip_src, htons(sport), ip_hdr->ip_p); 
     127        } 
    123128        return; 
    124129} 
  • honeytrap/trunk/src/readconf.c

    r1201 r1238  
    293293                                        " %s%c", my_argv[optind], 0); 
    294294                        } 
    295                         my_argv[optind++]
     295                        optind++
    296296                } 
    297297                DEBUG_FPRINTF(stdout, "  Command line bpf expression is '%s'\n", bpf_cmd_ext);