Skip to content

Commit b0e8340

Browse files
committed
Fixed -e flag
To use dash -e flag, pass url name like www.url.com and it will try to establish an https connection than http if the secure conneciton fails. If both fail then an error message is printed. If the connection is successful than we search for the onion domain name and the others that were passed with the flag.
1 parent 980d661 commit b0e8340

3 files changed

Lines changed: 91 additions & 49 deletions

File tree

modules/getweblinks.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,40 @@
77

88

99
def valid_url(url):
10-
"""Checks if url is valid using regular expression matching
10+
"""Checks for any valid url using regular expression matching
1111
12-
Matches all possible url patterns with the url that is passed and returns
13-
True if it is a url and returns False if it is not.
12+
Matches all possible url patterns with the url that is passed and
13+
returns True if it is a url and returns False if it is not.
1414
1515
Args:
1616
url: string representing url to be checked
1717
1818
Returns:
19-
bool: True if valid url format, False if not
19+
bool: True if valid url format and False if not
2020
"""
21-
regex = re.compile(
22-
r'^(?:http|ftp)s?://' # http:// or https://
23-
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
24-
r'localhost|' # localhost...
25-
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
26-
r'(?::\d+)?' # optional port
27-
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
2821

22+
pattern = r"^https?:\/\/(www\.)?([a-z,A-Z,0-9]*)\.([a-z, A-Z]+)(.*)"
23+
regex = re.compile(pattern)
2924
if regex.match(url):
3025
return True
26+
return False
27+
28+
29+
def valid_onion_url(url):
30+
"""Checks for valid onion url using regular expression matching
31+
32+
Only matches onion urls
3133
34+
Args:
35+
url: string representing url to be checked
36+
37+
Returns:
38+
bool: True if valid onion url format, False if not
39+
"""
40+
pattern = r"^https?:\/\/(www\.)?([a-z,A-Z,0-9]*)\.([a-z,A-Z]+)(.*)"
41+
regex = re.compile(pattern)
42+
if regex.match(url):
43+
return True
3244
return False
3345

3446

@@ -68,24 +80,25 @@ def getLinks(soup, ext=False, live=False):
6880
b_colors = Bcolors()
6981
if isinstance(soup, BeautifulSoup):
7082
websites = []
83+
7184
links = soup.find_all('a')
7285
for ref in links:
7386
url = ref.get('href')
74-
if url and valid_url(url):
75-
if ext:
76-
for extension in ext:
77-
if not url.endswith(extension):
78-
continue
79-
websites.append(url)
87+
if ext:
88+
if url and valid_url(url):
89+
websites.append(url)
90+
else:
91+
if url and valid_onion_url(url):
92+
websites.append(url)
8093

8194
"""Pretty print output as below"""
8295
print(''.join((b_colors.OKGREEN,
8396
'Websites Found - ', b_colors.ENDC, str(len(websites)))))
8497
print('------------------------------------')
85-
if live:
86-
for link in websites:
87-
print(next(get_link_status(link, b_colors)))
98+
99+
for link in websites:
100+
print(next(get_link_status(link, b_colors)))
101+
return websites
102+
88103
else:
89104
raise('Method parameter is not of instance BeautifulSoup')
90-
91-
return websites

modules/pagereader.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,52 @@
22

33
from bs4 import BeautifulSoup
44
from modules.bcolors import Bcolors
5+
from requests.exceptions import ConnectionError, HTTPError
6+
from sys import exit
57

68

7-
def readPage(site):
9+
def connection_msg(site):
10+
yield "Attempting to connect to {site}".format(site=site)
11+
12+
13+
def readPage(site, extension=False):
814
headers = {'User-Agent':
915
'TorBot - Onion crawler | www.github.com/DedSecInside/TorBot'}
1016
attempts_left = 3
11-
12-
while (attempts_left):
17+
err = " "
18+
while attempts_left:
1319
try:
14-
response = requests.get(site, headers=headers)
15-
page = BeautifulSoup(response.text, 'html.parser')
16-
return page
17-
except Exception as e:
20+
if not extension:
21+
print(next(connection_msg(site)))
22+
response = requests.get(site, headers=headers)
23+
print("Connection successful.")
24+
page = BeautifulSoup(response.text, 'html.parser')
25+
return page
26+
if extension and attempts_left == 3:
27+
print(next(connection_msg('https://'+site)))
28+
response = requests.get('https://'+site, headers=headers)
29+
print("Connection successful.")
30+
page = BeautifulSoup(response.text, 'html.parser')
31+
return page
32+
if extension and attempts_left == 2:
33+
print(next(connection_msg('http://'+site)))
34+
response = requests.get('http://'+site, headers=headers)
35+
print("Connection successful.")
36+
page = BeautifulSoup(response.text, 'html.parser')
37+
return page
38+
if extension and attempts_left == 1:
39+
msg = ''.join(("There has been an {err} while attempting to ",
40+
"connect to {site}.")).format(err=err, site=site)
41+
exit(msg)
42+
43+
except (HTTPError, ConnectionError) as e:
1844
attempts_left -= 1
19-
error = e
45+
err = e
2046

21-
raise error
47+
if err == HTTPError:
48+
raise("There has been an HTTP error after three attempts.")
49+
if err == ConnectionError:
50+
raise("There has been a connection error after three attempts.")
2251

2352

2453
def get_ip():

torBot.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def header():
8686
{FAIL} + {BOLD}
8787
__ ____ ____ __ ______
8888
/ /_/ __ \/ __ \/ /_ ____/_ __/
89-
/ __/ / / / /_/ / __ \/ __ \/ /
90-
/ /_/ /_/ / _, _/ /_/ / /_/ / /
89+
/ __/ / / / /_/ / __ \/ __ \/ /
90+
/ /_/ /_/ / _, _/ /_/ / /_/ / /
9191
\__/\____/_/ |_/_____/\____/_/ V{VERSION}
9292
{FAIL} + {On_Black}
9393
#######################################################
@@ -98,13 +98,14 @@ def header():
9898
{FAIL} + "LICENSE: GNU Public License" + {END}""".format(
9999
D3DSEC=D3DSEC, INS1DE=INS1DE, FAIL=b_color.FAIL,
100100
BOLD=b_color.BOLD, VERSION=__VERSION, END=b_color.ENDC,
101-
On_Black=b_color.On_Black,WHITE=b_color.WHITE
101+
On_Black=b_color.On_Black, WHITE=b_color.WHITE
102102
)
103103
print(header)
104104

