duanshang3230 2016-08-22 14:40 采纳率: 100%
浏览 232
已采纳

Golang:如何检查HTTP请求是否在TCP有效负载字节中

I am using the gopacket package and every time I have a TCP packet I want to check if the payload contains an HTTP request. Is there an easy way to do that instead of writing my own parser? There is also a function (see: func ReadRequest(b *bufio.Reader)) which returns a Request struct but I do not know what kind of input I should use. tcp.Payload is the byte[] array that seems to have the information I need to parse (see the following example):

// Get the TCP layer from this packet
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
    fmt.Printf("TCP ")
    // Get actual TCP data from this layer
    tcp, _ := tcpLayer.(*layers.TCP)
    srcPort = tcp.SrcPort
    dstPort = tcp.DstPort
    if tcp.SYN {
        fmt.Print("[SYN] ")
    }
    if tcp.ACK {
        fmt.Print("[ACK] ")
    }
    if tcp.FIN {
        fmt.Print("[FIN] ")
    }
    fmt.Printf("%s:%d > %s:%d ", srcIP, srcPort, dstIP, dstPort)

    fmt.Println(string(tcp.Payload))
}

After sending an HTTP request I get the following output:

PKT [001] TCP [SYN] 192.168.2.6:59095 > 192.168.3.5:80
PKT [002] TCP [SYN] [ACK] 192.168.3.5:80 > 192.168.2.6:59095
PKT [003] TCP [ACK] 192.168.2.6:59095 > 192.168.3.5:80
PKT [004] TCP [ACK] 192.168.2.6:59095 > 192.168.3.5:80 GET /certificates/test.pdf HTTP/1.1
User-Agent: Wget/1.15 (linux-gnu)
Accept: */*
Host: 192.168.3.5
Connection: Keep-Alive

Any suggestions are welcome...

  • 写回答

1条回答 默认 最新

  • dqftyn1717 2016-08-25 15:37
    关注

    With help from this question: How to parse http headers in Go the following attempt with http.ReadRequest() works...

    if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
       tcp, _ := tcpLayer.(*layers.TCP)
       if len(tcp.Payload) != 0 {
          reader := bufio.NewReader(bytes.NewReader(tcp.Payload))
          httpReq, err := http.ReadRequest(reader)
          ...
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条