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.

297 lines
10 KiB

  1. __author__ = 'DarkWeb'
  2. '''
  3. DWForums 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. import urllib.parse as urlparse
  14. import os, time
  15. from datetime import date
  16. import subprocess
  17. from bs4 import BeautifulSoup
  18. from Forums.Initialization.prepare_parser import new_parse
  19. from Forums.DWForums.parser import dwForums_links_parser
  20. from Forums.Utilities.utilities import cleanHTML
  21. counter = 1
  22. baseURL = 'http://dwforumuugiyderhybcpfxmlmoawgq6z3w6hk45nrnem3p7kwszhybad.onion/'
  23. # Opens Tor Browser, crawls the website
  24. def startCrawling():
  25. forumName = getForumName()
  26. driver = getAccess()
  27. if driver != 'down':
  28. try:
  29. login(driver)
  30. crawlForum(driver)
  31. except Exception as e:
  32. print(driver.current_url, e)
  33. closeDriver(driver)
  34. new_parse(forumName, baseURL, False)
  35. # Login using premade account credentials and do login captcha manually
  36. def login(driver):
  37. #click login button
  38. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  39. (By.CSS_SELECTOR, ".button--icon--user")))
  40. login_link = driver.find_element(by=By.CSS_SELECTOR, value=".button--icon--user")
  41. login_link.click()
  42. #entering username and password into input boxes
  43. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  44. (By.XPATH, "/html/body/div[4]/div/div[2]/div/form/div[1]")))
  45. container = driver.find_element(by=By.XPATH, value="/html/body/div[4]/div/div[2]/div/form/div[1]")
  46. # print(container.get_attribute("outerHTML"))
  47. boxes = container.find_elements(by=By.CLASS_NAME, value="input")
  48. # print(len(boxes))
  49. #Username here
  50. boxes[0].send_keys('nice_reamer08')
  51. #Password here
  52. boxes[1].send_keys('tjpv$]Nc}XG@`%LM')
  53. # no captcha on this site
  54. # click the verify(submit) button
  55. driver.find_element(by=By.CSS_SELECTOR, value=".button--icon--login").click()
  56. # wait for listing page show up (This Xpath may need to change based on different seed url)
  57. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  58. (By.CSS_SELECTOR, '.p-staffBar-inner > div:nth-child(4) > div:nth-child(1) > a:nth-child(1)')))
  59. # Returns the name of the website
  60. def getForumName():
  61. name = 'DWForums'
  62. return name
  63. # Return the link of the website
  64. def getFixedURL():
  65. url = 'http://dwforumuugiyderhybcpfxmlmoawgq6z3w6hk45nrnem3p7kwszhybad.onion/'
  66. return url
  67. # Closes Tor Browser
  68. def closeDriver(driver):
  69. # global pid
  70. # os.system("taskkill /pid " + str(pro.pid))
  71. # os.system("taskkill /t /f /im tor.exe")
  72. print('Closing Tor...')
  73. driver.close()
  74. time.sleep(3)
  75. return
  76. # Creates FireFox 'driver' and configure its 'Profile'
  77. # to use Tor proxy and socket
  78. def createFFDriver():
  79. from Forums.Initialization.forums_mining import config
  80. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  81. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  82. ff_prof.set_preference("places.history.enabled", False)
  83. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  84. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  85. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  86. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  87. ff_prof.set_preference("signon.rememberSignons", False)
  88. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  89. ff_prof.set_preference("network.dns.disablePrefetch", True)
  90. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  91. ff_prof.set_preference("permissions.default.image", 3)
  92. ff_prof.set_preference("browser.download.folderList", 2)
  93. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  94. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  95. ff_prof.set_preference('network.proxy.type', 1)
  96. ff_prof.set_preference("network.proxy.socks_version", 5)
  97. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  98. ff_prof.set_preference('network.proxy.socks_port', 9150)
  99. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  100. ff_prof.set_preference("javascript.enabled", True)
  101. ff_prof.update_preferences()
  102. service = Service(config.get('TOR', 'geckodriver_path'))
  103. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  104. driver.maximize_window()
  105. return driver
  106. def getAccess():
  107. url = getFixedURL()
  108. driver = createFFDriver()
  109. try:
  110. driver.get(url)
  111. return driver
  112. except:
  113. driver.close()
  114. return 'down'
  115. # Saves the crawled html page
  116. def savePage(driver, page, url):
  117. cleanPage = cleanHTML(driver, page)
  118. filePath = getFullPathName(url)
  119. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  120. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  121. return
  122. # Gets the full path of the page to be saved along with its appropriate file name
  123. def getFullPathName(url):
  124. from Forums.Initialization.forums_mining import config, CURRENT_DATE
  125. mainDir = os.path.join(config.get('Project', 'shared_folder'), "Forums/" + getForumName() + "/HTML_Pages")
  126. fileName = getNameFromURL(url)
  127. if isDescriptionLink(url):
  128. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  129. else:
  130. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  131. return fullPath
  132. # Creates the file name from passed URL
  133. def getNameFromURL(url):
  134. global counter
  135. name = ''.join(e for e in url if e.isalnum())
  136. if (name == ''):
  137. name = str(counter)
  138. counter = counter + 1
  139. return name
  140. def getInterestedLinks():
  141. links = []
  142. # Hacking
  143. links.append('http://dwforumuugiyderhybcpfxmlmoawgq6z3w6hk45nrnem3p7kwszhybad.onion/forums/hacking-forum.33/')
  144. # # Beginner Carding and Fraud
  145. # links.append('http://dwforumuugiyderhybcpfxmlmoawgq6z3w6hk45nrnem3p7kwszhybad.onion/forums/remote-administration.34/')
  146. # # Cracking Tools
  147. # links.append('http://dwforumuugiyderhybcpfxmlmoawgq6z3w6hk45nrnem3p7kwszhybad.onion/forums/cracking-tools.35/')
  148. # # Cracking Tutorials and Other Methods - error here about file not exisitng
  149. # links.append('http://dwforumuugiyderhybcpfxmlmoawgq6z3w6hk45nrnem3p7kwszhybad.onion/forums/cracking-tutorials-other-methods.36/')
  150. # # Combolists and Configs
  151. # links.append('http://dwforumuugiyderhybcpfxmlmoawgq6z3w6hk45nrnem3p7kwszhybad.onion/forums/combolists-and-configs.58/')
  152. # # Paid Software and Antivirus
  153. # links.append('http://dwforumuugiyderhybcpfxmlmoawgq6z3w6hk45nrnem3p7kwszhybad.onion/forums/paid-softwares-and-antivirus.59/')
  154. return links
  155. def crawlForum(driver):
  156. print("Crawling the DWForums forum")
  157. linksToCrawl = getInterestedLinks()
  158. i = 0
  159. while i < len(linksToCrawl):
  160. link = linksToCrawl[i]
  161. print('Crawling :', link)
  162. try:
  163. has_next_page = True
  164. count = 0
  165. while has_next_page:
  166. try:
  167. driver.get(link)
  168. except:
  169. driver.refresh()
  170. html = driver.page_source
  171. savePage(driver, html, link)
  172. topics = topicPages(html)
  173. for topic in topics:
  174. has_next_topic_page = True
  175. counter = 1
  176. page = topic
  177. while has_next_topic_page:
  178. itemURL = urlparse.urljoin(baseURL, str(page))
  179. try:
  180. driver.get(itemURL)
  181. except:
  182. driver.refresh()
  183. savePage(driver, driver.page_source, topic + f"page{counter}")
  184. # comment out
  185. if counter == 2:
  186. break
  187. try:
  188. page = driver.find_element(By.LINK_TEXT, value='Next').get_attribute('href')
  189. if page == "":
  190. raise NoSuchElementException
  191. counter += 1
  192. except NoSuchElementException:
  193. has_next_topic_page = False
  194. for i in range(counter):
  195. driver.back()
  196. # comment out
  197. break
  198. # comment out
  199. if count == 1:
  200. break
  201. try:
  202. temp = driver.find_element(by=By.LINK_TEXT, value="Next")
  203. link = temp.get_attribute('href')
  204. if link == "":
  205. raise NoSuchElementException
  206. count += 1
  207. except NoSuchElementException:
  208. has_next_page = False
  209. except Exception as e:
  210. print(link, e)
  211. i += 1
  212. input("Crawling DWForums forum done sucessfully. Press ENTER to continue\n")
  213. # Returns 'True' if the link is Topic link
  214. def isDescriptionLink(url):
  215. if '/threads/' in url:
  216. return True
  217. return False
  218. # Returns True if the link is a listingPage link
  219. def isListingLink(url):
  220. if '/forums/' in url:
  221. return True
  222. return False
  223. # calling the parser to define the links
  224. def topicPages(html):
  225. soup = BeautifulSoup(html, "html.parser")
  226. #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)
  227. return dwForums_links_parser(soup)
  228. def crawler():
  229. startCrawling()
  230. # print("Crawling and Parsing BestCardingWorld .... DONE!")