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