Portscanner not working properly, probably semanti

2019-08-19 13:11发布

问题:

I asked in Any servers with open tcp ports known? about open tcp ports on websites and was told that stackoverflow itself has tcp port 80 open. I executed my program and it kind of misbehaved.

import optparse
from socket import *
from threading import *

screenLock = Semaphore(value=1)

def connScan(tgtHost, tgtPort):
    try:
        connSkt = Socket(AF_INET, SOCK_STREAM)
        connSkt.connect((tgtHost, tgtPort))
        connSkt.send('ExploitMessage\r\n')
        results = connSkt.recv(100)
        screenLock.acquire()
        print('[+]%d/tcpopen'%tgtPort)
        print('[+] ' + str(results))
        connSkt.close()
    except:
        screenLock.acquire()
        print('[-]%d/tcp closed'%tgtPort)
    finally:
        screenLock.release()


def portScan(tgtHost, tgtPorts):
    try:
        tgtIP = gethostbyname(tgtHost)
    except:
        print("[-] Cannot resolve '%s': Unknown host"%tgtHost)
        return

    try:
        tgtName = gethostbyaddr(tgtIP)
        print("\n[+] Scan results for: " + tgtName[0] + ' , ' + tgtIP)
    except:
        print("\n[+] Scan results for " + tgtIP)

    setdefaulttimeout(1)

    for tgtPort in tgtPorts:
        t = Thread(target=connScan, args=(tgtHost, int(tgtPort)))
        t.start()

def main():
    parser = optparse.OptionParser('usage%prog ' + ' -H <target host> -p <target port>')
    parser.add_option('-H', dest='tgtHost', type='string', help='specify target Host')
    parser.add_option('-p', dest='tgtPort', type='string', help='specify target port[s] separated by comma')
    (options, args) = parser.parse_args()
    tgtHost = options.tgtHost
    tgtPorts = str(options.tgtPort).split(',')
    if ((tgtHost == None) | (tgtPorts[0]==None)):
        print(parser.usage)
        exit(0)
    portScan(tgtHost, tgtPorts)

if __name__ == "__main__":
    main()

Ths is my entire code. It does properly give me the "real" name of stackoverflow but says the tcp port would be closed. What am I doing wrong? I have tried it with other sites that should have a certain port open, but it always claims the port would be closed.

This is the output in the command shell:

[+] Scan results for: li547-15.members.linode.com , 198.74.50.15
[-]80/tcp closed

Please tell m why it's not working.