handle apple dhcp lease with missing 'name' and 'lease' informations

This commit is contained in:
Pyrrvs 2020-12-14 19:15:34 +01:00
parent f0612e4688
commit 12a6fddbd8
3 changed files with 55 additions and 8 deletions

View File

@ -2452,7 +2452,7 @@ type appleDhcpLeaseEntry struct {
func readAppleDhcpdLeaseEntry(in chan byte) (entry *appleDhcpLeaseEntry, err error) {
entry = &appleDhcpLeaseEntry{extra: map[string]string{}}
validFieldCount := 0
mandatoryFieldCount := 0
// Read up to the lease item and validate that it actually matches
_, ch := consumeOpenClosePair('{', '}', in)
for insideBraces := true; insideBraces; {
@ -2486,7 +2486,7 @@ func readAppleDhcpdLeaseEntry(in chan byte) (entry *appleDhcpLeaseEntry, err err
switch key {
case "ip_address":
entry.ipAddress = val
validFieldCount++
mandatoryFieldCount++
case "identifier":
fallthrough
case "hw_address":
@ -2514,24 +2514,22 @@ func readAppleDhcpdLeaseEntry(in chan byte) (entry *appleDhcpLeaseEntry, err err
} else {
entry.hwAddress = decodedLease
}
validFieldCount++
mandatoryFieldCount++
case "lease":
entry.lease = val
validFieldCount++
case "name":
entry.name = val
validFieldCount++
default:
// Just stash it for now because we have no idea what it is.
entry.extra[key] = val
}
}
// we have most likely parsed the whole file
if validFieldCount == 0 {
if mandatoryFieldCount == 0 {
return nil, nil
}
// an entry is composed of 5 mandatory fields, we'll check that they all have been set during the parsing
if validFieldCount < 5 {
// an entry is composed of 3 mandatory fields, we'll check that they all have been set during the parsing
if mandatoryFieldCount < 3 {
return entry, fmt.Errorf("Error entry `%v` is missing mandatory information", entry)
}
return entry, nil

View File

@ -913,6 +913,31 @@ func TestParserReadAppleDhcpdLeaseEntry(t *testing.T) {
if result.extra["fake"] != expected_extra_1["fake"] {
t.Errorf("expected extra %v, got %v", expected_extra_1["fake"], result.extra["fake"])
}
test_2 := `{
ip_address=192.168.111.4
hw_address=1,0:c:56:3c:e7:23
identifier=1,0:c:56:3c:e7:23
}`
expected_2 := map[string]string{
"ipAddress": "192.168.111.4",
"hwAddress": "000c563ce723",
"id": "000c563ce723",
}
result, err = readAppleDhcpdLeaseEntry(consumeAppleLeaseString(test_2))
if err != nil {
t.Errorf("error parsing entry: %v", err)
}
if result.ipAddress != expected_2["ipAddress"] {
t.Errorf("expected ipAddress %v, got %v", expected_2["ipAddress"], result.ipAddress)
}
if hex.EncodeToString(result.hwAddress) != expected_2["hwAddress"] {
t.Errorf("expected hwAddress %v, got %v", expected_2["hwAddress"], hex.EncodeToString(result.hwAddress))
}
if hex.EncodeToString(result.id) != expected_2["id"] {
t.Errorf("expected id %v, got %v", expected_2["id"], hex.EncodeToString(result.id))
}
}
func TestParserReadAppleDhcpdLeases(t *testing.T) {
@ -1023,6 +1048,23 @@ func TestParserReadAppleDhcpdLeases(t *testing.T) {
t.Errorf("expected hardware address %s, got %s", test_4["hwAddress"], hex.EncodeToString(res.hwAddress))
}
}
test_5 := map[string]string{
"ipAddress": "127.0.0.20",
"id": "0dead099aabc",
"hwAddress": "0dead099aabc",
}
test_5_findings := filter_ipAddr(test_5["ipAddress"], results)
if len(test_5_findings) != 1 {
t.Errorf("expected %d matching entries, got %d", 1, len(test_5_findings))
} else {
res := find_id(test_5["id"], test_5_findings)
if res == nil {
t.Errorf("unable to find item with id %v", test_5["id"])
} else if hex.EncodeToString(res.hwAddress) != test_5["hwAddress"] {
t.Errorf("expected hardware address %s, got %s", test_5["hwAddress"], hex.EncodeToString(res.hwAddress))
}
}
}
func TestParserTokenizeNetworkingConfig(t *testing.T) {

View File

@ -31,3 +31,10 @@
lease=0x5fd72edc
name=vagrant-2019
}
# this entry does not have all fields
{
ip_address=127.0.0.20
hw_address=1,d:ea:d0:99:aa:bc
identifier=1,d:ea:d0:99:aa:bc
}