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.

330 lines
11 KiB

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