prince singh
prince singh

Reputation: 11

Network Scanner- Python

I'm working on a project to design a network scanner. target_network_input.py is a script that asks the user to input the target network range. It prints all the possible networks on the console along with the number of networks.

In active_networks.py, I'm trying to accept the the list of networks returned by target_network_input.py file and loop it through the entire list of networks to check if any of the requests, ICMP, UDP, TCP, ARP responds to it. If it does, append it inside an empty list and once the list is exhausted, print the live hosts but it just shows an empty list

# target_network_input.py

#!/usr/local/bin/python3
import ipaddress
def process_network():
    n = input('Enter the target network range (e.g., 192.168.1.0/24):  ')
    try:
        net = ipaddress.IPv4Network(n, strict = False)
        net_list = list(net.hosts())
        net_num = net.num_addresses
        print(f'Usable hosts: {net_list}\n')
        print(f'Total number of addresses (including network and broadcast): {net_num}')
        return net_list
    except ValueError:
        print('Invalid IP network range')

if __name__ == '__main__':
    #Standalone execution logic(if needed)
    print('This script is used to process network ranges')
    process_network()



#active_networks.py 

#!/usr/local/bin/python3
from scapy.all import IP, ICMP, TCP, UDP, ARP, sr1, srp, Ether
import target_network_input
import logging 

logging.basicConfig(level = logging.DEBUG) 

net_list = target_network_input.process_network()

#Main function to find active hosts
def active_hosts(net_list):
    live_hosts = []
    for ip in net_list:
        if icmp_ping(ip):
            live_hosts.append(ip)
        elif tcp_ping(ip):
            live_hosts.append(ip)
        elif udp_ping(ip):
            live_hosts.append(ip)
        elif arp_ping(ip):
            live_hosts.append(ip)
        else:
            continue
    print(live_hosts)
    return live_hosts
    
#Identify networks through ICMP requests 
def icmp_ping(ip):
    try:
        packet = IP(dst = ip) / ICMP()
        response = sr1(packet, timeout = 1, verbose = 0)
        return response is not None
    except Exception as e:
        return False 
    
#Identify networks through TCP requests 
def tcp_ping(ip, port = 80):
    try:
        packet = IP(dst = ip) / TCP(dport = port, flags = 'S')
        response = sr1(packet, timeout = 1, verbose = 0)
        return response and response.haslayer(TCP) and response[TCP].flags == 'SA'
    except Exception as e:
        return False
    
#Identify networks through UDP requests 
def udp_ping(ip, port = 53):
    try:
        packet = IP(dst = ip) / UDP(dport = port)
        response = sr1(packet, timeout = 1, verbose = 0)
        return response is not None
    except Exception as e:
        return False
    
#Identify networks through ARP requests
def arp_ping(ip):
    try: 
        arp_request = ARP(pdst = ip)
        broadcast = Ether(dst = 'ff:ff:ff:ff:ff:ff')
        packet = broadcast / arp_request
        response = srp(packet, timeout = 1, verbose = 0)[0]
        return len(response) > 0
    except Exception as e:
        return False 

active_hosts(net_list)
    

    

I was expecting that it should accept network range as input, display the possible networks in the range, and then check for each network inside the list by sending the different requests if they're active or not, and if active append them into an empty list but it just returns an empty list

Upvotes: 1

Views: 52

Answers (0)

Related Questions