Mar-06-2022, 03:28 PM
I've got an old and simple procedural port scanner script that I'm working on refactoring into a class. They are certainly not functioning similarly and I was hoping for some productive feedback. Here is the procedural script ->
The OOP class refactor attempt isn't going so well. It returns every port as closed and obviously doesn't return any banners either.
Hoping some of you fine folks with more experience than me can point out where I need to make corrections and do better.
Thanks
import socket from IPy import IP def scan(target, port_num=100): converted_ip = check_ip(target) print('\n' + f'[*] Scanning Target {str(target)}') for port in range(1, port_num+1): port_scan(converted_ip, port) def check_ip(ip): try: IP(ip) return ip except ValueError: return socket.gethostbyname(ip) def get_banner(s): return s.recv(1024) def port_scan(ipaddress, port): try: sock = socket.socket() sock.settimeout(0.5) sock.connect((ipaddress, port)) try: banner = get_banner(sock) # I'm not sure why the top one doesn't work # print(f'[+] Open Port {str(port)}: {str(banner.decode().strip('\n'))}') print(f'[+] Open Port {str(port)}: ' + str(banner.decode().strip('\n'))) except: print(f'[+] Open Port {str(port)}') except: print(f"Closed port at: {str(port)}") if __name__ == "__main__": targets = input('[?] Enter Target/s to Scan (separate multiple targets with a comma): ') ports_to_scan = int(input('[?] How Many Ports Do You Want To Scan: ')) if ',' in targets: for ip_add in targets.split(','): scan(ip_add.strip(' '), ports_to_scan) else: scan(targets, ports_to_scan)And here is my class refactor attempt ->
import socket from IPy import IP def check_ip(ip): try: IP(ip) return ip except ValueError: return socket.gethostbyname(ip) def get_banner(s): return s.recv(1024) class Portscanner: def __init__(self): self.sock = socket.socket() def scan(self, target, port_num=100): converted_ip = check_ip(target) print('\n' + f'[*] Scanning Target {str(target)}') for port in range(1, port_num+1): self.port_scan(converted_ip, port) def port_scan(self, ip, port): try: self.sock.settimeout(0.5) self.sock.connect((ip, port)) try: banner = get_banner(self.sock) print(f'[+] Open Port {str(port)}: ' + str(banner.decode().strip('\n'))) except: print(f'[+] Open Port {str(port)}') except: print(f"Closed port at: {str(port)}")And then from main.py ->
from portscanner import Portscanner targets = input('[?] Enter Target/s to Scan (separate multiple targets with a comma): ') ports_to_scan = int(input('[?] How Many Ports Do You Want To Scan: ')) portscanner = Portscanner() if ',' in targets: for ip_add in targets.split(','): portscanner.scan(ip_add.strip(' '), ports_to_scan) else: portscanner.scan(targets, ports_to_scan)I've been using scanme.nmap.org to test the outcomes. The first code (the procedural way) works fine. It returns the open ports and the banners if any.
The OOP class refactor attempt isn't going so well. It returns every port as closed and obviously doesn't return any banners either.
Hoping some of you fine folks with more experience than me can point out where I need to make corrections and do better.
Thanks