Python: Port Scanning

Right now I’m brushing up on my Python, as well as learning about firewalls. I’m currenly working with an opensource firewall utility thats available on many routers, called pfSense. So far I’ve been using a book called, pfSense: The Definitive Guide

In the course of learning some of the basics of firewalls, I thought I’d investigate Python’s native abilities on the topic, available by importing the Socket library.

Scanner.py - Code for returning active ports
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import socket
def retBanner(ip, port):
    try:
        socket.setdefaulttimeout(2)
        s = socket.socket()
        s.connect((ip, port))
        banner = s.recv(1024)
        return banner
    except:
        return
def main():
    iplist = [66]  # IP address of my raspberry pi
    for x in iplist:  # Could be range, instead of list
        ip = '192.168.11.' + str(x)
        for port in range(1,35000):  # Defines Port Range
            banner = retBanner(ip, port)
            if banner:  # [+] Errorhandling; printing active ports
                print '[+] ' + ip + ':' + str(port) + ' - ' + banner
if __name__ == '__main__':
    main()
Results
1
2
3
## Results ##
$[+] 192.168.11.66:SSH-2.0-OpenSSH_6.1p1 Debian-4 # SSH Server
$[+] 192.168.11.66:RFB 003.007 # VNC server