如何从 TCP 数据包中获取有效负载?

How to get the payload from TCP packet?

我想使用 pcapplusplus 库或其他类似库从 tcp 数据包中提取 tcp 有效负载数据。我搜索文档但无法找到如何获取 tcp 有效负载,但我几乎可以通过使用各种 api 获取 tcp header 中的任何内容。我怎样才能得到有效载荷?

Public Member Functions
    TcpLayer (uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
 
    TcpLayer ()
 
    TcpLayer (uint16_t portSrc, uint16_t portDst)
 
    TcpLayer (const TcpLayer &other)
 
TcpLayer &  operator= (const TcpLayer &other)
 
tcphdr *    getTcpHeader () const
 
uint16_t    getSrcPort () const
 
uint16_t    getDstPort () const
 
TcpOption   getTcpOption (TcpOptionType option) const
 
TcpOption   getFirstTcpOption () const
 
TcpOption   getNextTcpOption (TcpOption &tcpOption) const
 
size_t  getTcpOptionCount () const
 
TcpOption   addTcpOption (const TcpOptionBuilder &optionBuilder)
 
TcpOption   addTcpOptionAfter (const TcpOptionBuilder &optionBuilder, TcpOptionType prevOptionType=TCPOPT_Unknown)
 
bool    removeTcpOption (TcpOptionType optionType)
 
bool    removeAllTcpOptions ()
 
uint16_t    calculateChecksum (bool writeResultToPacket)
 
void    parseNextLayer ()
 
size_t  getHeaderLen () const
 
void    computeCalculateFields ()
 
std::string     toString () const
 
OsiModelLayer   getOsiModelLayer () const

这些 API 用于获取 tcp header 数据。

经过大量搜索,我得到了答案。它是 TcpStreamData::getData() 方法。您可以查看 this link 以获得 API 文档。

为了获得(任何)层有效载荷,您应该使用 getLayerPayload() API,例如:

pcpp::TcpLayer* tcpLayer = myPacket.getLayerOfType<pcpp::TcpLayer>();
uint8_t* payload = tcpLayer->getLayerPayload();
size_t payloadSize = tcpLayer->getLayerPayloadSize();