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.

289 lines
11 KiB

  1. __author__ = 'Helium'
  2. '''
  3. Anon 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.AnonMarket.parser import AnonMarket_links_parser
  22. from MarketPlaces.Utilities.utilities import cleanHTML
  23. counter = 1
  24. baseURL = 'http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.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. opentor()
  29. mktName = getMKTName()
  30. driver = getAccess()
  31. if driver != 'down':
  32. try:
  33. crawlForum(driver)
  34. except Exception as e:
  35. print(driver.current_url, e)
  36. closetor(driver)
  37. new_parse(mktName, baseURL, True)
  38. # Opens Tor Browser
  39. #prompts for ENTER input to continue
  40. def opentor():
  41. from MarketPlaces.Initialization.markets_mining import config
  42. global pid
  43. print("Connecting Tor...")
  44. pro = subprocess.Popen(config.get('TOR', 'firefox_binary_path'))
  45. pid = pro.pid
  46. time.sleep(7.5)
  47. input('Tor Connected. Press ENTER to continue\n')
  48. return
  49. # Returns the name of the website
  50. #return: name of site in string type
  51. def getMKTName():
  52. name = 'AnonMarket'
  53. return name
  54. # Return the base link of the website
  55. #return: url of base site in string type
  56. def getFixedURL():
  57. url = 'http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion'
  58. return url
  59. # Closes Tor Browser
  60. #@param: current selenium driver
  61. def closetor(driver):
  62. # global pid
  63. # os.system("taskkill /pid " + str(pro.pid))
  64. # os.system("taskkill /t /f /im tor.exe")
  65. print('Closing Tor...')
  66. driver.close()
  67. time.sleep(3)
  68. return
  69. # Creates FireFox 'driver' and configure its 'Profile'
  70. # to use Tor proxy and socket
  71. def createFFDriver():
  72. from MarketPlaces.Initialization.markets_mining import config
  73. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  74. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  75. ff_prof.set_preference("places.history.enabled", False)
  76. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  77. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  78. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  79. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  80. ff_prof.set_preference("signon.rememberSignons", False)
  81. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  82. ff_prof.set_preference("network.dns.disablePrefetch", True)
  83. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  84. ff_prof.set_preference("permissions.default.image", 2)
  85. ff_prof.set_preference("browser.download.folderList", 2)
  86. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  87. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  88. ff_prof.set_preference('network.proxy.type', 1)
  89. ff_prof.set_preference("network.proxy.socks_version", 5)
  90. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  91. ff_prof.set_preference('network.proxy.socks_port', 9150)
  92. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  93. ff_prof.set_preference("javascript.enabled", False)
  94. ff_prof.update_preferences()
  95. service = Service(config.get('TOR', 'geckodriver_path'))
  96. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  97. driver.maximize_window()
  98. return driver
  99. #the driver 'gets' the url, attempting to get on the site, if it can't access return 'down'
  100. #return: return the selenium driver or string 'down'
  101. def getAccess():
  102. url = getFixedURL()
  103. driver = createFFDriver()
  104. try:
  105. driver.get(url)
  106. return driver
  107. except:
  108. driver.close()
  109. return 'down'
  110. def savePage(driver, page, url):
  111. cleanPage = cleanHTML(driver, page)
  112. filePath = getFullPathName(url)
  113. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  114. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  115. return
  116. # Gets the full path of the page to be saved along with its appropriate file name
  117. #@param: raw url as crawler crawls through every site
  118. def getFullPathName(url):
  119. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  120. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMKTName() + "/HTML_Pages")
  121. fileName = getNameFromURL(url)
  122. if isDescriptionLink(url):
  123. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  124. else:
  125. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  126. return fullPath
  127. # Creates the file name from passed URL, gives distinct name if can't be made unique after cleaned
  128. #@param: raw url as crawler crawls through every site
  129. def getNameFromURL(url):
  130. global counter
  131. name = ''.join(e for e in url if e.isalnum())
  132. if (name == ''):
  133. name = str(counter)
  134. counter = counter + 1
  135. return name
  136. # returns list of urls, here is where you can list the different urls of interest, the crawler runs through this list
  137. #in this example, there are a couple of categories some threads fall under such as
  138. # Guides and Tutorials, Digital Products, and Software and Malware
  139. #as you can see they are categories of products
  140. def getInterestedLinks():
  141. links = []
  142. # # Software
  143. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/civil_softwares')
  144. # # Malware
  145. links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/malware')
  146. # # Bootkits
  147. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/bootkits')
  148. # # Backdoors
  149. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/backdoors')
  150. # # Keyloggers
  151. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/keyloggers')
  152. # # Wireless Trackers
  153. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/wireless_trackers')
  154. # # Screen Scrapers
  155. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/screen_scrapers')
  156. # # Mobile Forensic Tools
  157. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/mobile_forensics_tools')
  158. # # Wifi Jammers
  159. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/wifi_jammers')
  160. # # Carding
  161. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/carding')
  162. # # Worms
  163. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/worms')
  164. # # Viruses
  165. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/viruses')
  166. # # Trojans
  167. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/trojans')
  168. # # Botnets
  169. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/botnets')
  170. # # Security Technology
  171. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/security_technology')
  172. # # Hacks
  173. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/hacks')
  174. # # Exploit kits
  175. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/exploit_kit')
  176. # # Security
  177. # links.append('http://2r7wa5og3ly4umqhmmqqytae6bufl5ql5kz7sorndpqtrkc2ri7tohad.onion/category/security')
  178. return links
  179. # gets links of interest to crawl through, iterates through list, where each link is clicked and crawled through
  180. #topic and description pages are crawled through here, where both types of pages are saved
  181. #@param: selenium driver
  182. def crawlForum(driver):
  183. print("Crawling Anon Market")
  184. linksToCrawl = getInterestedLinks()
  185. for link in linksToCrawl:
  186. print('Crawling :', link)
  187. has_next_page = True
  188. while has_next_page:
  189. try:
  190. driver.get(link)
  191. except:
  192. driver.refresh()
  193. html = driver.page_source
  194. savePage(driver, html, link)
  195. # Get all product links on the current page
  196. products_list = productPages(html)
  197. for item in products_list:
  198. itemURL = urlparse.urljoin(baseURL, str(item))
  199. try:
  200. driver.get(itemURL)
  201. except:
  202. driver.refresh()
  203. savePage(driver, driver.page_source, item)
  204. driver.back() # Go back to listing after visiting each product
  205. # Find the active page number
  206. active_page_element = driver.find_element(By.XPATH, '//div[@class="page activepage"]')
  207. current_page = int(active_page_element.text)
  208. # Locate the next page link
  209. try:
  210. next_page_element = active_page_element.find_element(By.XPATH, 'following-sibling::a[1]')
  211. link = next_page_element.get_attribute('href')
  212. except NoSuchElementException:
  213. has_next_page = False
  214. print("Crawling Anon Market done.")
  215. # Returns 'True' if the link is a description link
  216. #@param: url of any url crawled
  217. #return: true if is a description page, false if not
  218. def isDescriptionLink(url):
  219. if 'product' in url:
  220. return True
  221. return False
  222. # Returns True if the link is a listingPage link
  223. #@param: url of any url crawled
  224. #return: true if is a Listing page, false if not
  225. def isListingLink(url):
  226. if 'category' in url:
  227. return True
  228. return False
  229. # calling the parser to define the links, the html is the url of a link from the list of interested link list
  230. #@param: link from interested link list ie. getInterestingLinks()
  231. #return: list of description links that should be crawled through
  232. def productPages(html):
  233. soup = BeautifulSoup(html, "html.parser")
  234. return AnonMarket_links_parser(soup)
  235. def crawler():
  236. startCrawling()
  237. # print("Crawling and Parsing Nexus .... DONE!")