Changeset 1389

Show
Ignore:
Timestamp:
09/22/07 22:05:09 (1 year ago)
Author:
till
Message:

honeytrap - non-blocking reads for connectback sessions

Files:

Legend:

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

    r1387 r1389  
     1/* connectback.c 
     2 * Copyright (C) 2007 Tillmann Werner <tillmann.werner@gmx.de> 
     3 * 
     4 * This file is free software; as a special exception the author gives 
     5 * unlimited permission to copy and/or distribute it, with or without 
     6 * modifications, as long as this notice is preserved. 
     7 * 
     8 * This program is distributed in the hope that it will be useful, but 
     9 * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the 
     10 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
     11 */ 
     12 
     13#include <arpa/inet.h> 
     14#include <errno.h> 
     15#include <netdb.h> 
    116#include <stdio.h> 
    217#include <stdlib.h> 
    318#include <string.h> 
    419#include <strings.h> 
    5 #include <netdb.h> 
    620#include <sys/socket.h> 
    7 #include <arpa/inet.h> 
     21#include <sys/time.h> 
     22#include <sys/types.h> 
    823#include <unistd.h> 
    9 #include <errno.h> 
    1024 
    1125#include <logging.h> 
     
    1529#include "honeytrap.h" 
    1630#include "sc_action.h" 
     31#include "signals.h" 
    1732#include "sock.h" 
    1833#include "tcpip.h" 
     
    2035 
    2136int connectback(struct sc_action* sa, int haskey) { 
    22         int sockfd, t, bytes_read, total_bytes; 
    23         fd_set read_fds; 
    24         struct timeval st; 
    25         struct sockaddr_in sock; 
    26         u_char buffer[BUFSIZ], *attack_string; 
    27         Attack *a; 
     37        int                    sockfd, t, bytes_read, total_bytes; 
     38        fd_set                 rfds; 
     39        struct timeval         st; 
     40        struct sockaddr_in     sock; 
     41        u_char                 buffer[BUFSIZ], *attack_string; 
     42        Attack                 *a; 
    2843 
    2944        if ((a = new_virtattack(*(struct in_addr*) &sa->m_localhost, *(struct in_addr*) &sa->m_action.m_connectback.m_remotehost, 
     
    5267                return(0); 
    5368        } 
    54         logmsg(LOG_INFO, 1, "CSPM - Connected back to %s:%d.\n", 
     69        logmsg(LOG_INFO, 1, "CSPM - Successfully connected back to %s:%d.\n", 
    5570                        inet_ntoa(*(struct in_addr *)&sa->m_action.m_connectback.m_remotehost), 
    5671                        sa->m_action.m_connectback.m_remoteport); 
     
    6479        /* send key */ 
    6580        if (haskey) { 
     81logmsg(LOG_INFO, 1, "CSPM - Sending key.\n"); 
    6682                if (write(sockfd, &sa->m_action.m_connectback.m_key, sizeof(sa->m_action.m_connectback.m_key)) < 
    6783                        sizeof(sa->m_action.m_connectback.m_key)) {  
     
    7591        /* read data */ 
    7692        for(;;) { 
    77                 FD_ZERO(&read_fds); 
    78                 FD_SET(sockfd, &read_fds); 
     93                FD_ZERO(&rfds); 
     94                FD_SET(sockfd, &rfds); 
    7995 
    8096                st.tv_sec  = 10; 
    8197                st.tv_usec = 0; 
    8298 
    83                 switch (t = select(FD_SETSIZE, &read_fds, NULL, NULL, &st)) { 
    84                         case -1: 
    85                                 fprintf(stderr, "Error with select(): %s.\n", strerror(errno)); 
    86                                 exit(1); 
    87                         case  0: 
    88                                 break; 
    89                         default: 
     99                switch (t = select(MAX(sigpipe[0], sockfd), &rfds, NULL, NULL, &st)) { 
     100                case -1: 
     101                        fprintf(stderr, "Error with select(): %s.\n", strerror(errno)); 
     102                        exit(1); 
     103                case  0: 
     104                        break; 
     105                default: 
     106                        if (FD_ISSET(sigpipe[0], &rfds) && (check_sigpipe() == -1)) { 
     107                                logmsg(LOG_ERR, 1, "Error - Signal handling failed in dynamic server process.\n"); 
     108                                exit(EXIT_FAILURE); 
     109                        } 
     110                        if (FD_ISSET(sockfd, &rfds)) {  
    90111                                if ((bytes_read = read(sockfd, buffer, BUFSIZ)) < 0) {  
    91112                                        logmsg(LOG_ERR, 1, "CSPM - Error while reading data from connectback socket: %s.\n", 
     
    124145                                // process data 
    125146                                return(process_data(attack_string, total_bytes, NULL, 0, a->a_conn.l_port, a)); 
     147                        } 
    126148                } 
    127149        }