Skip to content

Commit 7ee15b1

Browse files
authored
Merge pull request #81 from zmap/phillip/abort-if-no-censys-tokens
Abort Censys annotation if we run out of credits
2 parents 27059a5 + b1e4811 commit 7ee15b1

2 files changed

Lines changed: 21 additions & 27 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ They offer a free tier that allows for a limited number of queries per month, wh
179179

180180
> [!NOTE]
181181
> The free account (as of April 2026) allows a single concurrent request and 100 requests per month.
182-
> Once you've used all your credits, the `censys:` annotation will be empty for any following IPs until your credits refill the following month.
182+
> Once you've used all your credits, the Censys API will return an error upon further requests until your credits reset at the beginning of the next month or you purchase additional credits.
183+
> ZAnnotate will quit with an error if this occurs to prevent silent errors, so if you have a large dataset to annotate, be mindful of your credit usage and prioritize only annotating the IPs you have credits for.
183184
> With the free account only offering a single concurrent request, you'll want to leave `--censys-threads=1` unless you pay for a higher tier.
184185
185186
1. Create an account at [Censys.io](https://censys.io) and get a Personal Access Token (PAT) from Personal Settings > Personal Access Tokens.

‎censys.go‎

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io"
2121
"net"
2222
"net/http"
23+
"strings"
2324

2425
log "github.com/sirupsen/logrus"
2526
)
@@ -103,39 +104,31 @@ func (a *CensysAnnotator) Annotate(ip net.IP) interface{} {
103104
log.Debugf("failed to close response body: %v", err)
104105
}
105106
}(res.Body)
106-
if res.StatusCode != http.StatusOK {
107-
log.Debugf("failed to annotate ip %s with censys: %s", ip.String(), res.Status)
107+
body, _ := io.ReadAll(res.Body)
108+
if res.StatusCode >= 400 && res.StatusCode < 500 {
109+
// From https://docs.censys.com/reference/get-started#step-6-handle-http-response-codes
110+
// 4XX errors are not transient and so we'll abort and report to the user
111+
log.Fatalf("censys api returned an http status '%s' with message: '%s'. "+
112+
"Cannot continue to annotate, please check that you have sufficient API credits and your PAT is correct", res.Status, strings.TrimSpace(string(body)))
113+
114+
} else if res.StatusCode != http.StatusOK {
115+
// Should be a transient error, log and move on
116+
log.Debugf("censys api returned an http %s status with message: '%s'. Skipping Censys annotation for this IP: %s", res.Status, strings.TrimSpace(string(body)), ip.String())
108117
return nil
109118
}
110-
body, _ := io.ReadAll(res.Body)
111-
var result any
119+
// We have a successful response, unmarshall it.
120+
// Struct taken from v1.1 of Censys API docs
121+
var result struct {
122+
Result struct {
123+
Resource any `json:"resource"`
124+
} `json:"result"`
125+
}
112126
err = json.Unmarshal(body, &result)
113127
if err != nil {
114128
log.Debugf("failed to parse censys response for ip %s: %v", ip.String(), err)
115129
return nil
116130
}
117-
// By default, Censys' result is wrapped in a result[resource[real_data]]. We'll unwrap that here
118-
dropResultCast, ok := result.(map[string]interface{})
119-
if !ok {
120-
log.Debugf("failed to unwrap censys response for ip %s: %v", ip.String(), result)
121-
return result
122-
}
123-
dropResult := dropResultCast["result"]
124-
if dropResult == nil {
125-
log.Debugf("failed to unwrap censys response for ip %s: %v", ip.String(), result)
126-
return result
127-
}
128-
dropResourceCast, ok := dropResult.(map[string]interface{})
129-
if !ok {
130-
log.Debugf("failed to unwrap censys response for ip %s: %v", ip.String(), result)
131-
return result
132-
}
133-
dropResource := dropResourceCast["resource"]
134-
if dropResource == nil {
135-
log.Debugf("failed to unwrap censys response for ip %s: %v", ip.String(), result)
136-
return result
137-
}
138-
return dropResource
131+
return result.Result.Resource
139132
}
140133

141134
func (a *CensysAnnotator) Close() error {

0 commit comments

Comments
 (0)