对一个个ipv6地址进行扫描时间需要太久,为了解决这个问题,我们将掩码比较小的ipv6网段进行细分成/72网段地址,并且可以对这些网段同时扫描,极大的减少扫描到存活ipv6地址的时间
具体脚本如下
import ipaddress
from scapy.all import *
import concurrent.futures
def ping_ipv6_address(address):
"""
Ping an IPv6 address and return True if it is alive, otherwise False.
"""
icmpv6_packet = IPv6(dst=address)/ICMPv6EchoRequest()
response = sr1(icmpv6_packet, timeout=2, verbose=0)
if response and ICMPv6EchoReply in response:
print(f"{address} is alive")
return True
return False
def scan_ipv6_network(subnet):
"""
Scan an IPv6 subnet and return a list of addresses that are alive.
"""
alive_addresses = []
for ip in subnet:
if ping_ipv6_address(str(ip)):
alive_addresses.append(str(ip))
return alive_addresses
def split_ipv6_network(network, target_prefix_length):
"""
Split an IPv6 network into smaller subnets with target_prefix_length.
"""
net = ipaddress.IPv6Network(network)
subnets = list(net.subnets(new_prefix=target_prefix_length))
return subnets
def get_ipv6_network():
"""
Prompt the user to enter an IPv6 network and validate the input.
"""
while True:
try:
network = input("Enter the IPv6 network to scan (e.g., 2449:8C38:80:220::/64): ")
ipv6_network = ipaddress.IPv6Network(network) # Validate input
return ipv6_network
except ValueError as ve:
print("Invalid input, please enter a valid IPv6 network.")
def main():
while True:
ipv6_network = get_ipv6_network()
# Determine the target prefix length for splitting
if ipv6_network.prefixlen <= 64:
target_prefix_length = 72
else:
target_prefix_length = 64
# Split the network into subnets with the target prefix length
subnets = split_ipv6_network(ipv6_network, target_prefix_length)
print(f"Splitting {ipv6_network} into /{target_prefix_length} subnets:")
for subnet in subnets:
print(f"Subnet: {subnet}")
# Perform parallel scanning of each subnet
with concurrent.futures.ThreadPoolExecutor() as executor:
future_to_subnet = {executor.submit(scan_ipv6_network, subnet): subnet for subnet in subnets}
for future in concurrent.futures.as_completed(future_to_subnet):
subnet = future_to_subnet[future]
try:
alive_addresses = future.result()
if alive_addresses:
print(f"Alive addresses in {subnet}:")
for address in alive_addresses:
print(address)
except Exception as e:
print(f"Error scanning {subnet}: {e}")
# Ask if the user wants to scan another network
response = input("Do you want to scan another IPv6 network? (yes/no): ")
if response.lower() != "yes":
break
if __name__ == "__main__":
main()