Project 2

Home PortfolioProject 2

Port Scanner

This program is one of the most important tools for Ethical Hackers. With it we are able to extract important technical information about our target such as which services are being run on the target device and whether their versions are outdated.

The Python port scanner script systematically probes specified target IP addresses for open ports using Python’s socket library for communication and termcolor for output formatting. Key functions like scan and scan_port manage the scanning process and individual port connections, respectively, with built-in error handling for reliability. User input prompts accommodate both single and multiple target IP addresses, while iterative port scanning attempts TCP connections across defined port ranges. Output messages are formatted for clarity, distinguishing between scan initiation and open port discoveries. This tool is essential for proactive network security assessments, enabling professionals to detect vulnerabilities and enhance defenses responsibly within authorized contexts, adhering to ethical and legal standards.

Adjustments and Explanation:

1. Imports and Formatting:
        ○ Ensure that imports (socket and termcolor) are properly aligned and
free from indentation errors

import socket
import termcolor

2. Indentation and Function Definitions:
     ○ Correct indentation ensures that  functions and blocks of code are properly nested. Python relies on consistent indentation to define code                 blocks.

def scan(target, ports):
print(‘\n’ + ‘ Starting Scan For ‘ + str(target))
for port in range(1, ports + 1):
scan_port(target, port)

def scan_port(ipaddress, port):

try:
sock = socket.socket()
sock.connect((ipaddress, port))
print(“[+] Port ” + str(port) + ” Opened”)
sock.close()
except:
pass
def main():
targets = input(“[*] Enter Targets To Scan (split them by
‘,’): “)
ports = int(input(“[*] Enter How Many Ports You Want To Scan:
“))
if ‘,’ in targets:
print(termcolor.colored(“[*] Scanning Multiple Targets”,
‘green’))
for ip_addr in targets.split(‘,’):
scan(ip_addr.strip(‘ ‘), ports)
else:
scan(targets, ports)
if __name__ == “__main__”:
main()

3. Explanation of Changes:

○ Indentation: Each function (scan, scan_port, main) and the main block
(if __name__ == “__main__”:) should be properly indented for
clarity and correct execution.
○ Function Parameters: In scan_port, corrected the connect method to
use (ipaddress, port) in the tuple format.
○ Loop Range: Adjusted range(1, ports + 1) in scan(target,
ports) to ensure it scans up to and including the specified number of
ports (ports).
○ Error Handling: Added a generic except: block to catch any exceptions
that may occur during socket operations. However, it’s generally
recommended to specify the specific exceptions (except
socket.error for socket-related errors) for better error handling.

4. Execution Flow: The main() function gathers user input for targets and ports, then determines whether the input is a single target or multiple targets separated by commas. It then initiates the scanning process accordingly.

Usage:
● When you run this script, it will prompt you to enter the targets (IP addresses) you
want to scan and the number of ports to scan.
● If scanning multiple targets (separated by commas), it will iterate over each
target and scan the specified ports.
● It will print messages using termcolor to highlight different stages of the
scanning process.
Make sure to run this script with appropriate permissions and only against systems for which you have authorization. Port scanning without permission can violate laws and ethical standards. Always use this knowledge responsibly and ethically.