PaddingGenerator.py 1.5 KB

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