Parsi Coders
سورس کد Thrip Port Scanner - نسخه قابل چاپ

+- Parsi Coders (http://parsicoders.com)
+-- انجمن: Software Development Programming (http://parsicoders.com/forumdisplay.php?fid=37)
+--- انجمن: C and C++ (http://parsicoders.com/forumdisplay.php?fid=54)
+--- موضوع: سورس کد Thrip Port Scanner (/showthread.php?tid=2774)



سورس کد Thrip Port Scanner - Amin_Mansouri - 09-13-2012

Thrip Port Scannerاسکنر multithreaded هست که اسکن آدرس های IP بر روی یک پورت مشخص شده به طور همزمان است. این بسیار مفید است برای پیدا کردن سیستم های ناامن زمانی که با پورت مانند 23 (شبکه راه دور) و یا 8080، است که از رابط وب برای روترهای مورد استفاده قرار می گیرد.نویسنده این برنامه هیچ مسئولیتی در قبال اقدامات کاربران آن بر عهده نمیگیرد.

منبع : www.parsicoders.com www.pxr.ir

کد:
/*
Compile with: gcc thrip.c -o thrip -pthread

This is a multithreaded scanner that scans many ip addresses on a specified port simultaneously.  It is very useful for finding insecure systems when used with such ports as 23 (Telnet) or 8080, which is the web interface for many routers.  The author of this program takes no responsiblity for the actions of its users.  If you have any comments or suggestions, feeel free to contact me at this email: grell64@gmail.com.  Thanks and enjoy.

-Grell
*/

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>


void usage(char *);

void *net_thread(void *);

FILE *glob_file;

int glob_port;

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

static pthread_mutex_t mtx2 = PTHREAD_MUTEX_INITIALIZER;

int main(int argc, char *argv[])
{
  int numthreads, rc;
  long i;

  srand(time(NULL));
  if(argc != 4)
    usage(argv[0]);
  
  glob_port = atoi(argv[1]);
  numthreads = atoi(argv[2]);
  unsigned long *taskids[numthreads];
  pthread_t thread_array[numthreads];

  glob_file = fopen(argv[3], "w+");

  // generate array of random ip addresses
  for(i = 0; i <= numthreads; i++)
    {
    taskids[i] = (long *) malloc(sizeof(long));
    *taskids[i] = i;
    rc = pthread_create(&thread_array[i], NULL, net_thread, (void *) taskids[i]);
    }

  // wait for all threads to finish
  
  for(i = 0; i <= numthreads; i++)
  {
    pthread_join(thread_array[i], NULL);
  }
  return 0;
}

void usage(char *progname)
{
  fprintf(stderr, "Usage: %s <port> <numthread> <output file>\n", progname);
  exit(1);
}

void *net_thread(void *thr_arg)
{
  int sockfd, s, g;
  unsigned long connip;
  unsigned int a, b, c, d;
  struct sockaddr_in mysock;
  char str_ip[30];

  connip = (rand()) * (long) (thr_arg);

  // separate bytes in connip

  a = (connip >> 24 & 0xFF);
  b = (connip >> 16 & 0xFF);
  c = (connip >> 8 & 0xFF);
  d = (connip & 0xFF);

  if(a == 0x7F){ //loopback
    return 0;
  }

  s = pthread_mutex_lock(&mtx);
  printf("Scanning: %d.%d.%d.%d\n", a, b, c, d);
  fflush(stdout);
  s = pthread_mutex_unlock(&mtx);

  if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
    perror("socket");
    pthread_exit(NULL);
  }

  snprintf(str_ip, sizeof(str_ip), "%d.%d.%d.%d", a, b, c, d);

  bzero(&mysock, sizeof(mysock));

  mysock.sin_family = AF_INET;
  mysock.sin_port = htons(glob_port);     // short, network byte order
  mysock.sin_addr.s_addr = htonl(connip);

  if((connect(sockfd, (struct sockaddr *) &mysock, sizeof(mysock))) == -1){
    switch(errno){
      case ECONNREFUSED:
      case ETIMEDOUT:
        // add mutex
        s = pthread_mutex_lock(&mtx);
        fprintf(glob_file, "Port %d Closed on %s\n", glob_port, str_ip);
        fflush(glob_file);
        s = pthread_mutex_unlock(&mtx);
        break;
      default:
        break;
    }
    return 0;
  }
  /* connection succeeds */
  else{
    g = pthread_mutex_lock(&mtx2);
    fprintf(glob_file, "Port %d Open on %s\n", glob_port, str_ip);
    fflush(glob_file);
    g = pthread_mutex_unlock(&mtx2);
    return 0;
  }
  
  return 0;
}