1

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

3
  • There's 2 key problems I found with these scripts, start by looking into if your try except blocks are catching any errors Commented Jan 2, 2025 at 10:24
  • Thank you for reviewing the code. I was able to fix the problem with the net_list as it was also returning a string IPv4Address along with every IP4 addresses, which was causing problem in traversing through the list of IP addresses Commented Jan 2, 2025 at 16:02
  • That's close to the problem I identified. What I found is that all the sr1 calls were failing, and the catch statements weren't printing the error, which was operation not permitted. The problem after that was the scapy library throwing errors, because the IP class expects a string for the destination, but the program was passing an IPv4Address object. Casting the IPv4Address objects to strings made the whole thing work for me Commented Jan 2, 2025 at 23:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.