this is based on calsyslab project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
9.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. __author__ = 'DarkWeb'
  2. '''
  3. Quest Market Crawler (Selenium)
  4. '''
  5. from selenium import webdriver
  6. from selenium.common.exceptions import NoSuchElementException
  7. from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
  8. from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
  9. from selenium.webdriver.firefox.service import Service
  10. from selenium.webdriver.common.by import By
  11. from selenium.webdriver.support import expected_conditions as EC
  12. from selenium.webdriver.support.ui import WebDriverWait
  13. from PIL import Image
  14. import urllib.parse as urlparse
  15. import os, re, time
  16. from datetime import date
  17. import subprocess
  18. from bs4 import BeautifulSoup
  19. from MarketPlaces.Initialization.prepare_parser import new_parse
  20. from MarketPlaces.Quest.parser import quest_links_parser
  21. from MarketPlaces.Utilities.utilities import cleanHTML
  22. counter = 1
  23. baseURL = 'http://questxwvkwvsw2qgeeljz4fbv6cq2kbmapo7tw5heu4nng2ufgykapid.onion'
  24. # Opens Tor Browser, crawls the website
  25. def startCrawling():
  26. marketName = getMarketName()
  27. driver = getAccess()
  28. if driver != 'down':
  29. try:
  30. login(driver)
  31. crawlForum(driver)
  32. except Exception as e:
  33. print(driver.current_url, e)
  34. closeDriver(driver)
  35. new_parse(marketName, False)
  36. # Login using premade account credentials and do login captcha manually
  37. def login(driver):
  38. #wait for login page
  39. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  40. (By.XPATH, "/html/body/div[2]/div/div[3]/div/div/div/div[1]/form/div[4]/div/div/button")))
  41. #entering username and password into input boxes
  42. usernameBox = driver.find_element(by=By.XPATH, value='//*[@id="username"]')
  43. #Username here
  44. usernameBox.send_keys('blabri')
  45. passwordBox = driver.find_element(by=By.XPATH, value='//*[@id="password"]')
  46. #Password here
  47. passwordBox.send_keys('fishowal')
  48. '''
  49. # wait for captcha page show up
  50. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  51. (By.XPATH, "/html/body/div[2]/div/div[3]/div/div/div/div[1]/form/div[3]/div/img")))
  52. # save captcha to local
  53. driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div[3]/div/div/div/div[1]/form/div[3]/div/img').screenshot(
  54. r'..\Quest\captcha.png')
  55. # This method will show image in any image viewer
  56. im = Image.open(r'..\Quest\captcha.png')
  57. im.show()
  58. # wait until input space show up
  59. inputBox = driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div[3]/div/div/div/div[1]/form/div[3]/input')
  60. # ask user input captcha solution in terminal
  61. userIn = input("Enter solution: ")
  62. # send user solution into the input space
  63. inputBox.send_keys(userIn)
  64. # click the verify(submit) button
  65. driver.find_element(by=By.XPATH, value="/html/body/div[2]/div/div[3]/div/div/div/div[1]/form/div[4]/div/div/button").click()
  66. '''
  67. input("Press ENTER when CAPTCHA is completed\n")
  68. # wait for listing page show up (This Xpath may need to change based on different seed url)
  69. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  70. (By.XPATH, '/html/body/div[5]/div/div/div/span')))
  71. # Returns the name of the website
  72. def getMarketName():
  73. name = 'Quest'
  74. return name
  75. # Return the link of the website
  76. def getFixedURL():
  77. url = 'http://questxwvkwvsw2qgeeljz4fbv6cq2kbmapo7tw5heu4nng2ufgykapid.onion'
  78. return url
  79. # Closes Tor Browser
  80. def closeDriver(driver):
  81. # global pid
  82. # os.system("taskkill /pid " + str(pro.pid))
  83. # os.system("taskkill /t /f /im tor.exe")
  84. print('Closing Tor...')
  85. driver.close()
  86. time.sleep(3)
  87. return
  88. # Creates FireFox 'driver' and configure its 'Profile'
  89. # to use Tor proxy and socket
  90. def createFFDriver():
  91. from MarketPlaces.Initialization.markets_mining import config
  92. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  93. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  94. ff_prof.set_preference("places.history.enabled", False)
  95. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  96. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  97. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  98. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  99. ff_prof.set_preference("signon.rememberSignons", False)
  100. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  101. ff_prof.set_preference("network.dns.disablePrefetch", True)
  102. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  103. ff_prof.set_preference("permissions.default.image", 3)
  104. ff_prof.set_preference("browser.download.folderList", 2)
  105. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  106. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  107. ff_prof.set_preference('network.proxy.type', 1)
  108. ff_prof.set_preference("network.proxy.socks_version", 5)
  109. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  110. ff_prof.set_preference('network.proxy.socks_port', 9150)
  111. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  112. ff_prof.set_preference("javascript.enabled", False)
  113. ff_prof.update_preferences()
  114. service = Service(config.get('TOR', 'geckodriver_path'))
  115. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  116. driver.maximize_window()
  117. return driver
  118. def getAccess():
  119. url = getFixedURL()
  120. driver = createFFDriver()
  121. try:
  122. driver.get(url)
  123. return driver
  124. except:
  125. driver.close()
  126. return 'down'
  127. # Saves the crawled html page
  128. def savePage(driver, page, url):
  129. cleanPage = cleanHTML(driver, page)
  130. filePath = getFullPathName(url)
  131. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  132. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  133. return
  134. # Gets the full path of the page to be saved along with its appropriate file name
  135. def getFullPathName(url):
  136. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  137. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMKTName() + "/HTML_Pages")
  138. fileName = getNameFromURL(url)
  139. if isDescriptionLink(url):
  140. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  141. else:
  142. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  143. return fullPath
  144. # Creates the file name from passed URL
  145. def getNameFromURL(url):
  146. global counter
  147. name = ''.join(e for e in url if e.isalnum())
  148. if name == '':
  149. name = str(counter)
  150. counter = counter + 1
  151. return name
  152. def getInterestedLinks():
  153. links = []
  154. # # Digital - Services
  155. # links.append('http://questxwvkwvsw2qgeeljz4fbv6cq2kbmapo7tw5heu4nng2ufgykapid.onion/category/8ae67900-22ed-11ec-a710-31f963ce8d35')
  156. # # Digital - Software
  157. # links.append('http://questxwvkwvsw2qgeeljz4fbv6cq2kbmapo7tw5heu4nng2ufgykapid.onion/category/92809300-22ed-11ec-b143-af312e1dab77')
  158. # # Digital - Tutorials
  159. # links.append('http://questxwvkwvsw2qgeeljz4fbv6cq2kbmapo7tw5heu4nng2ufgykapid.onion/category/9d1592b0-22ed-11ec-b82d-c3d2878a8716')
  160. # # Digital - Malware
  161. # links.append('http://questxwvkwvsw2qgeeljz4fbv6cq2kbmapo7tw5heu4nng2ufgykapid.onion/category/a35bae90-22ed-11ec-ad2e-410f5a5339b5')
  162. # # Digital - Hacking
  163. # links.append('http://questxwvkwvsw2qgeeljz4fbv6cq2kbmapo7tw5heu4nng2ufgykapid.onion/category/b4252cf0-22ed-11ec-8032-751549438ed5')
  164. # Digital - Exploits
  165. links.append('http://questxwvkwvsw2qgeeljz4fbv6cq2kbmapo7tw5heu4nng2ufgykapid.onion/category/c0c3ac60-22ed-11ec-9e97-41cd1912fdee')
  166. return links
  167. def crawlForum(driver):
  168. print("Crawling the Quest market")
  169. linksToCrawl = getInterestedLinks()
  170. i = 0
  171. while i < len(linksToCrawl):
  172. link = linksToCrawl[i]
  173. print('Crawling :', link)
  174. try:
  175. has_next_page = True
  176. count = 0
  177. while has_next_page:
  178. try:
  179. driver.get(link)
  180. except:
  181. driver.refresh()
  182. html = driver.page_source
  183. savePage(driver, html, link)
  184. list = productPages(html)
  185. for item in list:
  186. itemURL = urlparse.urljoin(baseURL, str(item))
  187. try:
  188. driver.get(itemURL)
  189. except:
  190. driver.refresh()
  191. savePage(driver, driver.page_source, item)
  192. driver.back()
  193. # comment out
  194. break
  195. # comment out
  196. if count == 1:
  197. break
  198. try:
  199. nav = driver.find_element(by=By.XPATH, value='/html/body/div[6]/nav')
  200. li = nav.find_elements(By.TAG_NAME, 'li')
  201. a = li[-1].find_element(By.TAG_NAME, 'a')
  202. link = a.get_attribute('href')
  203. if link == "":
  204. raise NoSuchElementException
  205. count += 1
  206. except NoSuchElementException:
  207. has_next_page = False
  208. except Exception as e:
  209. print(link, e)
  210. i += 1
  211. input("Crawling Quest market done sucessfully. Press ENTER to continue\n")
  212. # Returns 'True' if the link is Topic link
  213. def isDescriptionLink(url):
  214. if 'product' in url:
  215. return True
  216. return False
  217. # Returns True if the link is a listingPage link
  218. def isListingLink(url):
  219. if 'category' in url:
  220. return True
  221. return False
  222. # calling the parser to define the links
  223. def productPages(html):
  224. soup = BeautifulSoup(html, "html.parser")
  225. return quest_links_parser(soup)
  226. def crawler():
  227. startCrawling()
  228. # print("Crawling and Parsing BestCardingWorld .... DONE!")