Fixed duplicated ips on scan output
This commit is contained in:
Lorikaz
2026-01-22 09:10:54 +03:00
committed by GitHub
parent b1bfc55e0f
commit 04ed2cc5ab
3 changed files with 35 additions and 6 deletions

View File

@@ -182,6 +182,10 @@ extern volatile int stop_signal;
extern pthread_mutex_t output_mutex;
extern FILE *output_file_ptr;
extern int quiet_mode;
extern uint8_t *seen_ips;
#define IS_IP_SEEN(ip) (seen_ips[(uint32_t)(ip) >> 3] & (1 << ((uint32_t)(ip) & 7)))
#define MARK_IP_SEEN(ip) (seen_ips[(uint32_t)(ip) >> 3] |= (1 << ((uint32_t)(ip) & 7)))
#endif

View File

@@ -6,6 +6,7 @@ volatile int stop_signal = 0;
pthread_mutex_t output_mutex = PTHREAD_MUTEX_INITIALIZER;
FILE *output_file_ptr = NULL;
int quiet_mode = 0;
uint8_t *seen_ips = NULL;
extern pthread_t writer_thread_id;
@@ -282,6 +283,13 @@ int main(int argc, char *argv[]) {
if (!quiet_mode) printf("[*] Dry run mode - no packets will be sent\n");
return 0;
}
/* Allocate 512 MiB for de-duplication bitset */
seen_ips = (uint8_t *)calloc(1ULL << 29, 1);
if (seen_ips == NULL) {
fprintf(stderr, "[-] Failed to allocate de-duplication bitset (512 MiB)\n");
return 1;
}
port_range_t *port_ranges = NULL;
int num_port_ranges = parse_port_range(config.port_range, &port_ranges);
@@ -323,7 +331,6 @@ int main(int argc, char *argv[]) {
// Performance self-test for shuffle uniqueness (spot checks)
if (!quiet_mode && total_packets > 1) {
printf("[*] Verifying shuffle integrity...\n");
uint64_t v1 = blackrock_shuffle(&config.blackrock, 0);
uint64_t v2 = blackrock_shuffle(&config.blackrock, total_packets - 1);
if (v1 >= total_packets || v2 >= total_packets) {
@@ -553,6 +560,7 @@ int main(int argc, char *argv[]) {
pfring_zc_destroy_cluster(config.zc_cluster);
#endif
if (!quiet_mode) printf("[*] Scan completed.\n");
if (seen_ips) free(seen_ips);
return 0;
}

View File

@@ -108,8 +108,16 @@ void process_packet(const uint8_t *packet, int length, stats_t *stats,
atomic_fetch_add(&stats->ports_open, 1);
char src_ip_str[16];
int_to_ip(iph->saddr, src_ip_str);
push_to_writer(src_ip_str);
uint32_t ip_hbo = ntohl(iph->saddr);
/* Atomic check-and-set in bitset */
uint8_t bit = 1 << (ip_hbo & 7);
uint8_t *byte_ptr = &seen_ips[ip_hbo >> 3];
if (!(atomic_fetch_or((_Atomic uint8_t *)byte_ptr, bit) & bit)) {
int_to_ip(iph->saddr, src_ip_str);
push_to_writer(src_ip_str);
}
} else if (tcph->rst) {
atomic_fetch_add(&stats->rst_replies, 1);
@@ -121,8 +129,15 @@ void process_packet(const uint8_t *packet, int length, stats_t *stats,
atomic_fetch_add(&stats->ports_open, 1);
char src_ip_str[16];
int_to_ip(iph->saddr, src_ip_str);
push_to_writer(src_ip_str);
uint32_t ip_hbo = ntohl(iph->saddr);
uint8_t bit = 1 << (ip_hbo & 7);
uint8_t *byte_ptr = &seen_ips[ip_hbo >> 3];
if (!(atomic_fetch_or((_Atomic uint8_t *)byte_ptr, bit) & bit)) {
int_to_ip(iph->saddr, src_ip_str);
push_to_writer(src_ip_str);
}
} else if (iph->protocol == IPPROTO_ICMP) {
struct icmphdr *icmph = (struct icmphdr *)(packet + offset + (iph->ihl * 4));
@@ -197,7 +212,9 @@ void *receiver_thread(void *arg) {
setsockopt(sock, SOL_PACKET, PACKET_IGNORE_OUTGOING, &one, sizeof(one));
int fanout_arg = (1 | (PACKET_FANOUT_HASH << 16));
setsockopt(sock, SOL_PACKET, PACKET_FANOUT, &fanout_arg, sizeof(fanout_arg));
if (setsockopt(sock, SOL_PACKET, PACKET_FANOUT, &fanout_arg, sizeof(fanout_arg)) < 0) {
if (!quiet_mode) fprintf(stderr, "[!] Warning: setsockopt(PACKET_FANOUT) failed (errno %d: %s). Duplicate reception may occur at OS level.\n", errno, strerror(errno));
}
unsigned int frame_idx = 0;
while (ctx->running && !stop_signal) {