Python端口扫描 Fay·D·Flourite

python端口扫描器

之前学socket也主要是为了这个,废话不多说直接放脚本

import socket
import threading
import time
import linecache
import re

url=input("输入需要扫描的网址>>:")

try:
    ip=socket.gethostbyname(url)
    print("扫描网址的ip地址是:",ip)
    print(socket.gethostbyaddr(url))
except:
    print("输入地址有误,无法查到ip地址")

#打开端口大全读取常用端口
print('总共扫描端口数:')
print(len(open('D:/script/portnum.txt','r').readlines()))
print('-----------------------分割线-----------------------')

def getport(num):
    port=linecache.getline('D:/script/portnum.txt',num)
    return port
    

#和端口套接字进行连接,失败则表示端口未开放
def portscanner(ip,port):
    target=(ip,port)
    try:    
        scan=socket.socket()
        scan.connect(target)
        recv=scan.recv(1024)
        print(port)
        print("端口开放,以下为反馈信息:")
        print(recv.decode('utf-8'))
        scan.close()
    except:
        print(port)
        print("端口未开放")

for i in range(1,len(open('D:/script/portnum.txt','r').readlines())):
    listtostr=re.findall(r"\d+",getport(i))
    print(listtostr)
    scanningport=int(''.join(str(s) for s in listtostr))
    portscanner(ip,scanningport)
    print("-----------------------分割线-----------------------")

并不完善,没有加多线程,只能一个一个跑,速度非常慢,后续的完善计划是添加多线程,并且在扫出开放端口后能自主查询该端口的作用之类的

思路简单,创建套接字去连接端口就可,不开放就会连接失败,开放就连接成功,网上搜一个端口大全保存到本地,扫的时候去读文件里的端口一个一个扫就行了。

2020年2月27日更新

加了个多线程,速度提升了不少,这里贴上源码

import socket
import threading
import time
import linecache
import re

url=input("输入需要扫描的网址>>:")

threadnum=int(input("输入线程数量>>:"))

allnum=int(len(open('D:/script/portnum.txt','r').readlines()))

def getaddress(url):
    ip=socket.gethostbyname(url)
    print("扫描网址的ip地址是:",ip)
    print(socket.gethostbyaddr(url))
    return ip

try:
    ip=getaddress(url)
except:
    print("输入地址有误,无法查到ip地址,请重新输入")
    while True:
        try:
            url=input("输入需要扫描的网址>>:")
            ip=getaddress(url)
            break
        except:
            print("输入地址有误,无法查到ip地址,请重新输入")
            continue


#打开端口大全读取常用端口

#读取总端口数
def howmuchport(allnum):
    print('总共扫描端口数:')
    print(allnum)
    print('-----------------------分割线-----------------------')

#读取指定行的端口,用循环得到每一行
def getport(num):
    port=linecache.getline('D:/script/portnum.txt',num)
    listtostr=re.findall(r"\d+",port)
    scanningport=int(''.join(str(s) for s in listtostr))
    return scanningport
    

#和端口套接字进行连接,失败则表示端口未开放
def portscanner(ip,port):
    target=(ip,port)
    try:
        scan=socket.socket()
        scan.connect(target)
        recv=scan.recv(1024)
        print(port)
        print("端口开放,以下为反馈信息:")
        print(recv.decode('utf-8'))
        scan.close()
        print('-----------------------分割线-----------------------')
    except:
        print(port)
        print("端口未开放")
        print('-----------------------分割线-----------------------')
        
#进行每个线程的任务分配
def thread_distribute(num,threadnum,allnum):
    t=[]
    a=int(int(allnum)/int(threadnum))
    for i in range(0,a+1):
        number=int(num)+threadnum*i
        if number <= int(allnum):
            t.append(number)
    return t

#执行扫描
def startscan(ip,portlist):
    for i in portlist:
        portscanner(ip,getport(i))


def main():
    howmuchport(allnum)
    threads=[]
    for i in range(1,threadnum+1):
        portlist=thread_distribute(i,threadnum,allnum)
        t=threading.Thread(target=startscan,args=(ip,portlist))
        threads.append(t)

    for i in range(threadnum):
        threads[i].start()
        time.sleep(2)

    for i in range(threadnum):
        threads[i].join()
        time.sleep(2)


if __name__ == "__main__":
    main()

后续工具多了后或许会做个框架把工具集合起来