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.

352 lines
12 KiB

  1. __author__ = 'DarkWeb'
  2. '''
  3. Royal Marketplace Crawler (Selenium)
  4. '''
  5. from selenium import webdriver
  6. from selenium.webdriver.support.select import Select
  7. from selenium.common.exceptions import NoSuchElementException
  8. from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
  9. from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
  10. from selenium.webdriver.firefox.service import Service
  11. from selenium.webdriver.common.by import By
  12. from selenium.webdriver.support import expected_conditions as EC
  13. from selenium.webdriver.support.ui import WebDriverWait
  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. from bs4 import BeautifulSoup
  20. from MarketPlaces.Initialization.prepare_parser import new_parse
  21. from MarketPlaces.TheDarkMarket.parser import thedarkmarket_links_parser
  22. from MarketPlaces.Utilities.utilities import cleanHTML
  23. counter = 1
  24. baseURL = 'http://dark3xolguutzr2cn5twjyu6c3db2z3ai3aqyqascml5cdrleh3s2hqd.onion/'
  25. # Opens Tor Browser, crawls the website
  26. def startCrawling():
  27. marketName = getMarketName()
  28. driver = getAccess()
  29. if driver != 'down':
  30. try:
  31. crawlForum(driver)
  32. except Exception as e:
  33. print(driver.current_url, e)
  34. closeDriver(driver)
  35. new_parse(marketPlace=marketName, url=baseURL, createLog=True)
  36. def captcha(driver):
  37. '''
  38. # wait for captcha page
  39. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  40. (By.XPATH, "/html/body/div[2]/div/div/div/div/form/div/div[2]/button")))
  41. inputChars = driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div/div/div/form/div/div[2]/div[1]/input')
  42. inputNum = driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div/div/div/form/div/div[2]/div[2]/input')
  43. driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div/div/div/form/div/div[1]/div/div').screenshot(
  44. r'..\Royal\captcha1.png')
  45. im = Image.open(r'..\Royal\captcha1.png')
  46. im.show()
  47. chars = input("Enter characters: ")
  48. inputChars.send_keys(chars)
  49. num = input("Enter number of wrong puzzle pieces: ")
  50. inputNum.send_keys(num)
  51. # click the verify(submit) button
  52. driver.find_element(by=By.XPATH, value="/html/body/div[2]/div/div/div/div/form/div/div[2]/button").click()
  53. '''
  54. input("Press ENTER when CAPTCHA is completed\n")
  55. # wait for login page
  56. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  57. (By.XPATH, "/html/body/div[2]/div/div/div[2]/h1")))
  58. '''
  59. temp = driver.find_element(by=By.XPATH, value='/html/body/div/div/form/div[1]')
  60. boxes = temp.find_elements(by=By.TAG_NAME, value='input')
  61. for box in boxes:
  62. # click box to update captcha image
  63. box.click()
  64. # save clock captcha to local
  65. time.sleep(1)
  66. driver.find_element(by=By.XPATH, value='/html/body/div/div/form/div[1]/div').screenshot(
  67. r'..\Royal\captcha1.png')
  68. im = Image.open(r'..\Royal\captcha1.png')
  69. im.show()
  70. letter = input("Enter letter: ")
  71. box.send_keys(letter)
  72. # click the verify(submit) button
  73. driver.find_element(by=By.XPATH, value="/html/body/div/div/form/button[1]").click()
  74. # wait for login page
  75. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  76. (By.XPATH, "/html/body/div[1]/div/div/div[2]/form/input[3]")))
  77. '''
  78. # Login using premade account credentials and do login captcha manually
  79. def login(driver):
  80. # wait for login page
  81. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  82. (By.XPATH, "/html/body/div[2]/div/div/div[2]/form/div[4]")))
  83. # entering username and password into input boxes
  84. usernameBox = driver.find_element(by=By.XPATH, value='//*[@id="username"]')
  85. # Username here
  86. usernameBox.send_keys('blabri')
  87. passwordBox = driver.find_element(by=By.XPATH, value='//*[@id="password"]')
  88. # Password here
  89. passwordBox.send_keys('fishowal')
  90. # click "Login"
  91. driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div/div[2]/form/div[4]').click()
  92. '''
  93. # wait for captcha page show up
  94. time.sleep(3)
  95. # save captcha to local
  96. driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div/div[2]/form/div[4]/label/div/div').screenshot(
  97. r'..\Royal\captcha2.png')
  98. # This method will show image in any image viewer
  99. im = Image.open(r'..\Royal\captcha2.png')
  100. im.show()
  101. # ask user input captcha solution in terminal
  102. userIn = input("Enter location of wrong pieces (squares are numbered 1-24 left to right, # # #): ")
  103. squares = userIn.split()
  104. # send user solution into the input space
  105. for id in squares:
  106. driver.find_element(by=By.XPATH, value='//*[@id="cl[' + str((int(id)-1)) + ']"]').click()
  107. # click the verify(submit) button
  108. driver.find_element(by=By.XPATH, value="/html/body/div[2]/div/div/div[2]/form/div[4]/label/div/div/div/button").click()
  109. '''
  110. input("Press ENTER when CAPTCHA is completed\n")
  111. # wait for listing page show up (This Xpath may need to change based on different seed url)
  112. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  113. (By.XPATH, '/html/body/div[3]/div/div[5]/div[1]')))
  114. # Returns the name of the website
  115. def getMarketName():
  116. name = 'TheDarkMarket'
  117. return name
  118. # Return the link of the website
  119. def getFixedURL():
  120. url = 'http://dark3xolguutzr2cn5twjyu6c3db2z3ai3aqyqascml5cdrleh3s2hqd.onion/'
  121. return url
  122. # Closes Tor Browser
  123. def closeDriver(driver):
  124. # global pid
  125. # os.system("taskkill /pid " + str(pro.pid))
  126. # os.system("taskkill /t /f /im tor.exe")
  127. print('Closing Tor...')
  128. driver.close()
  129. time.sleep(3)
  130. return
  131. # Creates FireFox 'driver' and configure its 'Profile'
  132. # to use Tor proxy and socket
  133. def createFFDriver():
  134. from MarketPlaces.Initialization.markets_mining import config
  135. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  136. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  137. ff_prof.set_preference("places.history.enabled", False)
  138. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  139. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  140. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  141. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  142. ff_prof.set_preference("signon.rememberSignons", False)
  143. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  144. # ff_prof.set_preference("network.dns.disablePrefetch", True)
  145. # ff_prof.set_preference("network.http.sendRefererHeader", 0)
  146. ff_prof.set_preference("permissions.default.image", 3)
  147. ff_prof.set_preference("browser.download.folderList", 2)
  148. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  149. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  150. ff_prof.set_preference('network.proxy.type', 1)
  151. ff_prof.set_preference("network.proxy.socks_version", 5)
  152. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  153. ff_prof.set_preference('network.proxy.socks_port', 9150)
  154. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  155. ff_prof.set_preference("javascript.enabled", False)
  156. ff_prof.update_preferences()
  157. service = Service(config.get('TOR', 'geckodriver_path'))
  158. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  159. driver.maximize_window()
  160. return driver
  161. def getAccess():
  162. url = getFixedURL()
  163. driver = createFFDriver()
  164. try:
  165. driver.get(url)
  166. return driver
  167. except:
  168. driver.close()
  169. return 'down'
  170. # Saves the crawled html page
  171. def savePage(driver, page, url):
  172. cleanPage = cleanHTML(driver, page)
  173. filePath = getFullPathName(url)
  174. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  175. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  176. return
  177. # Gets the full path of the page to be saved along with its appropriate file name
  178. def getFullPathName(url):
  179. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  180. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMarketName() + "/HTML_Pages")
  181. fileName = getNameFromURL(url)
  182. if not isListingLink(url):
  183. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  184. else:
  185. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  186. return fullPath
  187. # Creates the file name from passed URL
  188. def getNameFromURL(url):
  189. global counter
  190. name = ''.join(e for e in url if e.isalnum())
  191. if name == '':
  192. name = str(counter)
  193. counter = counter + 1
  194. return name
  195. def getInterestedLinks():
  196. links = []
  197. # Digital - Fraud Software
  198. links.append(baseURL + 'product-category/hacking/')
  199. # # Digital - Guides and Tutorials
  200. # links.append('http://royalrnpvfbodtt5altnnzano6hquvn2d5qy55oofc2zyqciogcevrad.onion/category/Guides%20&%20Tutorials')
  201. # # Digital - Legitimate Software
  202. # links.append('http://royalrnpvfbodtt5altnnzano6hquvn2d5qy55oofc2zyqciogcevrad.onion/category/Legitimiate%20Software')
  203. # # Services - Carding
  204. # links.append('http://royalrnpvfbodtt5altnnzano6hquvn2d5qy55oofc2zyqciogcevrad.onion/category/Carding')
  205. return links
  206. def crawlForum(driver):
  207. print("Crawling The Dark Market")
  208. linksToCrawl = getInterestedLinks()
  209. i = 0
  210. while i < len(linksToCrawl):
  211. link = linksToCrawl[i]
  212. print('Crawling :', link)
  213. try:
  214. has_next_page = True
  215. count = 0
  216. while has_next_page:
  217. try:
  218. driver.get(link)
  219. except:
  220. driver.refresh()
  221. html = driver.page_source
  222. savePage(driver, html, link)
  223. list = productPages(html)
  224. for item in list:
  225. itemURL = urlparse.urljoin(baseURL, str(item))
  226. try:
  227. driver.get(itemURL)
  228. except:
  229. driver.refresh()
  230. savePage(driver, driver.page_source, item)
  231. driver.back()
  232. # comment out
  233. # break
  234. # comment out
  235. # if count == 1:
  236. # break
  237. # Try finding next page
  238. try:
  239. nav = driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div/div[1]/div[2]/nav')
  240. li = nav.find_elements(by=By.TAG_NAME, value='li')
  241. a = li[-1].find_element(by=By.TAG_NAME, value='a')
  242. link = a.get_attribute('href')
  243. if link == "":
  244. raise NoSuchElementException
  245. count += 1
  246. except NoSuchElementException:
  247. has_next_page = False
  248. except Exception as e:
  249. print(link, e)
  250. i += 1
  251. input("Crawling Royal forum done sucessfully. Press ENTER to continue\n")
  252. # Returns 'True' if the link is Topic link
  253. def isDescriptionLink(url):
  254. if '/product/' in url:
  255. return True
  256. return False
  257. # Returns True if the link is a listingPage link
  258. def isListingLink(url):
  259. if 'category' in url:
  260. return True
  261. return False
  262. # calling the parser to define the links
  263. def productPages(html):
  264. soup = BeautifulSoup(html, "html.parser")
  265. return darkmarket_links_parser(soup)
  266. def crawler():
  267. startCrawling()
  268. # print("Crawling and Parsing BestCardingWorld .... DONE!")