-
-
Notifications
You must be signed in to change notification settings - Fork 337
/
censys_domain.go
229 lines (186 loc) · 5.04 KB
/
censys_domain.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Copyright (c) 2017, AverageSecurityGuy
# All rights reserved.
Find subdomains using Censys certificate information.
Usage:
$ go run censys_domain.go domain
*/
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"sort"
"strings"
"time"
)
const url = "https://censys.io/api/v1/search/certificates"
const uid = ""
const secret = ""
// The Query struct is used to create a query JSON object for Censys searches.
type Query struct {
Query string `json:"query"`
Fields []string `json:"fields"`
Page int `json:"page"`
Flatten bool `json:"flatten"`
}
// The Result struct holds a single result from the Censys JSON response.
type Result struct {
CommonNames []string `json:"parsed.subject.common_name"`
DnsNames []string `json:"parsed.extensions.subject_alt_name.dns_names"`
Names []string `json:"parsed.names"`
}
// The Metadata struct holds metadata information from the Censys JSON response.
type Metadata struct {
Count int `json:"count"`
Query string `json:"query"`
Time int `json:"backend_time"`
Page int `json:"page"`
Pages int `json:"pages"`
}
// The Response struct holds a Censys JSON response.
type Response struct {
Status string `json:"status"`
Results []Result `json:"results"`
Metadata Metadata `json:"metadata"`
}
// The Research struct holds the end result of our iterative queries.
type Research struct {
Domain string
Searched []string
Search []string
Hosts []string
}
var research Research
// The In function determines if a string is in a slice of strings.
func in(items []string, item string) bool {
sort.Strings(items)
i := sort.SearchStrings(items, item)
if i < len(items) && items[i] == item {
return true
}
return false
}
// Pop removes the first item in the slice and returns the item and the new
// slice.
func pop(items []string) ([]string, string) {
item := items[0]
items = items[1:]
return items, item
}
// The request function makes a Censys request and processes the response. If
// there are no errors a response struct is returned.
func request(data *bytes.Buffer) (Response, error) {
var response Response
client := &http.Client{}
req, err := http.NewRequest("POST", url, data)
if err != nil {
return response, err
}
req.SetBasicAuth(uid, secret)
resp, err := client.Do(req)
if err != nil {
return response, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return response, err
}
if resp.StatusCode == 429 {
return response, errors.New("Rate limit exceeded. Please wait and try again.")
} else if resp.StatusCode == 500 {
return response, errors.New("Internal Server Error.")
} else {
err := json.Unmarshal(body, &response)
if err != nil {
return response, err
}
}
time.Sleep(3 * time.Second)
return response, nil
}
// The search function queries the Censys API for subdomains related to the
// given domain.
func search(domain string) ([]Result, error) {
fmt.Printf(".")
var results []Result
var query Query
pages := 100
query.Query = fmt.Sprintf("parsed.subject.common_name: /.*\\.%s/", domain)
query.Fields = []string{"parsed.names", "parsed.extensions.subject_alt_name.dns_names", "parsed.subject.common_name"}
query.Flatten = true
for i := 1; i <= pages; i++ {
query.Page = i
j, err := json.Marshal(query)
if err != nil {
research.Search = append(research.Search, domain)
return results, err
}
resp, err := request(bytes.NewBuffer(j))
if err != nil {
research.Search = append(research.Search, domain)
return results, err
}
results = append(results, resp.Results...)
pages = resp.Metadata.Pages
}
research.Searched = append(research.Searched, domain)
return results, nil
}
// Process the results and update the subdomains map with the results.
func process(results []Result, domain string) {
var subs []string
for _, r := range results {
if len(r.CommonNames) == 0 {
continue
}
if strings.HasSuffix(r.CommonNames[0], domain) {
subs = append(subs, r.CommonNames...)
subs = append(subs, r.DnsNames...)
subs = append(subs, r.Names...)
}
}
// Add new subdomains to our Hosts and Search slices.
for _, sub := range subs {
if !in(research.Hosts, sub) {
research.Hosts = append(research.Hosts, sub)
}
if !in(research.Searched, sub) && !in(research.Search, sub) {
research.Search = append(research.Search, sub)
}
}
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: censys_domain.go domain")
os.Exit(0)
}
domain := os.Args[1]
research.Domain = domain
fmt.Printf("[*] Iteratively searching for subdomains for %s\n", domain)
results, err := search(domain)
if err != nil {
fmt.Printf("\n[-] Error: %s", err)
}
process(results, domain)
for {
if len(research.Search) == 0 {
break
}
research.Search, domain = pop(research.Search)
results, err := search(domain)
if err != nil {
fmt.Printf("\n[-] Error: %s", err)
break
}
process(results, domain)
}
fmt.Println("")
fmt.Printf("Subdomains for %s\n", research.Domain)
fmt.Println(strings.Join(research.Hosts, "\n"))
}