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.

289 lines
9.4 KiB

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