PaddingGenerator.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from scapy.packet import Raw
  2. import sys
  3. import PayloadGenerator
  4. def add_padding(packet, bytes_padding = 0):
  5. '''
  6. Adds padding to a packet with the given amount of bytes, but a maximum of 100 bytes.
  7. :param packet: the packet that will be extended with the additional payload
  8. :param bytes_padding: the amount of bytes that will be appended to the packet
  9. :return: the initial packet, extended with the wanted amount of bytes of padding
  10. '''
  11. if(bytes_padding > 100):
  12. bytes_padding = 100
  13. payload = PayloadGenerator.generate_Payload(bytes_padding)
  14. packet = packet / Raw(load = payload)
  15. return packet
  16. def equal_length(list_of_packets, length = 0):
  17. '''
  18. Equals the length of a given set of packets on the given length. If the given length is smaller than the largest
  19. packet, all the other packets are extended to the largest packet's length.
  20. :param list_of_packets: The given set of packet.
  21. :param length: The length each packet should have.
  22. :return: The set of extended packets.
  23. '''
  24. largest_packet = length
  25. for packet in list_of_packets:
  26. packet_length = sys.getsizeof(packet)
  27. if(packet_length > length):
  28. largest_packet = packet_length
  29. for packet in list_of_packets:
  30. bytes_padding = largest_packet - sys.getsizeof(packet)
  31. if(bytes_padding > 0):
  32. add_padding(packet, bytes_padding)
  33. return list_of_packets