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.

324 lines
11 KiB

  1. __author__ = 'DarkWeb'
  2. '''
  3. HiddenMarket 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.webdriver.common.by import By
  11. from selenium.webdriver.support import expected_conditions as EC
  12. from selenium.webdriver.support.ui import WebDriverWait
  13. from PIL import Image
  14. import urllib.parse as urlparse
  15. import os, re, time
  16. import subprocess
  17. import configparser
  18. from bs4 import BeautifulSoup
  19. from MarketPlaces.Initialization.prepare_parser import new_parse
  20. from MarketPlaces.HiddenMarket.parser import hiddenmarket_links_parser
  21. from MarketPlaces.Utilities.utilities import cleanHTML
  22. counter = 1
  23. baseURL = 'http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/'
  24. # Opens Tor Browser, crawls the website
  25. def startCrawling():
  26. opentor()
  27. marketName = 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. closetor(driver)
  36. # new_parse(marketName, baseURL, False)
  37. # Opens Tor Browser
  38. def opentor():
  39. from MarketPlaces.Initialization.markets_mining import config
  40. global pid
  41. print("Connecting Tor...")
  42. pro = subprocess.Popen(config.get('TOR', 'firefox_binary_path'))
  43. pid = pro.pid
  44. time.sleep(7.5)
  45. input('Tor Connected. Press ENTER to continue\n')
  46. return
  47. # Login using premade account credentials and do login captcha manually
  48. def login(driver):
  49. # wait for login page
  50. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  51. (By.XPATH, "/html/body/div[3]/div[3]")))
  52. # entering username and password into input boxes
  53. # usernameBox = driver.find_element(by=By.XPATH, value='//*[@id="username"]')
  54. # Username here
  55. # usernameBox.send_keys('ct1234')
  56. # passwordBox = driver.find_element(by=By.XPATH, value='//*[@id="password"]')
  57. # Password here
  58. # passwordBox.send_keys('DementedBed1230')
  59. '''
  60. # wait for captcha page show up
  61. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  62. (By.XPATH, "/html/body/main/div/div/div/div/div/form/div[3]/div/div[1]/label/img")))
  63. # save captcha to local
  64. driver.find_element(by=By.XPATH, value='/html/body/main/div/div/div/div/div/form/div[3]/div/div[1]/label/img').screenshot(
  65. r'..\captcha.png')
  66. # This method will show image in any image viewer
  67. im = Image.open(r'..\captcha.png')
  68. im.show()
  69. # wait until input space show up
  70. inputBox = driver.find_element(by=By.XPATH, value='//*[@id="captcha"]')
  71. # ask user input captcha solution in terminal
  72. userIn = input("Enter solution: ")
  73. # send user solution into the input space
  74. inputBox.send_keys(userIn)
  75. # click the verify(submit) button
  76. driver.find_element(by=By.XPATH, value="/html/body/main/div/div/div/div/div/form/div[4]/button").click()
  77. '''
  78. # input("Press ENTER when CAPTCHA is completed\n")
  79. # wait for listing page show up (This Xpath may need to change based on different seed url)
  80. # WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  81. # (By.XPATH, '/html/body/main/div/div/div[1]/div/div[1]/div/h5')))
  82. # Returns the name of the website
  83. def getMKTName():
  84. name = 'HiddenMarket'
  85. return name
  86. # Return the link of the website
  87. def getFixedURL():
  88. url = 'http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/'
  89. return url
  90. # Closes Tor Browser
  91. def closetor(driver):
  92. # global pid
  93. # os.system("taskkill /pid " + str(pro.pid))
  94. # os.system("taskkill /t /f /im tor.exe")
  95. print('Closing Tor...')
  96. driver.quit()
  97. time.sleep(3)
  98. return
  99. # Creates FireFox 'driver' and configure its 'Profile'
  100. # to use Tor proxy and socket
  101. def createFFDriver():
  102. from MarketPlaces.Initialization.markets_mining import config
  103. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  104. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  105. # ff_prof.set_preference("places.history.enabled", False)
  106. # ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  107. # ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  108. # ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  109. # ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  110. # ff_prof.set_preference("signon.rememberSignons", False)
  111. # ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  112. # ff_prof.set_preference("network.dns.disablePrefetch", True)
  113. # ff_prof.set_preference("network.http.sendRefererHeader", 0)
  114. # ff_prof.set_preference("permissions.default.image", 3)
  115. # ff_prof.set_preference("browser.download.folderList", 2)
  116. # ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  117. # ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  118. ff_prof.set_preference('network.proxy.type', 1)
  119. ff_prof.set_preference("network.proxy.socks_version", 5)
  120. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  121. ff_prof.set_preference('network.proxy.socks_port', 9150)
  122. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  123. ff_prof.set_preference("javascript.enabled", False)
  124. ff_prof.update_preferences()
  125. service = Service(config.get('TOR', 'geckodriver_path'))
  126. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  127. return driver
  128. def getAccess():
  129. url = getFixedURL()
  130. driver = createFFDriver()
  131. try:
  132. driver.get(url)
  133. return driver
  134. except:
  135. driver.close()
  136. return 'down'
  137. # Saves the crawled html page
  138. def savePage(page, url):
  139. cleanPage = cleanHTML(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. def getFullPathName(url):
  146. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  147. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMKTName() + "/HTML_Pages")
  148. fileName = getNameFromURL(url)
  149. if isDescriptionLink(url):
  150. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  151. else:
  152. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  153. return fullPath
  154. # Creates the file name from passed URL
  155. def getNameFromURL(url):
  156. global counter
  157. name = ''.join(e for e in url if e.isalnum())
  158. if name == '':
  159. name = str(counter)
  160. counter = counter + 1
  161. return name
  162. def getInterestedLinks():
  163. links = []
  164. # # Civil Software
  165. # links.append('http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/category/civil_softwares')
  166. # # Tutorials - Carding
  167. # links.append('http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/category/carding')
  168. # # Digital - Hacks
  169. links.append('http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/category/hacks')
  170. # Digital - Exploit Kit
  171. # links.append('http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/category/exploit_kit')
  172. # # 0Day
  173. # links.append('http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/category/0day')
  174. # Digital Forensics
  175. # links.append('http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/category/digital_forensics')
  176. # Tutorials - Mining
  177. # links.append('http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/category/mining')
  178. # Tutorials - Worms
  179. # links.append('http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/category/worms')
  180. # Tutorials - Viruses
  181. # links.append('http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/category/viruses')
  182. # Tutorials - Trojans
  183. # links.append('http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/category/trojans')
  184. # Tutorials - Botnets
  185. # links.append('http://mipx6eedtsvfgfcmbm3utjedgjez2w4dzjdrbhtd2mt3cicwhhzspxqd.onion/category/botnets')
  186. return links
  187. def crawlForum(driver):
  188. print("Crawling the HiddenMarket market")
  189. linksToCrawl = getInterestedLinks()
  190. i = 0
  191. while i < len(linksToCrawl):
  192. link = linksToCrawl[i]
  193. print('Crawling :', link)
  194. categoryLink = link
  195. maxNumPages = 0 # temp value.
  196. try:
  197. has_next_page = True
  198. count = 0
  199. pageCount = 1
  200. while has_next_page:
  201. try:
  202. driver.get(link)
  203. if pageCount == 1:
  204. maxNumPages = int(driver.find_element(by=By.CLASS_NAME, value='main')
  205. .find_element(by=By.CLASS_NAME, value='pages')
  206. .find_elements(By.CLASS_NAME, value='page')[-1].text)
  207. except:
  208. driver.refresh()
  209. html = driver.page_source
  210. savePage(html, link)
  211. list = productPages(html)
  212. for item in list:
  213. itemURL = urlparse.urljoin(baseURL, str(item))
  214. try:
  215. driver.get(itemURL)
  216. except:
  217. driver.refresh()
  218. savePage(driver.page_source, item)
  219. driver.back()
  220. # comment out
  221. break
  222. # comment out
  223. # if count == 1:
  224. # break
  225. try:
  226. pageCount += 1
  227. if pageCount > maxNumPages:
  228. raise NoSuchElementException
  229. pageLink = "/" + str(pageCount) + "/"
  230. link = categoryLink + pageLink
  231. count += 1
  232. except NoSuchElementException:
  233. has_next_page = False
  234. except Exception as e:
  235. print(link, e)
  236. i += 1
  237. input("Crawling HiddenMarket market done sucessfully. Press ENTER to continue\n")
  238. # Returns 'True' if the link is Topic link
  239. def isDescriptionLink(url):
  240. if 'product' in url:
  241. return True
  242. return False
  243. # Returns True if the link is a listingPage link
  244. def isListingLink(url):
  245. if 'category' in url:
  246. return True
  247. return False
  248. # calling the parser to define the links
  249. def productPages(html):
  250. soup = BeautifulSoup(html, "html.parser")
  251. return hiddenmarket_links_parser(soup)
  252. def crawler():
  253. startCrawling()
  254. # print("Crawling and Parsing BestCardingWorld .... DONE!")