C++ Windows Port Scanner

Can you code for windows in c/c++ if so, continue reading, as we are going to code a basic windows port scanner in the c/c++ language. This is a command line port scanner tool made from source (c++).

Windows Port Scanner With Source Code
windows port scanner

So, why building a port scanner? and why in c?

As the nature or this site, open port, we are into port checker, and port scanning so why not understanding deeper this field by developing a small basic open port scanner for the windows operation system.

If we simplify the subject we need to code a small piece of code that can check one port at the time and then we can loop it with a list of ports that we want to check, slow? yes, not elegant? yes, but will do the job Yes!

Once we have this piece of code we can build on top of it something more elegant that use threads to check more then one port at the time as an example.

We can also use more then one method to check if port is open, it will be more advanced port scanner but we can use the Winpcap library to add more scan methods, like the SYN scan, I may do this scanner upgrade in another post.

Why C/C++?

C/C++ is a great language and I love developing all kind of tools using this language, it also support deeper option as if we want to change this tool to a more advanced port scanner c/c++ is my chosen language, if you need to dive deep into the system internal or deeper into drivers this is your language..

So a windows port scanner

Yes, we are going to develop a windows port scanner using windows sockets for IPv4, its a connect() base port scanner, this will be a free port scanner that you can use, change and give.

It will be base on a console application, aka command line tool that you can run against a remoter host to check what ports are open on that host.

Port scanner tool commands:

The Port scanner tool will handle the following parameters from the command line:

  • -h host : the target host we want to check its ports.
  • -p [port, port range]: the ports that we want to check on the target host.
  • -v : verbose more, show more information.
  • -?: show this help.

As a base rule only the -h (target host) is a mandatory field, we will have a list of defaults port to scan, in a case that we do not get port or ports to check as a parameter to the tool.

Here is an example of open port, using the tool:

windows open port scanner
windows open port scanner

So what is advanced port scanner?

Advanced port scanner will have to support more scanning methods, like mentioned above, for example a half connection test, also known as SYN scan, where we only send the first packet and base on the replay we can say if the port is open or not.

Multi threader scanner, for fast performance, can also be use to scan full subnet – range of IP addresses.

Lets do some code!

Here is the function that do the test, it get an IP address and a port number.

The bold line is the connect method that try to connect to the port, if mange port is open..


BOOL PortScannerFunc(char *ip, unsigned int port)
{
char cMsg[1024]={0};
SOCKET sock;

int iResult;
sockaddr_in clientService;
memset(&clientService,0x00,sizeof(clientService));
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(ip);

sock = INVALID_SOCKET;
sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET)
{
printf("Socket return error, code: %d\n",WSAGetLastError());
closesocket(sock);
return FALSE;
}

if(g_pTarget->verbus == 1)
printf("Checking port %ld: ",port);
clientService.sin_port = htons(port);
iResult = connect(sock, (SOCKADDR *) & clientService, sizeof (clientService));
if (iResult != SOCKET_ERROR)
{
printf(" Found Open Port: %ld (",port);
ShowPortNumberInfo(port);
printf(")\n");

}
else
{
if(g_pTarget->verbus == 1)
printf(" closed!\n");
}

return TRUE;
}

 

Download the full tool code here (c/c++):

WindowsPortScannerSrc (4025 downloads )

Here is the tool help screen:

free port scanner help screen
free port scanner help screen


FreePortScanner - A small command line port scanner, with source code in c.

How To Use:
FreePortScanner -h <host> -p [port, port range] -v

Options:
-h <host>: the target host we want to check its ports.
-p [port, port range]: the ports that we want to check on the target host.
-v: verbose more, show more information.
-?: show this help.

Example:
FreePortScanner -h openport.net -p 80-500
Will scan from port 80 to port 500 on the openport.net web site.

Page on site: https://openport.net/windows-port-scanner/

Coded for and by the OpenPort.net team.
Copyright (c) 2018 by OpenPort.net, All rights reserved.

Disclaimer:
The software is provided “AS IS” without any warranty,
either expressed or implied, including, but not limited to,
the implied warranties of merchantability and fitness for a particular
purpose. The author will not be liable for any special, incidental,
consequential or indirect damages due to loss of data or any other reason.

License:
This utility is released as freeware. You are allowed to freely distribute
this utility via floppy disk, DVD/CD-ROM, USB Devices, Internet, or in any
other way, as long as you don’t charge anything for this. If you distribute
this utility, you must include all files in the distribution package,
without any modification!

Source Code:
In addition to the above, in the source code section you are allow to freely
do what ever you want with the code, as long as you drop a line for the
OpenPort.net in your code and/or credits page.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

Compile and build using Visual Studio:

I am using visual studio version 2015 for the development of this tool, here are the steps to create a console application add the code to it compile it and run the Free Port Scanner tool.

Please do the following:

  • Open visual studio.
  • Press File->New-Project and press enter.
  • Under Templates->Visual C++ -> Windows>Win32 (In  the New Project dialog).
  • Select Win32 Console Application.
  • In the name enter: FreePortScanner ( you can also set your project folder).
  • Verify that Create Directory for the solution is selected.
  • Now press OK.
  • Press Next and select the Empty Project.
  • And press Finish.

In Visual Studio Project Window do the following:

  • Press on solution.
  • Expend the FreePortScanner->Source Files
  • Right Click on Source File -> Add.
  • Here you have 2 options:
    • 1 add new item, select cpp set the name to FreePortScanner and press add.
    • 2 add existing item and navigate to the file you downloaded from this site (source)
    • If you chose option1, to add an item, copy the code from this site and paste it into the new file.
  • Press Build->Rebuild Solution in the top menu.
  • If all pass well you will have an exe file that you can run.

You need to open the command line, navigate to the debug folder and run the tool.

  • Right click on the solution name
  • Select open folder in file explorer.
  • Go one folder up and enter debug folder.
  • Select and copy the folder location
  • Press Windows +R
  • Write CMD and press enter.
  • Type cd <debug folder name> use right click paste and press enter.
  • Type FreePortScanner -? and press enter to show the tool help.
  • To run a scan use FreePortScanner -h <host name>

Where to go from here:

You can start by coding the functionality that I write about under advanced port scanner.

It is also possible to create a nice to use GUI for the tool.