-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_url_extractor.py
427 lines (349 loc) · 17 KB
/
static_url_extractor.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env python
'''
Automatic Test
1. Download the web document
2. Anlalyze the web document.
3. Download scripts connected to the web document.
4. Download style sheets connected to the web document.
'''
import itertools
import script as script_search
import style as style_search
from argparse import Namespace, ArgumentParser
from pathlib import Path
from copy import deepcopy
from signal import signal, SIGPIPE, SIG_DFL
from bs4 import BeautifulSoup
from glue import fill_url_scheme, OutputPathResolver, ResourceLoader
import requests
import tinycss2
import urllib
from urllib.request import urlopen
markup_resolver = ''
pageURL = None
hostname = None
recursiveResourceList = []
allURLs = []
failedURLs = []
allResources = []
failedResources = []
levelCount = []
pureURLs = []
def parse_args(*args, **kwargs) -> Namespace:
prsr = ArgumentParser(
description='Auto download web document with linked, scripts and stylesheets')
prsr.add_argument(
'url', metavar='URL', help='address of web document')
#prsr.add_argument(
# '-b', '--webroot',
# metavar='DIRECTORY', help='root directory of website', required=True, type=Path)
result = prsr.parse_args(*args, **kwargs)
return result
def ResolveURL(url):
if (len(url) >= 2 and url[0] == '/' and url[1] != '/'):
url = url[1:]
if (len(url) >= 2 and url[0] == '/' and url[1] == '/'):
temp = url[2:]
url = "http://" + temp
if (len(url) >= 3 and url[0] == '.' and url[1] == '.' and url[2] == '/'):
url = url[3:]
if (url.find(':') == -1):
temp = hostname
if temp[len(temp)-1] != '/':
temp = temp + '/'
temp = temp + url
url = temp
#url = url.replace("https://", "http://")
return url
def CCS_Find_Resources(resource_text):
urlDictionary = {}
rawUrlDictionary = {}
global pageURL
#print ("CCS pageURL2 = " + pageURL)
##resource_resolver = deepcopy(markup_resolver)
##resource_resolver.resource_url = ccs_url
##resource_text = ResourceLoader.download(resource_resolver.resource_url)
#response = urlopen(ccs_url)
# print(response)
# print(response.info())
# print(response.info().get_content_type())
#print(resource_text)
rules, encoding = tinycss2.parse_stylesheet_bytes(css_bytes=str.encode(resource_text))
for rule in rules:
contents = '';
if (isinstance(rule, tinycss2.ast.QualifiedRule) or isinstance(rule, tinycss2.ast.AtRule)):
contents = rule.content
if contents == None:
continue
for token in contents:
if (isinstance(token, tinycss2.ast.URLToken)):
url = token.value
url = url.strip()
if (len(url) == 0):
continue
url = ResolveURL(token.value)
if (url not in urlDictionary):
urlDictionary[url] = url
rawUrlDictionary[url] = token.value
#print(url)
return (urlDictionary, rawUrlDictionary)
#resource_resolver = deepcopy(markup_resolver)
#resource_resolver.resource_url = token.value
#if resource_resolver.output_dir is None or resource_resolver.output_path is None:
# continue
#print("Downloading " + resource_resolver.resource_url)
#resource_text = ResourceLoader.download(resource_resolver.resource_url)
#resource_resolver.output_dir.mkdir(parents=True, exist_ok=True)
#resource_resolver.output_path.write_text(resource_text)
def main(args: Namespace) -> None:
signal(SIGPIPE, SIG_DFL)
count = 0
prevCount = 0
r = requests.get(args.url)
#print (r.content)
global pageURL
global recursiveResourceList
global allURLs
global allResources
global hostname
allURLs.append((0, args.url, "None"))
pureURLs.append(args.url)
allResources.append((args.url))
#levelCount.append(1)
isFirst = True
level = 1
maxLevel = 1
while (isFirst or len(recursiveResourceList) > 0):
if (isFirst):
pageURL = r.url
level = 1
else:
elem = recursiveResourceList.pop()
pageURL = elem[1]
level = elem[0] + 1
if maxLevel < level:
maxLevel = level
###print ("Popped: " + pageURL)
index = pageURL.find("://")
index2 = pageURL[index+3:].find("/")
hostname = pageURL[0:index + 3 + index2]
scheme = ""
###print ("Hostname : " + str(hostname))
if (hostname.find("http://") == 0):
scheme = "http:"
else:
scheme = "https:"
#if (index < 0):
# pageURL = hostname + pageURL
#print("The URL is wrong: " + str(pageURL))
#return
#if (isFirst):
#index2 = pageURL[index+3:].find("/")
#hostname = pageURL[0:index + 3 + index2]
#print ("Hostname : " + str(hostname))
#if (pageURL[len(pageURL)-1] != '/'):
# pageURL = pageURL + '/'
args.url = pageURL
#####print ("pageURL = " + pageURL)
#webroot = args.webroot.expanduser().resolve()
global markup_resolver
#print("Webroot: " + str(webroot))
#markup_resolver = OutputPathResolver(fill_url_scheme(args.url, webroot), webroot)
contents = ResourceLoader.download(pageURL)
#print ("Contents:")
#print (contents)
if isFirst or pageURL.endswith(".html"):
#print ("In " + pageURL)
html_tree = BeautifulSoup(contents, features='html.parser')
#markup_resolver.output_dir.mkdir(parents=True, exist_ok=True)
# markup_resolver.output_path.write_text(html_tree.prettify())
#markup_resolver.output_path.write_text(markup)
########## script_tags = script_search.find_external_scripts(html_tree)
#####all_link_tags = style_search.find_external_import_links(html_tree)
css_link_tags = style_search.find_external_stylesheets(html_tree)
icon_link_tags = style_search.find_external_icons(html_tree)
#img_tags = style_search.find_imgs(html_tree)
src_tags = style_search.find_srcs(html_tree)
url_meta_tags = style_search.find_meta_urls(html_tree)
# Download all static URLs referenced by <script> and <link> tags
# Download all static URLs sourced by <img> tags
########## script_url_iter = (script_search.pick_src(it) for it in script_tags)
#####all_link_url_iter = (style_search.pick_href(it) for it in all_link_tags)
css_link_url_iter = (style_search.pick_href(it) for it in css_link_tags)
icon_link_url_iter = (style_search.pick_href(it) for it in icon_link_tags)
url_meta_iter = (script_search.pick_url(it) for it in url_meta_tags)
src_url_iter = (script_search.pick_src(it) for it in src_tags)
#####resource_url_iter = itertools.chain(script_url_iter, all_link_url_iter, img_url_iter)
resource_url_iter = itertools.chain(css_link_url_iter, icon_link_url_iter, url_meta_iter, src_url_iter)
for resource_url in resource_url_iter:
resource_url = resource_url.strip()
if (resource_url == "about:blank") or resource_url[0:5] == "data:" or len(resource_url) == 0:
continue
#resource_resolver = deepcopy(markup_resolver)
#resource_resolver.resource_url = resource_url
#if resource_resolver.output_dir is None or resource_resolver.output_path is None:
# continue
#try:
# print ("Resource URL : " + resource_url)
# resource_resolver.output_dir.mkdir(parents=True, exist_ok=True)
#except Exception as e:
# print (e)
# continue
final_url = ResolveURL(resource_url)
if (final_url.find("data:") == 0):
###print ("Directly Imbedded URL: " + final_url)
continue
resourceName = resource_url[resource_url.rfind("/") + 1:]
try:
#urllib.request.urlretrieve(final_url, resource_resolver.output_path)
###print ("Before URL: " + resource_url + "(hostname=" + hostname + ")")
#print ("0=" + resource_url[0] + ", 1=" + resource_url[1], ", 2=" + resource_url[2] + ", 3=" + resource_url[3])
#print (str(resource_url[0] == '/') + ", " + str(resource_url[1] == '/'))
if (resource_url[0] == '/' and resource_url[1] == '/'):
resource_url = scheme + resource_url
elif (resource_url[0] == '/'):
resource_url = hostname + resource_url
elif (resource_url.find("://") < 0):
resource_url = hostname + "/" + resource_url
# resource_text = ResourceLoader.download(hostname + resource_url)
# else:
###print ("After URL: " + resource_url)
if (resource_url in pureURLs):
continue
pureURLs.append(resource_url)
#####resource_text = ResourceLoader.download(resource_url)
###print ("Final_URL: " + resourceName + " " + resource_url)
allURLs.append((level, resource_url, pageURL))
allResources.append(resourceName)
###print ("Check: " + resource_url)
if resourceName.endswith(".css") or resourceName.endswith(".html"):
###print ("Pushed: " + resource_url)
recursiveResourceList.append((level, resource_url))
count = count + 1
#print ("Final URL: " + resource_url[resource_url.rfind("/") + 1:] + ", " + final_url + " (" + resource_url + ") downloaded at")
# print (resource_resolver.output_path)
# print()
except Exception as e:
print ("FAILURE_Final_URL: " + final_url + " " + resource_url)
print (e)
failedURLs.append((level, resource_url, pageURL))
failedResources.append(resourceName)
#print ("FAILURE: Final URL: " + final_url + " (" + resource_url + ") downloading at")
#print (resource_resolver.output_path)
#print(e)
continue
#resource_text = ResourceLoader.download(resource_resolver.resource_url)
#resource_resolver.output_path.write_text(resource_text)
# Download all static URLs referenced in the stylesheets
#css_link_tags = style_search.find_external_stylesheets(html_tree)
#stylesheet_url_iter = (style_search.pick_href(it) for it in css_link_tags)
#for resource_url in stylesheet_url_iter:
#for resource_url in recursiveResourceList:
elif pageURL.endswith(".css"):
#print("CCS IN")
#resource_resolver = deepcopy(markup_resolver)
#resource_resolver.resource_url = resource_url
# print ("RESOURCE_URL::: " + resource_url)
#if resource_resolver.output_dir is None or resource_resolver.output_path is None:
# continue
#resource_text = ResourceLoader.download(pageURL)
#resource_text = ResourceLoader.download(resource_resolver.resource_url)
#resource_resolver.output_dir.mkdir(parents=True, exist_ok=True)
#resource_resolver.output_path.write_text(resource_text)
(urlDictionary, rawUrlDictionary) = CCS_Find_Resources(contents)
for resource in urlDictionary:
resource = resource.strip()
if (resource == "about:blank") or resource[0:5] == "data:" or len(resource) == 0:
continue
#resource_resolver2 = deepcopy(markup_resolver)
#resource_resolver2.resource_url = rawUrlDictionary[resource]
#print ("Resource: " + resource + "(" + resource_resolver2.resource_url + ")")
#response = requests.get(resource)
#if resource_resolver2.output_dir is None or resource_resolver2.output_path is None:
# continue
#resource_text2 = response.content #= ResourceLoader.download(resource_resolver2.resource_url)
#try:
# resource_resolver2.output_dir.mkdir(parents=True, exist_ok=True)
#except Exception as e:
# print (e)
# continue
#urllib.request.urlretrieve("https://telegram.org/../img/dropdown_1x.png", resource_resolver2.output_path)
try:
#urllib.request.urlretrieve(resource, resource_resolver2.output_path)
if (rawUrlDictionary[resource][0] == '/' and rawUrlDictionary[resource][1] == '/'):
rawUrlDictionary[resource] = scheme + rawUrlDictionary[resource]
elif (rawUrlDictionary[resource][0] == '/'):
rawUrlDictionary[resource] = hostname + rawUrlDictionary[resource]
elif(rawUrlDictionary[resource].find("://") < 0):
rawUrlDictionary[resource] = hostname + "/" + rawUrlDictionary[resource]
# resource_text = ResourceLoader.download(hostname + resource_url)
# else:
if (rawUrlDictionary[resource] in pureURLs):
continue
pureURLs.append(rawUrlDictionary[resource])
#####resource_text = ResourceLoader.download(rawUrlDictionary[resource])
#if (rawUrlDictionary[resource][0] == '/'):
# resource_text = ResourceLoader.download(hostname + rawUrlDictionary[resource])
#else:
# ResourceLoader.download(rawUrlDictionary[resource])
resourceName = rawUrlDictionary[resource][rawUrlDictionary[resource].rfind("/") + 1:]
###print ("Final_URL: " + resourceName + ", " + rawUrlDictionary[resource])
allURLs.append((level, rawUrlDictionary[resource], pageURL))
allResources.append(resourceName)
###print ("Check: " + resource_url)
if resourceName.endswith(".css") or resourceName.endswith(".html"):
###print ("Pushed: " + resource_url)
recursiveResourceList.append((level, resource_url))
#print ("Final_URL: " + rawUrlDictionary[resource][rawUrlDictionary[resource].rfind("/") + 1:] + ", " + resource + " (" + rawUrlDictionary[resource] + ") downloaded at")
#print (resource_resolver.output_path)
#print()
count = count + 1
except Exception as e:
###print ("FAILURE: Final URL: " + resource + ", " + rawUrlDictionary[resource])
failedURLs.append((level, rawUrlDictionary[resource], pageURL))
failedResources.append(resource)
#print (resource_resolver.output_path)
#print (e)
continue
#resource_resolver2.output_path.write_byte(resource_text2)
#fileTemp = open("/home/skyer/temp.png", "wb")
#fileTemp.write(resource_text2)
#fileTemp.close()
#print(resource_resolver2.output_path)
#return
isFirst = False
#print ("Level " + str(level) + " Static URLs: " + str(count - prevCount))
#level = level + 1
#levelCount.append(count - prevCount)
#prevCount = count
levelCount = [0] * (maxLevel + 1)
index = 0
for url in allURLs:
index = index + 1
levelCount[url[0]] = levelCount[url[0]] + 1
print()
print("<Static URLs>")
index = 0
for url in allURLs:
print("Static_URL (Depth=" + str(url[0]) + "): " + str(allResources[index]) + " " + str(url[1]) + " (Referer=" + str(url[2]) + ")")
index = index + 1
print()
print("<Failed Static URLs>")
index = 0
for url in failedURLs:
print("Failed_URL (level=" + str(url[0]) + "): " + str(failedResources[index]) + " " + str(url[1]) + " (Referer=" + str(url[2]) + ")")
index = index + 1
if (len(failedURLs) == 0):
print ("None")
print()
index = 0
print("<Static URLs in Each Dependency Depth>")
for eachCount in levelCount:
print("[Depth " + str(index) + "] : " + str(eachCount))
index = index + 1
print()
print("- Total Static URLs: " + str(len(allURLs)))
print("- Total Failed URLs: " + str(len(failedURLs)))
if __name__ == '__main__':
main(parse_args())
# EOF