-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagHunter.py
167 lines (128 loc) · 4.97 KB
/
TagHunter.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
# -*- coding: utf-8 -*-
"""TCC.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1yhYgEBFBEDGmrnUFXyZ_DSSO3RWakKPG
"""
from bs4 import BeautifulSoup
import requests
import json
import datetime
import pymysql
import threading
import random
"""
TAG HUNTER
@params str url
@params str sitename
@params list tagContent[[str(tag), dict(features), (optional)str(parameter to get the content)], [str(tag), dict(features), (optional)str(parameter to get the content)], [str(tag), dict(features), (optional)str(parameter to get the content)]]
"""
class TagHunter:
def __init__(self, url, sitename, tagContent):
self.sitename = sitename
self.url = []
self.urlInicial = url
self.forUrls = []
self.tagContent = tagContent
self.url.append(url)
self.pages = []
self.content = []
self.dirtyContent = []
self.urls_ja_escaneadas = []
self.confirmPage = True
self.confirmAnalyze = True
self.confirmUrls = True
self.main()
def main(self):
while self.confirmPage == True or self.confirmAnalyze == True:
t1 = threading.Thread(target=self.GetPages)
t2 = threading.Thread(target=self.GetPages)
t3 = threading.Thread(target=self.Analyze)
t1.start()
t2.start()
t3.start()
print(len(self.url))
t1.join()
t2.join()
t3.join()
self.confirmClean = self.CleanContent()
def Analyze(self):
try:
if len(self.pages) != 0:
pageForAnalyze = self.pages.pop()
else:
return False
page = pageForAnalyze[1]
tempContent = []
for z in self.tagContent:
result = page.find_all(z[0])
for y in result:
tempContent.append(y)
result= [pageForAnalyze[0], tempContent]
self.dirtyContent.append(result)
except KeyError:
return False
else:
return True
def CleanContent(self):
while True:
try:
for x in self.tagContent:
for key,val in x[1].items():
tempContent = []
result = []
for y in self.dirtyContent:
for z in y[1]:
if z.get(key) != None:
if val in z.get(key):
tempContent.append(z)
temploc = [y[0], tempContent]
if temploc not in result:
result.append(temploc)
if len(x) == 3:
for x in len(0, result):
for z in range(0, len(result[x][1])):
result[x][1].append(result[x][1][z].get(x[2]))
self.content.append(result)
return True
except KeyError:
return False
else:
pass
return True
def GetPages(self):
try:
tempUrl = []
tempPages = []
nUrl = len(self.url)
if nUrl != 0:
if nUrl != 1:
tempUrl = self.url.pop(random.randrange(0, nUrl))
else:
tempUrl = self.url.pop()
else:
self.confirmPage = False
tempPages = []
if tempUrl not in self.urls_ja_escaneadas:
self.urls_ja_escaneadas.append(tempUrl)
resposta = requests.get(tempUrl)
if resposta.status_code == 200:
tempPages.append(tempUrl)
tempPages.append(BeautifulSoup(resposta.content, "html.parser"))
self.pages.append(tempPages)
self.url = list(set(self.url))
bs = BeautifulSoup(resposta.content, "html.parser")
links = bs.find_all('a')
links = list(set(links))
for link in links:
if link["href"].startswith(self.urlInicial) and link["href"] not in self.urls_ja_escaneadas and link["href"] not in self.urls_ja_escaneadas and ".html" not in tempUrl and ".php" not in tempUrl:
self.url.append(link["href"])
elif link["href"].startswith("/") and tempUrl+link["href"] != tempUrl+"/" and tempUrl+link["href"] not in self.urls_ja_escaneadas and link["href"] not in self.urls_ja_escaneadas and ".html" not in tempUrl and ".php" not in tempUrl:
self.url.append(tempUrl + link["href"])
elif link["href"].startswith(tempUrl) and tempUrl+link["href"] != tempUrl+"/" and link["href"] not in self.urls_ja_escaneadas and link["href"] not in self.urls_ja_escaneadas and ".html" not in tempUrl and ".php" not in tempUrl:
self.url.append(link["href"])
self.confirmPage = True
except KeyError:
self.confirmPage = False
queue = [["https://araquari.ifc.edu.br/","araquari.ifc",[["div", {"class":"page-subheader"}]]], ["https://ctf-br.org","ctf-br.org",[["h1",{"h1":"page-introduce-title"}]]]]
print(TagHunter("https://araquari.ifc.edu.br/","araquari.ifc",[["div", {"class":"page-subheader"}]]).content)