Uncover the infinite in IT

Table of Contents
< All Topics

Multiscan script

Step 1: Create the Script

Create the Python script (multiscan.py) in your desired directory. 

import subprocess
import time
import os
import shutil
import platform

def install_missing_packages():
    missing_packages = []

    if not is_tool_installed("nmap"):
        missing_packages.append("nmap")

    if not is_tool_installed("sslscan"):
        missing_packages.append("sslscan")

    if missing_packages:
        print("The following packages are missing:")
        for package in missing_packages:
            print(package)

        response = input("Do you want to install the missing packages? (y/n): ").strip().lower()
        if response == 'y':
            if platform.system() == "Linux" and platform.linux_distribution()[0] == "debian":
                install_command = ["sudo", "apt", "update", "&&", "sudo", "apt", "install", "-y"] + missing_packages
            elif platform.system() == "Linux" and platform.linux_distribution()[0] == "redhat":
                install_command = ["sudo", "yum", "install", "-y"] + missing_packages
            else:
                print("Unsupported OS.")
                exit(1)

            subprocess.run(install_command)
        else:
            print("Exiting the script as required tools are not installed.")
            exit(1)

def is_tool_installed(tool_name):
    try:
        if shutil.which(tool_name):
            return True
        else:
            return False
    except Exception:
        return False

def get_target_input():
    print("Choose the target input option:")
    print("1. Single target")
    print("2. Targets list file")

    option = input("Enter the option number (1/2): ")
    return option

def get_target_single():
    target_ip = input("Enter the target IP address to scan: ")
    return [target_ip]

def get_target_list_file():
    targets_file = input("Enter the path to the targets list file: ")
    if not os.path.exists(targets_file):
        print("File not found. Exiting.")
        exit(1)
    with open(targets_file, "r") as file:
        targets = file.read().splitlines()
    return targets

def get_scan_option():
    print("Choose a scan option:")
    print("1. nmap scan")
    print("2. sslscan scan")
    print("3. Both")

    option = input("Enter the option number (1/2/3): ")
    return option

def get_nmap_port_options():
    print("Choose an option for nmap port scanning:")
    print("1. Default port range (no -p option)")
    print("2. Single port")
    print("3. Port range")
    print("4. Full port range (1-65535)")

    option = input("Enter the option number (1/2/3/4): ")

    if option == "1":
        return ""
    elif option == "2":
        port = input("Enter the port to scan: ")
        return f"-p {port}"
    elif option == "3":
        start_port = input("Enter the starting port: ")
        end_port = input("Enter the ending port: ")
        return f"-p {start_port}-{end_port}"
    elif option == "4":
        print("You've selected the full port range (1-65535). This may take a while to complete.")
        return "-p 1-65535"
    else:
        print("Invalid option. Using the default port range.")
        return ""

def create_target_directory(target_ip):
    # Define the log directory path
    log_directory = os.path.join("/var/log", target_ip.replace(".", "_"))

    if not os.path.exists(log_directory):
        os.makedirs(log_directory, exist_ok=True)

    return log_directory

def run_nmap_scan(target_ip, port_options, log_file):
    nmap_options = ["-Pn", "--reason", "-A", "--version-all"]
    nmap_command = ["nmap"] + nmap_options

    if port_options:
        nmap_command.extend(port_options.split())  # Split port_options into a list

    nmap_command.extend(target_ip)

    with open(log_file, "a") as log:
        result = subprocess.run(nmap_command, text=True, capture_output=True)
        log.write(result.stdout)
        print(f"nmap scan complete. Results logged to {os.path.abspath(log_file)}")
        print(result.stdout)

def run_sslscan(target_ip, log_file):
    sslscan_command = ["sslscan", target_ip]
    with open(log_file, "a") as log:
        result = subprocess.run(sslscan_command, text=True, capture_output=True)
        log.write(result.stdout)
        print(f"sslscan complete. Results logged to {os.path.abspath(log_file)}")
        print(result.stdout)

def main():
    install_missing_packages()
    option = get_target_input()
    targets = []

    if option == "1":
        targets = get_target_single()
    elif option == "2":
        targets = get_target_list_file()

    scan_option = get_scan_option()
    nmap_port_options = ""

    if scan_option == "1" or scan_option == "3":
        nmap_port_options = get_nmap_port_options()

    for target_ip in targets:
        log_directory = create_target_directory(target_ip)

        if scan_option == "1" or scan_option == "3":
            if is_tool_installed("nmap"):
                nmap_log_file = os.path.join(log_directory, f"nmap_results_{time.strftime('%Y%m%d_%H%M%S')}.log")
                run_nmap_scan([target_ip], nmap_port_options, nmap_log_file)
            else:
                print("nmap is not installed. Skipping nmap scan.")

        if scan_option == "2" or scan_option == "3":
            if is_tool_installed("sslscan"):
                sslscan_log_file = os.path.join(log_directory, f"sslscan_results_{time.strftime('%Y%m%d_%H%M%S')}.log")
                run_sslscan(target_ip, sslscan_log_file)
            else:
                print("sslscan is not installed. Skipping sslscan.")

        print(f"Results are logged in the following directories and log files:")
        if scan_option == "1" or scan_option == "3":
            if is_tool_installed("nmap"):
                print(f"Nmap results: {os.path.abspath(nmap_log_file)}")

        if scan_option == "2" or scan_option == "3":
            if is_tool_installed("sslscan"):
                print(f"Sslscan results: {os.path.abspath(sslscan_log_file)}")

if __name__ == "__main__":
    main()

Step 2: Navigate to the Script Directory

Change your working directory to where the script is located:

cd /scripts

Step 3: Run the Script

Execute the script using the following command:

python3 multiscan.py

Step 4: Enter Target Information

You’ll be prompted to choose the target input option. Select one of the following:

  1. Single target
  2. Targets list file (you need to create a file with IP addresses one under the other)

Example (Single Target):

Choose the target input option:
1. Single target
2. Targets list file
Enter the option number (1/2): 1
Enter the target IP address to scan: 192.168.1.1

Example (Targets List File):

Choose the target input option:
1. Single target
2. Targets list file
Enter the option number (1/2): 2
Enter the path to the targets list file: /path/to/targets.txt

Step 5: Choose Scan Options

You’ll then be prompted to choose a scan option:

  1. Nmap scan
  2. Sslscan scan
  3. Both

Example:

Choose a scan option:
1. Nmap scan
2. Sslscan scan
3. Both
Enter the option number (1/2/3): 3

Step 6: Configure Nmap Port Options

If you selected an Nmap scan or both, you’ll be asked to configure Nmap port scanning options:

Example:

Choose an option for Nmap port scanning:
1. Default port range (no -p option)
2. Single port
3. Port range
4. Full port range (1-65535)
Enter the option number (1/2/3/4): 2
Enter the port to scan: 22

Step 7: View Results

The script will execute the selected scans, and you’ll see the results. If Nmap is chosen, the results will be saved in /var/log/<target_ip>/nmap_results_<timestamp>.log. If Sslscan is chosen, the results will be saved in /var/log/<target_ip>/sslscan_results_<timestamp>.log.

Example:

Results are logged in the following directories and log files:
Nmap results: /var/log/192_168_1_1/nmap_results_20231105_120000.log
Sslscan results: /var/log/192_168_1_1/sslscan_results_20231105_120001.log

Congratulations! You’ve successfully run the network scanning script. Feel free to explore the logs for detailed information about the scan results.