Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
140 lines (110 sloc) 2.68 KB
/*
hardscan.c - a simples e rapido escaner de portas
Copyright (C) 2004,2013 Alexandre Mulatinho
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
compile:
gcc -O2 -lpthread -o hardscan hardscan.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MLEN 65535
struct remote {
pthread_t tid;
char address[16];
int port;
} host[MLEN];
char *strkey(char *buf, int who, short del)
{
char *str = (char*)malloc(strlen(buf));
short ch, i, f=0, x=0, loop=0;
for (i=0;i<strlen(buf);i++) {
ch = *(buf+i);
if (ch==del && !i && !who)
return NULL;
if (ch==del)
loop++;
if (loop==who) {
if (i==0 || f!=0) {
*(str+x) = *(buf+i); x++;
}
f++;
}
}
*(str+x)='\0';
str=(char*)realloc(str,x);
return (char*)str;
}
void *newconn(struct remote *host)
{
int soq;
struct sockaddr_in net;
struct timeval tv;
net.sin_port = htons(host->port);
net.sin_family = AF_INET;
net.sin_addr.s_addr = inet_addr(host->address);
tv.tv_sec = 10;
tv.tv_usec = 0;
if ((soq = socket(AF_INET,SOCK_STREAM,0))) {
if (!setsockopt(soq, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv))) {
if (!connect(soq, (struct sockaddr*)&net, sizeof(net))) {
fprintf(stdout, "\033[1m%5d\033[m, esta aberta mestre! :]\n", host->port);
}
}
}
close(soq);
pthread_exit(NULL);
}
int check_if_num(char *p)
{
int x, ret = 0;
for (x=0; x < strlen(p); x++) {
if (p[x] != '-' && (p[x] < '0' || p[x] > '9'))
return ret;
else if (p[x] == '-')
ret++;
}
return ret;
}
int main(int argc, char **argv)
{
int total, min, max, r;
char *range;
if (argc < 2) {
fprintf(stderr, "uso: %s <ip> [1-65535]\n", argv[0]);
return -0xdead;
}
if (argv[2]) {
if (!(r=check_if_num(argv[2]))) {
fprintf(stderr, "err, the argument is not a number\n");
return -0xdead;
}
range = strkey(argv[2], 0, '-');
min = atoi(range);
range = strkey(argv[2], r, '-');
max = atoi(range);
} else {
min = 1;
max = MLEN;
}
fprintf(stdout, "scanning ports on host '%s'..\n", argv[1]);
for (total=0; min < max; min++, total++) {
host[total].port = min; memset(host[total].address, '\0', sizeof(host[total].address));
snprintf(host[total].address, sizeof(host[total].address), "%s", argv[1]);
pthread_create(&host[total].tid, NULL, (void*)&newconn, (void*)&host[total]);
usleep(424);
}
for (r=0; r<total; r++) {
pthread_join(host[r].tid, NULL);
usleep(242);
}
return 0;
}