Visit our newest sister site!
Hundreds of free aircraft flight manuals
Civilian • Historical • Military • Declassified • FREE!


TUCoPS :: Hacking Techniques :: portchk.c

portchk.c Takes either command line input or file input and checks if a given port on a remote host is accesible.




/*
 * 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;
}


TUCoPS is optimized to look best in Firefox® on a widescreen monitor (1440x900 or better).
Site design & layout copyright © 1986-2014 AOH