Portscanner not working properly, probably semantic error

自闭症网瘾萝莉.ら 提交于 2019-12-11 14:52:27

问题


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.

来源:https://stackoverflow.com/questions/46121844/portscanner-not-working-properly-probably-semantic-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!