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.

360 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. linksToCrawl = getInterestedLinks()
  189. visited = set(linksToCrawl)
  190. initialTime = time.time()
  191. i = 0
  192. count = 0
  193. while i < len(linksToCrawl):
  194. link = linksToCrawl[i]
  195. print('Crawling :', link)
  196. try:
  197. try:
  198. driver.get(link)
  199. except:
  200. driver.refresh()
  201. html = driver.page_source
  202. savePage(html, link)
  203. has_next_page = True
  204. while has_next_page:
  205. list = topicPages(html)
  206. for item in list:
  207. itemURL = urlparse.urljoin(baseURL, str(item))
  208. try:
  209. driver.get(itemURL)
  210. except:
  211. driver.refresh()
  212. savePage(driver.page_source, item)
  213. driver.back()
  214. #variable to check if there is a next page for the topic
  215. has_next_topic_page = True
  216. counter = 1
  217. # check if there is a next page for the topics
  218. while has_next_topic_page:
  219. # try to access next page of th topic
  220. itemURL = urlparse.urljoin(baseURL, str(item))
  221. try:
  222. driver.get(itemURL)
  223. except:
  224. driver.refresh()
  225. savePage(driver.page_source, item)
  226. # if there is a next page then go and save....
  227. # next page in the topic?
  228. try:
  229. temp = driver.find_element(By.XPATH, '/html/body/div/div[2]/div/div[2]/div') # /html/body/div/div[2]/div/div[2]/div/
  230. item = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href') #/html/body/div/div[2]/div/div[2]/div
  231. if item == "":
  232. raise NoSuchElementException
  233. else:
  234. counter += 1
  235. except NoSuchElementException:
  236. has_next_topic_page = False
  237. # end of loop
  238. for i in range(counter):
  239. driver.back()
  240. # comment out
  241. break
  242. # comment out
  243. if count == 1:
  244. count = 0
  245. break
  246. try:
  247. temp = driver.find_element(by=By.XPATH, value = '/html/body/div/div[2]/div/div[2]/div')
  248. link = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href')
  249. if link == "":
  250. raise NoSuchElementException
  251. try:
  252. driver.get(link)
  253. except:
  254. driver.refresh()
  255. html = driver.page_source
  256. savePage(html, link)
  257. count += 1
  258. except NoSuchElementException:
  259. has_next_page = False
  260. except Exception as e:
  261. print(link, e)
  262. i += 1
  263. # finalTime = time.time()
  264. # print finalTime - initialTime
  265. input("Crawling CryptBB forum done successfully. Press ENTER to continue\n")
  266. # Returns 'True' if the link is Topic link, may need to change for every website
  267. def isDescriptionLink(url):
  268. if 'thread' in url:
  269. return True
  270. return False
  271. # Returns True if the link is a listingPage link, may need to change for every website
  272. def isListingLink(url):
  273. if 'forum' in url:
  274. return True
  275. return False
  276. # calling the parser to define the links
  277. def topicPages(html):
  278. soup = BeautifulSoup(html, "html.parser")
  279. return cryptBB_links_parser(soup)
  280. def crawler():
  281. startCrawling()
  282. # print("Crawling and Parsing BestCardingWorld .... DONE!")