Advertisement
sighting

TCP Portscanner

Jan 1st, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. // TCP Portscanner by iotpackets & netflooding
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/time.h>
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11. #include <net/if.h>
  12. #include <netinet/ip.h>
  13. #include <netinet/tcp.h>
  14. #include <errno.h>
  15. #include <netdb.h>
  16.  
  17. int Timeout;
  18. int least;
  19. int max;
  20.  
  21. int resolve(char * , char *);
  22. int resolve(char * site , char* ip)
  23. {
  24. struct hostent *he;
  25. struct in_addr **addr_list;
  26. int i;
  27.  
  28. if ( (he = gethostbyname( site ) ) == NULL)
  29. {
  30. // get the host info
  31. herror("gethostbyname");
  32. return 1;
  33. }
  34.  
  35. addr_list = (struct in_addr **) he->h_addr_list;
  36.  
  37. for(i = 0; addr_list[i] != NULL; i++)
  38. {
  39. //Return the first one;
  40. strcpy(ip , inet_ntoa(*addr_list[i]) );
  41. return 0;
  42. }
  43.  
  44. return 1;
  45. }
  46.  
  47. void portscan(char *target, int port)
  48. {
  49. int Socket;
  50. struct timeval timeout;
  51. struct sockaddr_in sock;
  52.  
  53. if(Timeout < 0 || Timeout == 0)
  54. Timeout = 3; // set to 3 for default
  55. timeout.tv_sec = Timeout;
  56. timeout.tv_usec = 0;
  57. Socket = socket(AF_INET, SOCK_STREAM, 0);
  58. setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
  59. setsockopt(Socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
  60. sock.sin_family = AF_INET;
  61. sock.sin_port = htons(port);
  62. sock.sin_addr.s_addr = inet_addr(target);
  63. if(connect(Socket, (struct sockaddr *)&sock, sizeof(sock)) == -1)
  64. goto end;
  65. else
  66. {
  67. printf("[\x1b[32m+\x1b[37m] Open Port %s:%d open!\n", target, port);
  68. goto end;
  69. }
  70. end:
  71. close(Socket);
  72. return;
  73. }
  74.  
  75. int main(int argc, char **argv)
  76. {
  77. if(argc < 5 || argc > 5)
  78. {
  79. printf("[\x1b[31m?\x1b[37m] Usage: %s <host> <least> <max> <timeout>\n", argv[0]);
  80. exit(0);
  81. }
  82. char host[60];
  83. resolve(argv[1], host);
  84. printf("[\x1b[34m!\x1b[37m] Portscanner started on %s\n", host);
  85. least = atoi(argv[2]);
  86. if(least == 0 || least < 0)
  87. least = 1;
  88. max = atoi(argv[3]);
  89. if(max == 0 || max < 0)
  90. max = 500;
  91. Timeout = atoi(argv[4]);
  92. int port;
  93. for(port=least; port < max; port++)
  94. {
  95. portscan(host, port);
  96. }
  97. printf("[\x1b[33m?\x1b[37m] Finished, Scanned ports %d - %d\n", least, max);
  98. return;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement