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