File: knockpy.py

package info (click to toggle)
knockpy 4.1.0-4
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 256 kB
  • sloc: python: 560; makefile: 3
file content (370 lines) | stat: -rw-r--r-- 10,333 bytes parent folder | download | duplicates (2)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from modules import zonetransfer
from modules import header
from modules import resolve
from modules import wildcard
from modules import save_report
from modules import virustotal_subdomains

from urlparse import urlparse

import sys
import json
import os.path
import datetime
import argparse

__author__='Gianni \'guelfoweb\' Amato'
__version__='4.1'
__url__='https://github.com/guelfoweb/knock'
__description__='''\
___________________________________________

knock subdomain scan
knockpy v.'''+__version__+'''
Author: '''+__author__+'''
Github: '''+__url__+'''
___________________________________________
'''
__epilog__='''
example:
  knockpy domain.com
  knockpy domain.com -w wordlist.txt
  knockpy -r domain.com or IP
  knockpy -c domain.com
  knockpy -j domain.com

For virustotal subdomains support you can setting your API KEY in the
config.json file.
 
'''

def loadfile_wordlist(filename):
	filename = open(filename,'r')
	wlist = filename.read().split('\n')
	filename.close
	return filter(None, wlist)

def print_header():
	print """
  _  __                 _                
 | |/ /                | |   """+__version__+"""            
 | ' / _ __   ___   ___| | ___ __  _   _ 
 |  < | '_ \ / _ \ / __| |/ / '_ \| | | |
 | . \| | | | (_) | (__|   <| |_) | |_| |
 |_|\_\_| |_|\___/ \___|_|\_\ .__/ \__, |
                            | |     __/ |
                            |_|    |___/ 
"""

def print_header_scan():
	print '\nIp Address\tStatus\tType\tDomain Name\t\t\tServer'
	print '----------\t------\t----\t-----------\t\t\t------'

def get_tab(string):
		if len(str(string)) > 23:
			return '\t'
		elif len(str(string)) > 15 and len(str(string)) <= 23:
			return '\t\t'
		else:
			return '\t\t\t'

subdomain_csv_list = []
def print_output(data):
	if data['alias']:
		
		for alias in data['alias']:
			ip_alias = data['ipaddress'][0]
			try:
				server_type = str(data['http_response']['http_headers']['server'])
			except:
				server_type = ''

			row = ip_alias+'\t'+str(data['status'])+'\t'+'alias'+'\t'+str(alias)+get_tab(alias)+str(server_type)
			print (row)
			subdomain_csv_list.append(ip_alias+','+str(data['status'])+','+'alias'+','+str(alias)+','+str(server_type))
		
		for ip in data['ipaddress']:
			try:
				server_type = str(data['http_response']['http_headers']['server'])
			except:
				server_type = ''

			row = ip+'\t'+str(data['status'])+'\t'+'host'+'\t'+str(data['hostname'])+get_tab(data['hostname'])+str(server_type)
			print (row)
			subdomain_csv_list.append(ip+','+str(data['status'])+','+'host'+','+str(data['hostname'])+','+str(server_type))
	else:
		
		for ip in data['ipaddress']:
			try:
				server_type = str(data['http_response']['http_headers']['server'])
			except:
				server_type = ''

			row = ip+'\t'+str(data['status'])+'\t'+'host'+'\t'+str(data['hostname'])+get_tab(data['hostname'])+str(server_type)
			print (row)
			subdomain_csv_list.append(ip+','+str(data['status'])+','+'host'+','+str(data['hostname'])+','+str(server_type))

def init(text, resp=False):
	if resp:
		print(text)
	else:
		print(text),

