当前位置:主页 > 软件编程 > Python代码 >

Python写安全小工具之TCP全连接端口扫描器

时间:2022-06-21 10:01:56 | 栏目:Python代码 | 点击:

前言:

通过端口扫描我们可以知道目标主机都开放了哪些服务,下面通过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()

说明:

您可能感兴趣的文章:

相关文章