105105

106-
def main():
107-
connect(LOCALHOST, PORT)
106+
def main(conn=False):
107+
if conn:
108+
connect(LOCALHOST, PORT)
108109
parser = argparse.ArgumentParser()
109110
parser.add_argument("-v", "--version",
110111
action="store_true",
@@ -137,6 +138,13 @@ def main():
137138
"scanned site, (very slow)")))
138139
args = parser.parse_args()
139140

141+
link = args.url
142+
if '.onion' in link:
143+
pass
144+
else:
145+
print("You must use -e/--extension to use domains other than .onion.")
146+
exit()
147+
140148
# If flag is -v, --update, -q/--quiet then user only runs that operation
141149
# because these are single flags only
142150
if args.version:
@@ -147,13 +155,11 @@ def main():
147155
exit()
148156
if not args.quiet:
149157
header()
150-
151158
# If url flag is set then check for accompanying flag set. Only one
152159
# additional flag can be set with -u/--url flag
153160
if args.url:
154161
print("Tor IP Address :", pagereader.get_ip())
155-
link = args.url
156-
html_content = pagereader.readPage(link)
162+
html_content = pagereader.readPage(link, args.extension)
157163
# -m/--mail
158164
if args.mail:
159165
emails = getemails.getMails(html_content)
@@ -166,15 +172,9 @@ def main():
166172
if args.save:
167173
print('Nothing to save.\n')
168174
else:
169-
if args.live:
170-
live = True
171-
else:
172-
live = False
173-
if args.extension:
174-
ext = True
175-
else:
176-
ext = False
177-
links = getweblinks.getLinks(soup=html_content, live=live, ext=ext)
175+
links = getweblinks.getLinks(soup=html_content,
176+
live=args.live,
177+
ext=args.extension)
178178
if args.save:
179179
savefile.saveJson("Links", links)
180180
else:
@@ -186,7 +186,7 @@ def main():
186186
if __name__ == '__main__':
187187

188188
try:
189-
main()
189+
main(conn=True)
190190

191191
except KeyboardInterrupt:
192192
print("Interrupt received! Exiting cleanly...")

0 commit comments

Comments
 (0)