From 94b791198b35ae093106d6ee9478a4ff1ab3a1ef Mon Sep 17 00:00:00 2001 From: Dario Lombardo Date: Tue, 22 Apr 2014 18:02:32 +0200 Subject: [PATCH] Added prime number offset generation --- Makefile.am | 2 +- millerrabin.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ millerrabin.h | 11 +++++++ scanner.c | 18 ++++++++---- 4 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 millerrabin.c create mode 100644 millerrabin.h diff --git a/Makefile.am b/Makefile.am index 93c0255..e6a9829 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,3 +1,3 @@ bin_PROGRAMS = namescan -namescan_SOURCES = main.c radar.c scanner.c sock.c dns.c fingerprint.c log.c cmdline.c cmdline.h config.h dns.h fingerprint.h log.h radar.h scanner.h sock.h list.c list.h +namescan_SOURCES = main.c radar.c scanner.c sock.c dns.c fingerprint.c log.c cmdline.c cmdline.h config.h dns.h fingerprint.h log.h radar.h scanner.h sock.h list.c list.h millerrabin.c millerrabin.h AM_CPPFLAGS = -Wall -Werror diff --git a/millerrabin.c b/millerrabin.c new file mode 100644 index 0000000..bd7b4d8 --- /dev/null +++ b/millerrabin.c @@ -0,0 +1,80 @@ + +#include + +/* + Code taken from + http://rosettacode.org/wiki/Miller-Rabin_primality_test#Deterministic_up_to_341.2C550.2C071.2C728.2C321 +*/ + +// calcul a^n%mod +size_t power(size_t a, size_t n, size_t mod) +{ + size_t power = a; + size_t result = 1; + + while (n) + { + if (n & 1) + result = (result * power) % mod; + power = (power * power) % mod; + n >>= 1; + } + return result; +} + +// n−1 = 2^s * d with d odd by factoring powers of 2 from n−1 +bool witness(size_t n, size_t s, size_t d, size_t a) +{ + size_t x = power(a, d, n); + size_t y = 0; + + while (s) { + y = (x * x) % n; + if (y == 1 && x != 1 && x != n-1) + return false; + x = y; + --s; + } + if (y != 1) + return false; + return true; +} + +/* + * if n < 1,373,653, it is enough to test a = 2 and 3; + * if n < 9,080,191, it is enough to test a = 31 and 73; + * if n < 4,759,123,141, it is enough to test a = 2, 7, and 61; + * if n < 1,122,004,669,633, it is enough to test a = 2, 13, 23, and 1662803; + * if n < 2,152,302,898,747, it is enough to test a = 2, 3, 5, 7, and 11; + * if n < 3,474,749,660,383, it is enough to test a = 2, 3, 5, 7, 11, and 13; + * if n < 341,550,071,728,321, it is enough to test a = 2, 3, 5, 7, 11, 13, and 17. + */ + +bool is_prime_mr(size_t n) +{ + if (((!(n & 1)) && n != 2 ) || (n < 2) || (n % 3 == 0 && n != 3)) + return false; + if (n <= 3) + return true; + + size_t d = n / 2; + size_t s = 1; + while (!(d & 1)) { + d /= 2; + ++s; + } + + if (n < 1373653) + return witness(n, s, d, 2) && witness(n, s, d, 3); + if (n < 9080191) + return witness(n, s, d, 31) && witness(n, s, d, 73); + if (n < 4759123141) + return witness(n, s, d, 2) && witness(n, s, d, 7) && witness(n, s, d, 61); + if (n < 1122004669633) + return witness(n, s, d, 2) && witness(n, s, d, 13) && witness(n, s, d, 23) && witness(n, s, d, 1662803); + if (n < 2152302898747) + return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11); + if (n < 3474749660383) + return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13); + return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13) && witness(n, s, d, 17); +} diff --git a/millerrabin.h b/millerrabin.h new file mode 100644 index 0000000..eb773b5 --- /dev/null +++ b/millerrabin.h @@ -0,0 +1,11 @@ + +#ifndef __MILLERRABIN_H__ +#define __MILLERRABIN_H__ + +#include +#include + +/* Returns true is n is prime, using the Miller-Rabin algorhytm */ +bool is_prime_mr(size_t n); + +#endif diff --git a/scanner.c b/scanner.c index 3476e0b..64f1dad 100644 --- a/scanner.c +++ b/scanner.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -111,12 +112,6 @@ void scanner(scanner_params_t* sp) return; } - if (sp->randomize == false) { - off = 1; - } else { - off = 7; // TODO - } - struct sockaddr_in sin; memset(&sin, 0x0, sizeof(sin)); sin.sin_family = AF_INET; @@ -145,6 +140,17 @@ void scanner(scanner_params_t* sp) diff = ntohl(sp->ranges[i].ip_to) - ntohl(sp->ranges[i].ip_from) + 1; ptr = ntohl(sp->ranges[i].ip_from); + if (sp->randomize == false) { + off = 1; + } else { + do { + off = rand() % diff; + if (off == 0) + off = 1; + } while(!is_prime_mr(off)); + LOG_DEBUG("Using %u as offset\n", off); + } + if (diff % off == 0 && off > 1) { printf("%u %u\n", diff, off); LOG_ERROR("Collision detected. Specify another offset\n");