-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcensys.go
More file actions
147 lines (126 loc) · 4.22 KB
/
censys.go
File metadata and controls
147 lines (126 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* ZAnnotate Copyright 2026 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package zannotate
import (
"encoding/json"
"flag"
"io"
"net"
"net/http"
log "github.com/sirupsen/logrus"
)
type CensysAnnotatorFactory struct {
BasePluginConf
client *http.Client // Shared client across threads
personalToken string // User's personal access token for API auth
}
// Censys Annotator Factory (Global)
func (a *CensysAnnotatorFactory) MakeAnnotator(i int) Annotator {
var v CensysAnnotator
v.Factory = a
v.Id = i
return &v
}
func (a *CensysAnnotatorFactory) Initialize(_ *GlobalConf) error {
a.client = http.DefaultClient
return nil
}
func (a *CensysAnnotatorFactory) GetWorkers() int {
return a.Threads
}
func (a *CensysAnnotatorFactory) Close() error {
return nil
}
func (a *CensysAnnotatorFactory) IsEnabled() bool {
return a.Enabled
}
func (a *CensysAnnotatorFactory) AddFlags(flags *flag.FlagSet) {
flags.BoolVar(&a.Enabled, "censys", false, "censys internet intelligence")
flags.StringVar(&a.personalToken, "censys-pat", "", "censys API personal access token (PAT)")
flags.IntVar(&a.Threads, "censys-threads", 1, "how many enrichment threads to use. Note that free plan only allows 1 concurrent API request at a time")
}
// CensysAnnotator (Per-Worker)
type CensysAnnotator struct {
Factory *CensysAnnotatorFactory
Id int
}
func (a *CensysAnnotator) Initialize() (err error) {
return nil
}
func (a *CensysAnnotator) GetFieldName() string {
return "censys"
}
var censysAPIHostLookupURL = "https://api.platform.censys.io/v3/global/asset/host/"
// Annotate performs a Censys host lookup for the given IP address and returns the results.
// If an error occurs or a lookup fails, it returns nil
func (a *CensysAnnotator) Annotate(ip net.IP) interface{} {
req, err := http.NewRequest("GET", censysAPIHostLookupURL+ip.String(), nil)
if err != nil {
// If we can't even form a request, we'll fail to enrich anything. Erroring out.
log.Fatalf("could not form an http request for enriching with censys data for ip %s: %v", ip.String(), err)
}
req.Header.Add("accept", "application/json")
req.Header.Add("authorization", "Bearer "+a.Factory.personalToken)
res, err := a.Factory.client.Do(req)
if err != nil {
log.Debugf("failed to annotate ip %s with censys: %v", ip.String(), err)
return nil
}
defer func(Body io.ReadCloser) {
err = Body.Close()
if err != nil {
log.Debugf("failed to close response body: %v", err)
}
}(res.Body)
if res.StatusCode != http.StatusOK {
log.Debugf("failed to annotate ip %s with censys: %s", ip.String(), res.Status)
return nil
}
body, _ := io.ReadAll(res.Body)
var result any
err = json.Unmarshal(body, &result)
if err != nil {
log.Debugf("failed to parse censys response for ip %s: %v", ip.String(), err)
return nil
}
// By default, Censys' result is wrapped in a result[resource[real_data]]. We'll unwrap that here
dropResultCast, ok := result.(map[string]interface{})
if !ok {
log.Debugf("failed to unwrap censys response for ip %s: %v", ip.String(), result)
return result
}
dropResult := dropResultCast["result"]
if dropResult == nil {
log.Debugf("failed to unwrap censys response for ip %s: %v", ip.String(), result)
return result
}
dropResourceCast, ok := dropResult.(map[string]interface{})
if !ok {
log.Debugf("failed to unwrap censys response for ip %s: %v", ip.String(), result)
return result
}
dropResource := dropResourceCast["resource"]
if dropResource == nil {
log.Debugf("failed to unwrap censys response for ip %s: %v", ip.String(), result)
return result
}
return dropResource
}
func (a *CensysAnnotator) Close() error {
return nil
}
func init() {
s := new(CensysAnnotatorFactory)
RegisterAnnotator(s)
}