[Javanetsim-cvs] IceScan/icesockets crandom.h, NONE, 1.1 sock_utils.h, 1.2, 1.3 sock_utils.cc, 1.2,
Status: Beta
Brought to you by:
darkkey
From: Alexander B. <da...@us...> - 2006-12-21 17:35:11
|
Update of /cvsroot/javanetsim/IceScan/icesockets In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv30605/icesockets Modified Files: sock_utils.h sock_utils.cc crawsocket.h Added Files: crandom.h Log Message: no message --- NEW FILE: crandom.h --- /* * crandom.h -- Contains the crandom class for random byte * generating. */ /* * Copyright (C) 2006 by Alexander Bolshev[Key] <key(at)timeold.ru> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation, * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ #ifndef _crandom_H #define _crandom_H #include "sock_types.h" /* Portions of code below are from libdnet package: * Copyright (c) 2000-2004 Dug Song <du...@mo...> * All rights reserved, all wrongs reversed. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The names of the authors and copyright holders may not be used to * endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* start of modified code from libdnet */ #ifdef _WIN32 # undef _WIN32_WINNT # define _WIN32_WINNT 0x0400 # include <wincrypt.h> # define inline __inline #endif struct rand_handle { uint8_t i; uint8_t j; uint8_t s[256]; u_char *tmp; int tmplen; }; typedef rand_handle rand_t; static inline void rand_init(rand_t *rand) { int i; for (i = 0; i < 256; i++) rand->s[i] = i; rand->i = rand->j = 0; } static inline void rand_addrandom(rand_t *rand, u_char *buf, int len) { int i; uint8_t si; rand->i--; for (i = 0; i < 256; i++) { rand->i = (rand->i + 1); si = rand->s[rand->i]; rand->j = (rand->j + si + buf[i % len]); rand->s[rand->i] = rand->s[rand->j]; rand->s[rand->j] = si; } rand->j = rand->i; } /* end of modified code from libdnet */ class crandom{ rand_t *r; public: crandom(){ r = rand_open(); } int get(rand_t *r, void *buf, size_t len){ return rand_get(r, buf, len); } /* start of modified code from libdnet */ uint8_t rand_uint8() { return (rand_getbyte(r)); } uint16_t rand_uint16() { uint16_t val; val = rand_getbyte(r) << 8; val |= rand_getbyte(r); return (val); } uint32_t rand_uint32() { uint32_t val; val = rand_getbyte(r) << 24; val |= rand_getbyte(r) << 16; val |= rand_getbyte(r) << 8; val |= rand_getbyte(r); return (val); } /* end of modified code from libdnet */ ~crandom(){ rand_close(r); } private: /* start of modified code from libdnet */ rand_t *rand_open(void) { rand_t *r; u_char seed[256]; #ifdef _WIN32 HCRYPTPROV hcrypt = 0; CryptAcquireContext(&hcrypt, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); CryptGenRandom(hcrypt, sizeof(seed), seed); CryptReleaseContext(hcrypt, 0); #else struct timeval *tv = (struct timeval *)seed; int fd; if ((fd = open("/dev/arandom", O_RDONLY)) != -1 || (fd = open("/dev/urandom", O_RDONLY)) != -1) { read(fd, seed + sizeof(*tv), sizeof(seed) - sizeof(*tv)); close(fd); } gettimeofday(tv, NULL); #endif if ((r = (rand_t*) malloc(sizeof(*r))) != NULL) { rand_init(r); rand_addrandom(r, seed, 128); rand_addrandom(r, seed + 128, 128); r->tmp = NULL; r->tmplen = 0; } return (r); } static uint8_t rand_getbyte(rand_t *r) { uint8_t si, sj; r->i = (r->i + 1); si = r->s[r->i]; r->j = (r->j + si); sj = r->s[r->j]; r->s[r->i] = sj; r->s[r->j] = si; return (r->s[(si + sj) & 0xff]); } int rand_get(rand_t *r, void *buf, size_t len) { u_char *p; u_int i; for (p = (u_char*) buf, i = 0; i < len; i++) { p[i] = rand_getbyte(r); } return (0); } int rand_set(rand_t *r, const void *buf, size_t len) { rand_init(r); rand_addrandom(r, (u_char *)buf, len); rand_addrandom(r, (u_char *)buf, len); return (0); } int rand_add(rand_t *r, const void *buf, size_t len) { rand_addrandom(r, (u_char *)buf, len); return (0); } rand_t * rand_close(rand_t *r) { if (r != NULL) { if (r->tmp != NULL) free(r->tmp); free(r); } return (NULL); } /* end of modified code from libdnet */ }; #endif /* _crandom_H */ Index: crawsocket.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/crawsocket.h,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** crawsocket.h 21 Dec 2006 16:29:36 -0000 1.20 --- crawsocket.h 21 Dec 2006 17:35:07 -0000 1.21 *************** *** 2,8 **** #define ICERAWSOCKET_H #include "csocket.h" #include "cethwrapper.h" ! #include "sock_types.h" class crawsocket : csocket{ --- 2,10 ---- #define ICERAWSOCKET_H + #include "sock_types.h" #include "csocket.h" + #include "cpcapreader.h" #include "cethwrapper.h" ! #include "crandom.h" class crawsocket : csocket{ *************** *** 135,138 **** --- 137,141 ---- unsigned long ack, unsigned char flags, unsigned short window, char *data, const unsigned short datalen) { + crandom r; #ifdef WIN32 *************** *** 165,179 **** if (seq) tcp->th_seq = htonl(seq); ! else tcp->th_seq = rand() + rand(); if (flags & TH_ACK && ack) tcp->th_ack = htonl(seq); else if (flags & TH_ACK) ! tcp->th_ack = rand() + rand(); tcp->th_off = 5; tcp->th_flags = flags; ! unsigned short ttl = 121; if (window) --- 168,182 ---- if (seq) tcp->th_seq = htonl(seq); ! else tcp->th_seq = r.rand_uint16(); if (flags & TH_ACK && ack) tcp->th_ack = htonl(seq); else if (flags & TH_ACK) ! tcp->th_ack = r.rand_uint16(); tcp->th_off = 5; tcp->th_flags = flags; ! unsigned short ttl = (r.rand_uint8() + 50) % 255; if (window) *************** *** 188,192 **** ip->ihl = 5; ip->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr) + datalen); ! ip->id = rand(); ip->ttl = ttl; ip->protocol = IPPROTO_TCP; --- 191,195 ---- ip->ihl = 5; ip->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr) + datalen); ! ip->id = r.rand_uint16(); ip->ttl = ttl; ip->protocol = IPPROTO_TCP; *************** *** 217,220 **** --- 220,255 ---- } + bool recieved_tcp_segment(cpcapreader &p, crawsocket &rawsend, icestring &source, struct sockaddr_in &saddress, int dest_port, int MAGIC_PORT, int th_flags, int timeout){ + int bytes; + char *response; + struct iphdr *ip; + struct tcphdr *tcp; + unsigned int len; + struct timeval tv1, tv2; + gettimeofday(&tv1, 0); + bool stop = false; + int port; + + while(!stop){ + response = p.read_packet(&len, NULL); + port = 0; + if(response){ + ip = (struct iphdr *) response; + if(ip->saddr == saddress.sin_addr.s_addr && ip->protocol == IPPROTO_TCP){ + tcp = (struct tcphdr *) (response + 4 * ip->ihl); + + if ((tcp->th_flags & th_flags) && !(th_flags & (TH_ACK|TH_SYN))){ + if(ntohs(tcp->th_sport) == dest_port) + return true; + } + } + } + gettimeofday(&tv2, 0); + + if(SUB_TIMEVALS(tv2,tv1) >= timeout) + stop = true; + } + } + int send_icmp_packet(icestring Hostname, int Icmp_type, int Icmp_code, int Icmp_id, int Icmp_seq, int Icmp_checksum, void* Icmp_data, int Icmp_data_len){ Index: sock_utils.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_utils.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** sock_utils.h 21 Dec 2006 16:36:27 -0000 1.2 --- sock_utils.h 21 Dec 2006 17:35:07 -0000 1.3 *************** *** 9,12 **** --- 9,16 ---- #define SUB_TIMEVALS(a,b) (((a).tv_sec - (b).tv_sec) * 1000000 + (a).tv_usec - (b).tv_usec) + #ifndef MAX + #define MAX(a,b) (a > b ? a : b) + #define MIN(a,b) (a < b ? a : b) + #endif #ifdef WIN32 *************** *** 36,43 **** icestring find_icefile_path(char *fname); - // from Fyodor's "The Art of Port Scanning" - int get_random_bytes(void *buf, int numbytes); - - void init_rand(); - #endif --- 40,42 ---- Index: sock_utils.cc =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_utils.cc,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** sock_utils.cc 21 Dec 2006 16:36:27 -0000 1.2 --- sock_utils.cc 21 Dec 2006 17:35:07 -0000 1.3 *************** *** 114,174 **** } - // from Fyodor's "The Art of Port Scanning" - int get_random_bytes(void *buf, int numbytes) { - static char bytebuf[2048]; - static char badrandomwarning = 0; - static int bytesleft = 0; - int tmp; - int res; - struct timeval tv; - FILE *fp = NULL; - unsigned int i; - short *iptr; - - if (numbytes < 0 || numbytes > 0xFFFF) return -1; - - if (bytesleft == 0) { - fp = fopen("/dev/arandom", "r"); - if (!fp) fp = fopen("/dev/urandom", "r"); - if (!fp) fp = fopen("/dev/random", "r"); - if (fp) { - res = (int) fread(bytebuf, 1, sizeof(bytebuf), fp); - if (res != sizeof(bytebuf)) { - printf("Failed to read from /dev/urandom or /dev/random\n"); - fclose(fp); - fp = NULL; - } - bytesleft = sizeof(bytebuf); - } - if (!fp) { - if (badrandomwarning == 0) { - badrandomwarning++; - gettimeofday(&tv, NULL); - srand((tv.tv_sec ^ tv.tv_usec) ^ getpid()); - } - - for(i=0; i < sizeof(bytebuf) / sizeof(short); i++) { - iptr = (short *) ((char *)bytebuf + i * sizeof(short)); - *iptr = rand(); - } - bytesleft = (sizeof(bytebuf) / sizeof(short)) * sizeof(short); - } else fclose(fp); - } - if (numbytes <= bytesleft) { /* we can cover it */ - memcpy(buf, bytebuf + (sizeof(bytebuf) - bytesleft), numbytes); - bytesleft -= numbytes; - return 0; - } - memcpy(buf, bytebuf + (sizeof(bytebuf) - bytesleft), bytesleft); - tmp = bytesleft; - bytesleft = 0; - return get_random_bytes((char *)buf + tmp, numbytes - tmp); - } - void init_rand(){ - unsigned i; - get_random_bytes(&i, sizeof(i)); - srand(i); - } --- 114,118 ---- |