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.7 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=False
  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. return driver
  116. def getAccess():
  117. driver = createFFDriver()
  118. try:
  119. driver.get(BASE_URL)# open url in browser
  120. return driver
  121. except:
  122. driver.close()# close tab
  123. return 'down'
  124. # Saves the crawled html page
  125. def savePage(page, url):
  126. cleanPage = cleanHTML(page)
  127. filePath = getFullPathName(url)
  128. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  129. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  130. return
  131. # Gets the full path of the page to be saved along with its appropriate file name
  132. def getFullPathName(url):
  133. from Forums.Initialization.forums_mining import config, CURRENT_DATE
  134. mainDir = os.path.join(config.get('Project', 'shared_folder'), "Forums/" + FORUM_NAME + "/HTML_Pages")
  135. fileName = getNameFromURL(url)
  136. if isDescriptionLink(url):
  137. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  138. else:
  139. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  140. return fullPath
  141. # Creates the file name from passed URL
  142. def getNameFromURL(url):
  143. global counter
  144. name = ''.join(e for e in url if e.isalnum())
  145. if (name == ''):
  146. name = str(counter)
  147. counter = counter + 1
  148. return name
  149. def getInterestedLinks():
  150. links = []
  151. # # general hacking
  152. links.append('https://procrax.cx/forums/general-hacking.24/')
  153. # # hacking security tools
  154. links.append('https://procrax.cx/forums/hacking-security-tools.20/')
  155. # # hacktube
  156. links.append('https://procrax.cx/forums/hacktube.22/')
  157. # # cardable
  158. # links.append('https://procrax.cx/forums/cardable-websites.28/')
  159. # # tools
  160. # links.append('https://procrax.cx/forums/tools-bots-validators.73/')
  161. # general forum
  162. # links.append('https://procrax.cx/forums/forum-discussions-updates.7/')
  163. return links
  164. def crawlForum(driver):
  165. print("Crawling the Procrax")
  166. linksToCrawl = getInterestedLinks()
  167. i = 0
  168. while i < len(linksToCrawl):
  169. link = linksToCrawl[i]
  170. print('Crawling :', link)
  171. try:
  172. has_next_page = True
  173. count = 0
  174. while has_next_page:
  175. try:
  176. driver.get(link)
  177. except:
  178. driver.refresh()
  179. html = driver.page_source
  180. savePage(html, link)
  181. topics = topicPages(html)
  182. for topic in topics:
  183. has_next_topic_page = True
  184. counter = 1
  185. page = topic
  186. while has_next_topic_page:
  187. itemURL = urlparse.urljoin(BASE_URL, str(page))
  188. try:
  189. driver.get(itemURL)
  190. except:
  191. driver.refresh()
  192. savePage(driver.page_source, topic + f"page{counter}") # very important
  193. # comment out
  194. # if counter == 2:
  195. # break
  196. try:
  197. page = driver.find_element(By.LINK_TEXT, value='Next').get_attribute('href')
  198. if page == "":
  199. raise NoSuchElementException
  200. counter += 1
  201. except NoSuchElementException:
  202. has_next_topic_page = False
  203. for i in range(counter):
  204. driver.back()
  205. # comment out
  206. # break
  207. # comment out
  208. if count == 20:
  209. break
  210. try:
  211. link = driver.find_element(by=By.LINK_TEXT, value='Next').get_attribute('href')
  212. if link == "":
  213. raise NoSuchElementException
  214. count += 1
  215. except NoSuchElementException:
  216. has_next_page = False
  217. except Exception as e:
  218. print(link, e)
  219. i += 1
  220. input("Crawling Procrax done successfully. Press ENTER to continue\n")
  221. # Returns 'True' if the link is Topic link, may need to change for every website
  222. def isDescriptionLink(url):
  223. if 'threads' in url:
  224. return True
  225. return False
  226. # Returns True if the link is a listingPage link, may need to change for every website
  227. def isListingLink(url):
  228. if 'forums' in url:
  229. return True
  230. return False
  231. # calling the parser to define the links
  232. def topicPages(html):
  233. soup = BeautifulSoup(html, "html.parser")
  234. #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)
  235. return procrax_links_parser(soup)
  236. def crawler():
  237. startCrawling()
  238. # print("Crawling and Parsing BestCardingWorld .... DONE!")