亲宝软件园·资讯

展开

Python TCP端口扫描器

​ okokabcd   ​ 人气:0

前言:

通过端口扫描我们可以知道目标主机都开放了哪些服务,下面通过TCP connect来实现一个TCP全连接端口扫描器。

一个简单的端口扫描器

#!/usr/bin/python3
# -*- coding: utf-8 -*-
from socket import *
def portScanner(host,port):
    try:
        s = socket(AF_INET,SOCK_STREAM)
        s.connect((host,port))
        print('[+] %d open' % port)
        s.close()
    except:
        print('[-] %d close' % port)
def main():
    setdefaulttimeout(1)
    for p in range(1,1024):
        portScanner('192.168.0.100',p)
if __name__ == '__main__':
    main()

说明:

多线程版本

#!/usr/bin/python3
# -*- coding: utf-8 -*-
from socket import *
import threading
lock = threading.Lock()
openNum = 0
threads = []
def portScanner(host,port):
    global openNum
    try:
        s = socket(AF_INET,SOCK_STREAM)
        s.connect((host,port))
        lock.acquire()
        openNum+=1
        print('[+] %d open' % port)
        lock.release()
        s.close()
    except:
        pass
def main():
    setdefaulttimeout(1)
    for p in range(1,1024):
        t = threading.Thread(target=portScanner,args=('192.168.0.100',p))
        threads.append(t)
        t.start()     

    for t in threads:
        t.join()
    print('[*] The scan is complete!')
    print('[*] A total of %d open port ' % (openNum))
if __name__ == '__main__':
    main()

说明:

加载全部内容

相关教程
猜你喜欢
用户评论