1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| import socket import subprocess import platform import ipaddress import threading from scapy.all import ARP, Ether, srp, IP, UDP, ICMP, sr1
COMMON_TCP_PORTS = [22, 53, 80, 443, 3389, 21, 25, 110, 143, 8080]
def icmp_ping(ip): command = ["ping", "-c", "1", ip] if platform.system() != "Windows" else ["ping", "-n", "1", ip] response = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return response.returncode == 0
def tcp_syn_scan(ip, ports=COMMON_TCP_PORTS): for port in ports: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(1) result = sock.connect_ex((ip, port)) sock.close() if result == 0: return True except socket.error: continue return False
def tcp_ack_scan(ip, ports=COMMON_TCP_PORTS): for port in ports: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(1) sock.connect_ex((ip, port)) sock.close() return True except socket.error: continue return False
def arp_ping(ip): arp_request = ARP(pdst=ip) ether_frame = Ether(dst="ff:ff:ff:ff:ff:ff") / arp_request result = srp(ether_frame, timeout=1, verbose=False)[0] return len(result) > 0
def udp_ping(ip, port=53): try: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.settimeout(1) sock.sendto(b"UDP test", (ip, port)) response = sr1(IP(dst=ip)/ICMP(), timeout=1, verbose=False) return response is None except socket.timeout: return False except socket.error: return False
def scan_host(ip, methods=["icmp", "arp", "udp", "syn", "ack"]): if "icmp" in methods and icmp_ping(ip): return True if "arp" in methods and ipaddress.ip_address(ip).is_private: if arp_ping(ip): return True if "udp" in methods and udp_ping(ip): return True if "syn" in methods and tcp_syn_scan(ip): return True if "ack" in methods and tcp_ack_scan(ip): return True return False
def scan_subnet(network, methods=["icmp", "syn", "ack", "arp", "udp"]): net = ipaddress.IPv4Network(network, strict=False) for ip in net.hosts(): threading.Thread(target=scan_and_print, args=(str(ip), methods)).start()
def scan_single_ip(ip, methods=["icmp", "syn", "ack", "arp", "udp"]): if scan_host(ip, methods): print(f"{ip} is Alive") else: print(f"{ip} is Dead")
def print_scan_results(ip, is_alive): if is_alive: print(f"{ip} is Alive")
def scan_and_print(ip, methods): is_alive = scan_host(ip, methods) print_scan_results(ip, is_alive)
def main(): target = input("Enter a subnet (e.g., 192.168.1.0/24) or IP (e.g., 192.168.1.1): ") if '/' not in target: scan_single_ip(target) else: scan_subnet(target)
if __name__ == "__main__": main()
|