Jump to content
dustyhawk

pscan2.c [windows]

Recommended Posts

nu e creat de mine, preluat de pe sv lu raducu :ayy:

#include "stdafx.h"
#include <winsock2.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <windows.h>
#include <process.h>
#include <string.h>
#include <ws2tcpip.h>
#define MAX_SOCKETS 1000
#define TIMEOUT 3
#define S_NONE       0
#define S_CONNECTING 1
WSADATA WsaDat;
struct conn_t {
	SOCKET s;
	char status;
	struct sockaddr_in addr;
};
struct conn_t connlist[MAX_SOCKETS];

void init_sockets(void);
void check_sockets(void);
void fatal(char *);

FILE *outfd;
int tot = 0;
int main(int argc, char *argv[])
{
	if (WSAStartup(MAKEWORD(2, 2), &WsaDat) != 0){
		return 0;
	}
	int done = 0, i, cip = 1, bb = 0, ret, k, ns, x;
	time_t scantime;
	char ip[20], outfile[128], last[256];

	if (argc < 3)
	{
		printf("Usage: %s <b-block> <port>\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	memset(&outfile, 0, sizeof(outfile));
	if (argc == 3)
		sprintf_s(outfile, sizeof(outfile)-1, "scan.log", argv[1], argv[2]);

	if (!(outfd = fopen(outfile, "a")))
	{
		perror(outfile);
		exit(EXIT_FAILURE);
	}
	printf("# scanning: ", argv[1]);
	fflush(stdout);

	memset(&last, 0, sizeof(last));
	init_sockets();
	scantime = time(0);

	while (!done)
	{
		for (i = 0; i < MAX_SOCKETS; i++)
		{
			if (cip == 255)
			{
				if ((bb == 255) || (argc >= 4))
				{
					ns = 0;
					for (k = 0; k < MAX_SOCKETS; k++)
					{
						if (connlist[k].status > S_NONE)
						{
							ns++;
							break;
						}
					}

					if (ns == 0)
						done = 1;

					break;
				}
				else
				{
					cip = 0;
					bb++;
					for (x = 0; x < strlen(last); x++)
						putchar('\b');
					memset(&last, 0, sizeof(last));
					sprintf_s(last, sizeof(last)-1, "%s.%d.* (total: %d) (%.1f%% done)",
						argv[1], bb, tot, (bb / 255.0) * 100);
					printf("%s", last);
					fflush(stdout);
				}
			}

			if (connlist[i].status == S_NONE)
			{
				connlist[i].s = socket(AF_INET, SOCK_STREAM, 0);
				if (connlist[i].s == -1)
					printf("Unable to allocate socket.\n");
				else
				{
						memset(&ip, 0, 20);
						sprintf(ip, "%s.%d.%d", argv[1], bb, cip);
						connlist[i].addr.sin_addr.s_addr = inet_addr(ip);
						if (connlist[i].addr.sin_addr.s_addr == -1)
							fatal("Invalid IP.");
						connlist[i].addr.sin_family = AF_INET;
						connlist[i].addr.sin_port = htons(atoi(argv[2]));
						connlist[i].status = S_CONNECTING;
						cip++;
					}
				}
			}

		check_sockets();
	}
	printf("\n# pscan completed in %u seconds. (found %d ips)\n", (time(0) - scantime), tot);
	fclose(outfd);
	WSACleanup();
	printf("\n# Press any key to exit\n");
	getchar(); // you might want to remove this...
	exit(EXIT_SUCCESS);
}

void init_sockets(void)
{
	int i;

	for (i = 0; i < MAX_SOCKETS; i++)
	{
		connlist[i].status = S_NONE;
		memset((struct sockaddr_in *)&connlist[i].addr, 0, sizeof(struct sockaddr_in));
	}
	return;
}
typedef struct {
	int i;
}t;

void pck(void *param){
	int ret;
	struct timeval timeout;
	timeout.tv_sec = 3;
	timeout.tv_usec = 0;
	t *args = (t*)param;
	int i = args->i;

	setsockopt(connlist[i].s, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
		sizeof(timeout));

	setsockopt(connlist[i].s, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
		sizeof(timeout));

	if (connect(connlist[i].s, (struct sockaddr *)&connlist[i].addr,
		sizeof(struct sockaddr_in)) != SOCKET_ERROR){
		tot++;
		fprintf(outfd, "%s\n",
			(char *)inet_ntoa(connlist[i].addr.sin_addr));
		closesocket(connlist[i].s);
		connlist[i].status = S_NONE;
	}
	else {
		closesocket(connlist[i].s);
		(connlist[i].s);
		connlist[i].status = S_NONE;
	}
}

void check_sockets(void)
{
	int i, ret;
	t *arg;
	arg = (t *)malloc(sizeof(t));
	for (i = 0; i < MAX_SOCKETS; i++)
	{
		if (connlist[i].status == S_CONNECTING)
		{
			arg->i = i;
			_beginthread(pck, 0, (void*)arg);
		}
	}
}

void fatal(char *err)
{
	int i;
	printf("Error: %s\n", err);
	for (i = 0; i < MAX_SOCKETS; i++)
	if (connlist[i].status >= S_CONNECTING)
		closesocket(connlist[i].s);
	fclose(outfd);
	exit(EXIT_FAILURE);
}

succes cu el csz =))


 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...