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.

303 lines
11 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. __author__ = 'Helium'
  2. '''
  3. LionMarketplace 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 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.LionMarketplace.parser import lionmarketplace_links_parser
  22. from MarketPlaces.Utilities.utilities import cleanHTML
  23. counter = 1
  24. baseURL = 'http://lionznqc2hg2wsp5vgruqait4cpknihwlje6hkjyi52lcl5ivyf7bcad.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. login(driver)
  34. crawlForum(driver)
  35. except Exception as e:
  36. print(driver.current_url, e)
  37. closetor(driver)
  38. # new_parse(forumName, baseURL, False)
  39. # Opens Tor Browser
  40. #prompts for ENTER input to continue
  41. def opentor():
  42. from MarketPlaces.Initialization.markets_mining import config
  43. global pid
  44. print("Connecting Tor...")
  45. pro = subprocess.Popen(config.get('TOR', 'firefox_binary_path'))
  46. pid = pro.pid
  47. time.sleep(7.5)
  48. input('Tor Connected. Press ENTER to continue\n')
  49. return
  50. # Returns the name of the website
  51. #return: name of site in string type
  52. def getMKTName():
  53. name = 'LionMarketplace'
  54. return name
  55. # Return the base link of the website
  56. #return: url of base site in string type
  57. def getFixedURL():
  58. url = 'http://lionznqc2hg2wsp5vgruqait4cpknihwlje6hkjyi52lcl5ivyf7bcad.onion/'
  59. return url
  60. # Closes Tor Browser
  61. #@param: current selenium driver
  62. def closetor(driver):
  63. # global pid
  64. # os.system("taskkill /pid " + str(pro.pid))
  65. # os.system("taskkill /t /f /im tor.exe")
  66. print('Closing Tor...')
  67. driver.close()
  68. time.sleep(3)
  69. return
  70. # Creates FireFox 'driver' and configure its 'Profile'
  71. # to use Tor proxy and socket
  72. def createFFDriver():
  73. from MarketPlaces.Initialization.markets_mining import config
  74. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  75. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  76. ff_prof.set_preference("places.history.enabled", False)
  77. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  78. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  79. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  80. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  81. ff_prof.set_preference("signon.rememberSignons", False)
  82. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  83. ff_prof.set_preference("network.dns.disablePrefetch", True)
  84. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  85. ff_prof.set_preference("permissions.default.image", 2)
  86. ff_prof.set_preference("browser.download.folderList", 2)
  87. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  88. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  89. ff_prof.set_preference('network.proxy.type', 1)
  90. ff_prof.set_preference("network.proxy.socks_version", 5)
  91. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  92. ff_prof.set_preference('network.proxy.socks_port', 9150)
  93. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  94. ff_prof.set_preference("javascript.enabled", False)
  95. ff_prof.update_preferences()
  96. service = Service(config.get('TOR', 'geckodriver_path'))
  97. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  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. # Manual captcha solver, waits fora specific element so that the whole page loads, finds the input box, gets screenshot of captcha
  111. # then allows for manual solving of captcha in the terminal
  112. #@param: current selenium web driver
  113. def login(driver):
  114. # wait for page to show up (This Xpath may need to change based on different seed url)
  115. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  116. (By.XPATH, "/html/body/div[2]/div[2]/div[2]/div[1]/div/div[2]/div")))
  117. # Saves the crawled html page, makes the directory path for html pages if not made
  118. def savePage(page, url):
  119. cleanPage = cleanHTML(page)
  120. filePath = getFullPathName(url)
  121. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  122. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  123. return
  124. # Gets the full path of the page to be saved along with its appropriate file name
  125. #@param: raw url as crawler crawls through every site
  126. def getFullPathName(url):
  127. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  128. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMKTName() + "/HTML_Pages")
  129. fileName = getNameFromURL(url)
  130. if isDescriptionLink(url):
  131. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  132. else:
  133. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  134. return fullPath
  135. # Creates the file name from passed URL, gives distinct name if can't be made unique after cleaned
  136. #@param: raw url as crawler crawls through every site
  137. def getNameFromURL(url):
  138. global counter
  139. name = ''.join(e for e in url if e.isalnum())
  140. if (name == ''):
  141. name = str(counter)
  142. counter = counter + 1
  143. return name
  144. # returns list of urls, here is where you can list the different urls of interest, the crawler runs through this list
  145. #in this example, there are a couple of categories some threads fall under such as
  146. # Guides and Tutorials, Digital Products, and Software and Malware
  147. #as you can see they are categories of products
  148. def getInterestedLinks():
  149. links = []
  150. # Software/Malware
  151. links.append('http://lionznqc2hg2wsp5vgruqait4cpknihwlje6hkjyi52lcl5ivyf7bcad.onion/category/16')
  152. # # Carding
  153. # links.append('http://lionznqc2hg2wsp5vgruqait4cpknihwlje6hkjyi52lcl5ivyf7bcad.onion/category/20')
  154. # # Hacker for hire
  155. # links.append('http://lionznqc2hg2wsp5vgruqait4cpknihwlje6hkjyi52lcl5ivyf7bcad.onion/category/0b19f3a0-c7e8-11ec-997b-0dcb6b05ce1d')
  156. # # Phishing
  157. # links.append('http://lionznqc2hg2wsp5vgruqait4cpknihwlje6hkjyi52lcl5ivyf7bcad.onion/category/18098bb0-c7e8-11ec-95e9-45b5e8898cbd')
  158. # # Ransomware
  159. # links.append('http://lionznqc2hg2wsp5vgruqait4cpknihwlje6hkjyi52lcl5ivyf7bcad.onion/category/ce72cee0-c7e7-11ec-a86b-c1ff2d3b2020')
  160. # # Exploits
  161. # links.append('http://lionznqc2hg2wsp5vgruqait4cpknihwlje6hkjyi52lcl5ivyf7bcad.onion/category/e26387c0-c7e7-11ec-a708-ab6dc5117763')
  162. # # Spamming and Anti-Captcha
  163. # links.append('http://lionznqc2hg2wsp5vgruqait4cpknihwlje6hkjyi52lcl5ivyf7bcad.onion/category/f08a9380-c7e7-11ec-918c-ffef7c670c97')
  164. # hacked accounts
  165. #links.append('http://lionznqc2hg2wsp5vgruqait4cpknihwlje6hkjyi52lcl5ivyf7bcad.onion/category/fd47b4a0-c7e7-11ec-937b-61246c4b12b3')
  166. return links
  167. # gets links of interest to crawl through, iterates through list, where each link is clicked and crawled through
  168. #topic and description pages are crawled through here, where both types of pages are saved
  169. #@param: selenium driver
  170. def crawlForum(driver):
  171. print("Crawling the LionMarketplace market")
  172. linksToCrawl = getInterestedLinks()
  173. i = 0
  174. while i < len(linksToCrawl):
  175. link = linksToCrawl[i]
  176. print('Crawling :', link)
  177. try:
  178. has_next_page = True
  179. count = 0
  180. while has_next_page:
  181. try:
  182. driver.get(link)
  183. except:
  184. driver.refresh()
  185. html = driver.page_source
  186. savePage(html, link)
  187. list = productPages(html)
  188. for item in list:
  189. itemURL = urlparse.urljoin(baseURL, str(item))
  190. try:
  191. driver.get(itemURL)
  192. except:
  193. driver.refresh()
  194. savePage(driver.page_source, item)
  195. driver.back()
  196. # comment out
  197. break
  198. # comment out
  199. if count == 1:
  200. break
  201. try:
  202. link = driver.find_element(by=By.XPATH, value=
  203. '/html/body/div[2]/div[2]/div/div[2]/nav/ul/li[5]/a').get_attribute('href')
  204. if link == "":
  205. raise NoSuchElementException
  206. count += 1
  207. except NoSuchElementException:
  208. has_next_page = False
  209. except Exception as e:
  210. print(link, e)
  211. i += 1
  212. input("Crawling LionMarketplace forum done sucessfully. Press ENTER to continue\n")
  213. # Returns 'True' if the link is a description link
  214. #@param: url of any url crawled
  215. #return: true if is a description page, false if not
  216. def isDescriptionLink(url):
  217. if 'product' in url:
  218. return True
  219. return False
  220. # Returns True if the link is a listingPage link
  221. #@param: url of any url crawled
  222. #return: true if is a Listing page, false if not
  223. def isListingLink(url):
  224. if 'category' in url:
  225. return True
  226. return False
  227. # calling the parser to define the links, the html is the url of a link from the list of interested link list
  228. #@param: link from interested link list ie. getInterestingLinks()
  229. #return: list of description links that should be crawled through
  230. def productPages(html):
  231. soup = BeautifulSoup(html, "html.parser")
  232. return lionmarketplace_links_parser(soup)
  233. # Drop links that "signout"
  234. # def isSignOut(url):
  235. # #absURL = urlparse.urljoin(url.base_url, url.url)
  236. # if 'signout' in url.lower() or 'logout' in url.lower():
  237. # return True
  238. #
  239. # return False
  240. def crawler():
  241. startCrawling()
  242. # print("Crawling and Parsing BestCardingWorld .... DONE!")