-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.py
83 lines (70 loc) · 2.66 KB
/
query.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
import json
import requests
import sys
import configparser2
UID = ""
SECRET = ""
API_URL = "https://www.censys.io/api/v1"
def auth(_UID, _SECRET):
# auth: Authentication data for censys API access
# Arguments:
# _UID: API UID
#
global UID; UID = _UID
global SECRET; SECRET = _SECRET
# TODO Do a validation of the credentials
def constructor(options):
# constructor: put the options together into a query string
# Arguments:
# options: the search options
# Returns:
# string object with the complete search query
connector = " AND "
query_string = ""
for option in options:
query_string += option + connector
return query_string[:-5]
def query(page, options): #This needs to return all the pages in a array instead, to prevent all these duplicate searches
# query: Constructs the query and sends the request to the censys API
# Arguments:
# page: Number of pages to return (100 results per page)
# options: the search options
# Returns:
# res object with the json result
query_string = constructor(options)
if UID == "" or SECRET == "":
raise Exception("ERROR: No UID or SECRET given, can not make request\r\n\r\n")
json_query = json.loads('{"query":"' + query_string + '", "page":' + str(page) + ', "fields":["ip", "tags"]}')
res = requests.post(API_URL + "/search/ipv4", json=json_query, auth=(UID, SECRET))
if res.status_code != 200:
print "error occurred: %s" % res.json()["error"]
sys.exit(1)
return res
def detail(ip):
# detail: Return the details
# Arguments:
# ip: IPv4 address to get details about
# Returns:
# multiline string with details (banners, titles) of services
res = requests.get(API_URL + "/view/ipv4/" + ip, auth=(UID, SECRET))
try:
details = "-"*60 + "\n\t\t" + str(res.json()['ip'])
if res.status_code == 200:
try:
details += "\nHTTP Service Title: " + res.json()['80']['http']['get']['title'] + "\nBody:\n" + res.json()['80']['http']['get']['body'][:300] + "\n"
except:
pass
try:
details += "\nFTP Banner: " + res.json()['21']['ftp']['banner']['banner'] + "\n"
except:
pass
try:
details += "\nSSH Banner: " + res.json()['22']['ssh']['banner']['raw_banner'] + "\n"
except:
pass
else:
details += "\n\t\tERROR: No data returned!"
except:
details = "\n\t\tERROR: No data returned!"
pass
return details.encode("utf8")