Skip to content

Commit

Permalink
Fix crash caused due to unknown or missing network or transport layers
Browse files Browse the repository at this point in the history
Parser would crash when confronted with no network or transport layer
information. This issue is fixed by checking if these layers are `nil`
before attempting to reference them.
  • Loading branch information
chazlever committed Jul 17, 2020
1 parent 658e5ba commit c65dd52
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,36 @@ PACKETLOOP:
continue
}

// Parse network layer information
networkLayer := packet.NetworkLayer()
if networkLayer == nil {
log.Error("Unknown/missing network layer for packet")
stats.PacketErrors += 1
continue
}
switch networkLayer.LayerType() {
case layers.LayerTypeIPv4:
ip4 = networkLayer.(*layers.IPv4)
schema.SourceAddress = ip4.SrcIP.String()
schema.DestinationAddress = ip4.DstIP.String()
schema.Ipv4 = true
stats.PacketIPv4 += 1
case layers.LayerTypeIPv6:
ip6 = networkLayer.(*layers.IPv6)
schema.SourceAddress = ip6.SrcIP.String()
schema.DestinationAddress = ip6.DstIP.String()
schema.Ipv4 = false
stats.PacketIPv6 += 1
}

// Parse DNS and transport layer information
msg = nil
transportLayer := packet.TransportLayer()
if transportLayer == nil {
log.Error("Unknown/missing transport layer for packet")
stats.PacketErrors += 1
continue
}
switch transportLayer.LayerType() {
case layers.LayerTypeTCP:
tcp = transportLayer.(*layers.TCP)
Expand Down Expand Up @@ -161,23 +188,6 @@ PACKETLOOP:
continue PACKETLOOP
}

// Parse network layer information
networkLayer := packet.NetworkLayer()
switch networkLayer.LayerType() {
case layers.LayerTypeIPv4:
ip4 = networkLayer.(*layers.IPv4)
schema.SourceAddress = ip4.SrcIP.String()
schema.DestinationAddress = ip4.DstIP.String()
schema.Ipv4 = true
stats.PacketIPv4 += 1
case layers.LayerTypeIPv6:
ip6 = networkLayer.(*layers.IPv6)
schema.SourceAddress = ip6.SrcIP.String()
schema.DestinationAddress = ip6.DstIP.String()
schema.Ipv4 = false
stats.PacketIPv6 += 1
}

// Ignore questions unless flag set
if !msg.Response && !DoParseQuestions && !DoParseQuestionsEcs {
continue PACKETLOOP
Expand Down

0 comments on commit c65dd52

Please sign in to comment.