Move common code into new package, 'common'

This commit is contained in:
maride 2019-11-29 14:32:07 +01:00
parent 14f36a8511
commit aa94fa12ec
9 changed files with 28 additions and 79 deletions

View File

@ -1,9 +1,9 @@
package arp package common
import "fmt" import "fmt"
// Appends the appendee to the array if it does not contain appendee yet // Appends the appendee to the array if it does not contain appendee yet
func appendIfUnique(appendee string, array []string) []string { func AppendIfUnique(appendee string, array []string) []string {
// Iterate over all elements and check values // Iterate over all elements and check values
for _, elem := range array { for _, elem := range array {
if elem == appendee { if elem == appendee {
@ -17,7 +17,7 @@ func appendIfUnique(appendee string, array []string) []string {
} }
// Prints each element, along with a small ASCII tree // Prints each element, along with a small ASCII tree
func printTree(strarr []string) { func PrintTree(strarr []string) {
// iterate over each element // iterate over each element
for iter, elem := range strarr { for iter, elem := range strarr {
// check if we got the last element // check if we got the last element

View File

@ -2,6 +2,7 @@ package arp
import ( import (
"fmt" "fmt"
"git.darknebu.la/maride/pancap/common"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/google/gopacket" "github.com/google/gopacket"
"github.com/google/gopacket/layers" "github.com/google/gopacket/layers"
@ -33,14 +34,14 @@ func ProcessARPPacket(packet gopacket.Packet) error {
if arppacket.Operation == layers.ARPRequest { if arppacket.Operation == layers.ARPRequest {
// Request packet // Request packet
participant.asked++ participant.asked++
participant.askedList = appendIfUnique(net.IP(arppacket.DstProtAddress).String(), participant.askedList) participant.askedList = common.AppendIfUnique(net.IP(arppacket.DstProtAddress).String(), participant.askedList)
// Add device entry // Add device entry
addDeviceEntry(sourceAddr, net.IP(arppacket.SourceProtAddress).String()) addDeviceEntry(sourceAddr, net.IP(arppacket.SourceProtAddress).String())
} else { } else {
// Response packet // Response packet
participant.answered++ participant.answered++
participant.answeredList = appendIfUnique(net.IP(arppacket.SourceProtAddress).String(), participant.answeredList) participant.answeredList = common.AppendIfUnique(net.IP(arppacket.SourceProtAddress).String(), participant.answeredList)
// Add device entry // Add device entry
addDeviceEntry(sourceAddr, net.IP(arppacket.SourceProtAddress).String()) addDeviceEntry(sourceAddr, net.IP(arppacket.SourceProtAddress).String())
@ -68,7 +69,7 @@ func printTrafficStats() {
} }
// And print it as a tree // And print it as a tree
printTree(tmparr) common.PrintTree(tmparr)
} }
// Prints an overview over all connected devices in the LAN // Prints an overview over all connected devices in the LAN
@ -81,7 +82,7 @@ func printLANOverview() {
} }
// And print it as a tree // And print it as a tree
printTree(tmparr) common.PrintTree(tmparr)
} }
// Returns the arpStats object for the given MAC address, or creates a new one // Returns the arpStats object for the given MAC address, or creates a new one

View File

@ -1,30 +0,0 @@
package dhcpv4
import "fmt"
// Appends the appendee to the array if it does not contain appendee yet
func appendIfUnique(appendee string, array []string) []string {
// Iterate over all elements and check values
for _, elem := range array {
if elem == appendee {
// ... found. Stop here
return array
}
}
// None found, append
return append(array, appendee)
}
// Prints each element, along with a small ASCII tree
func printTree(strarr []string) {
// iterate over each element
for iter, elem := range strarr {
// check if we got the last element
if iter < len(strarr) - 1 {
fmt.Printf("|- %s\n", elem)
} else {
fmt.Printf("'- %s\n\n", elem)
}
}
}

View File

@ -2,6 +2,7 @@ package dhcpv4
import ( import (
"fmt" "fmt"
"git.darknebu.la/maride/pancap/common"
"github.com/google/gopacket/layers" "github.com/google/gopacket/layers"
"log" "log"
) )
@ -71,7 +72,7 @@ func printHostnames() {
} }
// and print it as a tree. // and print it as a tree.
printTree(tmparr) common.PrintTree(tmparr)
} }
// Adds the given hostname to the hostname array, or patches an existing entry if found // Adds the given hostname to the hostname array, or patches an existing entry if found

View File

@ -2,6 +2,7 @@ package dhcpv4
import ( import (
"fmt" "fmt"
"git.darknebu.la/maride/pancap/common"
"github.com/google/gopacket/layers" "github.com/google/gopacket/layers"
) )
@ -11,11 +12,11 @@ var (
// Processes the DHCP request packet handed over // Processes the DHCP request packet handed over
func processRequestPacket(dhcppacket layers.DHCPv4) { func processRequestPacket(dhcppacket layers.DHCPv4) {
requestMAC = appendIfUnique(dhcppacket.ClientHWAddr.String(), requestMAC) requestMAC = common.AppendIfUnique(dhcppacket.ClientHWAddr.String(), requestMAC)
} }
// Prints the summary of all DHCP request packets // Prints the summary of all DHCP request packets
func printRequestSummary() { func printRequestSummary() {
fmt.Printf("%d unique DHCP requests\n", len(requestMAC)) fmt.Printf("%d unique DHCP requests\n", len(requestMAC))
printTree(requestMAC) common.PrintTree(requestMAC)
} }

View File

@ -2,6 +2,7 @@ package dhcpv4
import ( import (
"fmt" "fmt"
"git.darknebu.la/maride/pancap/common"
"github.com/google/gopacket/layers" "github.com/google/gopacket/layers"
"log" "log"
) )
@ -30,7 +31,7 @@ func printResponseSummary() {
} }
// Draw as tree // Draw as tree
printTree(tmpaddr) common.PrintTree(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 // 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

View File

@ -2,6 +2,7 @@ package dns
import ( import (
"fmt" "fmt"
"git.darknebu.la/maride/pancap/common"
"github.com/google/gopacket/layers" "github.com/google/gopacket/layers"
"golang.org/x/net/publicsuffix" "golang.org/x/net/publicsuffix"
"log" "log"
@ -37,16 +38,16 @@ func processDNSAnswer(answers []layers.DNSResourceRecord) {
processType(answerType, answer.Type) processType(answerType, answer.Type)
// Append full domain and base domain // Append full domain and base domain
answerDomains = appendIfUnique(name, answerDomains) answerDomains = common.AppendIfUnique(name, answerDomains)
// Check if we need to add the base name to the private list // Check if we need to add the base name to the private list
_, icannManaged := publicsuffix.PublicSuffix(name) _, icannManaged := publicsuffix.PublicSuffix(name)
if icannManaged { if icannManaged {
// TLD is managed by ICANN, add to the base list // TLD is managed by ICANN, add to the base list
answerBaseDomains = appendIfUnique(basename, answerBaseDomains) answerBaseDomains = common.AppendIfUnique(basename, answerBaseDomains)
} else { } else {
// it's not managed by ICANN, so it's private - add it to the private list // it's not managed by ICANN, so it's private - add it to the private list
answerPrivateDomains = appendIfUnique(name, answerPrivateDomains) answerPrivateDomains = common.AppendIfUnique(name, answerPrivateDomains)
} }
// Check if we got an A record answer // Check if we got an A record answer
@ -71,19 +72,19 @@ func printDNSAnswerSummary() {
// Output base domains answered with // Output base domains answered with
if len(answerBaseDomains) > 0 { if len(answerBaseDomains) > 0 {
fmt.Println("Answered with these base domains:") fmt.Println("Answered with these base domains:")
printTree(answerBaseDomains) common.PrintTree(answerBaseDomains)
} }
// Output private domains // Output private domains
if len(answerPrivateDomains) > 0 { if len(answerPrivateDomains) > 0 {
fmt.Println("Answered with these private (non-ICANN managed) domains:") fmt.Println("Answered with these private (non-ICANN managed) domains:")
printTree(answerPrivateDomains) common.PrintTree(answerPrivateDomains)
} }
// Check for public and private IPs // Check for public and private IPs
fmt.Printf("Answered with %d public IP addresses and %d private IP addresses\n", len(answerPublicIPv4), len(answerPrivateIPv4)) fmt.Printf("Answered with %d public IP addresses and %d private IP addresses\n", len(answerPublicIPv4), len(answerPrivateIPv4))
if len(answerPrivateIPv4) > 0 { if len(answerPrivateIPv4) > 0 {
fmt.Println("Private IP addresses in answer:") fmt.Println("Private IP addresses in answer:")
printTree(answerPrivateIPv4) common.PrintTree(answerPrivateIPv4)
} }
} }

View File

@ -21,20 +21,6 @@ func processType(typearr map[layers.DNSType]int, dnstype layers.DNSType) {
typearr[dnstype]++ typearr[dnstype]++
} }
// Appends the appendee to the array if it does not contain appendee yet
func appendIfUnique(appendee string, array []string) []string {
// Iterate over all elements and check values
for _, elem := range array {
if elem == appendee {
// ... found. Stop here
return array
}
}
// None found, append
return append(array, appendee)
}
// Checks if the given IP is in a private range or not // Checks if the given IP is in a private range or not
func ipIsPrivate(ip net.IP) bool { func ipIsPrivate(ip net.IP) bool {
// check every private IP block for our IP // check every private IP block for our IP
@ -49,19 +35,6 @@ func ipIsPrivate(ip net.IP) bool {
return false return false
} }
// Prints each element, along with a small ASCII tree
func printTree(strarr []string) {
// iterate over each element
for iter, elem := range strarr {
// check if we got the last element
if iter < len(strarr) - 1 {
fmt.Printf("|- %s\n", elem)
} else {
fmt.Printf("'- %s\n\n", elem)
}
}
}
// Generates a summary string for DNS types in the given array // Generates a summary string for DNS types in the given array
func generateDNSTypeSummary(typearr map[layers.DNSType]int) string { func generateDNSTypeSummary(typearr map[layers.DNSType]int) string {
var answerarr []string var answerarr []string

View File

@ -2,6 +2,7 @@ package dns
import ( import (
"fmt" "fmt"
"git.darknebu.la/maride/pancap/common"
"github.com/google/gopacket/layers" "github.com/google/gopacket/layers"
"golang.org/x/net/publicsuffix" "golang.org/x/net/publicsuffix"
"log" "log"
@ -36,16 +37,16 @@ func processDNSQuestion(questions []layers.DNSQuestion) {
processType(questionType, question.Type) processType(questionType, question.Type)
// Append full domain and base domain // Append full domain and base domain
questionDomains = appendIfUnique(name, questionDomains) questionDomains = common.AppendIfUnique(name, questionDomains)
// Check if we need to add the base name to the private list // Check if we need to add the base name to the private list
_, icannManaged := publicsuffix.PublicSuffix(name) _, icannManaged := publicsuffix.PublicSuffix(name)
if icannManaged { if icannManaged {
// TLD is managed by ICANN, add to the base list // TLD is managed by ICANN, add to the base list
questionBaseDomains = appendIfUnique(basename, questionBaseDomains) questionBaseDomains = common.AppendIfUnique(basename, questionBaseDomains)
} else { } else {
// it's not managed by ICANN, so it's private - add it to the private list // it's not managed by ICANN, so it's private - add it to the private list
questionPrivateDomains = appendIfUnique(name, questionPrivateDomains) questionPrivateDomains = common.AppendIfUnique(name, questionPrivateDomains)
} }
} }
} }
@ -60,12 +61,12 @@ func printDNSQuestionSummary() {
// Output base domains asked for // Output base domains asked for
if len(questionBaseDomains) > 0 { if len(questionBaseDomains) > 0 {
fmt.Println("Asked for these base domains:") fmt.Println("Asked for these base domains:")
printTree(questionBaseDomains) common.PrintTree(questionBaseDomains)
} }
// Output private domains // Output private domains
if len(questionPrivateDomains) > 0 { if len(questionPrivateDomains) > 0 {
fmt.Println("Asked for these private (non-ICANN managed) domains:") fmt.Println("Asked for these private (non-ICANN managed) domains:")
printTree(questionPrivateDomains) common.PrintTree(questionPrivateDomains)
} }
} }