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.

353 lines
12 KiB

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