Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import os
- import sys
- from scapy.all import sniff, IP, UDP, TCP, Raw, conf, get_if_list
- import binascii
- def is_admin():
- try:
- return os.getuid() == 0 # Unix-based systems
- except AttributeError:
- import ctypes
- return ctypes.windll.shell32.IsUserAnAdmin() != 0 # Windows
- class EnigmaIP2Monitor:
- def __init__(self):
- # Hardcoded parameters
- self.interface = None # Use default interface
- self.host = "192.168.1.230" # IP address of the Enigma IP2 receiver
- self.port = 9999 # Receive port from settings
- self.display_raw = True # Always display raw data
- self.running = False
- self.packet_count = 0
- def packet_callback(self, packet):
- """Process each captured packet"""
- self.packet_count += 1
- print(f"Packet #{self.packet_count} received")
- # Check if it's an IP packet
- if IP in packet:
- src_ip = packet[IP].src
- dst_ip = packet[IP].dst
- # Filter by Enigma IP2 receiver address
- if not (src_ip == self.host or dst_ip == self.host):
- print(f"Skipping packet with IPs: {src_ip} -> {dst_ip} (not matching {self.host})")
- return
- # Extract transport layer details
- transport_protocol = None
- src_port = None
- dst_port = None
- payload = None
- if TCP in packet:
- transport_protocol = "TCP"
- src_port = packet[TCP].sport
- dst_port = packet[TCP].dport
- if Raw in packet:
- payload = packet[Raw].load
- elif UDP in packet:
- transport_protocol = "UDP"
- src_port = packet[UDP].sport
- dst_port = packet[UDP].dport
- if Raw in packet:
- payload = packet[Raw].load
- # Filter by port
- if not (src_port == self.port or dst_port == self.port):
- print(f"Skipping packet with ports: {src_port} -> {dst_port} (not matching {self.port})")
- return
- # Display packet information
- print(f"\n{'='*60}")
- print(f"Time: {time.strftime('%Y-%m-%d %H:%M:%S')}")
- print(f"Protocol: {transport_protocol}")
- print(f"Source: {src_ip}:{src_port}")
- print(f"Destination: {dst_ip}:{dst_port}")
- if payload:
- print(f"Payload Length: {len(payload)} bytes")
- print("Raw Payload (hex):")
- print(binascii.hexlify(payload).decode())
- print("Raw Payload (ascii):")
- try:
- print(payload.decode('ascii', errors='replace'))
- except:
- print("Unable to decode as ASCII")
- print(f"{'='*60}")
- def start_capture(self, less_restrictive=False):
- """Start capturing packets"""
- try:
- self.running = True
- print(f"Starting packet capture for Enigma IP2")
- print(f"Monitoring IP: {self.host} on port: {self.port}")
- if self.interface:
- print(f"Using interface: {self.interface}")
- else:
- print("Using default interface")
- print("Press Ctrl+C to stop capturing")
- # Define the BPF filter
- bpf_filter = None
- if not less_restrictive:
- bpf_filter = f"host {self.host} and port {self.port}"
- print(f"Using filter: {bpf_filter}")
- else:
- bpf_filter = f"host {self.host}"
- print(f"Using less restrictive filter: {bpf_filter}")
- # Start sniffing
- sniff(
- iface=self.interface,
- filter=bpf_filter,
- prn=self.packet_callback,
- store=0
- )
- except KeyboardInterrupt:
- print("\nPacket capture stopped by user")
- except Exception as e:
- print(f"Error during packet capture: {e}")
- finally:
- self.running = False
- def list_interfaces():
- """Display all available network interfaces"""
- print("\nAvailable Network Interfaces:")
- for i, iface in enumerate(get_if_list()):
- print(f"{i+1}. {iface}")
- print()
- if __name__ == "__main__":
- # Check for admin privileges
- if not is_admin():
- print("WARNING: This script may need administrator privileges to capture packets.")
- print("Try running as administrator/with sudo if no packets are captured.")
- # Display available interfaces
- list_interfaces()
- # Create monitor with hardcoded settings
- monitor = EnigmaIP2Monitor()
- # Allow user to select interface
- use_specific_interface = input("Do you want to specify a network interface? (y/n): ").lower() == 'y'
- if use_specific_interface:
- interface_idx = int(input("Enter the number of the interface to use: ")) - 1
- monitor.interface = get_if_list()[interface_idx]
- # First try with normal filter
- print("\nStarting capture with standard filter...")
- monitor.start_capture(less_restrictive=False)
- # If no packets were captured, try with less restrictive filter
- if monitor.packet_count == 0:
- print("\nNo packets captured. Trying with less restrictive filter (host only)...")
- monitor.start_capture(less_restrictive=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement