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.

337 lines
11 KiB

  1. __author__ = 'DarkWeb'
  2. '''
  3. Kerberos 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, time
  16. from datetime import date
  17. import subprocess
  18. from bs4 import BeautifulSoup
  19. from MarketPlaces.Initialization.prepare_parser import new_parse
  20. from MarketPlaces.Kerberos.parser import kerberos_links_parser
  21. from MarketPlaces.Utilities.utilities import cleanHTML
  22. counter = 1
  23. baseURL = 'http://kerberosazmnfrjinmftp3im3cr7hw4nxbavm4ngofn64g24be7h3kqd.onion'
  24. # Opens Tor Browser, crawls the website
  25. def startCrawling():
  26. opentor()
  27. # marketName = getMarketName()
  28. driver = getAccess()
  29. if driver != 'down':
  30. captcha(driver)
  31. login(driver)
  32. crawlForum(driver)
  33. # new_parse(marketName, False)
  34. closetor(driver)
  35. # Opens Tor Browser
  36. def opentor():
  37. global pid
  38. print("Connecting Tor...")
  39. path = open('../../path.txt').readline().strip()
  40. pro = subprocess.Popen(path)
  41. pid = pro.pid
  42. time.sleep(7.5)
  43. input('Tor Connected. Press ENTER to continue\n')
  44. return
  45. def captcha(driver):
  46. # wait for captcha page
  47. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  48. (By.XPATH, "/html/body/div/div/img")))
  49. # too hard to code, requires manual completion
  50. # wait for login page
  51. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  52. (By.XPATH, "/html/body/div[1]/div[2]/div/form/div[10]/button")))
  53. # Login using premade account credentials and do login captcha manually
  54. def login(driver):
  55. #wait for login page
  56. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  57. (By.XPATH, "/html/body/div[1]/div[2]/div/form/div[10]/button")))
  58. #entering username and password into input boxes
  59. usernameBox = driver.find_element(by=By.XPATH, value='/html/body/div[1]/div[2]/div/form/input[1]')
  60. #Username here
  61. usernameBox.send_keys('blabri')
  62. passwordBox = driver.find_element(by=By.XPATH, value='/html/body/div[1]/div[2]/div/form/input[2]')
  63. #Password here
  64. passwordBox.send_keys('fishowal')
  65. # wait for captcha page show up
  66. # WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  67. # (By.XPATH, "/html/body/div/img[24]")))
  68. time.sleep(10)
  69. # save captcha to local
  70. driver.find_element(by=By.XPATH, value='/html/body/div[1]/div[2]/div/form/div[6]').screenshot(
  71. r'..\Kerberos\captcha.png')
  72. # This method will show image in any image viewer
  73. im = Image.open(r'..\Kerberos\captcha.png')
  74. im.show()
  75. # wait until input space show up
  76. inputBox = driver.find_element(by=By.XPATH, value='/html/body/div[1]/div[2]/div/form/input[3]')
  77. # ask user input captcha solution in terminal
  78. userIn = input("Enter solution: ")
  79. # send user solution into the input space
  80. inputBox.send_keys(userIn)
  81. # click the verify(submit) button
  82. driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  83. driver.find_element(by=By.XPATH, value="/html/body/div[1]/div[2]/div/form/div[10]/button").click()
  84. # wait for listing page show up (This Xpath may need to change based on different seed url)
  85. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  86. (By.XPATH, '//*[@id="breadcrumb"]')))
  87. # Returns the name of the website
  88. def getMarketName():
  89. name = 'Kerberos'
  90. return name
  91. # Return the link of the website
  92. def getFixedURL():
  93. url = 'http://kerberosazmnfrjinmftp3im3cr7hw4nxbavm4ngofn64g24be7h3kqd.onion'
  94. return url
  95. # Closes Tor Browser
  96. def closetor(driver):
  97. # global pid
  98. # os.system("taskkill /pid " + str(pro.pid))
  99. # os.system("taskkill /t /f /im tor.exe")
  100. print('Closing Tor...')
  101. driver.quit()
  102. time.sleep(3)
  103. return
  104. # Creates FireFox 'driver' and configure its 'Profile'
  105. # to use Tor proxy and socket
  106. def createFFDriver():
  107. file = open('../../path.txt', 'r')
  108. lines = file.readlines()
  109. ff_binary = FirefoxBinary(lines[0].strip())
  110. ff_prof = FirefoxProfile(lines[1].strip())
  111. ff_prof.set_preference("places.history.enabled", False)
  112. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  113. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  114. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  115. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  116. ff_prof.set_preference("signon.rememberSignons", False)
  117. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  118. ff_prof.set_preference("network.dns.disablePrefetch", True)
  119. # ff_prof.set_preference("network.http.sendRefererHeader", 0)
  120. ff_prof.set_preference("permissions.default.image", 3)
  121. ff_prof.set_preference("browser.download.folderList", 2)
  122. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  123. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  124. ff_prof.set_preference('network.proxy.type', 1)
  125. ff_prof.set_preference("network.proxy.socks_version", 5)
  126. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  127. ff_prof.set_preference('network.proxy.socks_port', 9150)
  128. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  129. ff_prof.set_preference("javascript.enabled", False)
  130. ff_prof.update_preferences()
  131. service = Service(executable_path=lines[2].strip())
  132. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  133. return driver
  134. def getAccess():
  135. url = getFixedURL()
  136. driver = createFFDriver()
  137. try:
  138. driver.get(url)
  139. return driver
  140. except:
  141. return 'down'
  142. # Saves the crawled html page
  143. def savePage(page, url):
  144. cleanPage = cleanHTML(page)
  145. filePath = getFullPathName(url)
  146. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  147. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  148. return
  149. # Gets the full path of the page to be saved along with its appropriate file name
  150. def getFullPathName(url):
  151. fileName = getNameFromURL(url)
  152. if isDescriptionLink(url):
  153. fullPath = r'..\Kerberos\HTML_Pages\\' + str(
  154. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  155. "%04d" % date.today().year) + r'\\' + r'Description\\' + fileName + '.html'
  156. else:
  157. fullPath = r'..\Kerberos\HTML_Pages\\' + str(
  158. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  159. "%04d" % date.today().year) + r'\\' + r'Listing\\' + fileName + '.html'
  160. return fullPath
  161. # Creates the file name from passed URL
  162. def getNameFromURL(url):
  163. global counter
  164. name = ''.join(e for e in url if e.isalnum())
  165. if name == '':
  166. name = str(counter)
  167. counter = counter + 1
  168. return name
  169. def getInterestedLinks():
  170. links = []
  171. # Services - Hacking
  172. links.append('http://kerberosazmnfrjinmftp3im3cr7hw4nxbavm4ngofn64g24be7h3kqd.onion/categories/99/block/price-none/ww/ww/1/')
  173. # Tutorials - Hacking
  174. links.append('http://kerberosazmnfrjinmftp3im3cr7hw4nxbavm4ngofn64g24be7h3kqd.onion/categories/122/block/price-none/ww/ww/1/')
  175. # Tutorials - Guides
  176. links.append('http://kerberosazmnfrjinmftp3im3cr7hw4nxbavm4ngofn64g24be7h3kqd.onion/categories/124/block/price-none/ww/ww/1/')
  177. # Tutorials - Other
  178. links.append('http://kerberosazmnfrjinmftp3im3cr7hw4nxbavm4ngofn64g24be7h3kqd.onion/categories/126/block/price-none/ww/ww/1/')
  179. # Software and Malware - Botnets
  180. links.append('http://kerberosazmnfrjinmftp3im3cr7hw4nxbavm4ngofn64g24be7h3kqd.onion/categories/129/block/price-none/ww/ww/1/')
  181. # Software and Malware - Malware
  182. links.append('http://kerberosazmnfrjinmftp3im3cr7hw4nxbavm4ngofn64g24be7h3kqd.onion/categories/130/block/price-none/ww/ww/1/')
  183. # Software and Malware - Trojans
  184. links.append('http://kerberosazmnfrjinmftp3im3cr7hw4nxbavm4ngofn64g24be7h3kqd.onion/categories/131/block/price-none/ww/ww/1/')
  185. # Software and Malware - Exploits / Kits
  186. links.append('http://kerberosazmnfrjinmftp3im3cr7hw4nxbavm4ngofn64g24be7h3kqd.onion/categories/133/block/price-none/ww/ww/1/')
  187. # Software and Malware - Other
  188. links.append('http://kerberosazmnfrjinmftp3im3cr7hw4nxbavm4ngofn64g24be7h3kqd.onion/categories/136/block/price-none/ww/ww/1/')
  189. return links
  190. def crawlForum(driver):
  191. print("Crawling the Kerberos market")
  192. linksToCrawl = getInterestedLinks()
  193. visited = set(linksToCrawl)
  194. initialTime = time.time()
  195. i = 0
  196. count = 0
  197. while i < len(linksToCrawl):
  198. link = linksToCrawl[i]
  199. print('Crawling :', link)
  200. try:
  201. try:
  202. driver.get(link)
  203. except:
  204. driver.refresh()
  205. html = driver.page_source
  206. savePage(html, link)
  207. has_next_page = True
  208. while has_next_page:
  209. list = productPages(html)
  210. for item in list:
  211. itemURL = urlparse.urljoin(baseURL, str(item))
  212. try:
  213. driver.get(itemURL)
  214. except:
  215. driver.refresh()
  216. savePage(driver.page_source, item)
  217. driver.back()
  218. # break
  219. if count == 1:
  220. count = 0
  221. break
  222. try:
  223. nav = driver.find_element(by=By.XPATH, value=
  224. '/html/body/div[3]/div[4]/div[4]/div/div[1]/div[28]')
  225. a = nav.find_element(by=By.LINK_TEXT, value="Next")
  226. link = a.get_attribute('href')
  227. if link == "":
  228. raise NoSuchElementException
  229. try:
  230. driver.get(link)
  231. except:
  232. driver.refresh()
  233. html = driver.page_source
  234. savePage(html, link)
  235. count += 1
  236. except NoSuchElementException:
  237. has_next_page = False
  238. except Exception as e:
  239. print(link, e)
  240. i += 1
  241. # finalTime = time.time()
  242. # print finalTime - initialTime
  243. input("Crawling Kerberos market done sucessfully. Press ENTER to continue\n")
  244. # Returns 'True' if the link is Topic link
  245. def isDescriptionLink(url):
  246. if 'item' in url:
  247. return True
  248. return False
  249. # Returns True if the link is a listingPage link
  250. def isListingLink(url):
  251. if 'categories' in url:
  252. return True
  253. return False
  254. # calling the parser to define the links
  255. def productPages(html):
  256. soup = BeautifulSoup(html, "html.parser")
  257. return kerberos_links_parser(soup)
  258. def crawler():
  259. startCrawling()
  260. # print("Crawling and Parsing BestCardingWorld .... DONE!")