Denizhalil

Port Scanning with C++

In today’s digital landscape, cybersecurity is of paramount importance. Detecting and addressing vulnerabilities within computer networks and systems is a fundamental part of defending against cyber threats. Port scanning is a common technique used to identify open ports on a target device, and in this article, we will walk you through how to create a basic port scanning tool using C++.

Introduction

Port scanning is the process of systematically probing a target device to determine whether specific ports are open (available for communication) or closed (unavailable). Using C++ to perform this scan, you can gain valuable insights into the security of a networked device. Below is the C++ code that accomplishes this task:
Not: Don’t forget to read our port scanning article with Python 😉

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int main(int argc, char* argv[]) {
    if (argc != 4) {
        std::cerr << "Usage: \n\t" << argv[0] << " <target-ip> <start-port> <end-port>" << std::endl;
        return 1;
    }

    const char* target_ip = argv[1];
    int start_port = std::atoi(argv[2]);
    int end_port = std::atoi(argv[3]);

    for (int port = start_port; port <= end_port; ++port) {
        int sock = socket(AF_INET, SOCK_STREAM, 0);
        if (sock == -1) {
            std::cerr << "Socket could not be created: " << strerror(errno) << std::endl;
            return 1;
        }

        sockaddr_in server_address;
        server_address.sin_family = AF_INET;
        server_address.sin_port = htons(port);
        if (inet_pton(AF_INET, target_ip, &server_address.sin_addr) <= 0) {
            std::cerr << "Invalid IP address" << std::endl;
            return 1;
        }

        if (connect(sock, (struct sockaddr*)&server_address, sizeof(server_address)) == 0) {
            std::cout << "Port " << port << " is open" << std::endl;
            close(sock);
        } else {
            // Port is closed
        }
    }
    return 0;
}

This code allows you to specify a target IP address and a range of ports to scan. It then attempts to establish a connection to each port within the specified range.

How the Code Works

  1. Initially, the program takes input from the user, including the target IP address, the starting port, and the ending port.
  2. Next, it enters a loop that iterates through the range of port numbers provided.
  3. At each iteration, a socket (communication endpoint) is created for the port.
  4. If the socket cannot be created, an error message is displayed, and the program exits.
  5. If the socket is successfully created, a connection attempt is made to the target IP address and port.
  6. If the connection is successful, the program reports that the port is open and then closes the socket.
  7. If the connection attempt fails, it is assumed that the port is closed.
  8. The scanning process continues until all ports in the specified range have been checked.

This code serves as a basic port scanning tool. However, it’s essential to remember that port scanning should only be conducted for legal and authorized purposes. Unauthorized scanning of others’ systems or networks is illegal and can have severe consequences.

Conclusion

In conclusion, we have learned how to create a basic port scanning tool using C++. Port scanning is a valuable technique for assessing the security of a networked device. Still, it should be used responsibly and with permission. When using such tools, always adhere to legal and ethical standards to avoid any legal consequences.

We hope this article has helped you understand the fundamentals of port scanning and its importance in network security. As you work to enhance your cybersecurity practices, use these tools responsibly and in alignment with legal and ethical guidelines.

For more information on network security, feel free to explore additional resources and tutorials available in the field. Stay secure and vigilant in an increasingly interconnected digital world.

Leave a Comment

Join our Mailing list!

Get all latest news, exclusive deals and academy updates.