Skip to content

Commit

Permalink
Reserialize (#8)
Browse files Browse the repository at this point in the history
* Reserialize the packet's raw bytes to pick up any layer changes

---------

Co-authored-by: Seth Wright <seth@getlantern.org>
  • Loading branch information
garmr-ulfr and Crosse committed Mar 14, 2024
1 parent 4935a8c commit c1fdb11
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions actions/tamper_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c1fdb11

Please sign in to comment.