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.

329 lines
11 KiB

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