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.

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