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.

273 lines
8.5 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. __author__ = 'Helium'
  2. '''
  3. Kingdom 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.common.exceptions import TimeoutException
  11. from selenium.webdriver.firefox.options import Options
  12. from selenium.webdriver.common.by import By
  13. from selenium.webdriver.support import expected_conditions as EC
  14. from selenium.webdriver.support.ui import WebDriverWait
  15. from selenium.webdriver.support.ui import Select
  16. from PIL import Image
  17. import base64
  18. from io import BytesIO
  19. import urllib.parse as urlparse
  20. import os, re, time
  21. from datetime import date
  22. import subprocess
  23. from bs4 import BeautifulSoup
  24. from MarketPlaces.Initialization.prepare_parser import new_parse
  25. from MarketPlaces.Kingdom.parser import kingdom_links_parser
  26. from MarketPlaces.Utilities.utilities import cleanHTML
  27. counter = 1
  28. baseURL = 'http://kingdomm7v6yed55o2rbspvs4exn5bzfxdizqaav27tw6gw4zc65vdad.onion'
  29. # Opens Tor Browser, crawls the website
  30. def startCrawling():
  31. mktName = getMKTName()
  32. driver = getAccess()
  33. if driver != 'down':
  34. try:
  35. login(driver)
  36. crawlForum(driver)
  37. except Exception as e:
  38. print(driver.current_url, e)
  39. closeDriver(driver)
  40. new_parse(mktName, baseURL, True)
  41. # Login using premade account credentials and do login captcha manually
  42. def login(driver):
  43. input("Press ENTER when CAPTCHA is completed\n")
  44. # wait for login page
  45. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  46. (By.XPATH, '//*[@id="login-form"]')))
  47. # entering username and password into input boxes
  48. usernameBox = driver.find_element(by=By.XPATH, value='//*[@id="loginformwithcaptcha-name"]')
  49. # Username here
  50. usernameBox.send_keys('blabri')
  51. passwordBox = driver.find_element(by=By.XPATH, value='//*[@id="loginformwithcaptcha-passwd"]')
  52. # Password here
  53. passwordBox.send_keys('fishowal')
  54. select = Select(driver.find_element(by=By.XPATH, value='//*[@id="loginformwithcaptcha-sessiontime"]'))
  55. select.select_by_visible_text('24 hours')
  56. input("Press ENTER when CAPTCHA and DDOS is completed\n")
  57. # wait for listing page show up (This Xpath may need to change based on different seed url)
  58. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  59. (By.XPATH, '/html/body/div/div/div[3]/div[1]/div/div[3]')))
  60. # Returns the name of the website
  61. def getMKTName():
  62. name = 'Kingdom'
  63. return name
  64. # Return the link of the website
  65. def getFixedURL():
  66. url = 'http://kingdomm7v6yed55o2rbspvs4exn5bzfxdizqaav27tw6gw4zc65vdad.onion'
  67. return url
  68. # Closes Tor Browser
  69. def closeDriver(driver):
  70. # global pid
  71. # os.system("taskkill /pid " + str(pro.pid))
  72. # os.system("taskkill /t /f /im tor.exe")
  73. print('Closing Tor...')
  74. driver.close()
  75. time.sleep(3)
  76. return
  77. # Creates FireFox 'driver' and configure its 'Profile'
  78. # to use Tor proxy and socket
  79. def createFFDriver():
  80. from MarketPlaces.Initialization.markets_mining import config
  81. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  82. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  83. ff_prof.set_preference("places.history.enabled", False)
  84. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  85. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  86. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  87. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  88. ff_prof.set_preference("signon.rememberSignons", False)
  89. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  90. # ff_prof.set_preference("network.dns.disablePrefetch", True)
  91. # ff_prof.set_preference("network.http.sendRefererHeader", 0)
  92. ff_prof.set_preference("permissions.default.image", 3)
  93. ff_prof.set_preference("browser.download.folderList", 2)
  94. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  95. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  96. ff_prof.set_preference('network.proxy.type', 1)
  97. ff_prof.set_preference("network.proxy.socks_version", 5)
  98. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  99. ff_prof.set_preference('network.proxy.socks_port', 9150)
  100. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  101. ff_prof.set_preference("javascript.enabled", False)
  102. ff_prof.update_preferences()
  103. service = Service(config.get('TOR', 'geckodriver_path'))
  104. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  105. driver.maximize_window()
  106. return driver
  107. def getAccess():
  108. url = getFixedURL()
  109. driver = createFFDriver()
  110. try:
  111. driver.get(url)
  112. return driver
  113. except:
  114. driver.close()
  115. return 'down'
  116. # Saves the crawled html page
  117. def savePage(driver, page, url):
  118. cleanPage = cleanHTML(driver, page)
  119. filePath = getFullPathName(url)
  120. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  121. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  122. return
  123. # Gets the full path of the page to be saved along with its appropriate file name
  124. def getFullPathName(url):
  125. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  126. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMKTName() + "/HTML_Pages")
  127. fileName = getNameFromURL(url)
  128. if isDescriptionLink(url):
  129. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  130. else:
  131. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  132. return fullPath
  133. # Creates the file name from passed URL
  134. def getNameFromURL(url):
  135. global counter
  136. name = ''.join(e for e in url if e.isalnum())
  137. if (name == ''):
  138. name = str(counter)
  139. counter = counter + 1
  140. return name
  141. def getInterestedLinks():
  142. links = []
  143. # Software and Malware
  144. links.append('http://kingdomm7v6yed55o2rbspvs4exn5bzfxdizqaav27tw6gw4zc65vdad.onion/offers?filter_category=127')
  145. # # Services
  146. links.append('http://kingdomm7v6yed55o2rbspvs4exn5bzfxdizqaav27tw6gw4zc65vdad.onion/offers?filter_category=45')
  147. # # guides and tutorials
  148. links.append('http://kingdomm7v6yed55o2rbspvs4exn5bzfxdizqaav27tw6gw4zc65vdad.onion/offers?filter_category=107')
  149. return links
  150. def crawlForum(driver):
  151. print("Crawling the Kingdom 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 == 1:
  181. # break
  182. try:
  183. link = driver.find_element(by=By.XPATH, value='//a[contains(text(), "»")]').get_attribute('href')
  184. if link == "":
  185. raise NoSuchElementException
  186. count += 1
  187. except NoSuchElementException:
  188. has_next_page = False
  189. except Exception as e:
  190. print(link, e)
  191. i += 1
  192. print("Crawling the Kingdom market done.")
  193. # Returns 'True' if the link is Topic link
  194. def isDescriptionLink(url):
  195. if 'view' in url:
  196. return True
  197. return False
  198. # Returns True if the link is a listingPage link
  199. def isListingLink(url):
  200. if 'filter_category' in url:
  201. return True
  202. return False
  203. # calling the parser to define the links
  204. def productPages(html):
  205. soup = BeautifulSoup(html, "html.parser")
  206. return kingdom_links_parser(soup)
  207. def crawler():
  208. startCrawling()