Project 1 - Network Discoverer

Project Objectives

In this course project, you will develop a Python script that performs network discovery.

Description

Network Discoverer is a tool that identifies all online interfaces that reside in the same LAN where at least one of local machine’s interfaces is. A network discovery tool usually reports all online interfaces by their MAC and IP addresses.

Example: Consider the network in Figure 1. If you run the network discoverer tool on router R, then the following should be reported:

  • A’s MAC address and A’s IP address
  • B’s MAC address and B’s IP address
  • C’s MAC address and C’s IP address
  • D’s MAC address and D’s IP address
  • E’s MAC address and E’s IP address

Figure 1 - Example Network
Figure 1 - Example Network

This is due to the fact that all these interfaces are in the same LAN where one of R’s interfaces is. However, if you run the tool on A, then the following should be reported:

  • B’s MAC address and B’s IP address
  • C’s MAC address and C’s IP address
  • R-1's MAC address and R-1's IP address (Router interface 1)

Similarly, if you run the tool on D, then the following should be reported:

  • E’s MAC address and E’s IP address
  • R-2's MAC address and R-2's IP address (Router interface 2)

Implementation Requirements

Your tool must follow these features:

  • The tool must be implemented in python3 and utilize scapy for custom packet generation. Aside from the netifaces library, no additional libraries are allowed to be used.
    • You can use Scapy in a Python script by importing the library: from scapy.all import *
    • You can use Netifaces in a Python script by importing the library: import netifaces
  • The first line of the .py script must be the location of the Python3 interpreter: #!/bin/env python3
  • The tool should not accept any argument as input.
  • The tool must list all available interfaces that the local machine has. You should be able to read all available interfaces of the machine.
    • For each interface, the tool must list its MAC and IP address. You should be able to read the MAC address, IP address, and network mask configured on each interface.
    • For each interface, the listed IP addresses must be reported in CIDRized form, e.g., 192.168.1.4/24. You should be able to translate the network mask to a CIDRized form.
  • You should use Scapy to identify all online machines accessible through each of your local machine’s interfaces.
    • One exception is the loobpack interface. Avoid probing through loopback interface.
  • The tool must report which interface is currently being used for probing the network.
  • It is desirable to eliminate verbosity of Scapy within your tool. You may use conf.verb = 0 for this purpose.
  • Finally, the tool must list all online interfaces in the LAN by reporting their MAC and IP addresses.
  • At the top of the script file, briefly describe how you discover online interfaces within the networks that are accessible through each of the interfaces on the system the tools is run on.

Tip: If you have Wireshark running while Scapy is transmitting & receiving, you'll have a better idea of what is happening in your program.

PyCharm setup

PyCharm is a cross-platform Python IDE available in both a commercial ($$) and community (free) version. If your coding would benefit from an IDE, you can install and configure it:

Install PyCharm (Linux):

sudo snap install pycharm-community --classic

Launch PyCharm via the application launcher GUI. Accept the user agreement, choose your privacy settings, choose your theme, and select "Start using PyCharm"

Create a New Project:

  • Choose your location, e.g. /home/USERNAME/comp177/project1
  • Choose "New environment using Virtualenv"
  • Ensure that your base interpreter is set to "python3.8" or similar
  • Uncheck "Create a main.py welcome script"

One thing is missing in PyCharm before you begin coding. PyCharm sets up Python for this project as a Virtual Environment, which is an isolated instance of Python3 with its own set of libraries distinct from the system Python. Perfect for coding complicated projects. But as a result, it's missing Scapy (which was installed to the system-wide Python).

  • Click on the Terminal tab (bottom of the window)
  • Note that the command prompt says "(venv)" so that you know your Python commands take effect inside the virtual environment
  • Install scapy here: pip3 install --pre scapy[basic]
  • Install netifaces here: pip3 install netifaces

Now that you have located the terminal in PyCharm, you can run your program from the same interface as your editor: sudo ./netdiscoverer.py

Tip: Is PyCharm not showing some Scapy functions as valid in its syntax highlighting? This solution worked for me, although it was a mild annoyance to need to specifically name each module I wanted to import: https://stackoverflow.com/questions/45691654/unresolved-reference-with-scapy

Resources

Deliverables

Submit a single Python script that implements network discovery to the Canvas CMS Project 1 assignment.

If using PyCharm, do not submit your venv environment. Before submitting, test to ensure you can run your program outside of PyCharm on the command line.

Sample Output

Running the tool may generate an output similar to the following in a terminal in Ubuntu Linux.

$ sudo ./netdiscoverer.py 
Interfaces:
* lo
* ens33
-----------
Interface details:
* lo:   MAC=00:00:00:00:00:00   IP=127.0.0.1/8
* ens33:        MAC=00:0c:29:cf:3d:82   IP=172.16.196.188/24
-----------
Scanning on interface ens33
Results:
Hosts responding: 3
Hosts not responding: 253
MAC=00:50:56:c0:00:08   IP=172.16.196.1
MAC=00:50:56:f0:b5:c7   IP=172.16.196.2
MAC=00:50:56:e4:36:19   IP=172.16.196.254