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.

300 lines
9.8 KiB

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