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.

307 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. #
  32. # if driver != 'down':
  33. # try:
  34. # login(driver)
  35. # crawlForum(driver)
  36. # except Exception as e:
  37. # print(driver.current_url, e)
  38. # closetor(driver)
  39. new_parse(
  40. forum=FORUM_NAME,
  41. url=BASE_URL,
  42. createLog=True
  43. )
  44. # Opens Tor Browser
  45. def opentor():
  46. from Forums.Initialization.forums_mining import config
  47. global pid
  48. print("Connecting Tor...")
  49. pro = subprocess.Popen(config.get('TOR', 'firefox_binary_path'))
  50. pid = pro.pid
  51. time.sleep(7.5)
  52. input('Tor Connected. Press ENTER to continue\n')
  53. return
  54. # Login using premade account credentials and do login captcha manually
  55. def login(driver):
  56. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  57. (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')))
  58. #entering username and password into input boxes
  59. usernameBox = driver.find_element(by=By.NAME, value='login')
  60. #Username here
  61. usernameBox.send_keys('cheese_pizza_man')#sends string to the username box
  62. passwordBox = driver.find_element(by=By.NAME, value='password')
  63. #Password here
  64. passwordBox.send_keys('Gr33nSp@m&3ggs')# sends string to passwordBox
  65. 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')
  66. clicker.click()
  67. # # wait for listing page show up (This Xpath may need to change based on different seed url)
  68. # # wait for 50 sec until id = tab_content is found, then cont
  69. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  70. (By.XPATH, '/html/body/div[1]/div[3]/div[2]/div[3]/div[1]/div/div[1]/div')))
  71. # Returns the name of the website
  72. def getForumName():
  73. name = 'Procrax'
  74. return name
  75. # Return the link of the website
  76. def getFixedURL():
  77. url = 'https://procrax.cx/'
  78. return url
  79. # Closes Tor Browser
  80. def closetor(driver):
  81. # global pid
  82. # os.system("taskkill /pid " + str(pro.pid))
  83. # os.system("taskkill /t /f /im tor.exe")
  84. print('Closing Tor...')
  85. driver.close() #close tab
  86. time.sleep(3)
  87. return
  88. # Creates FireFox 'driver' and configure its 'Profile'
  89. # to use Tor proxy and socket
  90. def createFFDriver():
  91. from Forums.Initialization.forums_mining import config
  92. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  93. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  94. ff_prof.set_preference("places.history.enabled", False)
  95. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  96. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  97. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  98. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  99. ff_prof.set_preference("signon.rememberSignons", False)
  100. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  101. ff_prof.set_preference("network.dns.disablePrefetch", True)
  102. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  103. ff_prof.set_preference("permissions.default.image", 3)
  104. ff_prof.set_preference("browser.download.folderList", 2)
  105. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  106. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  107. ff_prof.set_preference('network.proxy.type', 1)
  108. ff_prof.set_preference("network.proxy.socks_version", 5)
  109. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  110. ff_prof.set_preference('network.proxy.socks_port', 9150)
  111. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  112. ff_prof.set_preference("javascript.enabled", True)
  113. ff_prof.update_preferences()
  114. service = Service(config.get('TOR', 'geckodriver_path'))
  115. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  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!")