def main():
	parser = argparse.ArgumentParser(
		version=__version__,
		formatter_class=argparse.RawTextHelpFormatter,
		prog='knockpy',
		description=__description__,
		epilog = __epilog__)

	parser.add_argument('domain', help='target to scan, like domain.com')
	parser.add_argument('-w', help='specific path to wordlist file',
					nargs=1, dest='wordlist', required=False)
	parser.add_argument('-r', '--resolve', help='resolve single ip or domain name',
						action='store_true', required=False)
	parser.add_argument('-c', '--csv', help='save output in csv',
						action='store_true', required=False)
	parser.add_argument('-j', '--json', help='export full report in JSON',
						action='store_true', required=False)

						
	args = parser.parse_args()
	
	target = args.domain
	wlist = args.wordlist
	resolve_host = args.resolve
	save_scan_csv = args.csv
	save_scan_json = args.json

	print_header()

	'''
	start
	'''
	time_start = str(datetime.datetime.now())

	'''
	parse target domain
	'''
	if target.startswith("http") or target.startswith("ftp"):
		parsed_uri = urlparse(target)
		target = '{uri.netloc}'.format(uri=parsed_uri)

	'''
	check for virustotal subdomains
	'''
	init('+ checking for virustotal subdomains:', False)
	subdomain_list = []
	if os.path.isfile('config.json'): 
		with open('config.json') as data_file:    
			apikey = json.load(data_file)
			try:
				apikey_vt = apikey['virustotal']
				if apikey_vt != '':
					virustotal_list = virustotal_subdomains.get_subdomains(target, apikey_vt)
					if virustotal_list:
						init('YES', True)
						print(json.dumps(virustotal_list, indent=4, separators=(',', ': ')))
						for item in virustotal_list:
							subdomain = item.replace('.'+target, '')
							if subdomain not in subdomain_list:
								subdomain_list.append(subdomain)
					else:
						init('NO', True)
				else:
					init('SKIP', True)
					init('\tVirusTotal API_KEY not found', True)
					virustotal_list = []
			except:
				init('SKIP', True)
				init('\tVirusTotal API_KEY not found', True)
				virustotal_list = []
	else:
		init('SKIP', True)
		init('\tCONFIG FILE NOT FOUND', True)
		virustotal_list = []

	'''
	check for wildcard
	'''
	init('+ checking for wildcard:', False)
	wildcard_json = json.loads(wildcard.test_wildcard(target))
	if wildcard_json['enabled']:
		init('YES', True)
		print(json.dumps(wildcard_json['detected'], indent=4, separators=(',', ': ')))
	else:
		init('NO', True)

	'''
	check for zonetransfer
	'''
	init('+ checking for zonetransfer:', False)
	zonetransfer_json = json.loads(zonetransfer.zonetransfer(target))
	if zonetransfer_json['enabled']:
		init('YES', True)
		print(json.dumps(zonetransfer_json['list'], indent=4, separators=(',', ': ')))
		for item in zonetransfer_json['list']:
			subdomain = item.replace('.'+target, '')
			if subdomain not in subdomain_list:
				subdomain_list.append(subdomain)
	else:
		init('NO', True)
		
	'''
	optional argument -w WORDLIST
	'''
	if wlist: 
		wordlist = wlist[0]
	else:
		_ROOT = os.path.abspath(os.path.dirname(__file__))
		wordlist = os.path.join(_ROOT, 'wordlist', 'wordlist.txt')
	
	if not os.path.isfile(wordlist): 
		exit('File not found: ' + wordlist)
	
	word_list = loadfile_wordlist(wordlist)
	word_list = [item.lower() for item in word_list]
	subdomain_list = subdomain_list + word_list
	subdomain_list = list(set(subdomain_list))
	subdomain_list = sorted(subdomain_list)
	wordlist_count = len(subdomain_list)
	
	'''
	resolve domain
	'''
	init('+ resolving target:', False)
	response_resolve = json.loads(resolve.resolve(target))
	response_resolve.update({'wildcard': wildcard_json, 'zonetransfer': zonetransfer_json, 'virustotal': virustotal_list})
	response_resolve['ipaddress']
	if response_resolve['hostname']:
		init('YES', True)
	else:
		init('NO', True)
	
	ip_list = []
	try:
		del response_resolve['status']
		for ip in response_resolve['ipaddress']:
			ip_list.append(ip)
	except:
		pass
	
	time_end = str(datetime.datetime.now())
	
	stats = {'time_start': time_start, 'time_end': time_end}

	'''
	optional argument -r RESOLVE DOMAIN
	'''
	if resolve_host: 
		response_resolve = json.dumps(response_resolve, indent=4, separators=(',', ': '))
		print(response_resolve)
		exit()
	
	'''
	scan for subdomain
	'''
	init('- scaning for subdomain...', True)
		
	print_header_scan()

	subdomains_json_list = []

	import sys
	for item in subdomain_list:
		sys.stdout.write("%s\r" % item)
		sys.stdout.flush()
		subdomain_target = item+'.'+target
		subdomain_resolve = json.loads(resolve.resolve(subdomain_target))

		if subdomain_resolve['hostname']:
			try:
				status_code = subdomain_resolve['http_response']['status']['code']
			except:
				status_code = ''

			if wildcard_json['enabled']:
				wildcard_code = wildcard_json['detected']['status_code']
				if str(status_code) != '' and str(wildcard_code) != '' and str(status_code) == str(wildcard_code):
					try:
						content_length = str(subdomain_resolve['http_response']['http_headers']['content-length'])
					except:
						content_length = ''
					try:
						wildcard_content_length = wildcard_json['http_response']['http_headers']['content-length']
					except:
						wildcard_content_length = ''
					'''
					Experimental:
					content_length == '0' => This is a work around.
					'''
					if content_length == '0' or str(content_length) == str(wildcard_content_length):
						pass
					else:
						print_output(subdomain_resolve)
						subdomains_json_list.append(subdomain_resolve)
				else:
					print_output(subdomain_resolve)
					subdomains_json_list.append(subdomain_resolve)
			else:
				print_output(subdomain_resolve)
				subdomains_json_list.append(subdomain_resolve)		
		sys.stdout.write("%s\r" % ('                               ') )
		sys.stdout.flush()

	subdomain_found = []
	for items in subdomains_json_list:
		try:
			del items['status']
		except:
			pass
		
		if items['hostname'] not in subdomain_found:
			subdomain_found.append(str(items['hostname']))

		for item in items['alias']:
			if item not in subdomain_found:
				subdomain_found.append(str(item))

		for item in items['ipaddress']:
			ip_list.append(str(item))

	ipaddr_list = list(set(ip_list))
	ip_count = len(ipaddr_list)
	subdomain_found = list(set(subdomain_found))
	sub_count = len(subdomain_found)
	
	'''
	optional argument -s SAVE FULL SCAN REPORT
	'''

	stats = {'time_start': time_start, 'time_end': time_end, \
			'sub_count': sub_count, 'ip_count': ip_count, \
			'wordlist': {'filename': wordlist, 'item_count': wordlist_count}, \
			'knockpy': {'version': __version__, 'query': sys.argv, 'url': __url__}}

	try:
		del resolve_host_report['stats']
	except:
		pass

	if not resolve_host:
		if save_scan_csv:
			exit(save_report.export(target, subdomain_csv_list, 'csv'))
		elif save_scan_json:
			report_json = {'target_response': response_resolve, \
							'subdomain_response': subdomains_json_list, \
							'found': {'ipaddress': ipaddr_list, \
							'subdomain': subdomain_found, \
							'csv': subdomain_csv_list}, 'info': stats}
			report_json = json.dumps(report_json, indent=4, separators=(',', ': '))
			exit(save_report.export(target, report_json, 'json'))
		else:
			exit()

if __name__ == '__main__':
	main()