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.

310 lines
11 KiB

  1. __author__ = 'DarkWeb'
  2. '''
  3. ViceCity Market Forum 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, time
  16. from datetime import date
  17. import subprocess
  18. import configparser
  19. import subprocess
  20. from bs4 import BeautifulSoup
  21. from MarketPlaces.Initialization.prepare_parser import new_parse
  22. from MarketPlaces.ViceCity.parser import vicecity_links_parser
  23. from MarketPlaces.Utilities.utilities import cleanHTML
  24. counter = 1
  25. baseURL = 'http://52qlucglu6fuaqist2herssakipapig2higaaayu7446n55xw4ylxqid.onion/'
  26. # Opens Tor Browser, crawls the website, then parses, then closes tor
  27. #acts like the main method for the crawler, another function at the end of this code calls this function later
  28. def startCrawling():
  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. closeDriver(driver)
  38. new_parse(mktName, baseURL, True)
  39. # Returns the name of the website
  40. #return: name of site in string type
  41. def getMKTName():
  42. name = 'ViceCity'
  43. return name
  44. # Return the base link of the website
  45. #return: url of base site in string type
  46. def getFixedURL():
  47. url = 'http://52qlucglu6fuaqist2herssakipapig2higaaayu7446n55xw4ylxqid.onion/'
  48. return url
  49. # Closes Tor Browser
  50. #@param: current selenium driver
  51. def closeDriver(driver):
  52. # global pid
  53. # os.system("taskkill /pid " + str(pro.pid))
  54. # os.system("taskkill /t /f /im tor.exe")
  55. print('Closing Tor...')
  56. driver.close()
  57. time.sleep(3)
  58. return
  59. # Creates FireFox 'driver' and configure its 'Profile'
  60. # to use Tor proxy and socket
  61. def createFFDriver():
  62. from MarketPlaces.Initialization.markets_mining import config
  63. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  64. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  65. # ff_prof.set_preference("places.history.enabled", False)
  66. # ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  67. # ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  68. # ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  69. # ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  70. # ff_prof.set_preference("signon.rememberSignons", False)
  71. # ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  72. # ff_prof.set_preference("network.dns.disablePrefetch", True)
  73. # ff_prof.set_preference("network.http.sendRefererHeader", 0)
  74. ff_prof.set_preference("permissions.default.image", 3)
  75. ff_prof.set_preference("browser.download.folderList", 2)
  76. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  77. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  78. ff_prof.set_preference('network.proxy.type', 1)
  79. ff_prof.set_preference("network.proxy.socks_version", 5)
  80. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  81. ff_prof.set_preference('network.proxy.socks_port', 9150)
  82. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  83. ff_prof.set_preference("javascript.enabled", False)
  84. ff_prof.update_preferences()
  85. service = Service(config.get('TOR', 'geckodriver_path'))
  86. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  87. driver.maximize_window()
  88. return driver
  89. #the driver 'gets' the url, attempting to get on the site, if it can't access return 'down'
  90. #return: return the selenium driver or string 'down'
  91. def getAccess():
  92. url = getFixedURL()
  93. driver = createFFDriver()
  94. try:
  95. driver.get(url)
  96. return driver
  97. except:
  98. driver.close()
  99. return 'down'
  100. # Manual captcha solver, waits fora specific element so that the whole page loads, finds the input box, gets screenshot of captcha
  101. # then allows for manual solving of captcha in the terminal
  102. #@param: current selenium web driver
  103. def login(driver):
  104. # wait for first captcha page to show up (This Xpath may need to change based on different seed url)
  105. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  106. (By.XPATH, "/html/body/div/div/form/div/div[1]")))
  107. input("Press Enter once captcha done")
  108. #clicks button after captcha is inputted
  109. # driver.find_element(by=By.XPATH, value='/html/body/div/div/form/button').click()
  110. #wait for login page to show up
  111. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  112. (By.XPATH, '/html/body/div/div/div/form')))
  113. #puts username into box
  114. userBox = driver.find_element(by=By.XPATH, value='//*[@id="username"]')
  115. userBox.send_keys('ct1234')
  116. #waits for second catpcha to be inputted by user
  117. input("Press Enter once captcha done")
  118. #clicks on continue
  119. # driver.find_element(by=By.XPATH, value='/html/body/div/div/div/form/input[2]').click()
  120. #waits for password to show
  121. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  122. (By.XPATH, '/html/body/div/div/div/form/div[3]/input')))
  123. time.sleep(10) # give time for site to catch up
  124. # puts password into box
  125. passBox = driver.find_element(by=By.XPATH, value='/html/body/div/div/div/form/div[2]/input')
  126. passBox.send_keys('DementedBed123-')
  127. driver.find_element(by=By.XPATH, value='/html/body/div/div/div/form/div[3]/input').click()
  128. # wait for pin input to show
  129. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  130. (By.XPATH, '/html/body/div[1]/div/form/span')))
  131. pinBox = driver.find_element(by=By.XPATH, value='/html/body/div[1]/div/form/input[1]')
  132. pinBox.send_keys('12345')
  133. driver.find_element(by=By.XPATH, value='/html/body/div[1]/div/form/input[2]').click()
  134. # waits for main listing page before crawling to ensure everything goes well
  135. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  136. (By.XPATH, '/html/body/div[1]/div/div[2]')))
  137. # Saves the crawled html page, makes the directory path for html pages if not made
  138. def savePage(driver, page, url):
  139. cleanPage = cleanHTML(driver, page)
  140. filePath = getFullPathName(url)
  141. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  142. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  143. return
  144. # Gets the full path of the page to be saved along with its appropriate file name
  145. #@param: raw url as crawler crawls through every site
  146. def getFullPathName(url):
  147. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  148. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMKTName() + "/HTML_Pages")
  149. fileName = getNameFromURL(url)
  150. if isDescriptionLink(url):
  151. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  152. else:
  153. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  154. return fullPath
  155. # Creates the file name from passed URL, gives distinct name if can't be made unique after cleaned
  156. #@param: raw url as crawler crawls through every site
  157. def getNameFromURL(url):
  158. global counter
  159. name = ''.join(e for e in url if e.isalnum())
  160. if (name == ''):
  161. name = str(counter)
  162. counter = counter + 1
  163. return name
  164. # returns list of urls, here is where you can list the different urls of interest, the crawler runs through this list
  165. #in this example, there are a couple of categories some threads fall under such as
  166. # Guides and Tutorials, Digital Products, and Software and Malware
  167. #as you can see they are categories of products
  168. def getInterestedLinks():
  169. links = []
  170. # Digital - Fraud Software, Has Hacking and Guides
  171. links.append('http://52qlucglu6fuaqist2herssakipapig2higaaayu7446n55xw4ylxqid.onion/?category=150')
  172. # # Digital - Guides and Tutorials
  173. # links.append('http://52qlucglu6fuaqist2herssakipapig2higaaayu7446n55xw4ylxqid.onion/?category=94')
  174. # # Carding Services
  175. # links.append('http://52qlucglu6fuaqist2herssakipapig2higaaayu7446n55xw4ylxqid.onion/?category=155')
  176. # # Digital - Other (half junk half random stuff like: bots, rats, viruses, and guides)
  177. # links.append('http://52qlucglu6fuaqist2herssakipapig2higaaayu7446n55xw4ylxqid.onion/?category=153')
  178. return links
  179. # gets links of interest to crawl through, iterates through list, where each link is clicked and crawled through
  180. #topic and description pages are crawled through here, where both types of pages are saved
  181. #@param: selenium driver
  182. def crawlForum(driver):
  183. print("Crawling the ViceCity Market")
  184. linksToCrawl = getInterestedLinks()
  185. i = 0
  186. while i < len(linksToCrawl):
  187. link = linksToCrawl[i]
  188. print('Crawling :', link)
  189. try:
  190. has_next_page = True
  191. count = 0
  192. while has_next_page:
  193. try:
  194. driver.get(link)
  195. except:
  196. driver.refresh()
  197. html = driver.page_source
  198. savePage(driver, html, link)
  199. list = productPages(html)
  200. for item in list:
  201. itemURL = urlparse.urljoin(baseURL, str(item))
  202. try:
  203. driver.get(itemURL)
  204. except:
  205. driver.refresh()
  206. time.sleep(2.5) # to let page catchup
  207. savePage(driver, driver.page_source, item)
  208. time.sleep(2.5) # so site doesnt crash
  209. driver.back()
  210. # comment out
  211. # break
  212. # comment out
  213. if count == 1:
  214. break
  215. try:
  216. temp = driver.find_element(by=By.CLASS_NAME, value='pagination')
  217. link = temp.find_element(by=By.LINK_TEXT, value='Next').get_attribute('href')
  218. if link == "":
  219. raise NoSuchElementException
  220. count += 1
  221. except NoSuchElementException:
  222. has_next_page = False
  223. except Exception as e:
  224. print(link, e)
  225. i += 1
  226. print("Crawling the ViceCity market done.")
  227. # Returns 'True' if the link is a description link
  228. #@param: url of any url crawled
  229. #return: true if is a description page, false if not
  230. def isDescriptionLink(url):
  231. if 'listing' in url:
  232. return True
  233. return False
  234. # Returns True if the link is a listingPage link
  235. #@param: url of any url crawled
  236. #return: true if is a Listing page, false if not
  237. def isListingLink(url):
  238. if 'category' in url:
  239. return True
  240. return False
  241. # calling the parser to define the links, the html is the url of a link from the list of interested link list
  242. #@param: link from interested link list ie. getInterestingLinks()
  243. #return: list of description links that should be crawled through
  244. def productPages(html):
  245. soup = BeautifulSoup(html, "html.parser")
  246. return vicecity_links_parser(soup)
  247. def crawler():
  248. startCrawling()
  249. # print("Crawling and Parsing BestCardingWorld .... DONE!")