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.

256 lines
7.9 KiB

  1. __author__ = 'DarkWeb'
  2. '''
  3. DarkBay Marketplace 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.support.ui import WebDriverWait
  11. from selenium.webdriver.support.ui import Select
  12. from selenium.webdriver.support import expected_conditions as EC
  13. from selenium.webdriver.common.by import By
  14. from PIL import Image
  15. import urllib.parse as urlparse
  16. import os, re, time
  17. from datetime import date
  18. import subprocess
  19. import configparser
  20. from bs4 import BeautifulSoup
  21. from MarketPlaces.Initialization.prepare_parser import new_parse
  22. from MarketPlaces.DarkBay.parser import darkbay_links_parser
  23. from MarketPlaces.Utilities.utilities import cleanHTML
  24. counter = 1
  25. baseURL = 'http://darkbayx7a4sosoo4hqvoljqelgkusjlrqmt237ls6hndbplmel55oad.onion/'
  26. def startCrawling():
  27. mktName = getMKTName()
  28. # driver = getAccess()
  29. #
  30. # if driver != 'down':
  31. # try:
  32. # login(driver)
  33. # crawlForum(driver)
  34. # except Exception as e:
  35. # print(driver.current_url, e)
  36. # closeDriver(driver)
  37. new_parse(mktName, baseURL, True)
  38. # Returns the name of the website
  39. def getMKTName():
  40. name = 'DarkBay'
  41. return name
  42. # Return the base link of the website
  43. def getFixedURL():
  44. url = 'http://darkbayx7a4sosoo4hqvoljqelgkusjlrqmt237ls6hndbplmel55oad.onion/'
  45. return url
  46. # Closes Tor Browser
  47. def closeDriver(driver):
  48. # global pid
  49. # os.system("taskkill /pid " + str(pro.pid))
  50. # os.system("taskkill /t /f /im tor.exe")
  51. print('Closing Tor...')
  52. driver.close()
  53. time.sleep(3)
  54. return
  55. # Creates FireFox 'driver' and configure its 'Profile'
  56. # to use Tor proxy and socket
  57. def createFFDriver():
  58. from MarketPlaces.Initialization.markets_mining import config
  59. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  60. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  61. ff_prof.set_preference("places.history.enabled", False)
  62. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  63. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  64. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  65. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  66. ff_prof.set_preference("signon.rememberSignons", False)
  67. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  68. ff_prof.set_preference("network.dns.disablePrefetch", True)
  69. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  70. ff_prof.set_preference("permissions.default.image", 3)
  71. ff_prof.set_preference("browser.download.folderList", 2)
  72. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  73. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  74. ff_prof.set_preference('network.proxy.type', 1)
  75. ff_prof.set_preference("network.proxy.socks_version", 5)
  76. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  77. ff_prof.set_preference('network.proxy.socks_port', 9150)
  78. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  79. ff_prof.set_preference("javascript.enabled", False)
  80. ff_prof.update_preferences()
  81. service = Service(config.get('TOR', 'geckodriver_path'))
  82. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  83. driver.maximize_window()
  84. return driver
  85. #the driver 'gets' the url, attempting to get on the site, if it can't access return 'down'
  86. def getAccess():
  87. url = getFixedURL()
  88. driver = createFFDriver()
  89. try:
  90. driver.get(url)
  91. return driver
  92. except:
  93. driver.close()
  94. return 'down'
  95. def login(driver):
  96. # input("Press ENTER when CAPTCHA is complete and login page has loaded\n")
  97. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  98. (By.XPATH, '/html/body/div/nav/ul')))
  99. def savePage(driver, page, url):
  100. cleanPage = cleanHTML(driver, page)
  101. filePath = getFullPathName(url)
  102. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  103. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  104. return
  105. def getFullPathName(url):
  106. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  107. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMKTName() + "/HTML_Pages")
  108. fileName = getNameFromURL(url)
  109. if isDescriptionLink(url):
  110. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  111. else:
  112. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  113. return fullPath
  114. def getMKTName() -> str:
  115. name = 'DarkBay'
  116. return name
  117. def getNameFromURL(url):
  118. global counter
  119. name = ''.join(e for e in url if e.isalnum())
  120. if name == '':
  121. name = str(counter)
  122. counter = counter + 1
  123. return name
  124. def getInterestedLinks():
  125. links = []
  126. # # database
  127. # links.append('http://darkbayx7a4sosoo4hqvoljqelgkusjlrqmt237ls6hndbplmel55oad.onion/hacking/databases')
  128. # scripts
  129. links.append('http://darkbayx7a4sosoo4hqvoljqelgkusjlrqmt237ls6hndbplmel55oad.onion/hacking/scripts')
  130. # malware
  131. links.append('http://darkbayx7a4sosoo4hqvoljqelgkusjlrqmt237ls6hndbplmel55oad.onion/hacking/malware')
  132. # # services
  133. # links.append('http://darkbayx7a4sosoo4hqvoljqelgkusjlrqmt237ls6hndbplmel55oad.onion/hacking/services')
  134. # ebooks
  135. # links.append('http://darkbayx7a4sosoo4hqvoljqelgkusjlrqmt237ls6hndbplmel55oad.onion/hacking/books')
  136. return links
  137. def crawlForum(driver):
  138. print("Crawling the DarkBay market")
  139. linksToCrawl = getInterestedLinks()
  140. i = 0
  141. while i < len(linksToCrawl):
  142. link = linksToCrawl[i]
  143. print('Crawling :', link)
  144. try:
  145. has_next_page = True
  146. count = 0
  147. while has_next_page:
  148. try:
  149. driver.get(link)
  150. except:
  151. driver.refresh()
  152. html = driver.page_source
  153. savePage(driver, html, link)
  154. list = productPages(html)
  155. for item in list:
  156. itemURL = urlparse.urljoin(baseURL, str(item))
  157. try:
  158. driver.get(itemURL)
  159. except:
  160. driver.refresh()
  161. savePage(driver, driver.page_source, item)
  162. driver.back()
  163. # # comment out
  164. # break
  165. #
  166. # # comment out
  167. # if count == 1:
  168. # break
  169. try:
  170. link = driver.find_element(by=By.CLASS_NAME, value='pagination')
  171. link = link.find_element(by=By.TAG_NAME, value='span')
  172. link = link.find_element(by=By.XPATH, value='./following-sibling::a')
  173. link = link.get_attribute('href')
  174. if link == "":
  175. print('no next page')
  176. raise NoSuchElementException
  177. count += 1
  178. except NoSuchElementException:
  179. has_next_page = False
  180. except Exception as e:
  181. print(link, e)
  182. i += 1
  183. print("Crawling the DarkBay market done.")
  184. # Returns 'True' if the link is Topic link, may need to change for every website
  185. def isDescriptionLink(url):
  186. if 'offer' in url:
  187. return True
  188. return False
  189. # Returns True if the link is a listingPage link, may need to change for every website
  190. def isListingLink(url):
  191. if 'offer' not in url:
  192. return True
  193. return False
  194. def productPages(html):
  195. soup = BeautifulSoup(html, "html.parser")
  196. return darkbay_links_parser(soup)
  197. def crawler():
  198. startCrawling()