Unify logging process

This commit is contained in:
2019-12-03 23:51:03 +01:00
parent a368f18915
commit e9ec8ad46c
11 changed files with 124 additions and 72 deletions

View File

@@ -1,7 +1,7 @@
package dhcpv4
import (
"github.com/fatih/color"
"git.darknebu.la/maride/pancap/output"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
@@ -50,13 +50,8 @@ func HandleDHCPv4Packet(packet gopacket.Packet) error {
// Print summary after all packets are processed
func PrintDHCPv4Summary() {
headline := color.New(color.FgRed, color.Bold)
headline.Println("DHCP Network Overview")
printNetworkSummary()
headline.Println("DHCP Requests")
printRequestSummary()
headline.Println("DHCP Responses/Offers")
printResponseSummary()
headline.Println("DHCP Hostnames")
printHostnames()
output.PrintBlock("DHCP Network Overview", generateNetworkSummary())
output.PrintBlock("DHCP Requests", generateRequestSummary())
output.PrintBlock("DHCP Responses/Offers", generateResponseSummary())
output.PrintBlock("DHCP Hostnames", generateHostnamesSummary())
}

View File

@@ -39,8 +39,8 @@ func checkForHostname(dhcppacket layers.DHCPv4) {
// None found, means client or server doesn't support Hostname option field. Ignore.
}
// Prints the list of all hostnames encountered.
func printHostnames() {
// Generates the list of all hostnames encountered.
func generateHostnamesSummary() string {
var tmparr []string
// Construct meaningful text
@@ -72,7 +72,7 @@ func printHostnames() {
}
// and print it as a tree.
common.PrintTree(tmparr)
return common.GenerateTree(tmparr)
}
// Adds the given hostname to the hostname array, or patches an existing entry if found

View File

@@ -140,14 +140,16 @@ func formatDate(rawDate []byte) string {
return formattedDate
}
// Prints the summary of relevant DHCP options
func printNetworkSummary() {
fmt.Printf("Subnet Mask: %s\n", formatIP(networkSetup[layers.DHCPOptSubnetMask]))
fmt.Printf("Broadcast: %s\n", formatIP(networkSetup[layers.DHCPOptBroadcastAddr]))
fmt.Printf("Router: %s\n", formatIP(networkSetup[layers.DHCPOptRouter]))
fmt.Printf("DNS Server: %s\n", formatIP(networkSetup[layers.DHCPOptDNS]))
fmt.Printf("NTP Server: %s\n", formatIP(networkSetup[layers.DHCPOptNTPServers]))
fmt.Printf("Lease Time: %s\n", formatDate(networkSetup[layers.DHCPOptLeaseTime]))
fmt.Printf("Renewal Time: %s\n", formatDate(networkSetup[layers.DHCPOptT1]))
// Generates the summary of relevant DHCP options
func generateNetworkSummary() string {
// It's also possible to use strings.Builder here, but it produces code which is longer than this solution :shrug:
summary := fmt.Sprintf("Subnet Mask: %s\n", formatIP(networkSetup[layers.DHCPOptSubnetMask]))
summary = fmt.Sprintf("%sBroadcast: %s\n", summary, formatIP(networkSetup[layers.DHCPOptBroadcastAddr]))
summary = fmt.Sprintf("%sRouter: %s\n", summary, formatIP(networkSetup[layers.DHCPOptRouter]))
summary = fmt.Sprintf("%sDNS Server: %s\n", summary, formatIP(networkSetup[layers.DHCPOptDNS]))
summary = fmt.Sprintf("%sNTP Server: %s\n", summary, formatIP(networkSetup[layers.DHCPOptNTPServers]))
summary = fmt.Sprintf("%sLease Time: %s\n", summary, formatDate(networkSetup[layers.DHCPOptLeaseTime]))
summary = fmt.Sprintf("%sRenewal Time: %s\n", summary, formatDate(networkSetup[layers.DHCPOptT1]))
return summary
}

View File

@@ -15,8 +15,7 @@ func processRequestPacket(dhcppacket layers.DHCPv4) {
requestMAC = common.AppendIfUnique(dhcppacket.ClientHWAddr.String(), requestMAC)
}
// Prints the summary of all DHCP request packets
func printRequestSummary() {
fmt.Printf("%d unique DHCP requests\n", len(requestMAC))
common.PrintTree(requestMAC)
// Generates the summary of all DHCP request packets
func generateRequestSummary() string {
return fmt.Sprintf("%d unique DHCP requests\n%s", len(requestMAC), common.GenerateTree(requestMAC))
}

View File

@@ -15,8 +15,8 @@ func processResponsePacket(dhcppacket layers.DHCPv4, ethernetpacket layers.Ether
addResponseEntry(dhcppacket.ClientIP.String(), dhcppacket.YourClientIP.String(), dhcppacket.ClientHWAddr.String(), ethernetpacket.SrcMAC.String())
}
// Prints the summary of all DHCP offer packets
func printResponseSummary() {
// Generates the summary of all DHCP offer packets
func generateResponseSummary() string {
var tmpaddr []string
// Iterate over all responses
@@ -31,7 +31,7 @@ func printResponseSummary() {
}
// Draw as tree
common.PrintTree(tmpaddr)
return common.GenerateTree(tmpaddr)
}
// Adds a new response entry. If an IP address was already issued or a MAC asks multiple times for DNS, the case is examined further