python 端口快速扫描

笔记,直接看代码:

python 复制代码
import socket
import threading
 
def PortScan(target_ip, start_port=40000, end_port=65535, num_threads=16):
    open_ports = []
    thread_list = []
     
    def scan_ports(ip, start, end):
        local_open_ports = []
        for port in range(start, end + 1):
            try:
                with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                    s.settimeout(0.001)
                    result = s.connect_ex((ip, port))
                    if result == 0:
                        local_open_ports.append(port)
            except KeyboardInterrupt:
                break
            except Exception as e:
                pass
        open_ports.extend(local_open_ports)
     
    ports_per_thread = (end_port - start_port + 1) // num_threads
    for i in range(num_threads):
        thread_start_port = start_port + i * ports_per_thread
        thread_end_port = thread_start_port + ports_per_thread - 1
         
        thread = threading.Thread(target=scan_ports, args=(target_ip, thread_start_port, thread_end_port))
        thread.start()
        thread_list.append(thread)
    for thread in thread_list:
        thread.join()
     
    return open_ports
 
if __name__ == "__main__":
    target_ip = "127.0.0.1"
     
    print(f"开始多线程扫描 {target_ip} 上的端口...")
    open_ports = PortScan(target_ip)
     
    if open_ports:
        print(f"开放的端口:{open_ports}")
    else:
        print("没有开放的端口。")

执行结果:

python 复制代码
开始多线程扫描 127.0.0.1 上的端口...
开放的端口:[51735, 49664, 49665, 49666, 49667, 49668, 49673, 63139]

本机测试10秒内可以获得40000以下的端口

做出来用在向日葵rce扫描的(凑个热闹罢了)

总感觉这个多线程的作用似乎不大明显

相关推荐
梦想的旅途233 分钟前
Python实现企业微信文本消息发送
开发语言·python·企业微信
%471 小时前
DAY41
pytorch·python
敲代码还房贷1 小时前
VMware17 + Ubuntu22.04 共享 完整步骤
linux·python·ubuntu
大鹏说大话1 小时前
美业微信会员系统哪个好
开发语言
King of fraud1 小时前
Linux 下 MySQL 基础操作:创建用户、数据库与权限管理实战
linux·数据库·mysql
测试19981 小时前
Selenium 无法定位元素的几种解决方案
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
半亩码田2 小时前
C#转Python第3.6篇:Python 的 @property 比 C# 的 get/set 更灵活
java·python·c#
程序员与背包客_CoderZ2 小时前
高性能分布式KV存储引擎RocksDB入门与C/C++编码实战
c语言·开发语言·数据库·c++·分布式·分布式数据库·rocksdb
欧叶冲冲冲2 小时前
Python常见数据结构的CRUD(LeetCode高频版速查)
数据结构·python·leetcode
钱栈up2 小时前
Mac 开发机一键发版不用切环境:我这样改造了团队的后端部署脚本Maven编译卡住20分钟?我靠两步定位到2处隐蔽编译错误
开发语言·python·macos