Single-thread port scanner written in C

Source: Internet
Author: User

# Include <stdio. h>
# Include <string. h>
# Include <winsock2.h>

# Pragma comment (Lib, "ws2_32 ")

Void scan1 (char *);
Void scan2 (char *, char *, char *);
Void usage (char *);

Int main (INT argc, char * argv [])
{
If (argc! = 2 & argc! = 4) usage (argv [0]);
Else
{
If (argc = 2) scan1 (argv [1]);
Else
{
If (argc = 4) scan2 (argv [1], argv [2], argv [3]);
}
}
}

Void usage (char * Help)
{
Printf ("========================================== =====/N ");
Printf ("netxscan_port ver0.1 by netxfly@21cn.com/N ");
Printf ("% S <targetip> [Options]/n", help );
Printf ("[Option]/n ");
Printf ("<startport> <endport>/N ");
Printf ("Usage:/n ");
Printf ("% s 192.168.1.1/N", help );
Printf ("% s 192.168.1.1 21 5000/N", help );
Printf ("========================================== =====/N ");

Exit (-1 );
}

Void scan1 (char * target)
{

Int I;
Wsadata;
Socket netxfly;
Struct sockaddr_in sin;

Int ports [18] = {80,110,111,135,139,443,445,554,143 };

If (wsastartup (makeword (2, 2), & wsadata )! = 0)
{
Printf ("Startup Winsock failed.../N ");
Exit (-1 );
}

Memset (& sin, 0, sizeof (SIN ));

For (I = 0; I <18; I ++)
{
If (netxfly = socket (af_inet, sock_stream, 0) = invalid_socket)
{
Printf ("create socket err.../R/N ");
Exit (-1 );
}

Sin. sin_family = af_inet;
Sin. sin_addr.s_addr = inet_addr (target );
Sin. sin_port = htons (ports [I]);

If (connect (netxfly, (struct sockaddr *) & sin, sizeof (SIN ))! = Socket_error)
{
Printf ("port % d is open/R/N", ports [I]);
Closesocket (netxfly );
}
Closesocket (netxfly );
}

Wsacleanup ();
}

Void scan2 (char * target, char * Start, char * end)
{

Int I;
Wsadata;
Socket netxfly;
Struct sockaddr_in sin;

Int startport = atoi (start );
Int endport = atoi (end );

If (wsastartup (makeword (2, 2), & wsadata )! = 0)
{
Printf ("Startup Winsock failed.../N ");
Exit (-1 );
}

If (netxfly = socket (af_inet, sock_stream, 0) = invalid_socket)
{
Printf ("create socket err.../R/N ");
Exit (-1 );
}

Memset (& sin, 0, sizeof (SIN ));

For (I = startport; I <= endport; I ++)
{
If (netxfly = socket (af_inet, sock_stream, 0) = invalid_socket)
{
Printf ("create socket err.../R/N ");
Exit (-1 );
}

Sin. sin_family = af_inet;
Sin. sin_addr.s_addr = inet_addr (target );
Sin. sin_port = htons (I );

If (connect (netxfly, (struct sockaddr *) & sin, sizeof (SIN ))! = Socket_error)
{
Printf ("port % d Open/R/N", I );
Closesocket (netxfly );
}
Closesocket (netxfly );
}

Wsacleanup ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.