exploit the possibilities

portchk.c

portchk.c
Posted Jan 11, 2000
Authored by Missinglnk | Site tribune.intranova.net

Simple port checker that either takes command-line input or file input and checks each host if a given port is open.

tags | tool, scanner
systems | unix
MD5 | 205c235c03921ce3762f8a5922c406d1

portchk.c

Change Mirror Download
/*
* port checker for bob.
* - missnglnk@tribune.intranova.net
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/param.h>

#define MAXLEN 32768
extern int errno;

int
main(int argc, char **argv)
{
FILE *input;
int argswitch, fflag = 0, sflag = 0, sockfd, tport = -2,
verbose = 0;
char *ifile, *target, tmpdata[32768];
struct sockaddr_in rmt;
struct hostent *he;
unsigned long ip;

while ((argswitch = getopt(argc, argv, "f:h:p:v")) != -1) {
switch (argswitch) {
case 'f':
fflag = 1;
if (strlen(optarg) > MAXPATHLEN) {
printf("ERROR: Filename too long.\n");
return -1;
}
ifile = optarg;
break;
case 'h':
sflag = 1;
if (strlen(optarg) > MAXHOSTNAMELEN) {
printf("ERROR: Target hostname too long.\n");
return -1;
}
target = optarg;
break;
case 'p':
tport = atoi(optarg);
break;
case 'v':
verbose = 1;
break;
case '?':
default:
printf("port checker for bob.\n");
printf("%s [-f input file] [-p target port]\n", argv[0]);
printf("%s [-h single host] [-p target port]\n", argv[0]);
return -1;
break;
}
}

argc -= optind;
argv += optind;

if (sflag == 1 && fflag == 1) {
printf("ERROR: Cannot specify -f and -h together.\n");
return -1;
}
if (tport == -2) {
printf("ERROR: No port specified. (-p)\n");
return -1;
}
if (sflag == 1) {
if (verbose == 1) {
printf("Checking remote host %s\n", target);
printf("Checking port %d\n", tport);
}
if ((he = gethostbyname(target)) != NULL) {
ip = *(unsigned long *) he->h_addr;
} else if ((ip = inet_addr(target)) == NULL) {
if (verbose == 1) {
perror("Error resolving target");
}
return -1;
}
rmt.sin_family = AF_INET;
rmt.sin_addr.s_addr = ip;
rmt.sin_port = htons(tport);

if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
if (verbose == 1) {
perror("Error creating socket");
}
return -1;
}
if (connect(sockfd, (struct sockaddr *) & rmt, sizeof(rmt)) == 0) {
printf("%s\n", target);
} else {
if (verbose == 1) {
perror("Error connecting");
}
}

if (close(sockfd) < 0) {
if (verbose == 1) {
perror("Error closing socket");
}
return -1;
}
} else if (fflag == 1) {
if (verbose == 1) {
printf("Reading from %s\n", ifile);
}
if ((input = fopen(ifile, "r")) == NULL) {
perror("Error opening input file");
return -1;
}
if (fread(tmpdata, MAXLEN, 1, input) < 0) {
perror("Error reading from input file");
return -1;
}
for ((target = strtok(tmpdata, "\n")); target != NULL; ((target = strtok(NULL, "\n")) != NULL)) {
if (verbose == 1) {
printf("Checking remote host %s\n", target);
printf("Checking port %d\n", tport);
}
if ((he = gethostbyname(target)) != NULL) {
ip = *(unsigned long *) he->h_addr;
} else if ((ip = inet_addr(target)) == NULL) {
if (verbose == 1) {
perror("Error resolving target");
}
}
rmt.sin_family = AF_INET;
rmt.sin_addr.s_addr = ip;
rmt.sin_port = htons(tport);

if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
if (verbose == 1) {
perror("Error creating socket");
}
return -1;
}
if (connect(sockfd, (struct sockaddr *) & rmt, sizeof(rmt)) == 0) {
printf("%s\n", target);
} else {
if (verbose == 1) {
perror("Error connecting");
}
}

if (close(sockfd) < 0) {
if (verbose == 1) {
perror("Error closing socket");
}
return -1;
}
}

if (fclose(input) < 0) {
perror("Error closing input");
return -1;
}
} else {
printf("ERROR: No scan type specified, bob.\n");
return -1;
}

return 0;
}

Comments

RSS Feed Subscribe to this comment feed

No comments yet, be the first!

Login or Register to post a comment

File Archive:

November 2019

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Nov 1st
    28 Files
  • 2
    Nov 2nd
    1 Files
  • 3
    Nov 3rd
    1 Files
  • 4
    Nov 4th
    12 Files
  • 5
    Nov 5th
    19 Files
  • 6
    Nov 6th
    65 Files
  • 7
    Nov 7th
    22 Files
  • 8
    Nov 8th
    18 Files
  • 9
    Nov 9th
    1 Files
  • 10
    Nov 10th
    1 Files
  • 11
    Nov 11th
    11 Files
  • 12
    Nov 12th
    65 Files
  • 13
    Nov 13th
    27 Files
  • 14
    Nov 14th
    22 Files
  • 15
    Nov 15th
    18 Files
  • 16
    Nov 16th
    0 Files
  • 17
    Nov 17th
    0 Files
  • 18
    Nov 18th
    0 Files
  • 19
    Nov 19th
    0 Files
  • 20
    Nov 20th
    0 Files
  • 21
    Nov 21st
    0 Files
  • 22
    Nov 22nd
    0 Files
  • 23
    Nov 23rd
    0 Files
  • 24
    Nov 24th
    0 Files
  • 25
    Nov 25th
    0 Files
  • 26
    Nov 26th
    0 Files
  • 27
    Nov 27th
    0 Files
  • 28
    Nov 28th
    0 Files
  • 29
    Nov 29th
    0 Files
  • 30
    Nov 30th
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2019 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close