Portscanner无法正常工作,可能是语义错误

时间:2017-09-08 17:35:09

标签: python sockets tcp port-scanning

我问Any servers with open tcp ports known? 关于在网站上打开tcp端口,并被告知stackoverflow本身已打开tcp端口80。我执行了我的程序,它有点行为不端。

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()

这是我的全部代码。它确实给了我真实的&#34; stackoverflow的名称,但表示将关闭tcp端口。我究竟做错了什么?我已尝试过应该打开某个端口的其他网站,但它总是声称该端口将被关闭。

这是命令shell中的输出:

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

请告诉我为什么它不起作用。

0 个答案:

没有答案