-
Notifications
You must be signed in to change notification settings - Fork 0
/
censys.go
executable file
·195 lines (164 loc) · 4.69 KB
/
censys.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
// censys project censys.go
package censys
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const APIURL = "https://www.censys.io/api/v1"
const SearchURL = "search"
const ExportURL = "export"
type Metadata struct {
Count int `json:"count"`
Query string `json:"query"`
Page int `jsno:"page"`
Pages int `json:"pages"`
}
type censysSearchIPv4 struct {
Status string `json:"status"`
MetaData Metadata `json:"metadata"`
Results []struct {
IP string `json:"ip"`
Protocols []string `json:"protocols"`
} `json:"results"`
}
type censysSearchWebsites struct {
Status string `json:"status"`
MetaData Metadata `json:"metadata"`
Results []struct {
Domain string `json:"domain"`
Rank []int `json:"alexa_rank"`
} `json:"results"`
}
type censysSearchCertificates struct {
Status string `json:"status"`
MetaData Metadata `json:"metadata"`
Results []struct {
FingerprintSHA256 []string `json:"parsed.fingerprint_sha256"`
SubjectDN []string `json:"parsed.subject_dn"`
IssuerDN []string `json:"parsed.issuer_dn"`
} `json:"results"`
}
type censysExport struct {
Status string `json:"status"`
Config struct {
Format string `json:"format"`
Compress bool `json:"compress"`
Headers bool `json:"headers"`
Flatten bool `json:"flatten"`
Query string `json:"query"`
} `json:"configuration"`
JobID string `json:"job_id"`
}
func getErrorString(StatusCode int, url, paramStr string) error {
switch StatusCode {
case 400:
return fmt.Errorf("Error %d -> Query could not be parsed. Url: %s. Param: %s", StatusCode, url, paramStr)
case 404:
return fmt.Errorf("Error %d -> Page not found. Url: %s. Param: %s", StatusCode, url, paramStr)
case 429:
return fmt.Errorf("Error %d -> Rate limit exceeded. Url: %s. Param: %s", StatusCode, url, paramStr)
case 500:
return fmt.Errorf("Error %d -> Internal server error. Url: %s. Param: %s", StatusCode, url, paramStr)
default:
return fmt.Errorf("unknown error code %d. Url: %s. Param: %s", StatusCode, url, paramStr)
}
}
func request(auth [2]string, url, paramStr *string) (*[]byte, error) {
data := bytes.NewBuffer([]byte(*paramStr))
req, err := http.NewRequest("POST", *url, data)
if err != nil {
panic(err)
}
//Set Headers
req.SetBasicAuth(auth[0], auth[1])
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if resp.StatusCode != 200 {
return nil, getErrorString(resp.StatusCode, *url, *paramStr)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return &body, nil
}
//Search Engine function
func Search(auth [2]string, index string, query string, page int) (*[]byte, error) {
//Make request URL and post param
url := fmt.Sprintf("%s/%s/%s", APIURL, SearchURL, index)
// fields := ``
paramStr := fmt.Sprintf(`{"query": "%s", "page": %d, "fields":[]}`, query, page)
//Read body request
body, err := request(auth, &url, ¶mStr)
if err != nil {
return nil, err
}
return body, nil
}
//Search IPv4
func SearchIPv4(auth [2]string, query string, page int) (*censysSearchIPv4, error) {
body, err := Search(auth, "ipv4", query, page)
if err != nil {
return nil, err
}
cS := censysSearchIPv4{}
if err = json.Unmarshal(*body, &cS); err != nil {
return nil, err
}
return &cS, nil
}
//Search WebSites
func SearchWebSites(auth [2]string, query string, page int) (*censysSearchWebsites, error) {
body, err := Search(auth, "websites", query, page)
if err != nil {
return nil, err
}
cS := censysSearchWebsites{}
if err = json.Unmarshal(*body, &cS); err != nil {
return nil, err
}
return &cS, nil
}
//Search Certificates
func SearchCertificates(auth [2]string, query string, page int) (*censysSearchCertificates, error) {
body, err := Search(auth, "certificates", query, page)
if err != nil {
return nil, err
}
cS := censysSearchCertificates{}
if err = json.Unmarshal(*body, &cS); err != nil {
return nil, err
}
return &cS, nil
}
//Export Engine function
func Export(auth [2]string, query string) (*censysExport, error) {
//Make request URL and post param
url := fmt.Sprintf("%s/%s", APIURL, ExportURL)
paramStr := fmt.Sprintf(`{"query": "%s", "format":"json"}`, query)
//Read body request
body, err := request(auth, &url, ¶mStr)
if err != nil {
return nil, err
}
cE := censysExport{}
if err = json.Unmarshal(*body, &cE); err != nil {
return nil, err
}
return &cE, nil
}
func GetExportStatus(auth [2]string, job_id string) (*[]byte, error) {
//Make request URL and post param
url := fmt.Sprintf("%s/%s", APIURL, ExportURL)
//Read body request
body, err := request(auth, &url, &job_id)
if err != nil {
return nil, err
}
return body, nil
}