1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <math.h>
#include <ctype.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netdb.h>
volatile int running_threads = 0;
volatile int found_srvs = 0;
volatile unsigned long per_thread = 0;
volatile unsigned long start = 0;
volatile unsigned long scanned = 0;
volatile unsigned long hosts_done = 0;
int sd;
int rval;
char responce[1024];
char *message="shell";
struct hostent *hostaddr;
struct sockaddr_in servaddr;
FILE *fd;
void *flood(void *par1)
{
running_threads++;
int thread_id = (int)par1;
unsigned long start_ip = htonl(ntohl(start)+(per_thread*thread_id));
unsigned long end = htonl(ntohl(start)+(per_thread*(thread_id+1)));
unsigned long w;
int y;
for(w=ntohl(start_ip);w<htonl(end);w++)
{
sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sd == -1)
{
printf ("erreur\n");
}
memset( &servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(80);
hostaddr = gethostbyname(w);
memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (rval == -1)
{
close(sd);
}
else
{
fprintf(fd,"%s", w);
fflush(fd);
close(sd);
}
scanned++;
hosts_done++;
}
running_threads--;
return;
}
void sighandler(int sig)
{
fclose(fd);
printf("\n");
exit(0);
}
int main(int argc, char *argv[ ])
{
if(argc < 5){
fprintf(stderr, "Paramteres non valides \n\n");
fprintf(stdout, "Utilisation : %s [Debut de la Ranger] [Fin de la ranger] [Fichier .txt] [Threads] \n", argv[0]);
exit(-1);
}
fd = fopen(argv[3], "a");
signal(SIGINT, &sighandler);
int threads = atoi(argv[4]);
pthread_t thread;
char *str_start = malloc(18);
memset(str_start, 0, 18);
str_start = argv[1];
char *str_end = malloc(18);
memset(str_end, 0, 18);
str_end = argv[2];
start = inet_addr(str_start);
per_thread = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start))) / threads;
unsigned long toscan = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start)));
int i;
for(i = 0;i<threads;i++){
pthread_create( &thread, NULL, &flood, (void *) i);
}
sleep(1);
printf("Scan en cours \n");
char *temp = (char *)malloc(17);
memset(temp, 0, 17);
sprintf(temp, "IP Trouver");
printf("%-16s", temp);
memset(temp, 0, 17);
sprintf(temp, "IP/s");
printf("%-16s", temp);
memset(temp, 0, 17);
sprintf(temp, "Threads");
printf("%-16s", temp);
memset(temp, 0, 17);
sprintf(temp, "Pourcentage");
printf("%s", temp);
printf("\n");
char *new;
new = (char *)malloc(16*6);
while (running_threads > 0)
{
printf("\r");
memset(new, '\0', 16*6);
sprintf(new, "%s|%-15lu", new, found_srvs);
sprintf(new, "%s|%-15d", new, scanned);
sprintf(new, "%s|%-15d", new, running_threads);
memset(temp, 0, 17);
int percent_done=((double)(hosts_done)/(double)(toscan))*100;
sprintf(temp, "%d%%", percent_done);
sprintf(new, "%s|%s", new, temp);
printf("%s", new);
fflush(stdout);
scanned = 0;
sleep(1);
}
printf("\n");
fclose(fd);
return 0;
} |
Partager