targets.cc

来自「Ubuntu packages of security software。 相」· CC 代码 · 共 1,812 行 · 第 1/5 页

CC
1,812
字号
	foundsomething = 1;	dotimeout = 1;	newstate = HOST_UP;	if (pingtype & PINGTYPE_TCP_USE_SYN) {	  if (tcp->th_flags & TH_RST) {	    newportstate = PORT_CLOSED;	  } else if ((tcp->th_flags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {	    newportstate = PORT_OPEN;	  }	}      } else if (ip->ip_p == IPPROTO_UDP) {	if (!ptech->rawudpscan) {	  continue;	}	udp = (udphdr_bsd *) (((char *) ip) + 4 * ip->ip_hl);	newport = ntohs(udp->uh_sport);	trynum = ntohs(udp->uh_dport) - sportbase;	if (trynum >= (u32) pt->max_tries) {	  if (o.debugging)	    error("Bogus trynum %d", trynum);	  continue;	}	/* Since this UDP response doesn't give us the sequence number, we'll have to brute force 	   lookup to find the hostnum */	for(hostnum = pt->group_end; hostnum != (u32) -1; hostnum--) {	  if (hostbatch[hostnum]->v4host().s_addr == ip->ip_src.s_addr)	    break;	}	if (hostnum == (u32) -1) {		  if (o.debugging > 1) 	    error("Warning, unexpected packet from machine %s", inet_ntoa(ip->ip_src));	  continue;	}		sequence = hostnum * pt->max_tries + trynum;	if (o.debugging) 	  log_write(LOG_STDOUT, "In response to UDP-ping, we got UDP packet back from %s port %hi (hostnum = %d trynum = %d\n", inet_ntoa(ip->ip_src), htons(udp->uh_sport), hostnum, trynum);	pingstyle = pingstyle_rawudp;	foundsomething = 1;	dotimeout = 1;	newstate = HOST_UP;      }    else if (o.debugging) {      error("Found whacked packet protocol %d in get_ping_results", ip->ip_p);    }    if (foundsomething) {        hostupdate(hostbatch, hostbatch[hostnum], newstate, dotimeout, 		 trynum, to, &time[sequence], &rcvdtime, pt, NULL, pingstyle);      if (newstate == HOST_UP && ip && bytes >= 20)	setTargetMACIfAvailable(hostbatch[hostnum], &linkhdr, ip, 0);    }    if (newport && newportstate != PORT_UNKNOWN) {      /* OK, we can add it, but that is only appropriate if this is one	 of the ports the user ASKED for */      /* This was for the old turbo mode -- which I no longer support now that ultra_scan() can handle parallel hosts.  Maybe I'll bring it back someday */      /*      if (ports && ports->tcp_count == 1 && ports->tcp_ports[0] == newport)	hostbatch[hostnum]->ports.addPort(newport, IPPROTO_TCP, NULL, 					  newportstate);      */    }  }  return 0;}static int sendconnecttcpquery(Target *hostbatch[], struct tcpqueryinfo *tqi,			Target *target, int probe_port_num, u16 seq, 			struct timeval *time, struct pingtune *pt, 			struct timeout_info *to, int max_sockets) {  int res,sock_err,i;  int tmpsd;  int hostnum, trynum;  struct sockaddr_storage sock;  struct sockaddr_in *sin = (struct sockaddr_in *) &sock;  struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &sock;  size_t socklen;    seq -= pt->seq_offset; // Because connect() pingscan doesn't send it over the wire  trynum = seq % pt->max_tries;  hostnum = seq / pt->max_tries;  assert(tqi->sockets_out <= max_sockets);  if (tqi->sockets_out == max_sockets) {    /* We've got to free one! */    for(i=0; i < trynum; i++) {      tmpsd = hostnum * pt->max_tries + i;      if (tqi->sockets[probe_port_num][tmpsd] >= 0) {	if (o.debugging) 	  log_write(LOG_STDOUT, "sendconnecttcpquery: Scavenging a free socket due to serious shortage\n");	close(tqi->sockets[probe_port_num][tmpsd]);	tqi->sockets[probe_port_num][tmpsd] = -1;	tqi->sockets_out--;	break;      }    }    if (i == trynum)      fatal("sendconnecttcpquery: Could not scavenge a free socket!");  }      /* Since we know we now have a free s0cket, lets take it */  assert(tqi->sockets[probe_port_num][seq] == -1);  tqi->sockets[probe_port_num][seq] =  socket(o.af(), SOCK_STREAM, IPPROTO_TCP);  if (tqi->sockets[probe_port_num][seq] == -1)     fatal("Socket creation in sendconnecttcpquery");  tqi->maxsd = MAX(tqi->maxsd, tqi->sockets[probe_port_num][seq]);  tqi->sockets_out++;  unblock_socket(tqi->sockets[probe_port_num][seq]);  init_socket(tqi->sockets[probe_port_num][seq]);  if (target->TargetSockAddr(&sock, &socklen) != 0)    fatal("Unable to get target sock in sendconnecttcpquery");  if (sin->sin_family == AF_INET)    sin->sin_port = htons(o.ping_synprobes[probe_port_num]);#if HAVE_IPV6  else sin6->sin6_port = htons(o.ping_synprobes[probe_port_num]);#endif //HAVE_IPV6  res = connect(tqi->sockets[probe_port_num][seq],(struct sockaddr *)&sock, socklen);  sock_err = socket_errno();  if ((res != -1 || sock_err == ECONNREFUSED)) {    /* This can happen on localhost, successful/failing connection immediately       in non-blocking mode */      hostupdate(hostbatch, target, HOST_UP, 1, trynum, to, 		 &time[seq], NULL, pt, tqi, pingstyle_connecttcp);    if (tqi->maxsd == tqi->sockets[probe_port_num][seq]) tqi->maxsd--;  }  else if (sock_err == ENETUNREACH) {    if (o.debugging)       error("Got ENETUNREACH from sendconnecttcpquery connect()");    hostupdate(hostbatch, target, HOST_DOWN, 1, trynum, to, 	       &time[seq], NULL, pt, tqi, pingstyle_connecttcp);  }  else {    /* We'll need to select() and wait it out */    FD_SET(tqi->sockets[probe_port_num][seq], &(tqi->fds_r));    FD_SET(tqi->sockets[probe_port_num][seq], &(tqi->fds_w));    FD_SET(tqi->sockets[probe_port_num][seq], &(tqi->fds_x));  }return 0;}static int sendconnecttcpqueries(Target *hostbatch[], struct tcpqueryinfo *tqi,			Target *target, u16 seq, 			struct timeval *time, struct pingtune *pt, 			struct timeout_info *to, int max_sockets) {  int i;  for( i=0; i<o.num_ping_synprobes; i++ ) {    if (i > 0 && o.scan_delay) enforce_scan_delay(NULL);    sendconnecttcpquery(hostbatch, tqi, target, i, seq, time, pt, to, max_sockets);  }  return 0;}static int sendrawudppingquery(int rawsd, struct eth_nfo *eth, Target *target, u16 probe_port,			       u16 seq, struct timeval *time, struct pingtune *pt) {int trynum = 0;unsigned short sportbase;if (o.magic_port_set) sportbase = o.magic_port;else {   sportbase = o.magic_port + 20;  trynum = seq % pt->max_tries;} o.decoys[o.decoyturn].s_addr = target->v4source().s_addr;  send_udp_raw_decoys( rawsd, eth, target->v4hostip(), 		      o.ttl, seq, 		      o.ipoptions, o.ipoptionslen, 		      sportbase + trynum, probe_port,  		      o.extra_payload, o.extra_payload_length); return 0;}static int sendrawtcppingquery(int rawsd, struct eth_nfo *eth, Target *target, int pingtype, u16 probe_port,			       u16 seq, struct timeval *time, struct pingtune *pt) {int trynum = 0;int myseq;unsigned short sportbase;unsigned long myack;if (o.magic_port_set) sportbase = o.magic_port;else {   sportbase = o.magic_port + 20;  trynum = seq % pt->max_tries;} myseq = (get_random_uint() << 22) + (seq << 6) + 0x1E; /* (response & 0x3F) better be 0x1E or 0x1F */ myack = (get_random_uint() << 22) + (seq << 6) + 0x1E; /* (response & 0x3F) better be 0x1E or 0x1F */ o.decoys[o.decoyturn].s_addr = target->v4source().s_addr; if (pingtype & PINGTYPE_TCP_USE_SYN) {      send_tcp_raw_decoys( rawsd, eth, target->v4hostip(),   			o.ttl, false,   			o.ipoptions, o.ipoptionslen,   			sportbase + trynum, probe_port,   			myseq, myack, 0, TH_SYN, 0, 0,   			(u8 *) "\x02\x04\x05\xb4", 4,   			o.extra_payload, o.extra_payload_length); } else {   send_tcp_raw_decoys( rawsd, eth, target->v4hostip(),   			o.ttl, false,   			o.ipoptions, o.ipoptionslen,   			sportbase + trynum, probe_port,   			myseq, myack, 0, TH_ACK, 0, 0,   			NULL, 0,   			o.extra_payload, o.extra_payload_length); } return 0;}static int sendrawtcpudppingqueries(int rawsd, eth_t *ethsd, Target *target, int pingtype, u16 seq, 				    struct timeval *time, struct pingtune *pt) {  int i;  struct eth_nfo eth;  struct eth_nfo *ethptr = NULL;  if (ethsd) {	  memcpy(eth.srcmac, target->SrcMACAddress(), 6);	  memcpy(eth.dstmac, target->NextHopMACAddress(), 6);	  eth.ethsd = ethsd;	  eth.devname[0] = '\0';	  ethptr = &eth;  } else ethptr = NULL;  if (pingtype & PINGTYPE_UDP) {    for( i=0; i<o.num_ping_udpprobes; i++ ) {      if (i > 0 && o.scan_delay) enforce_scan_delay(NULL);      sendrawudppingquery(rawsd, ethptr, target, o.ping_udpprobes[i], seq, time, pt);    }  }  if (pingtype & PINGTYPE_TCP_USE_ACK) {    for( i=0; i<o.num_ping_ackprobes; i++ ) {      if (i > 0 && o.scan_delay) enforce_scan_delay(NULL);      sendrawtcppingquery(rawsd, ethptr, target, PINGTYPE_TCP_USE_ACK, o.ping_ackprobes[i], seq, time, pt);    }  }  if (pingtype & PINGTYPE_TCP_USE_SYN) {    for( i=0; i<o.num_ping_synprobes; i++ ) {      if (i > 0 && o.scan_delay) enforce_scan_delay(NULL);      sendrawtcppingquery(rawsd, ethptr, target, PINGTYPE_TCP_USE_SYN, o.ping_synprobes[i], seq, time, pt);    }  }  return 0;}static int sendpingquery(int sd, int rawsd, eth_t *ethsd, Target *target,  		  u16 seq, unsigned short id, struct scanstats *ss, 		  struct timeval *time, int pingtype, struct pingtech ptech) {struct ppkt {  u8 type;  u8 code;  u16 checksum;  u16 id;  u16 seq;  u8 data[1500]; /* Note -- first 4-12 bytes can be used for ICMP header */} pingpkt;u32 *datastart = (u32 *) pingpkt.data;int datalen = sizeof(pingpkt.data); int icmplen=0;int decoy;int res;struct sockaddr_in sock;struct eth_nfo eth;struct eth_nfo *ethptr = NULL;char *ping = (char *) &pingpkt;if (ethsd) {    memcpy(eth.srcmac, target->SrcMACAddress(), 6);    memcpy(eth.dstmac, target->NextHopMACAddress(), 6);    eth.ethsd = ethsd;    eth.devname[0] = '\0';    ethptr = &eth;} else ethptr = NULL; if (pingtype & PINGTYPE_ICMP_PING) {   icmplen = 8;    pingpkt.type = 8; } else if (pingtype & PINGTYPE_ICMP_MASK) {   icmplen = 12;   *datastart++ = 0;   datalen -= 4;   pingpkt.type = 17; } else if (pingtype & PINGTYPE_ICMP_TS) {      icmplen = 20;   memset(datastart, 0, 12);   datastart += 12;   datalen -= 12;   pingpkt.type = 13; } else fatal("sendpingquery: unknown pingtype: %d", pingtype); if (o.extra_payload_length > 0) {   icmplen += MIN(datalen, o.extra_payload_length);   memset(datastart, 0, MIN(datalen, o.extra_payload_length)); }/* Fill out the ping packet */pingpkt.code = 0;pingpkt.id = id;pingpkt.seq = seq;pingpkt.checksum = 0;pingpkt.checksum = in_cksum((unsigned short *)ping, icmplen);if ( o.badsum )  pingpkt.checksum--;/* Now for our sock */if (ptech.icmpscan) {  memset((char *)&sock, 0, sizeof(sock));  sock.sin_family= AF_INET;  sock.sin_addr = target->v4host();    o.decoys[o.decoyturn].s_addr = target->v4source().s_addr;} for (decoy = 0; decoy < o.numdecoys; decoy++) {   if (ptech.icmpscan && decoy == o.decoyturn) {      int sock_err = 0;     /* FIXME: If EHOSTUNREACH (Windows does that) then we were	probably unable to obtain an arp response from the machine.	We should just consider the host down rather than ignoring	the error */     // Can't currently do the tracer because 'ping' has no IP header     //     PacketTrace::trace(PacketTrace::SENT, (u8 *) ping, icmplen);      if ((res = sendto(sd,(char *) ping,icmplen,0,(struct sockaddr *)&sock,		       sizeof(struct sockaddr))) != icmplen &&                        (sock_err = socket_errno()) != EHOSTUNREACH#ifdef WIN32        // Windows (correctly) returns this if we scan an address that is        // known to be nonsensical (e.g. myip & mysubnetmask)	&& sock_err != WSAEADDRNOTAVAIL#endif 		       ) {       fprintf(stderr, "sendto in sendpingquery returned %d (should be 8)!\n", res);       fprintf(stderr, "sendto: %s\n", strerror(sock_err));     }   } else {     send_ip_raw(rawsd, ethptr,     		&o.decoys[decoy], target->v4hostip(),     		IPPROTO_ICMP, o.ttl,     		o.ipoptions, o.ipoptionslen,     		ping, icmplen);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?