|
| 1 | +// Copyright © by Jeff Foley 2017-2025. All rights reserved. |
| 2 | +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package aviato |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | + "log/slog" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/owasp-amass/amass/v4/engine/plugins/support" |
| 16 | + et "github.com/owasp-amass/amass/v4/engine/types" |
| 17 | + "github.com/owasp-amass/amass/v4/utils/net/http" |
| 18 | + dbt "github.com/owasp-amass/asset-db/types" |
| 19 | + oam "github.com/owasp-amass/open-asset-model" |
| 20 | + "github.com/owasp-amass/open-asset-model/general" |
| 21 | + "github.com/owasp-amass/open-asset-model/org" |
| 22 | +) |
| 23 | + |
| 24 | +func (cs *companySearch) check(e *et.Event) error { |
| 25 | + _, ok := e.Entity.Asset.(*org.Organization) |
| 26 | + if !ok { |
| 27 | + return errors.New("failed to extract the Organization asset") |
| 28 | + } |
| 29 | + |
| 30 | + ds := e.Session.Config().GetDataSourceConfig(cs.plugin.name) |
| 31 | + if ds == nil || len(ds.Creds) == 0 { |
| 32 | + return nil |
| 33 | + } |
| 34 | + |
| 35 | + var keys []string |
| 36 | + for _, cr := range ds.Creds { |
| 37 | + if cr != nil && cr.Apikey != "" { |
| 38 | + keys = append(keys, cr.Apikey) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + since, err := support.TTLStartTime(e.Session.Config(), string(oam.Organization), string(oam.Organization), cs.name) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + var id *dbt.Entity |
| 48 | + if support.AssetMonitoredWithinTTL(e.Session, e.Entity, cs.plugin.source, since) { |
| 49 | + id = cs.lookup(e, e.Entity, since) |
| 50 | + } else { |
| 51 | + id = cs.query(e, e.Entity, keys) |
| 52 | + support.MarkAssetMonitored(e.Session, e.Entity, cs.plugin.source) |
| 53 | + } |
| 54 | + |
| 55 | + if id != nil { |
| 56 | + cs.process(e, e.Entity, id) |
| 57 | + } |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +func (cs *companySearch) lookup(e *et.Event, orgent *dbt.Entity, since time.Time) *dbt.Entity { |
| 62 | + if edges, err := e.Session.Cache().OutgoingEdges(orgent, since, "id"); err == nil { |
| 63 | + for _, edge := range edges { |
| 64 | + if tags, err := e.Session.Cache().GetEdgeTags(edge, |
| 65 | + since, cs.plugin.source.Name); err != nil || len(tags) == 0 { |
| 66 | + continue |
| 67 | + } |
| 68 | + if a, err := e.Session.Cache().FindEntityById(edge.ToEntity.ID); err == nil && a != nil { |
| 69 | + if id, ok := a.Asset.(*general.Identifier); ok && id != nil && id.Type == AviatoCompanyID { |
| 70 | + return a |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func (cs *companySearch) query(e *et.Event, orgent *dbt.Entity, apikey []string) *dbt.Entity { |
| 79 | + o := orgent.Asset.(*org.Organization) |
| 80 | + brand := support.ExtractBrandName(o.Name) |
| 81 | + |
| 82 | + var body string |
| 83 | + success := false |
| 84 | + for _, key := range apikey { |
| 85 | + headers := http.Header{"Content-Type": []string{"application/json"}} |
| 86 | + headers["Authorization"] = []string{"Bearer " + key} |
| 87 | + |
| 88 | + _ = cs.plugin.rlimit.Wait(context.TODO()) |
| 89 | + |
| 90 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 91 | + defer cancel() |
| 92 | + |
| 93 | + reqDSL := &dsl{ |
| 94 | + Offset: 0, |
| 95 | + Limit: 10, |
| 96 | + Filters: make(map[string]interface{}), |
| 97 | + } |
| 98 | + |
| 99 | + reqDSL.Filters["name"] = &dslEvalObj{ |
| 100 | + Operation: "eq", |
| 101 | + Value: brand, |
| 102 | + } |
| 103 | + |
| 104 | + dslJSON, err := json.Marshal(reqDSL) |
| 105 | + if err != nil { |
| 106 | + return nil |
| 107 | + } |
| 108 | + |
| 109 | + if resp, err := http.RequestWebPage(ctx, &http.Request{ |
| 110 | + URL: "https://data.api.aviato.co/company/search", |
| 111 | + Method: "POST", |
| 112 | + Header: headers, |
| 113 | + Body: string(dslJSON), |
| 114 | + }); err == nil && resp.StatusCode == 200 { |
| 115 | + success = true |
| 116 | + body = resp.Body |
| 117 | + break |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if !success { |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + var result companySearchResult |
| 126 | + if err := json.Unmarshal([]byte(body), &result); err != nil || len(result.Items) == 0 { |
| 127 | + return nil |
| 128 | + } |
| 129 | + |
| 130 | + return cs.store(e, orgent, result.Items[0].ID, 90) |
| 131 | +} |
| 132 | + |
| 133 | +func (cs *companySearch) store(e *et.Event, orgent *dbt.Entity, companyID string, conf int) *dbt.Entity { |
| 134 | + oamid := &general.Identifier{ |
| 135 | + UniqueID: fmt.Sprintf("%s:%s", AviatoCompanyID, companyID), |
| 136 | + ID: companyID, |
| 137 | + Type: AviatoCompanyID, |
| 138 | + } |
| 139 | + |
| 140 | + ident, err := e.Session.Cache().CreateAsset(oamid) |
| 141 | + if err != nil || ident == nil { |
| 142 | + return nil |
| 143 | + } |
| 144 | + |
| 145 | + _, _ = e.Session.Cache().CreateEntityProperty(ident, &general.SourceProperty{ |
| 146 | + Source: cs.plugin.source.Name, |
| 147 | + Confidence: conf, |
| 148 | + }) |
| 149 | + |
| 150 | + if err := cs.plugin.createRelation(e.Session, orgent, general.SimpleRelation{Name: "id"}, ident, conf); err != nil { |
| 151 | + return nil |
| 152 | + } |
| 153 | + return ident |
| 154 | +} |
| 155 | + |
| 156 | +func (cs *companySearch) process(e *et.Event, orgent, ident *dbt.Entity) { |
| 157 | + id := ident.Asset.(*general.Identifier) |
| 158 | + |
| 159 | + _ = e.Dispatcher.DispatchEvent(&et.Event{ |
| 160 | + Name: id.UniqueID, |
| 161 | + Entity: ident, |
| 162 | + Session: e.Session, |
| 163 | + }) |
| 164 | + |
| 165 | + o := orgent.Asset.(*org.Organization) |
| 166 | + e.Session.Log().Info("relationship discovered", "from", o.Name, "relation", "id", |
| 167 | + "to", id.UniqueID, slog.Group("plugin", "name", cs.plugin.name, "handler", cs.name)) |
| 168 | +} |
0 commit comments