Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. // system libraries
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. // multithreading libraries
  7. #include <pthread.h>
  8.  
  9. // networking libraries
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <arpa/inet.h>
  14. #include <netdb.h>
  15.  
  16. // defines
  17. #define PORT_END_NUMBER 65535
  18.  
  19. #define SOCKET_MAIN_THREAD 0
  20.  
  21. // globals
  22. int threads;
  23.  
  24. struct sockaddr_in *temp;
  25. char *ip = "127.0.0.1";
  26. int port;
  27. int status[5];
  28. int port_range[2];
  29.  
  30.  
  31. // future variables
  32. int global_port;
  33. int local_port;
  34.  
  35. // ! code begin
  36.  
  37. void NET_INIT(){
  38.   free(temp);
  39.   status[0] = socket(AF_INET, SOCK_STREAM, 0);
  40.   temp = malloc(sizeof(struct sockaddr_in));
  41.   temp->sin_family = AF_INET;
  42.   temp->sin_addr.s_addr = inet_addr(ip);
  43. }
  44.  
  45. int *ScanPort(char *ip, int port, int *Connect){
  46.   int *C;
  47.   C = malloc(sizeof(int));
  48.   Connect = malloc(sizeof(int));
  49.   temp = NULL;
  50.   free(temp);
  51.   NET_INIT();
  52.   temp->sin_port = htons(port);
  53.   *C = connect(status[SOCKET_MAIN_THREAD], (struct sockaddr*)&temp, sizeof(temp));
  54.   return C;
  55. }
  56.  
  57. int Scan(){
  58.   int NB;
  59.   int *S;
  60.   pthread_t local_id;
  61.  
  62.   NB = port_range[0];
  63.   while (NB < port_range[1]){
  64.     pthread_create(&local_id, NULL, S = ScanPort(ip, NB, 0), (void *)&local_id);
  65.     printf("ip: %s ; port: %d ; connect() status: %d\n", ip, NB, *S);
  66.     NB++;
  67.   }
  68.   return 0;
  69. }
  70. int NewportHelp(){
  71.   printf("newport .1 by unidef\n");
  72.   printf("multithreaded network scanner\n");
  73.   printf("./newport ip port-range-start port-range-end threads\n");
  74.   return 0;
  75. }
  76.  
  77. void Print_Variables(){
  78.   printf("\n*ip = %s\nport_range = %d - %d\nthreads = %d\n", ip, port_range[0], port_range[1], threads);
  79.   return;
  80. }
  81.  
  82. int main(int argc, char *argv[]){
  83.  
  84.   struct hostent *host;
  85.   long x;
  86.   if (argc != 5){
  87.     printf("beta software, please input right variables\n");
  88.     NewportHelp();
  89.     return 1;
  90.   }
  91.  
  92.   host = gethostbyname(argv[1]);
  93.   ip = host->h_addr_list;
  94.   port_range[0] = atoi(argv[2]);
  95.   port_range[1] = atoi(argv[3]) + 1;
  96.   threads = atoi(argv[4]);
  97.   printf("data successfully allocated\n");
  98.   printf("starting network scan on %s from port(s) %d to %d using %d threads\n", ip, port_range[0], port_range[1], threads);
  99.   Scan();
  100.   Print_Variables();
  101.  
  102.   return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement