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.

277 lines
9.0 KiB

  1. __author__ = 'DarkWeb'
  2. '''
  3. MGMGrand 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.MGMGrand.parser import mgm_links_parser
  23. from MarketPlaces.Utilities.utilities import cleanHTML
  24. counter = 1
  25. baseURL = 'http://duysanjqxo4svh35yqkxxe5r54z2xc5tjf6r3ichxd3m2rwcgabf44ad.onion'
  26. def startCrawling():
  27. mktName = getMKTName()
  28. driver = getAccess()
  29. if driver != 'down':
  30. try:
  31. login(driver)
  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. def getMKTName():
  39. name = 'MGMGrand'
  40. return name
  41. # Return the base link of the website
  42. def getFixedURL():
  43. url = 'http://duysanjqxo4svh35yqkxxe5r54z2xc5tjf6r3ichxd3m2rwcgabf44ad.onion'
  44. return url
  45. # Closes Tor Browser
  46. def closeDriver(driver):
  47. # global pid
  48. # os.system("taskkill /pid " + str(pro.pid))
  49. # os.system("taskkill /t /f /im tor.exe")
  50. print('Closing Tor...')
  51. driver.close()
  52. time.sleep(3)
  53. return
  54. # Creates FireFox 'driver' and configure its 'Profile'
  55. # to use Tor proxy and socket
  56. def createFFDriver():
  57. from MarketPlaces.Initialization.markets_mining import config
  58. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  59. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  60. ff_prof.set_preference("places.history.enabled", False)
  61. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  62. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  63. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  64. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  65. ff_prof.set_preference("signon.rememberSignons", False)
  66. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  67. ff_prof.set_preference("network.dns.disablePrefetch", True)
  68. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  69. ff_prof.set_preference("permissions.default.image", 3)
  70. ff_prof.set_preference("browser.download.folderList", 2)
  71. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  72. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  73. ff_prof.set_preference('network.proxy.type', 1)
  74. ff_prof.set_preference("network.proxy.socks_version", 5)
  75. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  76. ff_prof.set_preference('network.proxy.socks_port', 9150)
  77. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  78. ff_prof.set_preference("javascript.enabled", False)
  79. ff_prof.update_preferences()
  80. service = Service(config.get('TOR', 'geckodriver_path'))
  81. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  82. driver.maximize_window()
  83. return driver
  84. #the driver 'gets' the url, attempting to get on the site, if it can't access return 'down'
  85. def getAccess():
  86. url = getFixedURL()
  87. driver = createFFDriver()
  88. try:
  89. driver.get(url)
  90. return driver
  91. except:
  92. driver.close()
  93. return 'down'
  94. def login(driver):
  95. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  96. (By.XPATH, '//*[@id="username"]')))
  97. # entering username and password into input boxes
  98. usernameBox = driver.find_element(by=By.XPATH, value='//*[@id="username"]')
  99. # Username here
  100. usernameBox.send_keys('blabri')
  101. passwordBox = driver.find_element(by=By.XPATH, value='//*[@id="password"]')
  102. # Password here
  103. passwordBox.send_keys('fishowal')
  104. input("Press ENTER when captcha is solved and you're logged in")
  105. # Wait for the element to be visible
  106. WebDriverWait(driver, 100).until(
  107. EC.visibility_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/a/span'))
  108. )
  109. # Find the element and click it
  110. temp = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/a/span')
  111. temp.click()
  112. def savePage(driver, page, url):
  113. cleanPage = cleanHTML(driver, page)
  114. filePath = getFullPathName(url)
  115. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  116. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  117. return
  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. def getMKTName() -> str:
  128. name = 'MGMGrand'
  129. return name
  130. def getNameFromURL(url):
  131. global counter
  132. name = ''.join(e for e in url if e.isalnum())
  133. if name == '':
  134. name = str(counter)
  135. counter = counter + 1
  136. return name
  137. def getInterestedLinks():
  138. links = []
  139. # Carding
  140. links.append('http://duysanjqxo4svh35yqkxxe5r54z2xc5tjf6r3ichxd3m2rwcgabf44ad.onion/category/c54f4d30-9060-11eb-be9c-630550815967')
  141. # # softwares and malwares
  142. # links.append('http://duysanjqxo4svh35yqkxxe5r54z2xc5tjf6r3ichxd3m2rwcgabf44ad.onion/category/6a211fa0-9062-11eb-b3bd-d7d946c69ce2')
  143. # # social engineering tutorial
  144. # links.append('http://duysanjqxo4svh35yqkxxe5r54z2xc5tjf6r3ichxd3m2rwcgabf44ad.onion/category/5596f690-9063-11eb-9ebb-6b44dbdbc454')
  145. # # misc services
  146. # links.append('http://duysanjqxo4svh35yqkxxe5r54z2xc5tjf6r3ichxd3m2rwcgabf44ad.onion/category/b212fe30-9063-11eb-a479-35581612f6c2')
  147. # # hacking tutorials
  148. # links.append('http://duysanjqxo4svh35yqkxxe5r54z2xc5tjf6r3ichxd3m2rwcgabf44ad.onion/category/2ed713f0-9063-11eb-bf9c-232fd3b49a98')
  149. return links
  150. def crawlForum(driver):
  151. print("Crawling the MGM market")
  152. linksToCrawl = getInterestedLinks()
  153. i = 0
  154. while i < len(linksToCrawl):
  155. link = linksToCrawl[i]
  156. print('Crawling :', link)
  157. try:
  158. has_next_page = True
  159. count = 0
  160. while has_next_page:
  161. try:
  162. driver.get(link)
  163. except:
  164. driver.refresh()
  165. html = driver.page_source
  166. savePage(driver, html, link)
  167. list = productPages(html)
  168. for item in list:
  169. itemURL = urlparse.urljoin(baseURL, str(item))
  170. try:
  171. driver.get(itemURL)
  172. except:
  173. driver.refresh()
  174. savePage(driver, driver.page_source, item)
  175. driver.back()
  176. # # comment out
  177. break
  178. #
  179. # # comment out
  180. # if count == 4:
  181. # break
  182. try:
  183. li_tags = driver.find_element(by=By.CLASS_NAME, value='pagination mb-0').find_elements(by=By.TAG_NAME, value='li')
  184. a_tag = li_tags[-1].find_element(by=By.TAG_NAME, value='a')
  185. if a_tag:
  186. try:
  187. link = a_tag.get_attribute('href')
  188. print(link)
  189. except:
  190. link = ''
  191. if link == "":
  192. raise NoSuchElementException
  193. count += 1
  194. except NoSuchElementException:
  195. has_next_page = False
  196. except Exception as e:
  197. print(link, e)
  198. i += 1
  199. print("Crawling the MGMGrand market done.")
  200. # Returns 'True' if the link is Topic link, may need to change for every website
  201. def isDescriptionLink(url):
  202. if 'product' in url:
  203. return True
  204. return False
  205. # Returns True if the link is a listingPage link, may need to change for every website
  206. def isListingLink(url):
  207. if 'category' in url:
  208. return True
  209. return False
  210. def productPages(html):
  211. soup = BeautifulSoup(html, "html.parser")
  212. return mgm_links_parser(soup)
  213. def crawler():
  214. startCrawling()