Introduction
In this tutorial, you'll learn how to create a simple network monitoring tool that can help detect suspicious activity on your network - similar to the techniques used by Google's threat intelligence team. This tool will monitor network traffic and flag unusual patterns that might indicate a security breach or malicious activity. While this isn't the exact same technology used by Google's analysts, it demonstrates fundamental concepts in network security monitoring that are relevant to the article about infiltrating hacking gangs.
Prerequisites
Before starting this tutorial, you'll need:
- A computer with Python installed (version 3.6 or higher)
- Basic understanding of command line operations
- Network access to monitor (you can use your local network or a virtual environment)
- Optional: Virtual machine or network simulator for safe testing
Step-by-Step Instructions
Step 1: Set Up Your Python Environment
First, we need to create a clean Python environment for our project. Open your terminal or command prompt and create a new directory for this project:
mkdir network_monitor
cd network_monitor
Next, create a virtual environment to keep our dependencies isolated:
python -m venv monitor_env
source monitor_env/bin/activate # On Windows: monitor_env\Scripts\activate
This step is important because it ensures that our project dependencies don't interfere with other Python projects on your system.
Step 2: Install Required Libraries
We'll need several Python libraries to capture and analyze network traffic. Install them using pip:
pip install scapy pandas numpy
Why we need these libraries: Scapy is our network packet capture tool, pandas helps us analyze data, and numpy provides mathematical operations for pattern recognition.
Step 3: Create the Basic Network Monitor
Now let's create our main monitoring script. Create a file called network_monitor.py:
import time
import pandas as pd
from scapy.all import sniff, IP, TCP, UDP
from collections import defaultdict
# Global dictionary to store network statistics
total_packets = 0
packet_count = defaultdict(int)
# Function to process each captured packet
def process_packet(packet):
global total_packets
total_packets += 1
# Extract basic packet information
if IP in packet:
src_ip = packet[IP].src
dst_ip = packet[IP].dst
protocol = packet[IP].proto
# Count packets by source IP
packet_count[src_ip] += 1
# Display interesting packets
if TCP in packet or UDP in packet:
print(f"Packet #{total_packets}: {src_ip} -> {dst_ip} (Protocol: {protocol})")
# Main monitoring function
def start_monitoring(interface="eth0", duration=30):
print(f"Starting network monitoring on interface {interface} for {duration} seconds...")
print("Press Ctrl+C to stop monitoring")
try:
# Capture packets for specified duration
sniff(iface=interface, prn=process_packet, timeout=duration)
# Display summary
print("\nMonitoring complete. Summary:")
print(f"Total packets captured: {total_packets}")
# Show top 5 source IPs
sorted_ips = sorted(packet_count.items(), key=lambda x: x[1], reverse=True)
print("Top 5 source IPs:")
for ip, count in sorted_ips[:5]:
print(f" {ip}: {count} packets")
except KeyboardInterrupt:
print("\nMonitoring stopped by user")
except Exception as e:
print(f"Error during monitoring: {e}")
if __name__ == "__main__":
start_monitoring(duration=10)
This script sets up a basic packet sniffer that captures network traffic and counts packets by source IP address.
Step 4: Run Your Network Monitor
Save the file and run it to test your network monitor:
python network_monitor.py
You should see output showing captured packets. If you don't see much traffic, try running it while browsing the web or using network applications.
Step 5: Enhance Detection Capabilities
Let's improve our monitor to detect suspicious patterns. Modify your script to include anomaly detection:
import time
import pandas as pd
from scapy.all import sniff, IP, TCP, UDP
from collections import defaultdict
import statistics
# Global variables
total_packets = 0
packet_count = defaultdict(int)
source_ips = defaultdict(list)
# Function to detect suspicious patterns
def detect_anomalies(source_ips):
print("\nAnalyzing for suspicious patterns...")
# Check for IPs with unusually high packet counts
for ip, packets in source_ips.items():
if len(packets) > 10: # If IP has sent more than 10 packets
avg_packets = statistics.mean(packets)
if avg_packets > 5: # And average is above threshold
print(f"ALERT: Suspicious activity detected from {ip} (avg: {avg_packets:.2f} packets)")
# Enhanced packet processing function
def process_packet(packet):
global total_packets
total_packets += 1
if IP in packet:
src_ip = packet[IP].src
dst_ip = packet[IP].dst
# Store packet timestamps for anomaly detection
current_time = time.time()
source_ips[src_ip].append(current_time)
# Count packets by source
packet_count[src_ip] += 1
# Show interesting packets
if TCP in packet or UDP in packet:
print(f"Packet #{total_packets}: {src_ip} -> {dst_ip}")
# Main monitoring function
def start_monitoring(interface="eth0", duration=30):
print(f"Starting enhanced network monitoring on interface {interface} for {duration} seconds...")
print("Press Ctrl+C to stop monitoring")
try:
sniff(iface=interface, prn=process_packet, timeout=duration)
# Analyze for anomalies
detect_anomalies(source_ips)
print("\nMonitoring complete.")
print(f"Total packets captured: {total_packets}")
# Show top 5 source IPs
sorted_ips = sorted(packet_count.items(), key=lambda x: x[1], reverse=True)
print("Top 5 source IPs:")
for ip, count in sorted_ips[:5]:
print(f" {ip}: {count} packets")
except KeyboardInterrupt:
print("\nMonitoring stopped by user")
except Exception as e:
print(f"Error during monitoring: {e}")
if __name__ == "__main__":
start_monitoring(duration=15)
This enhanced version tracks packet timestamps to identify unusual traffic patterns that might indicate malicious activity.
Step 6: Test and Interpret Results
Run your enhanced monitor and observe the output:
python network_monitor.py
When you see "ALERT" messages, these indicate potential suspicious activity. The script looks for sources that are sending unusually high numbers of packets in a short time period.
Summary
In this tutorial, you've created a basic network monitoring tool that demonstrates key concepts from Google's threat intelligence work. While this is a simplified version of what professional security analysts use, it shows how network monitoring can help detect suspicious activity.
The tool you've built captures network packets, counts traffic patterns, and identifies anomalies that might indicate security threats. This approach mirrors how security teams like Google's threat intelligence group monitor networks for signs of malicious activity.
Remember that real-world network security monitoring involves much more sophisticated techniques, including deep packet inspection, machine learning algorithms, and integration with security information and event management (SIEM) systems. However, this foundation gives you an understanding of how such systems work and how they can be built.