-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcensys_io_poc.py
executable file
·148 lines (115 loc) · 4.25 KB
/
censys_io_poc.py
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
#!/usr/bin/python2
import sys
import os
import json
import requests
import pandas
from argparse import ArgumentParser
CENSYS_API_ADDR = 'https://www.censys.io/api/v1'
CENSYS_API_ID = 'fe857b4f-99d5-4239-955a-90bd65ba75bc'
CENSYS_API_SECRET = 'A95Maw51wALQVGTJmtGSmMcTUraAxebV'
CENSYS_KEYS = {
'endpoints':
{
'get':{
'view':{
'index':['ipv4','websites','certificates'],
'api_path':'/view/'
},
'data':{
'index':['series'],
'api_path':'/data/'
}
},
'post':{
'search':{
'index':['ipv4','websites','certificates'],
'api_path':'/search/'
},
'report':{'index':['ipv4','websites','certificates'],
'api_path':'/report/'},
'query':{
'index':None,
'api_path':'/query/'
},
'export':{
'index':None,
'api_path':'/export/'
}
}
}
}
class CensysAPI:
def __init__(self, apicommand, apikeys):
self.apicmd = apicommand
self.endkeys = apikeys
def search(self):
if self.apicmd[1] in self.endkeys['index']:
search_url = CENSYS_API_ADDR + self.endkeys['api_path'] + self.apicmd[1]
print search_url
search_obj = requests.post(search_url, auth=(CENSYS_API_ID,CENSYS_API_SECRET), data=json.dumps({'query':self.apicmd[2]}))
unpack_json_keys(search_obj.json())
def view(self):
if self.apicmd[1] in self.endkeys['index']:
search_url = CENSYS_API_ADDR + self.endkeys['api_path'] + self.apicmd[1] + '/' + self.apicmd[2]
print search_url
search_obj = requests.get(search_url, auth=(CENSYS_API_ID,CENSYS_API_SECRET))
unpack_json_keys(search_obj.json())
def report(self):
pass
def query(self):
pass
def data(self):
pass
def export(self):
pass
def unpack_list(jl):
'''function to unpack values from list. If values are a dict,
send them back to unpack_json_keys()'''
for obj in jl:
if isinstance(obj, dict):
unpack_json_keys(obj)
else:
print " -{}".format(obj)
def unpack_json_keys(jres):
'''function to unpack values from JSON dict. If values are a list,
send them to unpack_list'''
print '=' * 30
for k,v in jres.iteritems():
if isinstance(v, dict):
unpack_json_keys(v)
elif isinstance(v, list):
print k.upper()
unpack_list(v)
else:
print '{}: {}'.format(k.upper(),v)
print '=' * 30
def api_method(endpoint):
methods = CENSYS_KEYS['endpoints'].keys()
for m in methods:
if endpoint in CENSYS_KEYS['endpoints'][m].keys():
return m
def main():
endpoint = 'search'
index = 'ipv4'
query = 'healthways.com'
method = api_method(endpoint)
apikey = CENSYS_KEYS['endpoints'][method][endpoint]
apicmd = [endpoint,index,query]
load_api = CensysAPI(apicmd, apikey)
if endpoint == 'search':
load_api.search()
elif endpoint == 'view':
load_api.view()
elif endpoint == 'data':
load_api.data()
elif endpoint == 'report':
load_api.report()
elif endpoint == 'query':
load_api.query()
elif endpoint == 'export':
load_api.export()
else:
print "Endpoint not recognized."
if __name__ == '__main__':
main()