targetgroup.cc
来自「Ubuntu packages of security software。 相」· CC 代码 · 共 516 行 · 第 1/2 页
CC
516 行
else if (r && !*(r+1)) end = 255; } /* if (o.debugging > 2) log_write(LOG_STDOUT, "The first host is %d, and the last one is %d\n", start, end); */ if (start < 0 || start > end || start > 255 || end > 255) fatal("Your host specifications are illegal!"); if (j + (end - start) > 255) fatal("Your host specifications are illegal!"); for(k=start; k <= end; k++) addresses[i][j++] = k; last[i] = j-1; if (s) addy[i] = s + 1; } while (s); } } memset((char *)current, 0, sizeof(current)); ipsleft = (last[0] + 1) * (last[1] + 1) * (last[2] + 1) * (last[3] + 1); } else {#if HAVE_IPV6 int rc = 0; assert(af == AF_INET6); if (strchr(hostexp, '/')) { fatal("Invalid host expression: %s -- slash not allowed. IPv6 addresses can currently only be specified individually", hostexp); } targets_type = IPV6_ADDRESS; struct addrinfo hints; struct addrinfo *result = NULL; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_INET6; rc = getaddrinfo(hostexp, NULL, &hints, &result); if (rc != 0) { error("Failed to resolve given IPv6 hostname/IP: %s. Note that you can't use '/mask' or '[1-4,7,100-]' style ranges for IPv6. Error code %d: %s", hostexp, rc, gai_strerror(rc)); free(hostexp); if (result) freeaddrinfo(result); return 1; } assert(result->ai_addrlen == sizeof(struct sockaddr_in6)); struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) result->ai_addr; memcpy(ip6.s6_addr, sin6->sin6_addr.s6_addr, 16); ipsleft = 1; freeaddrinfo(result);#else // HAVE_IPV6 fatal("IPv6 not supported on your platform");#endif // HAVE_IPV6 } free(hostexp); return 0;}/* For ranges, skip all hosts in an octet, (mdmcl) * get_next_host should be used for skipping the last octet :-) * returns: number of hosts skipped */int TargetGroup::skip_range(_octet_nums octet) { unsigned long hosts_skipped = 0, /* number of hosts skipped */ oct = 0; /* octect number */ int i = 0; /* simple lcv */ /* This function is only supported for RANGES! */ if (targets_type != IPV4_RANGES) return -1; switch (octet) { case FIRST_OCTET: oct = 0; hosts_skipped = (last[1] + 1) * (last[2] + 1) * (last[3] + 1); break; case SECOND_OCTET: oct = 1; hosts_skipped = (last[2] + 1) * (last[3] + 1); break; case THIRD_OCTET: oct = 2; hosts_skipped = (last[3] + 1); break; default: /* Hmm, how'd you do that */ return -1; } /* catch if we try to take more than are left */ assert(ipsleft + 1>= hosts_skipped); /* increment the next octect that we can above us */ for (i = oct; i >= 0; i--) { if (current[i] < last[i]) { current[i]++; break; } else current[i] = 0; } /* reset all the ones below us to zero */ for (i = oct+1; i <= 3; i++) { current[i] = 0; } /* we actually don't skip the current, it was accounted for * by get_next_host */ ipsleft -= hosts_skipped - 1; return hosts_skipped;} /* Grab the next host from this expression (if any) and updates its internal state to reflect that the IP was given out. Returns 0 and fills in ss if successful. ss must point to a pre-allocated sockaddr_storage structure */int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) { int octet; struct sockaddr_in *sin = (struct sockaddr_in *) ss; struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) ss; startover: /* to handle nmap --resume where I have already scanned many of the IPs */ assert(ss); assert(sslen); if (ipsleft == 0) return -1; if (targets_type == IPV4_NETMASK) { memset(sin, 0, sizeof(struct sockaddr_in)); sin->sin_family = AF_INET; *sslen = sizeof(struct sockaddr_in);#if HAVE_SOCKADDR_SA_LEN sin->sin_len = *sslen;#endif if (currentaddr.s_addr <= endaddr.s_addr) { sin->sin_addr.s_addr = htonl(currentaddr.s_addr++); } else { error("Bogus target structure passed to %s", __func__); ipsleft = 0; return -1; } } else if (targets_type == IPV4_RANGES) { memset(sin, 0, sizeof(struct sockaddr_in)); sin->sin_family = AF_INET; *sslen = sizeof(struct sockaddr_in);#if HAVE_SOCKADDR_SA_LEN sin->sin_len = *sslen;#endif if (o.debugging > 2) { log_write(LOG_STDOUT, "doing %d.%d.%d.%d = %d.%d.%d.%d\n", current[0], current[1], current[2], current[3], addresses[0][current[0]],addresses[1][current[1]],addresses[2][current[2]],addresses[3][current[3]]); } /* Set the IP to the current value of everything */ sin->sin_addr.s_addr = htonl(addresses[0][current[0]] << 24 | addresses[1][current[1]] << 16 | addresses[2][current[2]] << 8 | addresses[3][current[3]]); /* Now we nudge up to the next IP */ for(octet = 3; octet >= 0; octet--) { if (current[octet] < last[octet]) { /* OK, this is the column I have room to nudge upwards */ current[octet]++; break; } else { /* This octet is finished so I reset it to the beginning */ current[octet] = 0; } } if (octet == -1) { /* It didn't find anything to bump up, I must have taken the last IP */ assert(ipsleft == 1); /* So I set current to last with the very final octet up one ... */ /* Note that this may make current[3] == 256 */ current[0] = last[0]; current[1] = last[1]; current[2] = last[2]; current[3] = last[3] + 1; } else { assert(ipsleft > 1); /* There must be at least one more IP left */ } } else { assert(targets_type == IPV6_ADDRESS); assert(ipsleft == 1);#if HAVE_IPV6 *sslen = sizeof(struct sockaddr_in6); memset(sin6, 0, *sslen); sin6->sin6_family = AF_INET6;#ifdef SIN_LEN sin6->sin6_len = *sslen;#endif /* SIN_LEN */ memcpy(sin6->sin6_addr.s6_addr, ip6.s6_addr, 16);#else fatal("IPV6 not supported on this platform");#endif // HAVE_IPV6 } ipsleft--; /* If we are resuming from a previous scan, we have already finished scans up to o.resume_ip. */ if (sin->sin_family == AF_INET && o.resume_ip.s_addr) { if (o.resume_ip.s_addr == sin->sin_addr.s_addr) o.resume_ip.s_addr = 0; /* So that we will KEEP the next one */ goto startover; /* Try again */ } return 0;}/* Returns the last given host, so that it will be given again next time get_next_host is called. Obviously, you should only call this if you have fetched at least 1 host since parse_expr() was called */int TargetGroup::return_last_host() { int octet; ipsleft++; if (targets_type == IPV4_NETMASK) { assert(currentaddr.s_addr > startaddr.s_addr); currentaddr.s_addr--; } else if (targets_type == IPV4_RANGES) { for(octet = 3; octet >= 0; octet--) { if (current[octet] > 0) { /* OK, this is the column I have room to nudge downwards */ current[octet]--; break; } else { /* This octet is already at the beginning, so I set it to the end */ current[octet] = last[octet]; } } assert(octet != -1); } else { assert(targets_type == IPV6_ADDRESS); assert(ipsleft == 1); } return 0;}/* Lookahead is the number of hosts that can be checked (such as ping scanned) in advance. Randomize causes each group of up to lookahead hosts to be internally shuffled around. The target_expressions array MUST REMAIN VALID IN MEMORY as long as this class instance is used -- the array is NOT copied. */HostGroupState::HostGroupState(int lookahead, int rnd, char *expr[], int numexpr) { assert(lookahead > 0); hostbatch = (Target **) safe_zalloc(sizeof(Target *) * lookahead); max_batch_sz = lookahead; current_batch_sz = 0; next_batch_no = 0; randomize = rnd; target_expressions = expr; num_expressions = numexpr; next_expression = 0;}HostGroupState::~HostGroupState() { free(hostbatch);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?