Untitled
unknown
python
2 years ago
2.5 kB
5
Indexable
from scapy.all import * import random class PcapModifier: def __init__(self, input_file, output_file): self.input_file = input_file self.output_file = output_file self.seen_addresses = {} def modify_pcap(self): packets = PcapReader(self.input_file) modified_packets = [] for packet in packets: if TCP in packet: src_ip = packet[IP].src src_mac = packet.src if src_ip not in self.seen_addresses: self.seen_addresses[src_ip] = {} if src_mac not in self.seen_addresses[src_ip]: self.seen_addresses[src_ip][src_mac] = {} dest_ip = self._generate_random_ip(src_ip) dest_mac = RandMAC() packet[IP].dst = dest_ip packet.dst = dest_mac # Recalculate TCP checksum del packet[TCP].chksum packet = self._fix_tcp_segmentation(packet) modified_packets.append(packet) wrpcap(self.output_file, modified_packets) def _generate_random_ip(self, src_ip): src_network = ipaddress.ip_network(src_ip + '/24') dest_ip = str(random.choice(list(src_network.hosts()))) return dest_ip def _fix_tcp_segmentation(self, packet): if Raw in packet: raw_data = packet[Raw].load del packet[Raw] max_segment_size = 1460 # Maximum Segment Size (MSS) for i in range(0, len(raw_data), max_segment_size): segment = raw_data[i:i + max_segment_size] segment_packet = packet.copy() segment_packet[Raw] = segment segment_packet[TCP].seq += i if i + max_segment_size >= len(raw_data): segment_packet[TCP].flags = 'FA' # Final segment segment_packet[TCP].chksum = None packet = packet.__class__(bytes(packet)) packet[TCP].chksum = packet[TCP].chksum # Recalculate checksum yield segment_packet def print_summary(self): print("Source IP and MAC addresses:") for src_ip, mac_dict in self.seen_addresses.items(): print("Source IP:", src_ip) for src_mac in mac_dict.keys(): print("Source MAC:", src_mac) # Usage example: modifier = PcapModifier('your_file.pcap', 'modified_file.pcap') modifier.modify_pcap() modifier.print_summary()
Editor is loading...