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.

357 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 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. import configparser
  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. config = configparser.ConfigParser()
  23. config.read('../../setup.ini')
  24. counter = 1
  25. baseURL = 'http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/'
  26. # Opens Tor Browser, crawls the website
  27. def startCrawling():
  28. opentor()
  29. forumName = getForumName()
  30. driver = getAccess()
  31. if driver != 'down':
  32. try:
  33. login(driver)
  34. crawlForum(driver)
  35. except Exception as e:
  36. print(driver.current_url, e)
  37. closetor(driver)
  38. new_parse(forumName, baseURL, False)
  39. # Opens Tor Browser
  40. def opentor():
  41. global pid
  42. print("Connecting Tor...")
  43. pro = subprocess.Popen(config.get('TOR', 'firefox_binary_path'))
  44. pid = pro.pid
  45. time.sleep(7.5)
  46. input('Tor Connected. Press ENTER to continue\n')
  47. return
  48. # Login using premade account credentials and do login captcha manually
  49. def login(driver):
  50. #click login button
  51. login_link = driver.find_element(
  52. by=By.XPATH, value='/html/body/div/div[2]/div/table/tbody/tr[2]/td/center/pre/strong/a[1]').\
  53. get_attribute('href')
  54. driver.get(login_link)# open tab with url
  55. #entering username and password into input boxes
  56. usernameBox = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/table/tbody/tr[2]/td[2]/input')
  57. #Username here
  58. usernameBox.send_keys('holyre')#sends string to the username box
  59. passwordBox = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/table/tbody/tr[3]/td[2]/input')
  60. #Password here
  61. passwordBox.send_keys('PlatinumBorn2')# sends string to passwordBox
  62. '''
  63. # wait for captcha page show up
  64. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  65. (By.XPATH, "/html/body/div/div[2]/div/form/div/input")))
  66. # save captcha to local
  67. driver.find_element(by=By.XPATH, value='//*[@id="captcha_img"]').screenshot(r'..\CryptBB\captcha.png')
  68. # This method will show image in any image viewer
  69. im = Image.open(r'..\CryptBB\captcha.png')
  70. im.show()
  71. # wait until input space show up
  72. inputBox = driver.find_element(by=By.XPATH, value='//*[@id="imagestring"]')
  73. # ask user input captcha solution in terminal
  74. userIn = input("Enter solution: ")
  75. # send user solution into the input space
  76. inputBox.send_keys(userIn)
  77. # click the verify(submit) button
  78. driver.find_element(by=By.XPATH, value="/html/body/div/div[2]/div/form/div/input").click()
  79. '''
  80. input("Press ENTER when CAPTCHA is completed\n")
  81. # wait for listing page show up (This Xpath may need to change based on different seed url)
  82. # wait for 50 sec until id = tab_content is found, then cont
  83. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  84. (By.XPATH, '//*[@id="tab_content"]')))
  85. # Returns the name of the website
  86. def getForumName():
  87. name = 'CryptBB'
  88. return name
  89. # Return the link of the website
  90. def getFixedURL():
  91. url = 'http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/'
  92. return url
  93. # Closes Tor Browser
  94. def closetor(driver):
  95. # global pid
  96. # os.system("taskkill /pid " + str(pro.pid))
  97. # os.system("taskkill /t /f /im tor.exe")
  98. print('Closing Tor...')
  99. driver.close() #close tab
  100. time.sleep(3)
  101. return
  102. # Creates FireFox 'driver' and configure its 'Profile'
  103. # to use Tor proxy and socket
  104. def createFFDriver():
  105. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  106. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  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(config.get('TOR', 'geckodriver_path'))
  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. from Forums.Initialization.forums_mining import CURRENT_DATE
  149. fileName = getNameFromURL(url)
  150. if isDescriptionLink(url):
  151. fullPath = r'..\\CryptBB\\HTML_Pages\\' + CURRENT_DATE + r'\\Description\\' + fileName + '.html'
  152. else:
  153. fullPath = r'..\\CryptBB\\HTML_Pages\\' + 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. '''
  215. #variable to check if there is a next page for the topic
  216. has_next_topic_page = True
  217. counter = 1
  218. # check if there is a next page for the topics
  219. while has_next_topic_page:
  220. # try to access next page of th topic
  221. itemURL = urlparse.urljoin(baseURL, str(item))
  222. try:
  223. driver.get(itemURL)
  224. except:
  225. driver.refresh()
  226. savePage(driver.page_source, item)
  227. # if there is a next page then go and save....
  228. # next page in the topic?
  229. try:
  230. temp = driver.find_element(By.XPATH, '/html/body/div/div[2]/div/div[2]/div') # /html/body/div/div[2]/div/div[2]/div/
  231. item = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href') #/html/body/div/div[2]/div/div[2]/div
  232. if item == "":
  233. raise NoSuchElementException
  234. has_next_topic_page = False
  235. else:
  236. counter += 1
  237. except NoSuchElementException:
  238. has_next_topic_page = False
  239. # end of loop
  240. for i in range(counter):
  241. driver.back()
  242. '''
  243. # comment out
  244. break
  245. # comment out
  246. if count == 1:
  247. count = 0
  248. break
  249. try:
  250. temp = driver.find_element(by=By.XPATH, value = '/html/body/div/div[2]/div/div[2]/div')
  251. link = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href')
  252. if link == "":
  253. raise NoSuchElementException
  254. try:
  255. driver.get(link)
  256. except:
  257. driver.refresh()
  258. html = driver.page_source
  259. savePage(html, link)
  260. count += 1
  261. except NoSuchElementException:
  262. has_next_page = False
  263. except Exception as e:
  264. print(link, e)
  265. i += 1
  266. # finalTime = time.time()
  267. # print finalTime - initialTime
  268. input("Crawling CryptBB forum done successfully. Press ENTER to continue\n")
  269. # Returns 'True' if the link is Topic link, may need to change for every website
  270. def isDescriptionLink(url):
  271. if 'thread' in url:
  272. return True
  273. return False
  274. # Returns True if the link is a listingPage link, may need to change for every website
  275. def isListingLink(url):
  276. if 'forum' in url:
  277. return True
  278. return False
  279. # calling the parser to define the links
  280. def topicPages(html):
  281. soup = BeautifulSoup(html, "html.parser")
  282. return cryptBB_links_parser(soup)
  283. def crawler():
  284. startCrawling()
  285. # print("Crawling and Parsing BestCardingWorld .... DONE!")