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.

331 lines
11 KiB

  1. __author__ = 'DarkWeb'
  2. '''
  3. CryptBB 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.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. from bs4 import BeautifulSoup
  18. from Forums.Initialization.prepare_parser import new_parse
  19. from Forums.CryptBB.parser import cryptBB_links_parser
  20. from Forums.Utilities.utilities import cleanHTML
  21. counter = 1
  22. baseURL = 'http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/'
  23. # Opens Tor Browser, crawls the website
  24. def startCrawling():
  25. forumName = getForumName()
  26. driver = getAccess()
  27. if driver != 'down':
  28. try:
  29. login(driver)
  30. crawlForum(driver)
  31. except Exception as e:
  32. print(driver.current_url, e)
  33. closeDriver(driver)
  34. new_parse(forumName, baseURL, True)
  35. # Login using premade account credentials and do login captcha manually
  36. def login(driver):
  37. #click login button
  38. login_link = driver.find_element(
  39. by=By.XPATH, value='/html/body/div/div[2]/div/table/tbody/tr[2]/td/center/pre/strong/a[1]').\
  40. get_attribute('href')
  41. driver.get(login_link)# open tab with url
  42. #entering username and password into input boxes
  43. usernameBox = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/table/tbody/tr[2]/td[2]/input')
  44. #Username here
  45. usernameBox.send_keys('holyre')#sends string to the username box
  46. passwordBox = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/table/tbody/tr[3]/td[2]/input')
  47. #Password here
  48. passwordBox.send_keys('PlatinumBorn2')# sends string to passwordBox
  49. '''
  50. # wait for captcha page show up
  51. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  52. (By.XPATH, "/html/body/div/div[2]/div/form/div/input")))
  53. # save captcha to local
  54. driver.find_element(by=By.XPATH, value='//*[@id="captcha_img"]').screenshot(r'..\CryptBB\captcha.png')
  55. # This method will show image in any image viewer
  56. im = Image.open(r'..\CryptBB\captcha.png')
  57. im.show()
  58. # wait until input space show up
  59. inputBox = driver.find_element(by=By.XPATH, value='//*[@id="imagestring"]')
  60. # ask user input captcha solution in terminal
  61. userIn = input("Enter solution: ")
  62. # send user solution into the input space
  63. inputBox.send_keys(userIn)
  64. # click the verify(submit) button
  65. driver.find_element(by=By.XPATH, value="/html/body/div/div[2]/div/form/div/input").click()
  66. '''
  67. input("Press ENTER when CAPTCHA is completed\n")
  68. # wait for listing page show up (This Xpath may need to change based on different seed url)
  69. # wait for 50 sec until id = tab_content is found, then cont
  70. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  71. (By.XPATH, '//*[@id="tab_content"]')))
  72. # Returns the name of the website
  73. def getForumName() -> str:
  74. name = 'CryptBB'
  75. return name
  76. # Return the link of the website
  77. def getFixedURL():
  78. url = 'http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/'
  79. return url
  80. # Closes Tor Browser
  81. def closeDriver(driver):
  82. # global pid
  83. # os.system("taskkill /pid " + str(pro.pid))
  84. # os.system("taskkill /t /f /im tor.exe")
  85. print('Closing Tor...')
  86. driver.close() #close tab
  87. time.sleep(3)
  88. return
  89. # Creates FireFox 'driver' and configure its 'Profile'
  90. # to use Tor proxy and socket
  91. def createFFDriver():
  92. from Forums.Initialization.forums_mining import config
  93. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  94. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  95. ff_prof.set_preference("places.history.enabled", False)
  96. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  97. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  98. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  99. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  100. ff_prof.set_preference("signon.rememberSignons", False)
  101. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  102. ff_prof.set_preference("network.dns.disablePrefetch", True)
  103. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  104. ff_prof.set_preference("permissions.default.image", 3)
  105. ff_prof.set_preference("browser.download.folderList", 2)
  106. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  107. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  108. ff_prof.set_preference('network.proxy.type', 1)
  109. ff_prof.set_preference("network.proxy.socks_version", 5)
  110. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  111. ff_prof.set_preference('network.proxy.socks_port', 9150)
  112. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  113. ff_prof.set_preference("javascript.enabled", True)
  114. ff_prof.update_preferences()
  115. service = Service(config.get('TOR', 'geckodriver_path'))
  116. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  117. driver.maximize_window()
  118. return driver
  119. def getAccess():
  120. url = getFixedURL()
  121. driver = createFFDriver()
  122. try:
  123. driver.get(url)
  124. return driver
  125. except:
  126. driver.close()
  127. return 'down'
  128. # Saves the crawled html page
  129. def savePage(driver, page, url):
  130. cleanPage = cleanHTML(driver, page)
  131. filePath = getFullPathName(url)
  132. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  133. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  134. return
  135. # Gets the full path of the page to be saved along with its appropriate file name
  136. def getFullPathName(url):
  137. from Forums.Initialization.forums_mining import config, CURRENT_DATE
  138. mainDir = os.path.join(config.get('Project', 'shared_folder'), "Forums/" + getForumName() + "/HTML_Pages")
  139. fileName = getNameFromURL(url)
  140. if not isListingLink(url):
  141. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  142. else:
  143. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  144. return fullPath
  145. # Creates the file name from passed URL
  146. def getNameFromURL(url):
  147. global counter
  148. name = ''.join(e for e in url if e.isalnum())
  149. if name == '':
  150. name = str(counter)
  151. counter = counter + 1
  152. return name
  153. def getInterestedLinks():
  154. links = []
  155. # Beginner Programming
  156. links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=86')
  157. # Beginner Carding and Fraud
  158. links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=91')
  159. # Beginner Hacking
  160. links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=87')
  161. # Newbie
  162. links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=84')
  163. # Beginner Hardware
  164. links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=89')
  165. # Training Challenges
  166. links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=96')
  167. # Darknet Discussions
  168. links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=88')
  169. # Public Leaks and Warez
  170. links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=97')
  171. # Sell
  172. links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=44')
  173. return links
  174. def crawlForum(driver):
  175. print("Crawling the CryptBB forum")
  176. linksToCrawl = getInterestedLinks()
  177. i = 0
  178. while i < len(linksToCrawl):
  179. link = linksToCrawl[i]
  180. print('Crawling :', link)
  181. try:
  182. has_next_page = True
  183. count = 0
  184. while has_next_page:
  185. try:
  186. driver.get(link)
  187. except:
  188. driver.refresh()
  189. html = driver.page_source
  190. savePage(driver, html, link)
  191. topics = topicPages(html)
  192. for topic in topics:
  193. has_next_topic_page = True
  194. counter = 1
  195. page = topic
  196. while has_next_topic_page:
  197. itemURL = urlparse.urljoin(baseURL, str(page))
  198. try:
  199. driver.get(itemURL)
  200. except:
  201. driver.refresh()
  202. if isListingLink(driver.current_url):
  203. break
  204. savePage(driver, driver.page_source, topic + f"page{counter}") # very important
  205. # # comment out
  206. # if counter == 2:
  207. # break
  208. try:
  209. temp = driver.find_element(By.XPATH, '/html/body/div/div[2]/div/div[2]/div')
  210. page = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href')
  211. if page == "":
  212. raise NoSuchElementException
  213. counter += 1
  214. except NoSuchElementException:
  215. has_next_topic_page = False
  216. # making sure we go back to the listing page (browser back button simulation)
  217. try:
  218. driver.get(link)
  219. except:
  220. driver.refresh()
  221. # # comment out
  222. # break
  223. #
  224. # # comment out
  225. # if count == 1:
  226. # break
  227. try:
  228. temp = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/div[2]/div')
  229. link = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href')
  230. if link == "":
  231. raise NoSuchElementException
  232. count += 1
  233. except NoSuchElementException:
  234. has_next_page = False
  235. except Exception as e:
  236. print(link, e)
  237. i += 1
  238. print("Crawling the CrypttBB forum done.")
  239. # Returns 'True' if the link is Topic link, may need to change for every website
  240. def isDescriptionLink(url):
  241. if 'thread' in url:
  242. return True
  243. return False
  244. # Returns True if the link is a listingPage link, may need to change for every website
  245. def isListingLink(url):
  246. if '.onion/forumdisplay' in url:
  247. return True
  248. return False
  249. # calling the parser to define the links
  250. def topicPages(html):
  251. soup = BeautifulSoup(html, "html.parser")
  252. return cryptBB_links_parser(soup)
  253. def crawler():
  254. startCrawling()
  255. # print("Crawling and Parsing BestCardingWorld .... DONE!")