No longer import hosts using their MAC addresses

This commit is contained in:
Brendan Le Glaunec
2018-07-03 14:05:04 +02:00
committed by Brendan LE GLAUNEC
parent 541d64168d
commit 456f7fffc5
3 changed files with 138 additions and 57 deletions
+9 -1
View File
@@ -114,13 +114,21 @@ func NmapParseResults(nmapResultFilePath string) ([]Stream, error) {
if err != nil {
continue
}
for _, addr := range host.Addresses {
err = validate.Struct(addr)
if err != nil {
continue
}
streams = append(streams, Stream{
Device: port.Service.Product,
Address: host.Address.Addr,
Address: addr.Addr,
Port: port.PortID,
})
}
}
}
return streams, nil
}
+89 -16
View File
@@ -107,6 +107,12 @@ func TestNmapParseResults(t *testing.T) {
Port: 1337,
}
invalidStreamMACAddress := Stream{
Device: "invalidDevice",
Address: "00:12:16:FB:02:17",
Port: 1337,
}
testCases := []struct {
fileExists bool
streamsXML *nmapResult
@@ -121,10 +127,12 @@ func TestNmapParseResults(t *testing.T) {
streamsXML: &nmapResult{
Hosts: []host{
{
Address: address{
Addresses: []address{
address{
Addr: validStream1.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -141,10 +149,12 @@ func TestNmapParseResults(t *testing.T) {
},
},
{
Address: address{
Addresses: []address{
address{
Addr: validStream2.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -165,6 +175,38 @@ func TestNmapParseResults(t *testing.T) {
fileExists: true,
},
// File exists
// Invalid stream, only a mac address
{
fileExists: true,
expectedStreams: []Stream{},
streamsXML: &nmapResult{
Hosts: []host{
{
Addresses: []address{
address{
Addr: invalidStreamMACAddress.Address,
AddrType: "mac",
},
},
Ports: ports{
Ports: []port{
{
PortID: invalidStreamMACAddress.Port,
State: state{
State: "open",
},
Service: service{
Name: "rtsp",
Product: invalidStreamMACAddress.Device,
},
},
},
},
},
},
},
},
// File exists
// Two invalid targets, no error
{
fileExists: true,
@@ -172,10 +214,12 @@ func TestNmapParseResults(t *testing.T) {
streamsXML: &nmapResult{
Hosts: []host{
{
Address: address{
Addresses: []address{
address{
Addr: invalidStreamNoAddress.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -192,10 +236,12 @@ func TestNmapParseResults(t *testing.T) {
},
},
{
Address: address{
Addresses: []address{
address{
Addr: invalidStreamNoPort.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -226,10 +272,12 @@ func TestNmapParseResults(t *testing.T) {
streamsXML: &nmapResult{
Hosts: []host{
{
Address: address{
Addresses: []address{
address{
Addr: "Camera with closed ports",
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -246,7 +294,8 @@ func TestNmapParseResults(t *testing.T) {
},
},
{
Address: address{
Addresses: []address{
address{
Addr: "Camera with closed ports",
AddrType: "ipv4",
},
@@ -254,6 +303,7 @@ func TestNmapParseResults(t *testing.T) {
},
},
},
},
// XML Unmarshal error
{
fileExists: true,
@@ -314,6 +364,9 @@ func TestNmapParseResults(t *testing.T) {
}
}
assert.Equal(t, true, foundStream, "wrong streams parsed")
if foundStream == false {
fmt.Printf("%+v\n", results)
}
}
}
assert.Equal(t, len(test.expectedStreams), len(results), "wrong streams parsed")
@@ -366,10 +419,12 @@ func TestDiscover(t *testing.T) {
streamsXML: &nmapResult{
Hosts: []host{
{
Address: address{
Addresses: []address{
address{
Addr: validStream1.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -386,10 +441,12 @@ func TestDiscover(t *testing.T) {
},
},
{
Address: address{
Addresses: []address{
address{
Addr: validStream2.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -420,10 +477,12 @@ func TestDiscover(t *testing.T) {
streamsXML: &nmapResult{
Hosts: []host{
{
Address: address{
Addresses: []address{
address{
Addr: validStream1.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -440,10 +499,12 @@ func TestDiscover(t *testing.T) {
},
},
{
Address: address{
Addresses: []address{
address{
Addr: validStream2.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -477,10 +538,12 @@ func TestDiscover(t *testing.T) {
streamsXML: &nmapResult{
Hosts: []host{
{
Address: address{
Addresses: []address{
address{
Addr: validStream1.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -497,10 +560,12 @@ func TestDiscover(t *testing.T) {
},
},
{
Address: address{
Addresses: []address{
address{
Addr: validStream2.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -533,10 +598,12 @@ func TestDiscover(t *testing.T) {
streamsXML: &nmapResult{
Hosts: []host{
{
Address: address{
Addresses: []address{
address{
Addr: invalidStreamNoAddress.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -553,10 +620,12 @@ func TestDiscover(t *testing.T) {
},
},
{
Address: address{
Addresses: []address{
address{
Addr: invalidStreamNoPort.Address,
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -597,10 +666,12 @@ func TestDiscover(t *testing.T) {
streamsXML: &nmapResult{
Hosts: []host{
{
Address: address{
Addresses: []address{
address{
Addr: "Camera with closed ports",
AddrType: "ipv4",
},
},
Ports: ports{
Ports: []port{
{
@@ -617,13 +688,15 @@ func TestDiscover(t *testing.T) {
},
},
{
Address: address{
Addresses: []address{
address{
Addr: "Camera with closed ports",
AddrType: "ipv4",
},
},
},
},
},
targets: "localhost",
ports: "554",
resultFilePath: "/tmp/results.xml",
+2 -2
View File
@@ -11,7 +11,7 @@ type nmapResult struct {
// Host represents a host discovered during a scan
type host struct {
XMLName xml.Name `xml:"host"`
Address address `xml:"address"`
Addresses []address `xml:"address"`
Ports ports `xml:"ports"`
}
@@ -19,7 +19,7 @@ type host struct {
type address struct {
XMLName xml.Name `xml:"address"`
Addr string `xml:"addr,attr"`
AddrType string `xml:"addrType,attr"`
AddrType string `xml:"addrType,attr" validate:"eq=ipv4|eq=ipv6"`
}
// Ports is the list of openned ports on a host