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.

320 lines
11 KiB

1 year ago
1 year ago
  1. __author__ = 'Helium'
  2. '''
  3. Procrax Forum Crawler (Selenium)
  4. rechecked and confirmed
  5. '''
  6. from selenium import webdriver
  7. from selenium.common.exceptions import NoSuchElementException
  8. from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
  9. from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
  10. from selenium.webdriver.firefox.service import Service
  11. from selenium.webdriver.common.by import By
  12. from selenium.webdriver.support import expected_conditions as EC
  13. from selenium.webdriver.support.ui import WebDriverWait
  14. from PIL import Image
  15. import urllib.parse as urlparse
  16. import os, re, time
  17. from datetime import date
  18. import configparser
  19. import subprocess
  20. from bs4 import BeautifulSoup
  21. from Forums.Initialization.prepare_parser import new_parse
  22. from Forums.Procrax.parser import procrax_links_parser
  23. from Forums.Utilities.utilities import cleanHTML
  24. counter = 1
  25. baseURL = 'https://procrax.cx/'
  26. # Opens Tor Browser, crawls the website
  27. def startCrawling():
  28. opentor()
  29. # forumName = getForumName()
  30. driver = getAccess()
  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, False)
  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. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  52. (By.XPATH, '/html/body/div[1]/div[3]/div[2]/div[3]/div[2]/div[1]/form/div/div/div/dl[4]/dd/div/div[2]/button/span')))
  53. #entering username and password into input boxes
  54. usernameBox = driver.find_element(by=By.NAME, value='login')
  55. #Username here
  56. usernameBox.send_keys('cheese_pizza_man')#sends string to the username box
  57. passwordBox = driver.find_element(by=By.NAME, value='password')
  58. #Password here
  59. passwordBox.send_keys('Gr33nSp@m&3ggs')# sends string to passwordBox
  60. clicker = driver.find_element(by=By.XPATH, value='/html/body/div[1]/div[3]/div[2]/div[3]/div[2]/div[1]/form/div/div/div/dl[4]/dd/div/div[2]/button/span')
  61. clicker.click()
  62. # # wait for listing page show up (This Xpath may need to change based on different seed url)
  63. # # wait for 50 sec until id = tab_content is found, then cont
  64. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  65. (By.XPATH, '/html/body/div[1]/div[3]/div[2]/div[3]/div[1]/div/div[1]/div')))
  66. # Returns the name of the website
  67. def getForumName():
  68. name = 'Procrax'
  69. return name
  70. # Return the link of the website
  71. def getFixedURL():
  72. url = 'https://procrax.cx/'
  73. return url
  74. # Closes Tor Browser
  75. def closetor(driver):
  76. # global pid
  77. # os.system("taskkill /pid " + str(pro.pid))
  78. # os.system("taskkill /t /f /im tor.exe")
  79. print('Closing Tor...')
  80. driver.close() #close tab
  81. time.sleep(3)
  82. return
  83. # Creates FireFox 'driver' and configure its 'Profile'
  84. # to use Tor proxy and socket
  85. def createFFDriver():
  86. from Forums.Initialization.forums_mining import config
  87. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  88. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  89. ff_prof.set_preference("places.history.enabled", False)
  90. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  91. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  92. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  93. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  94. ff_prof.set_preference("signon.rememberSignons", False)
  95. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  96. ff_prof.set_preference("network.dns.disablePrefetch", True)
  97. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  98. ff_prof.set_preference("permissions.default.image", 3)
  99. ff_prof.set_preference("browser.download.folderList", 2)
  100. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  101. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  102. ff_prof.set_preference('network.proxy.type', 1)
  103. ff_prof.set_preference("network.proxy.socks_version", 5)
  104. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  105. ff_prof.set_preference('network.proxy.socks_port', 9150)
  106. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  107. ff_prof.set_preference("javascript.enabled", True)
  108. ff_prof.update_preferences()
  109. service = Service(config.get('TOR', 'geckodriver_path'))
  110. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  111. return driver
  112. def getAccess():
  113. url = getFixedURL()
  114. driver = createFFDriver()
  115. try:
  116. driver.get(url)# open url in browser
  117. return driver
  118. except:
  119. driver.close()# close tab
  120. return 'down'
  121. # Saves the crawled html page
  122. def savePage(page, url):
  123. cleanPage = cleanHTML(page)
  124. filePath = getFullPathName(url)
  125. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  126. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  127. return
  128. # Gets the full path of the page to be saved along with its appropriate file name
  129. def getFullPathName(url):
  130. from Forums.Initialization.forums_mining import config, CURRENT_DATE
  131. mainDir = os.path.join(config.get('Project', 'shared_folder'), "Forums/" + getForumName() + "/HTML_Pages")
  132. fileName = getNameFromURL(url)
  133. if isDescriptionLink(url):
  134. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  135. else:
  136. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  137. return fullPath
  138. # Creates the file name from passed URL
  139. def getNameFromURL(url):
  140. global counter
  141. name = ''.join(e for e in url if e.isalnum())
  142. if (name == ''):
  143. name = str(counter)
  144. counter = counter + 1
  145. return name
  146. def getInterestedLinks():
  147. links = []
  148. # # general hacking
  149. # links.append('https://procrax.cx/forums/general-hacking.24/')
  150. # # hacking security tools
  151. # links.append('https://procrax.cx/forums/hacking-security-tools.20/')
  152. # # hacktube
  153. # links.append('https://procrax.cx/forums/hacktube.22/')
  154. # # cardable
  155. # links.append('https://procrax.cx/forums/cardable-websites.28/')
  156. # # tools
  157. # links.append('https://procrax.cx/forums/tools-bots-validators.73/')
  158. # general forum
  159. links.append('https://procrax.cx/forums/forum-discussions-updates.7/')
  160. return links
  161. def crawlForum(driver):
  162. print("Crawling the Procrax forum")
  163. linksToCrawl = getInterestedLinks()
  164. visited = set(linksToCrawl)
  165. initialTime = time.time()
  166. i = 0
  167. count = 0
  168. while i < len(linksToCrawl):
  169. link = linksToCrawl[i]
  170. print('Crawling :', link)
  171. try:
  172. try:
  173. driver.get(link)# open
  174. except:
  175. driver.refresh()
  176. html = driver.page_source
  177. savePage(html, link)
  178. has_next_page = True
  179. #loop through the topics
  180. while has_next_page:
  181. list = topicPages(html)# for multiple pages
  182. for item in list:
  183. #variable to check if there is a next page for the topic
  184. has_next_topic_page = True
  185. counter = 1
  186. # check if there is a next page for the topics
  187. while has_next_topic_page:
  188. # try to access next page of th topic
  189. itemURL = urlparse.urljoin(baseURL, str(item))
  190. try:
  191. driver.get(itemURL)
  192. except:
  193. driver.refresh()
  194. savePage(driver.page_source, item)
  195. # if there is a next page then go and save....
  196. # specific
  197. try:
  198. # temp = driver.find_element(By.XPATH, value='/html/body/div[1]/div[3]/div[2]/div[3]/div/div')
  199. item = driver.find_element(By.LINK_TEXT, value='Next').get_attribute('href')
  200. if item == "":
  201. raise NoSuchElementException
  202. has_next_topic_page = False
  203. else:
  204. counter += 1
  205. except NoSuchElementException:
  206. has_next_topic_page = False
  207. #end of loop
  208. for i in range(counter):
  209. driver.back()
  210. # # comment out
  211. # break
  212. #
  213. # # comment out
  214. # if count == 1:
  215. # count = 0
  216. # break
  217. try:# change depending on web page, #general
  218. # /html/body/div[1]/div[3]/div[2]/div[3]/div/div/div/div[1]/div/nav/div[1]
  219. # temp = driver.find_element(By.XPATH, value='/html/body/div[1]/div[3]/div[2]/div[3]/div/div/div/div[1]/div/nav/div[1]')
  220. link = driver.find_element(by=By.LINK_TEXT, value='Next').get_attribute('href')
  221. if link == "":
  222. raise NoSuchElementException
  223. try:
  224. driver.get(link)
  225. except:
  226. driver.refresh()
  227. html = driver.page_source
  228. savePage(html, link)
  229. count += 1
  230. except NoSuchElementException:
  231. has_next_page = False
  232. except Exception as e:
  233. print(link, e)
  234. i += 1
  235. # finalTime = time.time()
  236. # print finalTime - initialTime
  237. input("Crawling Procrax forum done successfully. Press ENTER to continue\n")
  238. # Returns 'True' if the link is Topic link, may need to change for every website
  239. def isDescriptionLink(url):
  240. if 'threads' in url:
  241. return True
  242. return False
  243. # Returns True if the link is a listingPage link, may need to change for every website
  244. def isListingLink(url):
  245. if 'forums' in url:
  246. return True
  247. return False
  248. # calling the parser to define the links
  249. def topicPages(html):
  250. soup = BeautifulSoup(html, "html.parser")
  251. #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)
  252. return procrax_links_parser(soup)
  253. def crawler():
  254. startCrawling()
  255. # print("Crawling and Parsing BestCardingWorld .... DONE!")