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.

340 lines
12 KiB

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