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)
          ...
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)