-
Notifications
You must be signed in to change notification settings - Fork 0
/
CensysSearch.py
160 lines (143 loc) ยท 6.01 KB
/
CensysSearch.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
149
150
151
152
153
154
155
156
157
158
159
160
from censys.ipv4 import *
from censys.base import *
import pickle
import argparse
import sys
import os
# help ์ค๋ช
๋ฌธ
help_desc = '''
Censys API๋ฅผ ํ์ฉํ OSINTํด ๊ฐ๋ฐ - ๋ณด์ ํ๋ก์ ํธ -
-- @dnsdudrla97
'''
# ํํฐ๋ง ๋ฐ์ดํฐ ๊ตฌ์กฐ
filter_fields = ['location.country', 'location.country_code', 'location.city', 'ip',
'protocols', 'autonomous_system.name',
'autonomous_system.asn',
'443.https.tls.certificate.parsed.subject.organization',
'443.https.tls.certificate.parsed.subject.common_name',
'443.https.tls.certificate.parsed.extensions.subject_alt_name.dns_names',
'993.imaps.tls.tls.certificate.parsed.subject.common_name',
'993.imaps.tls.tls.certificate.parsed.subject.organization',
'80.http.get.title',
'80.http.get.headers.server',
'80.http.get.body',
'metadata.os', 'tags']
# ์ธ์๋ฅผ ๋ฐํ์ผ๋ก ์ฟผ๋ฆฌ๋ฌธ์ ์ ์ํฉ๋๋ค.
def make_query(query):
if len(query.arguments) == 0:
q = '*'
else:
q = "(" + query.arguments[0] + ")"
if query.tags:
if ',' in query.tags:
tags_split = query.tags.split(',')
tags_q = " AND tags:" + " AND tags:".join(tags_split)
else:
tags_q = " AND tags: %s" % query.tags
q += tags_q
print(q)
if query.asn:
q += " AND autonomous_system.asn: %s" % query.asn
if query.country:
q += " AND location.country_code:%s" % query.country
if query.http_server:
q += " AND 80.http.get.headers.server:%s" % query.http_server
if query.html_title:
if " " in query.html_title:
title = "\"%s\"" % query.html_title
else:
title = query.html_title
q += " AND 80.http.get.title:%s" % title
if query.html_body:
if " " in query.html_body:
body = "\"%s\"" % query.html_body
else:
body = query.html_body
q += " AND 80.http.get.body:%s" % body
return q
# res, ipv4 ๊ธฐ๋ฐ์ ์ผ๋ฐ ์ ๋ณด๋ฅผ ๊ฒ์ -> dict ํํ
def SearchPrint(query):
max_title_len = 50
title_head = 'Title: '
cut = '[...]'
http_title = query.get('80.http.get.title', 'N/A')
as_name = query.get('autonomous_system.name', 'N/A')
as_num = query.get('autonomous_system.asn', '')
location = '{} / {}'.format(query.get('location.country_code',
'N/A'), query.get('location.city', 'N/A'))
os = query.get('metadata.os', 'N/A')
tags = query.get('tags', '')
ip = query.get('ip', 'N/A')
# ๋ฌธ์์ด ๋ณ๊ฒฝ '\n' -> '\\n'
http_title = http_title.replace('\n', '\\n')
http_title = http_title.replace('\r', '\\r')
# ํ์ดํ์ด ๊ธธ ๊ฒฝ์ฐ ๊ธธ์ด ์ ํ
if len(http_title) > (max_title_len-len(title_head)-1):
http_title = str(http_title[:max_title_len-len(title_head)-len(cut)-1]) + cut
print(ip.ljust(16) +
((title_head + '%s') % http_title).ljust(max_title_len) +
('AS: %s (%s)' % (as_name, as_num)).ljust(40) +
('Loc: %s' % location).ljust(30) +
('OS: %s' % os).ljust(15) +
('Tags: %s' % tags))
# API ์ค์ - ํ๊ฒฝ ๋ณ์ ํ์ผ ์์ฑ
def censys_api(api_key):
conf_file = "%s/.censys_API.p" % os.environ.get('HOME')
api = dict()
# ์ธ์๊ฐ ์์ ๊ฒฝ์ฐ ํด๋น ํ๊ฒฝ ๋ณ์ ํ์ผ ์์ฑ ๋ฐ ์ ์ฅ
if api_key.api_id and api_key.api_secret:
api['id'] = api_key.api_id
api['secret'] = api_key.api_secret
pickle.dump(api, open(conf_file, "wb"))
return api
# conf_file ์กด์ฌํ ์ ๋ก๋
if os.path.isfile(conf_file):
try:
api = pickle.load(open(conf_file, "rb"))
except:
print ("ํผํด ํ์ผ์ด ์์๋์์ต๋๋ค.")
sys.exit(-1)
if not api.get('id') or not api.get('secret'):
print ("ํผํฌ ํ์ผ ๊ตฌ์กฐ๊ฐ ๋ถ์ผ์น ํฉ๋๋ค.")
sys.exit(-2)
return api
# ํ๊ฒฝ ๋ณ์ ํ์ผ์ API๊ฐ ์กด์ฌ ํ์ง ์์์ ์ ์ฅ ํ ๋ฐํ
if 'CENSYS_API_ID' in os.environ and 'CENSYS_API_SECRET' in os.environ:
api['id'] = os.environ.get('CENSYS_API_ID')
api['secret'] = os.environ.get('CENSYS_API_SECRET')
pickle.dump(api, open(conf_file, "wb"))
return api
# ํด๋น ์์ค๊ฐ ํ์ฌ main ์์ ๋์ํ๋์ง
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=help_desc, formatter_class=argparse.RawTextHelpFormatter)
# ๋จ์ ๊ฒ์ ์ธ์
parser.add_argument('-a', '--asn', default=None,
help='๊ธฐ๊ด์ด,๋จ์ฒด๊ฐ ๋
๋ฆฝ์ ์ผ๋ก ์ด์ํ๋ ๋คํธ์ํฌ ๋ผ์ฐํฐ๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์')
parser.add_argument('-c', '--country', default=None,
help='๊ตญ๊ฐ ์ฝ๋๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์')
parser.add_argument('-S', '--http-server', default=None, help='์๋ฒ <header> ์ ๋ณด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์')
parser.add_argument('-t', '--html-title', default=None,
help='์น ํ์ด์ง <title> ์ ๋ณด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์')
parser.add_argument('-b', '--html-body', default=None,
help='์น ํ์ด์ง <body> ์ ๋ณด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์')
parser.add_argument('-T', '--tags', default=None,
help='๊ฒ์ ํ๊ณ ์ ํ๋ tag๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์ (-T tag1,tag2,... ) (ํค์๋ ์ฌ์ฉ์ \'list\' ์ฌ์ฉํด์ผ ์ ํํ ๋์ต๋๋ค.')
parser.add_argument('--api_id', default=None,
help='ํ๊ฒฝ๋ณ์๋ก ์ ์ ํ์ง ์์์ ์
๋ ฅํด ์ฃผ์ธ์ (API_ID)')
parser.add_argument('--api_secret', default=None,
help='ํ๊ฒฝ๋ณ์๋ก ์ ์ ํ์ง ์์์ ์
๋ ฅํด ์ฃผ์ธ์ (API_SECRET)')
parser.add_argument('arguments', metavar='arguments',
nargs='*', help='์ฟผ๋ฆฌ ๋์ด')
args = parser.parse_args()
# API ํค, ๋น๋ฐํค๋ฅผ ์ฒ๋ฆฌ ํฉ๋๋ค.
api = censys_api(args)
# print(api)
# API ์ธ์ฆ์ ์งํ ํฉ๋๋ค.
Censys_Auth = CensysIPv4(api_id=api['id'], api_secret=api['secret'])
# ์ธ์๋ฅผ ๋ฐ์์ ๋ถ๋ฅ ํ ์ฟผ๋ฆฌ๋ฌธ์ ๋ง๋ญ๋๋ค.
Query_string = make_query(args)
# ์ ๋ฆฌํ ์ฟผ๋ฆฌ๋ฅผ ๋ฐํ์ผ๋ก search ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์์ ์งํํฉ๋๋ค. (ํํฐ ๋ฆฌ์คํธ ๋ฅผ ์ฌ์ฉํ์ฌ ์ฐพ๊ณ ์ ํ๋ ์ ๋ณด๋ฅผ ๋ถ์ฐ์ํต๋๋ค.)
result = Censys_Auth.search(Query_string, fields=filter_fields)
for entity in result:
SearchPrint(entity)