Logo
Articles Compilers Libraries Books MiniBooklets Assembly C++ Rust Go Linux CPU Others Videos
Advertisement

Article by Ayman Alheraki on April 18 2026 04:39 PM

Building a Network Vulnerability Scanner with Modern C++ on Windows

Building a Network Vulnerability Scanner with Modern C++ on Windows

 

A Practical Guide to TCP Port Scanning, Banner Grabbing, and Basic Vulnerability Detection Using Only Standard Libraries and Winsock

Introduction

Network security assessment often begins with understanding which services are exposed on a target system. This article presents a complete, functional TCP port scanner and vulnerability detector written in modern C++ (C++17) for Windows. The tool performs concurrent port scanning, grabs service banners, and matches them against a database of known vulnerable software versions—all without relying on third-party libraries beyond the Windows Sockets API (Winsock2).

The scanner is designed to be:

  • Self-contained: Only standard C++ and Windows system libraries are used.

  • Efficient: A thread pool scans up to 100 ports simultaneously.

  • Educational: The code demonstrates practical use of threads, sockets, RAII, and pattern matching.

By the end of this article, you will have a working executable that can scan a target IP address, identify open ports, and flag potential vulnerabilities based on service banners.


Features

  • TCP Connect Scanning – Reliable detection of open ports using non‑blocking sockets with a 2‑second timeout.

  • Multi‑threaded Architecture – A custom thread pool enables fast concurrent scanning (configurable up to 100 threads).

  • Banner Grabbing – Sends protocol‑specific probes (e.g., HTTP HEAD request) to retrieve service banners.

  • Vulnerability Detection – Compares captured banners against a signature database of known vulnerable software (CVEs).

  • Comprehensive Reporting – Displays a summary and detailed table on the console, and saves the report to a text file.

  • RAII Winsock Management – Automatic initialisation and cleanup of the Windows Sockets library.

  • Command‑Line Options – Supports full port range scans (--full) and disabling banner grabbing (--no-banner).


Complete Source Code

The code below is ready to be saved as netvulnscan_win.cpp and compiled with Visual Studio or MinGW‑w64.

 

How the Scanner Works

1. Port Selection

  • By default, the scanner probes the first 1000 TCP ports plus a curated list of high‑value ports (e.g., 3306 for MySQL, 3389 for RDP).

  • The --full flag expands the target list to all 65,535 ports.

2. Concurrent Scanning with a Thread Pool

  • A custom ThreadPool class manages up to 100 worker threads.

  • Each port scan is submitted as a task to the pool, allowing parallel connection attempts.

3. Non‑Blocking TCP Connect

  • A non‑blocking socket is created and a connection is initiated.

  • The Windows select() function waits for the socket to become writable, with a 2‑second timeout.

  • If the connection succeeds, getsockopt(..., SO_ERROR, ...) confirms the port is open.

4. Banner Grabbing

  • After a successful connection, the socket is switched back to blocking mode.

  • A protocol‑specific probe is sent:

    • HTTP/HTTPS ports receive HEAD / HTTP/1.0\r\n\r\n.

    • All other ports receive a blank line (\r\n).

  • The response is read (up to 1024 bytes) and the first line is extracted as the banner.

5. Vulnerability Matching

  • The captured banner is converted to lowercase and compared against a signature database (VULN_SIGNATURES).

  • If a match is found, the corresponding CVE identifiers are recorded.

6. Reporting

  • Open ports are printed in real time.

  • A final report summarises the findings, displays a table of open ports, and lists any detected vulnerabilities.

  • The same information is saved to a text file (e.g., scan_report_192_168_1_1.txt).


Compilation Instructions

Using Visual Studio (2022 or newer)

  1. Create a new Console App (C++) project.

  2. Replace the default .cpp file with the code provided above.

  3. Right‑click the project → Properties.

  4. Navigate to C/C++ → Language and set C++ Language Standard to ISO C++17 Standard (/std:c++17) or ISO C++20.

  5. Build the solution (Build → Build Solution).

Using MinGW‑w64 (g++)

  1. Install MinGW‑w64 and ensure the bin folder is in your PATH.

  2. Open a Command Prompt in the folder containing netvulnscan_win.cpp.

  3. Run the following command:

     

Using Clang on Windows

 

Note: The -lws2_32 flag links the Winsock library. In Visual Studio, the #pragma comment(lib, "ws2_32.lib") directive handles this automatically.


Usage Examples

Scan a local machine or a remote host with default settings (top ~1000 ports):

 

Perform a full port scan (1–65535):

 

Disable banner grabbing (useful for faster, quieter scans):

 

Display help:

 

Sample Output (Abbreviated)

 


Limitations and Considerations

  • TCP Connect Scan Only: The scanner uses a full TCP handshake, which is logged by most firewalls and intrusion detection systems. A stealthier SYN scan would require raw sockets, which are heavily restricted on modern Windows without a kernel driver.

  • Basic Banner Matching: The vulnerability detection relies on simple substring matching. Real‑world fingerprinting is more complex and would require a larger database and regular expression support.

  • No Service Version Probing: Beyond the initial banner, no further probes are sent to elicit version information. Some services require protocol‑specific requests.

  • Timeout Values: The 2‑second timeout per port works well on local networks but may need adjustment for high‑latency connections.

  • Legal and Ethical Use: Only scan systems you own or have explicit permission to test. Unauthorised port scanning may be illegal in your jurisdiction.


Conclusion

This article has presented a fully functional network vulnerability scanner built with modern C++ and the Windows Sockets API. The tool demonstrates practical techniques in concurrent programming, network I/O, and security assessment. With minimal dependencies, it serves as an excellent foundation for learning or for extending into a more sophisticated security tool.

Feel free to enhance the scanner by expanding the vulnerability database, adding UDP scanning, or implementing more advanced service fingerprinting. The complete source code is provided above and can be compiled immediately on any Windows system with a C++17‑capable compiler.

Happy (and ethical) scanning!

Advertisements

Responsive Counter
General Counter
1242725
Daily Counter
987