2019-11-28 16:16:22 +00:00
|
|
|
package dhcpv4
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/google/gopacket"
|
|
|
|
"github.com/google/gopacket/layers"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Called on every DHCP (v4) packet
|
|
|
|
func HandleDHCPv4Packet(packet gopacket.Packet) error {
|
|
|
|
var dhcppacket layers.DHCPv4
|
|
|
|
var ethernetpacket layers.Ethernet
|
|
|
|
|
2019-11-28 17:48:54 +00:00
|
|
|
// For some reason I can't find an explanation for,
|
|
|
|
// packet.Layer(layers.LayerTypeDHCPv4).LayerContents(), which effectively is
|
|
|
|
// packet.Layers()[3].layerContents(), is empty, but
|
|
|
|
// packet.Layers()[2].layerPayload() contains the correct DHCP packet.
|
|
|
|
// ... although both calls should return the same bytes.
|
|
|
|
// TODO: Open an Issue on github.com/google/gopacket
|
|
|
|
|
2019-11-28 16:16:22 +00:00
|
|
|
// Decode raw packet into DHCPv4
|
2019-11-28 17:48:54 +00:00
|
|
|
decodeDHCPErr := dhcppacket.DecodeFromBytes(packet.Layers()[2].LayerPayload(), gopacket.NilDecodeFeedback)
|
2019-11-28 16:16:22 +00:00
|
|
|
if decodeDHCPErr != nil {
|
|
|
|
// Encountered an error during decoding, most likely a broken packet
|
|
|
|
return decodeDHCPErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// And decode raw packet into Ethernet
|
|
|
|
decodeEthernetErr := ethernetpacket.DecodeFromBytes(packet.Layer(layers.LayerTypeEthernet).LayerContents(), gopacket.NilDecodeFeedback)
|
|
|
|
if decodeEthernetErr != nil {
|
|
|
|
// Encountered an error during decoding, most likely a broken packet
|
|
|
|
return decodeEthernetErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Examine packet further
|
|
|
|
if dhcppacket.Operation == layers.DHCPOpRequest {
|
|
|
|
// Request packet
|
2019-11-28 18:38:41 +00:00
|
|
|
processRequestPacket(dhcppacket)
|
2019-11-28 16:16:22 +00:00
|
|
|
} else {
|
|
|
|
// Response/Offer packet
|
2019-11-28 18:38:41 +00:00
|
|
|
processResponsePacket(dhcppacket, ethernetpacket)
|
2019-11-28 16:16:22 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 22:43:17 +00:00
|
|
|
// Check for Hostname DHCP option (12)
|
|
|
|
checkForHostname(dhcppacket)
|
|
|
|
|
2019-11-28 16:16:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print summary after all packets are processed
|
|
|
|
func PrintDHCPv4Summary() {
|
|
|
|
headline := color.New(color.FgRed, color.Bold)
|
|
|
|
headline.Println("DHCP Requests")
|
|
|
|
printRequestSummary()
|
|
|
|
headline.Println("DHCP Responses/Offers")
|
|
|
|
printResponseSummary()
|
2019-11-28 22:43:17 +00:00
|
|
|
headline.Println("DHCP Hostnames")
|
|
|
|
printHostnames()
|
2019-11-28 16:16:22 +00:00
|
|
|
}
|