Network security focuses on protecting networks, systems, and data from cyber threats. Python provides powerful libraries for scanning, monitoring, intrusion detection, and penetration testing.
Key Areas of Network Security:
- Port Scanning – Identifying open ports & vulnerabilities.
- Packet Sniffing – Capturing & analyzing network traffic.
- Firewall & IDS/IPS – Detecting and preventing attacks.
- Intrusion Detection – Monitoring suspicious activity.
- Secure Network Communication – Encrypting data transfers.
Installing Required Libraries
pip install scapy requests socket paramiko
1. Port Scanning with Python (Using socket)
Identify open ports on a target machine.
import socket
def scan_ports(target, ports):
for port in ports:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex((target, port))
if result == 0:
print(f"Port {port} is OPEN")
sock.close()
# Example usage
scan_ports("scanme.nmap.org", [21, 22, 80, 443])
Detect open ports & vulnerabilities.
2. Packet Sniffing with Scapy
Capture and analyze network packets.
from scapy.all import sniff
def packet_callback(packet):
print(packet.summary())
# Sniff packets (requires admin privileges)
sniff(prn=packet_callback, count=5)
Monitor network traffic & detect threats.
3. Intrusion Detection (Detecting Suspicious Traffic)
from scapy.all import sniff, IP, TCP
def detect_intrusion(packet):
if packet.haslayer(IP):
if packet[IP].src == "192.168.1.100": # Example: Detect traffic from a specific IP
print(f"Possible intrusion detected from {packet[IP].src}")
sniff(prn=detect_intrusion, count=10)
Identify malicious traffic from attackers.
4. Secure SSH Connection (Using Paramiko)
import paramiko
def secure_ssh_login(host, username, password):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, username=username, password=password)
print("SSH Connection Successful ✅")
except:
print("SSH Connection Failed ❌")
# Example usage
secure_ssh_login("192.168.1.1", "admin", "password123")
Protect against SSH brute-force attacks.
5. Preventing Brute Force Attacks (Blocking Failed Logins)
failed_attempts = {}
def check_login_attempt(ip):
if ip not in failed_attempts:
failed_attempts[ip] = 1
else:
failed_attempts[ip] += 1
if failed_attempts[ip] > 3: # Block IP after 3 failed attempts
print(f"Blocking IP: {ip} due to multiple failed attempts!")
# Example usage
check_login_attempt("192.168.1.50")
check_login_attempt("192.168.1.50")
check_login_attempt("192.168.1.50")
check_login_attempt("192.168.1.50") # This should trigger blocking
Enhance authentication security.
6. Secure Network Communication with SSL/TLS
import ssl
import socket
context = ssl.create_default_context()
def secure_server_connection(host, port):
with socket.create_connection((host, port)) as sock:
with context.wrap_socket(sock, server_hostname=host) as secure_sock:
print(secure_sock.version())
# Example usage
secure_server_connection("www.google.com", 443)
Ensure encrypted communication over the network.
When to Use Network Security in Python?
✔ Penetration Testing – Identify security flaws in networks.
✔ Intrusion Detection – Monitor and detect cyber attacks.
✔ Secure Data Transmission – Encrypt communications.
✔ Automated Network Monitoring – Detect suspicious activity.