Skip to content

Commit

Permalink
added the IPNetRecord asset type
Browse files Browse the repository at this point in the history
  • Loading branch information
caffix committed Sep 23, 2024
1 parent 7a63dbf commit 056486b
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
20 changes: 18 additions & 2 deletions asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
NetworkEndpoint AssetType = "NetworkEndpoint"
DomainRecord AssetType = "DomainRecord"
AutnumRecord AssetType = "AutnumRecord"
IPNetRecord AssetType = "IPNetRecord"
Location AssetType = "Location"
Phone AssetType = "Phone"
EmailAddress AssetType = "EmailAddress"
Expand All @@ -40,8 +41,8 @@ const (

var AssetList = []AssetType{
IPAddress, Netblock, AutonomousSystem, FQDN, NetworkEndpoint, DomainRecord,
AutnumRecord, Location, Phone, EmailAddress, Person, Organization, SocketAddress,
URL, Fingerprint, TLSCertificate, ContactRecord, Source, Service,
AutnumRecord, IPNetRecord, Location, Phone, EmailAddress, Person, Organization,
SocketAddress, URL, Fingerprint, TLSCertificate, ContactRecord, Source, Service,
}

var locationRels = map[string][]AssetType{
Expand All @@ -57,6 +58,7 @@ var phoneRels = map[string][]AssetType{
var emailRels = map[string][]AssetType{
"source": {Source},
"monitored_by": {Source},
"domain": {FQDN},
}

var domainRecordRels = map[string][]AssetType{
Expand All @@ -82,6 +84,17 @@ var autnumRecordRels = map[string][]AssetType{
"rdap_url": {URL},
}

var ipnetRecordRels = map[string][]AssetType{
"source": {Source},
"monitored_by": {Source},
"whois_server": {FQDN},
"registrant": {ContactRecord},
"admin_contact": {ContactRecord},
"abuse_contact": {ContactRecord},
"technical_contact": {ContactRecord},
"rdap_url": {URL},
}

var personRels = map[string][]AssetType{
"source": {Source},
"monitored_by": {Source},
Expand All @@ -102,6 +115,7 @@ var netblockRels = map[string][]AssetType{
"source": {Source},
"monitored_by": {Source},
"contains": {IPAddress},
"registration": {IPNetRecord},
}

var autonomousSystemRels = map[string][]AssetType{
Expand Down Expand Up @@ -239,6 +253,8 @@ func assetTypeRelations(atype AssetType) map[string][]AssetType {
relations = domainRecordRels
case AutnumRecord:
relations = autnumRecordRels
case IPNetRecord:
relations = ipnetRecordRels
case Location:
relations = locationRels
case Phone:
Expand Down
45 changes: 45 additions & 0 deletions registration/ipnet_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright © by Jeff Foley 2017-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0

package registration

import (
"encoding/json"
"net/netip"

model "github.com/owasp-amass/open-asset-model"
)

// IPNetRecord represents the RDAP record for an IP network.
type IPNetRecord struct {
Raw string `json:"raw,omitempty"`
CIDR netip.Prefix `json:"cidr,omitempty"`
Handle string `json:"handle"`
StartAddress netip.Addr `json:"start_address"`
EndAddress netip.Addr `json:"end_address"`
Type string `json:"type"`
Name string `json:"name"`
Method string `json:"method,omitempty"`
Country string `json:"country,omitempty"`
ParentHandle string `json:"parent_handle,omitempty"`
WhoisServer string `json:"whois_server,omitempty"`
CreatedDate string `json:"created_date,omitempty"`
UpdatedDate string `json:"updated_date,omitempty"`
Status []string `json:"status,omitempty"`
}

// Key implements the Asset interface.
func (ip IPNetRecord) Key() string {
return ip.Handle
}

// AssetType implements the Asset interface.
func (ip IPNetRecord) AssetType() model.AssetType {
return model.IPNetRecord
}

// JSON implements the Asset interface.
func (ip IPNetRecord) JSON() ([]byte, error) {
return json.Marshal(ip)
}
64 changes: 64 additions & 0 deletions registration/ipnet_record_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright © by Jeff Foley 2017-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0

package registration

import (
"net/netip"
"testing"

model "github.com/owasp-amass/open-asset-model"
)

func TestIPNetRecordKey(t *testing.T) {
want := "NET-150-154-0-0-1"
as := IPNetRecord{Handle: want}

if got := as.Key(); got != want {
t.Errorf("IPNetRecord.Key() = %v, want %v", got, want)
}
}

func TestIPNetRecordAssetType(t *testing.T) {
var _ model.Asset = IPNetRecord{} // Verify proper implementation of the Asset interface
var _ model.Asset = (*IPNetRecord)(nil) // Verify the pointer properly implements the Asset interface.

w := IPNetRecord{}
want := model.IPNetRecord

if got := w.AssetType(); got != want {
t.Errorf("IPNetRecord.AssetType() = %v, want %v", got, want)
}
}
func TestIPNetRecord(t *testing.T) {
record := IPNetRecord{
CIDR: netip.MustParsePrefix("150.154.0.0/16"),
Handle: "NET-150-154-0-0-1",
StartAddress: netip.MustParseAddr("150.154.0.0"),
EndAddress: netip.MustParseAddr("150.154.255.255"),
Type: "IPv4",
Name: "REV-MVCC",
Method: "DIRECT ALLOCATION",
ParentHandle: "NET-150-0-0-0-0",
WhoisServer: "whois.arin.net",
CreatedDate: "1991-05-20 04:00:00",
UpdatedDate: "2024-03-28 18:47:50",
Status: []string{"active"},
}

// Test AssetType method
if record.AssetType() != model.IPNetRecord {
t.Errorf("Expected asset type %s, but got %s", model.IPNetRecord, record.AssetType())
}

// Test JSON method
expectedJSON := `{"cidr":"150.154.0.0/16","handle":"NET-150-154-0-0-1","start_address":"150.154.0.0","end_address":"150.154.255.255","type":"IPv4","name":"REV-MVCC","method":"DIRECT ALLOCATION","parent_handle":"NET-150-0-0-0-0","whois_server":"whois.arin.net","created_date":"1991-05-20 04:00:00","updated_date":"2024-03-28 18:47:50","status":["active"]}`
json, err := record.JSON()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if string(json) != expectedJSON {
t.Errorf("Expected JSON %s, but got %s", expectedJSON, string(json))
}
}

0 comments on commit 056486b

Please sign in to comment.