PaddingGenerator.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from scapy.packet import Raw
  2. import ID2TLib.PayloadGenerator as PayloadGenerator
  3. import numpy.random as random
  4. def add_padding(packet, bytes_padding = 0, user_padding=True, rnd = False):
  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. :param user_padding: true, if the function add_padding is called from another class and the user
  10. sets the padding manually
  11. :param rnd: adds a random padding betwing 0 and bytes_adding, if true
  12. :return: the initial packet, extended with the wanted amount of bytes of padding
  13. '''
  14. if(user_padding and bytes_padding > 100):
  15. bytes_padding = 100
  16. if (rnd is True):
  17. r = int(round(bytes_padding / 4)) #sets bytes_padding to any number between 0 and bytes_padding
  18. bytes_padding = random.random_integers(0, r) * 4 #, that's dividable by 4
  19. payload = PayloadGenerator.generate_payload(bytes_padding)
  20. packet[Raw].load += Raw(load=payload).load
  21. return packet
  22. def equal_length(list_of_packets, length = 0, padding = 0):
  23. '''
  24. Equals the length of a given set of packets on the given length. If the given length is smaller than the largest
  25. packet, all the other packets are extended to the largest packet's length.
  26. :param list_of_packets: The given set of packet.
  27. :param length: The length each packet should have.
  28. :return: The set of extended packets.
  29. '''
  30. largest_packet = length
  31. for packet in list_of_packets:
  32. packet_length = len(packet)
  33. if(packet_length > largest_packet):
  34. largest_packet = packet_length
  35. for packet in list_of_packets:
  36. bytes_padding = largest_packet - len(packet)
  37. if(bytes_padding > 0):
  38. add_padding(packet, bytes_padding, False, False)
  39. add_padding(packet, padding, False, True)
  40. return list_of_packets