0

I'm new to python and socket programming, and trying to scan a port of a WAN address.

I know from using nmap that he has port 80 and 443 open.

Why can't I read from it to see if it's open??? Also, when I scan my gateway with my program it shows that 21 and 23 are open which is what NMAP shows me. BUT nmap also tells me I have port 80 and 443 as well, yet my program doesn't pick those up.

Thanks

def return_banner(ip, port):
    try:
        socket.setdefaulttimeout(2)
        s = socket.socket()
        s.connect((ip, port)) #establish connection
        banner = s.recv(1024) #receive the first 1024 bytes from socket
        return banner
    except:
        return #will return none

def main():
lan_or_wan()
dictionary_of_open_services = {}
list_of_ports = [20, 21, 22, 23, 25, 80, 8080, 53, 67, 68, 443, 993, 143, 110]
list_of_ports.sort()
if (local == False):
    print "local = false"
    for port in list_of_ports:
        print "scanning: "+WAN_IP+":"+str(port)
        software_banner = return_banner(WAN_IP, port)
        if (software_banner != None):
            dictionary_of_open_services[WAN_IP+":"+str(port)] = software_banner
magna_nz
  • 1,243
  • 5
  • 23
  • 42
  • Given your use case, you *shouldn't* handle a timeout on connection as the same as a timeout on read: the former might indicate a filtered port. The later denotes an open port whose associated service does not send a banner. Two completely different things. See http://stackoverflow.com/questions/2719017/how-to-set-timeout-on-pythons-socket-recv-method for some solutions – Sylvain Leroux Apr 13 '15 at 11:22
  • So I should remove that socket.setdefaultimeout(2) then? – magna_nz Apr 13 '15 at 11:25

1 Answers1

0

Since no request is made, the connection is connected but idle. socket.setdefaulttimeout(2) gives a timeout error, hence None is returned. To grab the banner, certain request must be made.

To just check the port open, try connecting the port. If connection is successful return True else return False.

    try:
        s.connect((ip, port))
        s.close()
        return True
    except:
        return False
sumit-sampang-rai
  • 701
  • 1
  • 7
  • 16
  • _"To grab the banner, certain request must be made."_ Not necessary. Some services might spontaneously send a banner on connection. But I agree, _usually_, banner grabbing is done by sending some garbage on the port to get gather informations from the server "error message". – Sylvain Leroux Apr 13 '15 at 11:35
  • @SylvainLeroux that's why his scripts shows 21 and 23 are open but 80 and 443 are closed. – sumit-sampang-rai Apr 13 '15 at 11:36
  • Right: to check that, use telnet: `telnet localhot 21` should show you a banner (sometimes simply the login prompt) without further action. OTOH, `telnet localhost 80` should display a "blank" screen, until you actually send something to the server. – Sylvain Leroux Apr 13 '15 at 11:40
  • @srekcahrai is this what you mean about connecting and checking connection is successful? `s = socket.socket() #create port` `s.bind(("", 12345)) #bind to my own port` `s.listen(5) #listen to port` `result = s.connect_ex((ip, port)) #establish connection` `#then check the result?` – magna_nz Apr 13 '15 at 11:51