[Autosec-devel] sonar/plugins network_icmp.c,1.31,1.32
Brought to you by:
red0x
From: <re...@us...> - 2003-12-05 22:39:33
|
Update of /cvsroot/autosec/sonar/plugins In directory sc8-pr-cvs1:/tmp/cvs-serv21359/plugins Modified Files: network_icmp.c Log Message: Added support for hostmasks, hostunreachable addresses are correct now, indentation update Index: network_icmp.c =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/network_icmp.c,v retrieving revision 1.31 retrieving revision 1.32 diff -C2 -d -r1.31 -r1.32 *** network_icmp.c 10 Nov 2003 10:15:06 -0000 1.31 --- network_icmp.c 5 Dec 2003 22:39:30 -0000 1.32 *************** *** 396,400 **** if(mthis->pCon.bBroadcast > 0 && targets->extra_hosts == 0) recd.hosts += (unsigned int) mthis->pCon.bBroadcast * 255; ! low_seq = clock(); /*seq_no;*/ /* set the default timeout values */ --- 396,400 ---- if(mthis->pCon.bBroadcast > 0 && targets->extra_hosts == 0) recd.hosts += (unsigned int) mthis->pCon.bBroadcast * 255; ! low_seq = recd.start = clock(); /*seq_no;*/ /* set the default timeout values */ *************** *** 409,416 **** if(pthread_create(&tid, &attr, recv_thread, &recd) != 0) err_sys("could not create thread"); ! ! while(targets != NULL && counter <= num_hosts) { ! if(icmp_ping(targets) == PLUGIN_ERROR) { --- 409,427 ---- if(pthread_create(&tid, &attr, recv_thread, &recd) != 0) err_sys("could not create thread"); ! ! while(targets != NULL && counter < num_hosts) { ! /* we have an ip-range here */ ! if(targets->extra_hosts > 0) ! { ! /* increas the timeout */ ! recd.max += (timeout * targets->extra_hosts * CLOCKS_PER_SEC) / 1000; ! #ifdef DEBUG ! fprintf(stderr, "%s: increasing recd.hosts by %d\n", targets->entry, targets->extra_hosts); ! #endif ! recd.hosts += targets->extra_hosts; ! if(mthis->sCon.iVerbose > 0) ! err_msg("Increasing timeout, netmask detected: %d", recd.max); ! } if(icmp_ping(targets) == PLUGIN_ERROR) { *************** *** 497,503 **** --- 508,517 ---- int total_size = sizeof(struct icmp), dlen = 0; int bwrote; + unsigned int j; //struct timezone tz; //register struct timeval *tp; char *buf, *datapart; + struct sockaddr_in *sa; + struct in_addr ia; assert(target != NULL); *************** *** 508,598 **** assert(my_socket != -1); /* set the packet size to specified */ if(mthis->pCon.iSize != 0) total_size += mthis->pCon.iSize; ! /* or pick a random size */ else total_size += 64 + (int) (290.0 * rand() / (RAND_MAX + 64.0)); - - /* how much of the packet is data? */ - dlen = total_size - sizeof(struct icmp); - buf = (char *) malloc(total_size); - if(!buf) - { - err_ret("failed to malloc"); - return PLUGIN_ERROR; - } - memset(buf, 0, total_size); ! icmp_hdr = (struct icmp *) buf; ! icmp_hdr->icmp_type = my_type; ! icmp_hdr->icmp_code = my_code; ! icmp_hdr->icmp_id = (unsigned short) getpid(); ! icmp_hdr->icmp_cksum = 0; ! /* random sequence number, to make fingerprinting this tool harder */ ! /* clock works well, that way we can count round trip time as well */ ! icmp_hdr->icmp_seq = seq_no2 = clock();/*seq_no2 = low_seq + (int) (25.0 * rand() / (RAND_MAX + 1.0));*/ ! datapart = buf + sizeof(struct icmp); ! /* no payload */ ! if(!payload_file) ! { ! /* do we want a pattern? */ ! if(mthis->pCon.sPattern) ! { ! int i, len = strlen(mthis->pCon.sPattern); ! int num = dlen / len; ! for(i = 0; i < num; i++) ! { ! strncpy(datapart, mthis->pCon.sPattern, len); ! datapart += len; ! } ! } ! /* or random garbage */ ! else ! memset(datapart, ! (int) (256.0 * rand() / (RAND_MAX + 0.0)), ! dlen); ! } ! /* we've got a payload */ ! else ! { ! unsigned int len = 0, cur = 0; ! FILE *payload; ! payload = fopen(payload_file, "r"); ! if(!payload) ! { ! *(pthis.err) = PLUGIN_NOFILE; ! err_ret("could not open payload file"); ! return PLUGIN_ERROR; ! } ! fseek(payload, 0, SEEK_END); ! len = (unsigned int) ((ftell(payload) > dlen) ? dlen : ! ftell(payload)); ! fseek(payload, 0, SEEK_SET); ! while(!feof(payload) && cur < len) ! { ! int c = fgetc(payload); ! datapart[cur++] = c; ! } ! } ! gettimeofday(&tp, &(tz)); ! ((struct icmp *) buf)->icmp_cksum = ! checksum((unsigned short *) buf, total_size); ! bwrote = sendto(my_socket, buf, total_size, 0, ! target->addrinfo->ai_addr, sizeof(struct sockaddr)); ! if(bwrote == -1) ! { ! err_ret("sendto failed"); ! //perror("sendto"); ! return PLUGIN_ERROR; ! } ! if(bwrote < (int) total_size) ! { ! err_msg("wrote %d bytes\n", bwrote); ! } ! free(buf); return PLUGIN_OK; } --- 522,655 ---- assert(my_socket != -1); + srand(getpid() ^ (uid_t) time(NULL)); /* set the packet size to specified */ if(mthis->pCon.iSize != 0) total_size += mthis->pCon.iSize; ! /* or pick a random size 64 to 290*/ else total_size += 64 + (int) (290.0 * rand() / (RAND_MAX + 64.0)); ! for(j = 0; j <= target->extra_hosts; j++) ! { ! /* get the right address */ ! switch(target->addrinfo->ai_family) ! { ! case PF_INET: ! sa = (struct sockaddr_in *) target->addrinfo->ai_addr; ! ia = sa->sin_addr; ! if(j > 0) ! ia.s_addr = htonl(ntohl(ia.s_addr)+1); ! sa->sin_addr = ia; ! break; ! default: ! err_quit("address family not supported yet\n"); ! } ! /* how much of the packet is data? */ ! dlen = total_size - sizeof(struct icmp); ! buf = (char *) malloc(total_size); ! if(!buf) ! { ! err_ret("failed to malloc"); ! return PLUGIN_ERROR; ! } ! memset(buf, 0, total_size); ! icmp_hdr = (struct icmp *) buf; ! icmp_hdr->icmp_type = my_type; ! icmp_hdr->icmp_code = my_code; ! icmp_hdr->icmp_id = (unsigned short) getpid(); ! icmp_hdr->icmp_cksum = 0; ! /* random sequence number, to make fingerprinting this tool harder */ ! /* clock works well, that way we can count round trip time as well */ ! /* however, the down side is that they always increase... */ ! icmp_hdr->icmp_seq = seq_no2 = clock();/*seq_no2 = low_seq + (int) (25.0 * rand() / (RAND_MAX + 1.0));*/ ! datapart = buf + sizeof(struct icmp); ! /* no payload */ ! if(!payload_file) ! { ! /* do we want a pattern? */ ! if(mthis->pCon.sPattern) ! { ! int i, len = strlen(mthis->pCon.sPattern); ! int num = dlen / len; ! for(i = 0; i < num; i++) ! { ! strncpy(datapart, mthis->pCon.sPattern, len); ! datapart += len; ! } ! } ! /* or random garbage */ ! else ! { ! FILE *rp; ! rp = fopen("/dev/urandom", "r"); ! if(!rp) ! { ! #ifdef DEBUG ! fprintf(stderr, "network_icmp.c: could not open /dev/urandom\n"); ! #endif ! rp = fopen("/dev/random", "r"); ! if(!rp) ! { ! #ifdef DEBUG ! fprintf(stderr, "network_icmp.c: could not open /dev/random\n"); ! #endif ! memset(datapart, ! (int) (256.0 * rand() / (RAND_MAX + 0.0)), ! dlen); ! } ! } ! /* if we didn't set it already */ ! if(*datapart == 0) ! { ! fread(datapart, dlen, 1, rp); ! fclose(rp); ! } ! } ! } ! /* we've got a payload */ ! else ! { ! unsigned int len = 0, cur = 0; ! FILE *payload; ! payload = fopen(payload_file, "r"); ! if(!payload) ! { ! *(pthis.err) = PLUGIN_NOFILE; ! err_ret("could not open payload file"); ! free(buf); ! return PLUGIN_ERROR; ! } ! fseek(payload, 0, SEEK_END); ! len = (unsigned int) ((ftell(payload) > dlen) ? dlen : ! ftell(payload)); ! fseek(payload, 0, SEEK_SET); ! while(!feof(payload) && cur < len) ! { ! int c = fgetc(payload); ! datapart[cur++] = c; ! } ! } ! gettimeofday(&tp, &(tz)); ! ! ((struct icmp *) buf)->icmp_cksum = ! checksum((unsigned short *) buf, total_size); ! bwrote = sendto(my_socket, buf, total_size, 0, ! target->addrinfo->ai_addr, sizeof(struct sockaddr)); ! free(buf); ! if(bwrote == -1) ! { ! err_ret("sendto failed"); ! //perror("sendto"); ! return PLUGIN_ERROR; ! } ! if(bwrote < (int) total_size) ! { ! err_msg("wrote %d bytes\n", bwrote); ! } ! ! } return PLUGIN_OK; } *************** *** 635,640 **** while((done == 0 || clock() <= rec->max) && rec->responses < rec->hosts) { - tv.tv_sec = 0; tv.tv_usec = 0; FD_ZERO(&rfds); --- 692,697 ---- while((done == 0 || clock() <= rec->max) && rec->responses < rec->hosts) { tv.tv_usec = 0; + tv.tv_sec = 0; FD_ZERO(&rfds); *************** *** 738,748 **** if(icmphdr->icmp_type != resptype(my_type)) { ! if(icmphdr->icmp_type == ICMP_UNREACH) { ! mthis->output("%s: %s(%d) ", ! inet_ntoa(from.sin_addr), unreach_codes(icmphdr->icmp_code), icmphdr->icmp_code); rec->responses++; } else if(mthis->sCon.iVerbose > 0) --- 795,806 ---- if(icmphdr->icmp_type != resptype(my_type)) { ! if(icmphdr->icmp_type == ICMP_DEST_UNREACH) { ! mthis->message("%s: %s(%d) \n", ! inet_ntoa(icmphdr->icmp_ip.ip_dst), unreach_codes(icmphdr->icmp_code), icmphdr->icmp_code); rec->responses++; + break; } else if(mthis->sCon.iVerbose > 0) |