-
Notifications
You must be signed in to change notification settings - Fork 16
/
search_dark_web.py
215 lines (194 loc) · 8.03 KB
/
search_dark_web.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
from time import sleep
from interruptingcow import timeout
import sys
from bs4 import BeautifulSoup
import requests
import os
# -------------------- REQUEST SESSION PROXIES --------------------
def get_tor_session():
session = requests.session()
# Tor uses the 9050 port as the default socks port
session.proxies = {'http': 'socks5h://127.0.0.1:9050','https': 'socks5h://127.0.0.1:9050'}
return session
def crawl(option, deeplinks, link, intexts, session):
error=0
if option is "default":
length_of_web_links_to_crawl = len(deeplinks)
iterations = 0
while len(deeplinks) <= number_results or length_of_web_links_to_crawl <= iterations:
try:
with timeout(10):
crawl = session.get(deeplinks[iterations])
except:
error=1
if not error:
crawl = crawl.text
try:
soup = BeautifulSoup(crawl, "lxml")
except:
print("Error creating 'soup' object")
os.system("sudo service tor stop")
exit()
for a in soup.find_all('a', href=True):
if len(deeplinks) >= number_results:
print(" \033[0;32m LINKS COLLECTED!\033[0m")
os.system("sudo service tor stop")
exit()
darklink = isValidOnionAdress(deeplinks[iterations],session)
if darklink:
if not darklink in deeplinks:
if intexts in crawl or intexts == "":
print(darklink)
else:
print("valid link, but have not '" + intexts + "' inside: \033[0;31m" + darklink + "\033[0m")
iterations+=1
if option is "all":
try:
with timeout(10):
crawl = session.get(link)
except:
error = 1
if not error:
crawl = crawl.text
try:
soup = BeautifulSoup(crawl, "lxml")
except:
print("Error creating 'soup' object")
os.system("sudo service tor stop")
exit()
print("Crawling from : " + "[\033[0;31m" + link + "\033[0m]")
for a in soup.find_all('a', href=True):
if len(deeplinks) >= number_results:
print(" \033[0;32m LINKS COLLECTED!\033[0m")
os.system("sudo service tor stop")
exit()
darklink = isValidOnionAdress(a['href'],session)
if darklink:
if not darklink in deeplinks:
if intexts in crawl or intexts == "":
deeplinks.append(darklink)
print(darklink)
else:
print("valid link, but have not '" + intexts + "' inside: \033[0;31m" + darklink + "\033[0m")
else:
print("Skipping, takes to long")
def isValidOnionAdress(darklink,session):
if not ".onion" in darklink: # if there's not ".onion" in href
return False
if "http://" in darklink: # if we are here, the link contains a .onion so, lets 'clean' it
isvalid = darklink.split("http://")[1].split(".onion")[0]
isvalid = "http://" + isvalid + ".onion"
print(isvalid)
try:
with timeout(10):
maybevalid = session.get(isvalid) # can we connect to it?
except:
return False
if maybevalid.status_code is not 200:
return False
else:
return isvalid
def search(crawling, intexts, session):
darklinks = []
print("Searching. . . ")
#process first 5 pages
for page in range(1,5):
#http://underdj5ziov3ic7.onion/search/bitcoin/pg
#http://www.xmh57jrzrnw6insl.onion/4a1f6b371c/search.cgi?s=DRP&q=bitcoin
search_query = "http://underdj5ziov3ic7.onion/search/"+search_string+"/pg/"+str(page)
#search_query = "http://www.xmh57jrzrnw6insl.onion/4a1f6b371c/search.cgi?s=DRP&q="+search_string+"&np="+str(page)
print("Search query",search_query)
try:
content = session.get(search_query)
content = content.text
except:
print("\nError connecting to server")
exit()
try:
soup = BeautifulSoup(content, "lxml")
except:
print("\nError creating 'soup' object")
os.system("sudo service tor stop")
exit()
print(" \033[0;32m [OK]\033[0m")
print("Checking links ")
print(soup)
for a in soup.find_all('a', href=True): # for each href in browser response
if len(darklinks) >= number_results: # if reached number of links
print(len(darklinks))
print("SEARCH COMPLETE" + "\033[0;32m [OK]\033[0m")
os.system("sudo service tor stop")
exit()
darklink = isValidOnionAdress(a['href'],session)
darklinkd = True
try:
contain = session.get(darklink)
contain = contain.text
except:
darklinkd = False
if darklink and darklinkd: # if valid
if not darklink in darklinks: # if not present in list
if intexts in contain:
print(darklink)
darklinks.append(darklink) # add it
if "all" in crawling:
crawl("all", darklinks, darklink, intexts,session)
else:
print("valid link, but have not '" + intexts + "' inside: \033[0;31m" + darklink + "\033[0m")
if "none" in crawling:
print("Search completed.")
exit()
print("Not enought links in browser, crawling...")
if darklinks:
if "default" in crawling:
crawl("default", darklinks, darklink, intexts,session)
else:
print("Not enought links in browser, but crawl disabled")
os.system("sudo service tor stop")
exit()
else:
print("0 links!, cant crawl...")
os.system("sudo service tor stop")
exit()
def torproxy(session):
print("Checking Tor instance")
try:
print(session.get("http://httpbin.org/ip").text)
except Exception as exception:
print(" [\033[0;31mNot connected\033[0m]",exception)
print("Starting Tor instance ")
os.system("service tor start")
sleep(8)
print(" \033[0;32m [OK]\033[0m")
print("Checking Tor proxy ")
try:
print(session.get("http://httpbin.org/ip").text)
except Exception as exception:
print(" => [\033[0;31mERROR\033[0m] proxy is refusing connections",exception)
os.system("sudo service tor stop")
exit()
print(" \033[0;32m [OK]\033[0m")
# -------------------- MAIN PROGRAM --------------------
if len(sys.argv) not in [4,5] or sys.argv[3] not in ["all", "none", "default"]:
print("search_dark_web.py SEARCH NUMBER_OF_RESULTS crawl_options intext")
print("Crawl Options:")
print(" all) crawl each link")
print(" none) dont crawl")
print(" default) crawl if not enough links")
exit()
if __name__ == "__main__":
try:
session = get_tor_session()
torproxy(session) # set up tor proxy
search_string = sys.argv[1]
number_results = int(sys.argv[2])
crawld = sys.argv[3]
if len(sys.argv) is 5:
intext = sys.argv[4]
else:
intext = ""
search(crawld, intext,session)
except KeyboardInterrupt:
print("\nExiting. . .")
os.system("sudo service tor stop")
exit()