home *** CD-ROM | disk | FTP | other *** search
/ Hackers Handbook - Millenium Edition / Hackers Handbook.iso / files / exploits / lscan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-19  |  2.1 KB  |  99 lines

  1. /* 
  2.  
  3.     lscan.c --> listen scanner
  4.  
  5.             by f1ex
  6.  
  7.     usage: lscan <inputfile> <port number> [outputfile]
  8.  
  9.  
  10.     props to: duke well this 99% of his c0de...I just did a ripped =)
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <unistd.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <netdb.h>
  21. #include <string.h>
  22.  
  23. void usage(char *);
  24. void printheader(void);
  25. void testhost(char *);
  26.  
  27. FILE *of;
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31.     FILE *fp;
  32.     char host[1024];
  33.     int c;
  34.     c = 0;
  35.     printf("\npscan: port scanner for linux by f1ex\n");
  36.         printf("-------------------------------------\n\n");
  37.     if(argc < 2){
  38.         usage(argv[0]);
  39.         return 0;
  40.     }
  41.     if(argc == 3){
  42.         of = fopen(argv[2], "w");
  43.         printheader();
  44.     } else {
  45.         of = stdout; /* when using fprintf i can refer to stdout or log
  46.                 file without having to do conditions */
  47.     }
  48.     if((fp = fopen(argv[1], "r")) == NULL){
  49.         printf("error: input file does not exist\n");
  50.         return 0;
  51.     }
  52.     printf("scanning...");
  53.     while(fscanf(fp, "%s", &host) != EOF){
  54.         testhost(host);
  55.     }
  56.     printf("end of scan\n");
  57.     return 0;
  58. }
  59.         
  60. void usage(char *progname)
  61. {
  62.     printf("usage: %s <inputfile> [outputfile]\n", progname);
  63.     printf("\n\ninputfile: a list of hosts (or ip's) to scan\n");
  64.     printf("outputfile: optionally record results to a file instead of stdout\n\n\n");
  65. }
  66.  
  67. void printheader(void)
  68. {
  69.     fprintf(of, "port scan results file\n");
  70.     fprintf(of, "----------------------\n\n");
  71. }
  72.  
  73. void testhost(char *target)
  74. {
  75.     struct sockaddr_in server;
  76.     int sockfd, i;
  77.     char version[256];
  78.     struct hostent *hp;
  79.     printf("%s\n", target);
  80.     if((hp=(struct hostent *)gethostbyname(target)) == NULL) {
  81.         fprintf(of, "%s: unknown host\n", target);
  82.         return;
  83.     }
  84.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  85.     bzero(&server, sizeof(server));
  86.     server.sin_family = AF_INET;
  87.     server.sin_port = htons(31337);
  88.     memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
  89.     if((connect(sockfd, (struct sockaddr *)&server, sizeof(server))) == -1){
  90.         fprintf(of, "%s: connect error\n", target);
  91.         return;
  92.     }
  93.     else
  94.     {
  95.      fprintf(of, "%s: open", target);
  96.     }
  97.     close(sockfd);
  98.     return;
  99. }