This project is an implementation of a Network-based Intrusion Detection System in Python. The user-interface is a menu-driven function that performs the following actions based on the user input:
- Start / Stop IDS: Toggles the IDS thread to start or stop running based on the current state. When the IDS thread is started for the first time, the
ids.logfile is created to store the logging details. - View Life Traffic: Creates a new thread to show the user live captured packets from the default network interface.
- View Intrusion Logs: Summarizes the contents of the
ids.logfile in a tabular manner to show various attack details in the mentioned format. - Display Blocked IPs: Shows a list of IPs that have been blocked using IPtables
- Clear Blocked IP list: Clear the blacklisted IPs by unblocking them from IPtables firewall
- Unblock an IP: Unblock a single IP address by deleting the IPtables rule
- Exit: Stop running threads gracefully and exit the program
-
The following steps assume that the user has superuser (
sudo) privileges on the machine and that a Python3 virtual environment has already been created where the script is intended to run. If not, please create and activate a new virtual environment using the following commands:python3 -m venv .venv source .venv/bin/activate -
Install the required dependencies using the
requirements.txtfile by running the following command:pip3 install -r requirements.txt -
Start the main thread using the following command:
sudo $(which python3) main.py -
To exit, please enter 7 on the menu screen.
The assignment consists of the following files, each of which serves a single responsibility in the Intrusion Detection System. The following source code files have been submitted:
-
main.py: This is the entry point of the program and is responsible for handling the user input and triggering corresponding actions based on the user's choice.
It defines how the user input will be handled and how the logs will be aggregated and shown to the user from the
ids.logfile. It also defines the signal handlers forSIGINTandSIGTSTPthat have special uses in the implementation. The signal handler forSIGINTensures that the user exits the program using the menu option so that all resources can be cleaned up and the termination is graceful. -
ids_service.py: This file contains the core implementation of the Intrusion-Detection service and is responsible for the actions of the IDS thread, when it is running.
It monitors network packets to detect two types of attacks—TCP port scanning and OS fingerprinting—based on specific thresholds and patterns like port range and TCP flags. When suspicious behavior is detected, it logs the event asynchronously and blocks the offending IP using
iptables. The script maintains two separatepandas DataFramesto track recent packet data for both attack types and uses time-based windows to clean up old entries. -
iptables_handler.py: This file contains the functions that are used to interact with the system-wise
IPtablesfirewall service to block/unblock IP addresses, as desired by the user and the IDS thread. Each operation runs in a separate thread to ensure minimal blocking of the main IDS thread. -
utils.py: This is the utilities file and contains methods and constants that are used throughout the project. The constants include the choices that the user can make along with mutex locks that need to be aquired before printing to
STDOUTor performing an IP address blocking/unblocking operation. -
view_traffic.py: Finally, this file contains the function that will be called when the user chooses to view the live traffic. This is a blocking thread and the user will not be able to perform any other operation while this thread is running.
-
The IDS runs as a Python Thread, allowing it to operate independently and be managed cleanly (start/stop) within a larger application. The
assignment3.pyconstitutes this larger application and manages the user interface and input/output handling. -
It maintains rolling DataFrames for each attack type with timestamp-based filtering to keep only recent entries, enabling fast analysis.
-
The IDS supports safe shutdown via a
threading.Event, and usesthreading.Lockfor shared state like the blocked IP set. -
The
utils.pyfile implementsthread_safe_printand usesthreading.Lockto ensure thread-safe access and operations on shared resources like the console and theBLOCKED_IP_SET. It also declares global variables for IDS control such asIDS_THREAD,VIEW_TRAFFIC_THREAD, andFIREWALL_UPDATE_THREAD, which assist in managing various threads and configurations of the IDS system. -
Each intrusion is logged in a detailed, structured manner (including port/flag info, timestamps, etc.) to aid future analysis.
-
We have used the
iptclibrary to dynamically insertdrop rulesintoiptableswhen malicious activity is detected, effectively blocking attackers in real-time.