Jump to content
JIHAD

pscan2.c ported for Windows VS C 2013

Recommended Posts

Posted

aceasta este varianta demonstrativa.

n-am stat sa ma chinui f. mult.

astept sugestii.

//  pscan2.c ported on Windows
// JIHAD OWNS YOU
// just for demo purposes
// you might wanna check thread waiting before showing the final result
// enjoy!
#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);
}

Posted

Cateva ponturi de programare:

  • nu folosi precompiled headers pentru programe de mici dimensiuni. Acestea au rolul de a minimiza timpul de compilare pentru proiecte ample(de ex. jocuri BF3-4, CoD, etc)
  • ca si practica de programare este bine sa folosesti variabile care au nume sugestive(evita variabilele de doua sau trei litere in cazul in care acestea sunt greu de urmarit in cod)
  • poti pune mai multe comment-uri; te vor ajuta enorm cand vrei sa reiei sau modifici proiectul cu luni in urma dupa ce l-ai terminat
  • este bine ca incerci sa vezi cum functioneaza aceste aplicatii insa incerca sa te axezi pe ceva nou: nmap este open source si realizeaza deja functia programului de mai sus

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...