forked from DedSecInside/TorBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.py
More file actions
258 lines (213 loc) · 6.29 KB
/
Copy pathlink.py
File metadata and controls
258 lines (213 loc) · 6.29 KB
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
"""
This module is used to create a LinkNode that can be consumued by a LinkTree
and contains useful Link methods.
"""
import yaml
import requests
import requests.exceptions
import validators
import sys
import re
from bs4 import BeautifulSoup
from .utils import multi_thread
from .color import color
import sys
def get_emails(node):
"""Finds all emails associated with node
Args:
node (LinkNode): Node used to get emails from.
Returns:
emails (list): List of emails.
"""
emails = []
response = node.response.text
mails = re.findall(r'[\w\.-]+@[\w\.-]+', response)
for email in mails:
if LinkNode.valid_email(email):
emails.append(email)
return emails
def get_links(node):
"""Finds all links associated with node
Args:
node (LinkNode): Node used to get links from.
Returns:
links (list): List of links.
"""
links = []
for child in node.children:
link = child.get('href')
if link and LinkNode.valid_link(link):
links.append(link)
return links
def get_json_data(node):
"""Finds all link title associated with node
Args:
node (LinkNode): Node used to get links from.
Returns:
titles (list): List of Titles.
"""
json = []
for child in node.children:
link = child.get('href')
title = "Not Available"
if link and LinkNode.valid_link(link):
node = LinkNode(link)
title = node.name
json.append({"link":link,"title":title})
return json
def get_content(node):
"""Identifies the content associated with the node
Args:
node (LinkNode): Node used to get the content from.
Returns:
contetnt (list): List of Contents
"""
searchvalues=[]
Listname=""
content=[]
response = node.response.text
with open(r'{}/modules/Bitcoin.yaml'.format(sys.path[0])) as file:
website_identification_docs = yaml.load_all(file, Loader=yaml.FullLoader)
for identity_name in website_identification_docs:
for name,values in identity_name.items():
searchvalues = values
Listname = name
for keywords in searchvalues:
#if(node.find_all(string=re.compile(keywords))):
if(re.findall(keywords, response)):
content.append(Listname)
break
return content
def get_images(node):
"""Finds all images associated with node.
Args:
node (LinkNode): Node used to get links from.
Returns:
links (list): List of links.
"""
links = []
for child in node.children:
link = child.get('src')
if link and LinkNode.valid_link(link):
links.append(link)
return links
def get_metadata(node):
"""Collect response headers.
Args:
node (LinkNode): Node used to get metadata from.
Returns:
metadata (dict): Dictionary with metadata.
"""
return node.response.headers
class LinkNode:
"""Represents link node in a link tree."""
def __init__(self, link):
"""Initialises LinkNode object.
Args:
link (str): URL used to initialise node.
"""
# If link has invalid form, throw an error
if not self.valid_link(link):
raise ValueError("Invalid link format.")
self._children = []
self._emails = []
self._links = []
self._images = []
self._json_data = []
self._content = []
self._metadata = {}
# Attempts to connect to link, throws an error if link is unreachable
try:
self.response = requests.get(link)
except (requests.exceptions.ChunkedEncodingError,
requests.exceptions.HTTPError,
requests.exceptions.ConnectionError,
ConnectionError) as err:
print("Error connecting to Tor:", err)
sys.exit(1)
self._node = BeautifulSoup(self.response.text, 'html.parser')
self.uri = link
if not self._node.title:
self.name = "TITLE NOT FOUND"
self.status = color(link, 'yellow')
else:
self.name = self._node.title.string
self.status = color(link, 'green')
@property
def emails(self):
"""
Getter for node emails
"""
if not self._emails:
self._emails = get_emails(self)
return self._emails
@property
def content(self):
"""
Getter for website identification
"""
if not self._content:
self._content = get_content(self)
return self._content
@property
def json_data(self):
"""
Getter for node titles
"""
if not self._json_data:
self._json_data = get_json_data(self)
return self._json_data
@property
def links(self):
"""
Getter for node links
"""
if not self._links:
self._links = get_links(self)
return self._links
@property
def images(self):
"""
Getter for node images
"""
if not self._images:
self._images = get_images(self)
return self._images
@property
def children(self):
"""
Getter for node children
"""
if not self._children:
self._children = self._node.find_all('a')
return self._children
@property
def metadata(self):
"""
Getter for node metadata
"""
if not self._metadata:
self._metadata = get_metadata(self)
return self._metadata
@staticmethod
def valid_email(email):
"""Static method used to validate emails.
Args:
email (str): Email string to be validated.
Returns:
(bool): True if email string is valid, else false.
"""
if validators.email(email):
return True
return False
@staticmethod
def valid_link(link):
"""Static method used to validate links
Args:
link (str): URL string to be validated.
Returns:
(bool): True if URL string is valid, else false.
"""
if validators.url(link):
return True
return False