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 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. #
  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, baseURL, False)
  37. # Opens Tor Browser
  38. def opentor():
  39. from Forums.Initialization.forums_mining import config
  40. global pid
  41. print("Connecting Tor...")
  42. pro = subprocess.Popen(config.get('TOR', 'firefox_binary_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)# open tab with url
  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')#sends string to the username box
  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')# sends string to passwordBox
  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. # wait for 50 sec until id = tab_content is found, then cont
  82. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  83. (By.XPATH, '//*[@id="tab_content"]')))
  84. # Returns the name of the website
  85. def getForumName():
  86. name = 'CryptBB'
  87. return name
  88. # Return the link of the website
  89. def getFixedURL():
  90. url = 'http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/'
  91. return url
  92. # Closes Tor Browser
  93. def closetor(driver):
  94. # global pid
  95. # os.system("taskkill /pid " + str(pro.pid))
  96. # os.system("taskkill /t /f /im tor.exe")
  97. print('Closing Tor...')
  98. driver.close() #close tab
  99. time.sleep(3)
  100. return
  101. # Creates FireFox 'driver' and configure its 'Profile'
  102. # to use Tor proxy and socket
  103. def createFFDriver():
  104. from Forums.Initialization.forums_mining import config
  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 config, CURRENT_DATE
  149. mainDir = os.path.join(config.get('Project', 'shared_folder'), "Forums/" + getForumName() + "/HTML_Pages")
  150. fileName = getNameFromURL(url)
  151. if isDescriptionLink(url):
  152. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  153. else:
  154. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  155. return fullPath
  156. # Creates the file name from passed URL
  157. def getNameFromURL(url):
  158. global counter
  159. name = ''.join(e for e in url if e.isalnum())
  160. if name == '':
  161. name = str(counter)
  162. counter = counter + 1
  163. return name
  164. def getInterestedLinks():
  165. links = []
  166. # # Beginner Programming
  167. links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=86')
  168. # # Beginner Carding and Fraud
  169. # links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=91')
  170. # # Beginner Hacking
  171. # links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=87')
  172. # # Newbie
  173. # links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=84')
  174. # # Beginner Hardware
  175. # links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=89')
  176. # # Training Challenges
  177. # links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=96')
  178. # Darknet Discussions
  179. # links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=88')
  180. # # Public Leaks and Warez
  181. # links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=97')
  182. # # Hacked Accounts and Database Dumps
  183. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=30')
  184. # # Android Moded pak
  185. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=53')
  186. return links
  187. def crawlForum(driver):
  188. print("Crawling the CryptBB forum")
  189. linksToCrawl = getInterestedLinks()
  190. visited = set(linksToCrawl)
  191. initialTime = time.time()
  192. i = 0
  193. count = 0
  194. while i < len(linksToCrawl):
  195. link = linksToCrawl[i]
  196. print('Crawling :', link)
  197. try:
  198. try:
  199. driver.get(link)
  200. except:
  201. driver.refresh()
  202. html = driver.page_source
  203. savePage(html, link)
  204. has_next_page = True
  205. while has_next_page:
  206. list = topicPages(html)
  207. for item in list:
  208. itemURL = urlparse.urljoin(baseURL, str(item))
  209. try:
  210. driver.get(itemURL)
  211. except:
  212. driver.refresh()
  213. savePage(driver.page_source, item)
  214. driver.back()
  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. else:
  235. counter += 1
  236. except NoSuchElementException:
  237. has_next_topic_page = False
  238. # end of loop
  239. for i in range(counter):
  240. driver.back()
  241. # comment out
  242. break
  243. # comment out
  244. if count == 1:
  245. count = 0
  246. break
  247. try:
  248. temp = driver.find_element(by=By.XPATH, value = '/html/body/div/div[2]/div/div[2]/div')
  249. link = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href')
  250. if link == "":
  251. raise NoSuchElementException
  252. try:
  253. driver.get(link)
  254. except:
  255. driver.refresh()
  256. html = driver.page_source
  257. savePage(html, link)
  258. count += 1
  259. except NoSuchElementException:
  260. has_next_page = False
  261. except Exception as e:
  262. print(link, e)
  263. i += 1
  264. # finalTime = time.time()
  265. # print finalTime - initialTime
  266. input("Crawling CryptBB forum done successfully. Press ENTER to continue\n")
  267. # Returns 'True' if the link is Topic link, may need to change for every website
  268. def isDescriptionLink(url):
  269. if 'thread' in url:
  270. return True
  271. return False
  272. # Returns True if the link is a listingPage link, may need to change for every website
  273. def isListingLink(url):
  274. if 'forum' in url:
  275. return True
  276. return False
  277. # calling the parser to define the links
  278. def topicPages(html):
  279. soup = BeautifulSoup(html, "html.parser")
  280. return cryptBB_links_parser(soup)
  281. def crawler():
  282. startCrawling()
  283. # print("Crawling and Parsing BestCardingWorld .... DONE!")