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.

272 lines
9.6 KiB

  1. __author__ = 'Helium'
  2. '''
  3. Nexus 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.support.ui import WebDriverWait
  11. from selenium.webdriver.support import expected_conditions as EC
  12. from selenium.webdriver.common.by import By
  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. import configparser
  19. from bs4 import BeautifulSoup
  20. from MarketPlaces.Initialization.prepare_parser import new_parse
  21. from MarketPlaces.Nexus.parser import nexus_links_parser
  22. from MarketPlaces.Utilities.utilities import cleanHTML
  23. counter = 1
  24. baseURL = 'http://nexus2bmba34euohk3xo7og2zelkgbtc2p7rjsbxrjjknlecja2tdvyd.onion'
  25. # Opens Tor Browser, crawls the website, then parses, then closes tor
  26. #acts like the main method for the crawler, another function at the end of this code calls this function later
  27. def startCrawling():
  28. mktName = getMKTName()
  29. driver = getAccess()
  30. if driver != 'down':
  31. try:
  32. crawlForum(driver)
  33. except Exception as e:
  34. print(driver.current_url, e)
  35. closeDriver(driver)
  36. new_parse(mktName, baseURL, True)
  37. # Returns the name of the website
  38. #return: name of site in string type
  39. def getMKTName():
  40. name = 'Nexus'
  41. return name
  42. # Return the base link of the website
  43. #return: url of base site in string type
  44. def getFixedURL():
  45. url = 'http://nexus2bmba34euohk3xo7og2zelkgbtc2p7rjsbxrjjknlecja2tdvyd.onion'
  46. return url
  47. # Closes Tor Browser
  48. #@param: current selenium driver
  49. def closeDriver(driver):
  50. # global pid
  51. # os.system("taskkill /pid " + str(pro.pid))
  52. # os.system("taskkill /t /f /im tor.exe")
  53. print('Closing Tor...')
  54. driver.close()
  55. time.sleep(3)
  56. return
  57. # Creates FireFox 'driver' and configure its 'Profile'
  58. # to use Tor proxy and socket
  59. def createFFDriver():
  60. from MarketPlaces.Initialization.markets_mining import config
  61. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  62. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  63. ff_prof.set_preference("places.history.enabled", False)
  64. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  65. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  66. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  67. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  68. ff_prof.set_preference("signon.rememberSignons", False)
  69. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  70. ff_prof.set_preference("network.dns.disablePrefetch", True)
  71. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  72. ff_prof.set_preference("permissions.default.image", 3)
  73. ff_prof.set_preference("browser.download.folderList", 2)
  74. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  75. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  76. ff_prof.set_preference('network.proxy.type', 1)
  77. ff_prof.set_preference("network.proxy.socks_version", 5)
  78. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  79. ff_prof.set_preference('network.proxy.socks_port', 9150)
  80. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  81. ff_prof.set_preference("javascript.enabled", False)
  82. ff_prof.update_preferences()
  83. service = Service(config.get('TOR', 'geckodriver_path'))
  84. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  85. driver.maximize_window()
  86. return driver
  87. #the driver 'gets' the url, attempting to get on the site, if it can't access return 'down'
  88. #return: return the selenium driver or string 'down'
  89. def getAccess():
  90. url = getFixedURL()
  91. driver = createFFDriver()
  92. try:
  93. driver.get(url)
  94. return driver
  95. except:
  96. driver.close()
  97. return 'down'
  98. def savePage(driver, page, url):
  99. cleanPage = cleanHTML(driver, page)
  100. filePath = getFullPathName(url)
  101. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  102. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  103. return
  104. # Gets the full path of the page to be saved along with its appropriate file name
  105. #@param: raw url as crawler crawls through every site
  106. def getFullPathName(url):
  107. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  108. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMKTName() + "/HTML_Pages")
  109. fileName = getNameFromURL(url)
  110. if isListingLink(url):
  111. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  112. else:
  113. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  114. return fullPath
  115. # Creates the file name from passed URL, gives distinct name if can't be made unique after cleaned
  116. #@param: raw url as crawler crawls through every site
  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. # returns list of urls, here is where you can list the different urls of interest, the crawler runs through this list
  125. #in this example, there are a couple of categories some threads fall under such as
  126. # Guides and Tutorials, Digital Products, and Software and Malware
  127. #as you can see they are categories of products
  128. def getInterestedLinks():
  129. links = []
  130. # malware
  131. links.append('http://nexus2bmba34euohk3xo7og2zelkgbtc2p7rjsbxrjjknlecja2tdvyd.onion/categoria-produto/malware/')
  132. # # hacking-spam
  133. # links.append('http://nexus2bmba34euohk3xo7og2zelkgbtc2p7rjsbxrjjknlecja2tdvyd.onion/categoria-produto/hacking-spam/')
  134. # # hacking services
  135. # links.append('http://nexus2bmba34euohk3xo7og2zelkgbtc2p7rjsbxrjjknlecja2tdvyd.onion/categoria-produto/servicos/hacking/')
  136. # # programming services
  137. # links.append('http://nexus2bmba34euohk3xo7og2zelkgbtc2p7rjsbxrjjknlecja2tdvyd.onion/categoria-produto/servicos/programacao/')
  138. # # remote admin services
  139. # links.append('http://nexus2bmba34euohk3xo7og2zelkgbtc2p7rjsbxrjjknlecja2tdvyd.onion/categoria-produto/servicos/administracao-remota/')
  140. # # hacking guides
  141. # links.append('http://nexus2bmba34euohk3xo7og2zelkgbtc2p7rjsbxrjjknlecja2tdvyd.onion/categoria-produto/guias-tutoriais/guia-de-hacking/')
  142. # # malware guides
  143. # links.append('http://nexus2bmba34euohk3xo7og2zelkgbtc2p7rjsbxrjjknlecja2tdvyd.onion/categoria-produto/guias-tutoriais/guia-de-malware/')
  144. # # fraud guides
  145. # links.append('http://nexus2bmba34euohk3xo7og2zelkgbtc2p7rjsbxrjjknlecja2tdvyd.onion/categoria-produto/guias-tutoriais/guia-de-fraudes/')
  146. # # fraud software
  147. # links.append('http://nexus2bmba34euohk3xo7og2zelkgbtc2p7rjsbxrjjknlecja2tdvyd.onion/categoria-produto/fraudes/software-de-fraude/')
  148. return links
  149. # gets links of interest to crawl through, iterates through list, where each link is clicked and crawled through
  150. #topic and description pages are crawled through here, where both types of pages are saved
  151. #@param: selenium driver
  152. def crawlForum(driver):
  153. print("Crawling the Nexus market")
  154. linksToCrawl = getInterestedLinks()
  155. i = 0
  156. while i < len(linksToCrawl):
  157. link = linksToCrawl[i]
  158. print('Crawling :', link)
  159. try:
  160. has_next_page = True
  161. count = 0
  162. while has_next_page:
  163. try:
  164. driver.get(link)
  165. except:
  166. driver.refresh()
  167. html = driver.page_source
  168. savePage(driver, html, link)
  169. list = productPages(html)
  170. for item in list:
  171. itemURL = urlparse.urljoin(baseURL, str(item))
  172. try:
  173. driver.get(itemURL)
  174. except:
  175. driver.refresh()
  176. savePage(driver, driver.page_source, item)
  177. driver.back()
  178. # comment out
  179. # break
  180. # comment out
  181. if count == 1:
  182. break
  183. try:
  184. link = driver.find_element(by=By.XPATH, value=
  185. '/html/body/div[1]/div[2]/div/div/main/nav/ul/li[3]/a').get_attribute('href')
  186. if link == "":
  187. raise NoSuchElementException
  188. count += 1
  189. except NoSuchElementException:
  190. has_next_page = False
  191. except Exception as e:
  192. print(link, e)
  193. i += 1
  194. print("Crawling the Nexus market done.")
  195. # Returns 'True' if the link is a description link
  196. #@param: url of any url crawled
  197. #return: true if is a description page, false if not
  198. def isDescriptionLink(url):
  199. if 'produto' in url:
  200. return True
  201. return False
  202. # Returns True if the link is a listingPage link
  203. #@param: url of any url crawled
  204. #return: true if is a Listing page, false if not
  205. def isListingLink(url):
  206. if 'categoria-produto' in url:
  207. return True
  208. return False
  209. # calling the parser to define the links, the html is the url of a link from the list of interested link list
  210. #@param: link from interested link list ie. getInterestingLinks()
  211. #return: list of description links that should be crawled through
  212. def productPages(html):
  213. soup = BeautifulSoup(html, "html.parser")
  214. return nexus_links_parser(soup)
  215. def crawler():
  216. startCrawling()
  217. # print("Crawling and Parsing Nexus .... DONE!")