From c1fdb11d23de35e5390033c66e3081e2ae8daf14 Mon Sep 17 00:00:00 2001 From: garmr-ulfr <104022054+garmr-ulfr@users.noreply.github.com> Date: Thu, 14 Mar 2024 11:26:54 -0700 Subject: [PATCH] Reserialize (#8) * Reserialize the packet's raw bytes to pick up any layer changes --------- Co-authored-by: Seth Wright --- actions/tamper_action.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/actions/tamper_action.go b/actions/tamper_action.go index 815adf8..3fa6eeb 100644 --- a/actions/tamper_action.go +++ b/actions/tamper_action.go @@ -360,6 +360,13 @@ func (a *TCPTamperAction) Apply(packet gopacket.Packet) ([]gopacket.Packet, erro } } + sb := gopacket.NewSerializeBuffer() + err := gopacket.SerializePacket(sb, gopacket.SerializeOptions{}, packet) + if err != nil { + panic(err) + } + packet = gopacket.NewPacket(sb.Bytes(), layers.LayerTypeEthernet, gopacket.Default) + return a.Action.Apply(packet) } @@ -414,14 +421,6 @@ func tamperTCP(tcp *layers.TCP, field TCPField, valueGen tamperValueGen) { opt.OptionLength = uint8(tcpOptionLengths[field]) + 2 } } - - // let gopacket handle converting the modified TCP headers into []byte for us since we changed the struct fields - // instead of the underlying []byte directly. SerializeTo doesn't write the changes to the raw packet - // so we have to copy the formatted bytes back into the packet header. - sb := gopacket.NewSerializeBuffer() - tcp.SerializeTo(sb, gopacket.SerializeOptions{}) - tcp.Contents = make([]byte, len(sb.Bytes())) - copy(tcp.Contents, sb.Bytes()) } // tcpFlagsToUint32 converts a string of TCP flags to a uint32 bitmap.