Learn2Hack

Live one-one-one lessons


network security
Tutorials

-Using A VPN
-Hacking Basics
-Intro To Linux
-SQLmap
-Exploiting WebDAV
-Hack Wifi
-Ngrok with Beef-XSS
-Using Proxychains
-Netcat Basic Usage
-Nmap Tutorial
-Password Attacks
-Phishing
-How to use Wireshark

Python

-Intro To Python
-Intermediate Python
-Python Port Scanner
-Python Sandbox

Creating a Port Scanner Using Python


Python Scripting for Security




In this tutorial, we will learn how to create a Python port scanner that you can integrate into your website. A port scanner is a tool used to discover open ports on a networked device. By creating this scanner, your website visitors can check the open ports on their target IP addresses.


Table of Contents:

  1. Step 1: Importing the necessary modules
  2. Step 2: Creating the port scanning function
  3. Step 3: Getting user input
  4. Step 4: Main function
  5. Step 5: Run the port scanner

Prerequisites:


  1. *Basic understanding of Python

Step 1: Importing the necessary modules


We'll start by importing the required modules for our port scanner: socket and sys.


import socket
import sys

Step 2: Creating the port scanning function


Let's define a function called scan_ports that will take the target IP address and a range of ports to scan. The function will attempt to connect to each port and check if it's open.


def scan_ports(target_ip, start_port, end_port):
    open_ports = []
    for port in range(start_port, end_port + 1):
        try:
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                result = s.connect_ex((target_ip, port))
                if result == 0:
                    open_ports.append(port)
        except socket.error:
            pass
    return open_ports

Step 3: Getting user input


Next, we'll get the target IP address and the range of ports to scan from the user.


def get_input():
    target_ip = input("Enter the target IP address: ")
    start_port = int(input("Enter the starting port number: "))
    end_port = int(input("Enter the ending port number: "))
    return target_ip, start_port, end_port

Step 4: Main function


Now, let's create a main function that ties everything together. We'll call get_input to get the user input and then call scan_ports to perform the port scan.


def main():
    target_ip, start_port, end_port = get_input()
    open_ports = scan_ports(target_ip, start_port, end_port)

    if open_ports:
        print("Open ports:")
        for port in open_ports:
            print(f"Port {port} is open.")
    else:
        print("No open ports found.")

if __name__ == "__main__":
    main()

Step 5: Run the port scanner


Save the Python code in a file with a .py extension, for example, port_scanner.py. Open a terminal or command prompt, navigate to the directory where you saved the file, and run the script using the following command:

python port_scanner.py

The script will prompt you to enter the target IP address and the range of ports to scan. After entering the input, the script will perform the port scan and display the open ports, if any.

Important Note:

Port scanning can be seen as a security probing activity, and unauthorized scanning of networks and systems is illegal and unethical. Always ensure you have proper authorization before using any security-related tools or techniques.

Copyright © Learn2Hack 2023


Contact: info@learn2hack.io

Telephone: 1 306 992-2446

Policy
minicoders