Python port scanner only port 80 open

Viewed 246

When I run my python port scanner I only ever see port 80 open, regardless of the IP or website. I tested the code on testphp.vulnweb.com so I expected more than just port 80 open.

import socket
from IPy import IP

def scan(target):
    converted_ip = check_ip(target)
    print('\n' + '[-_0 Scanning Target] ' + str(target))
    for port in range(1, 100):
        scan_port(converted_ip, port)

def get_banner(s):
    return s.recv(1024)

def check_ip(ip):
    try:
        IP(ip)
        return(ip)
    except ValueError:
        return socket.gethostbyname(ip)

def scan_port(ipaddress, port):
    try:
        ## define a socket descriptor
        sock = socket.socket()
        sock.settimeout(0.5) ## in order to scan target faster, lose accuracy but gain time
        sock.connect((ipaddress, port))
        try:
            banner = get_banner(sock)
            print('[+] Open port' + str(port) + ' : ' + str(banner))
        except:
            print('[+] Open Port ' + str(port))
    except:
        pass


targets = input('[+] Enter Target/s To Scan (split multiple targets with ,): ')
if ',' in targets:
    for ip_add in targets.split(','):
        scan(ip_add.strip(' '))
else:
    scan(targets)
1 Answers

Well, there are probably more ports open, but you check only from 1 to 100. Most ports used in the wild are higher than that.

for port in range(1, 100):
    scan_port(converted_ip, port)

From the top of my head, I only know 4 commonly used ports in this range. SSH (22), SMTP (25), DNS (53), and HTTP (80).

HTTPS for example is most of the time on 443. MySQL has by default 3306. Redis has 6379.

So you should at least check up to 10000, probably even higher. The highest port number allowed is 65535.

Related