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.

327 lines
10 KiB

1 year ago
  1. __author__ = 'DarkWeb'
  2. '''
  3. Helium 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.support.ui import WebDriverWait
  11. from selenium.webdriver.support import expected_conditions as EC
  12. from selenium.webdriver.common.by import By
  13. from PIL import Image
  14. import urllib.parse as urlparse
  15. import os, time
  16. from datetime import date
  17. import subprocess
  18. from bs4 import BeautifulSoup
  19. from Forums.Initialization.prepare_parser import new_parse
  20. from Forums.Helium.parser import helium_links_parser
  21. from Forums.Utilities.utilities import cleanHTML
  22. counter = 1
  23. baseURL = 'http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/'
  24. # Opens Tor Browser, crawls the website
  25. def startCrawling():
  26. # opentor()
  27. # forumName = getForumName()
  28. driver = getAccess()
  29. if driver != 'down':
  30. login(driver)
  31. crawlForum(driver)
  32. closetor(driver)
  33. # new_parse(forumName, False)
  34. # Opens Tor Browser
  35. def opentor():
  36. global pid
  37. print("Connecting Tor...")
  38. path = open('../../path.txt').readline().strip()
  39. pro = subprocess.Popen(path)
  40. pid = pro.pid
  41. time.sleep(7.5)
  42. input('Tor Connected. Press ENTER to continue\n')
  43. return
  44. # Login using premade account credentials and do login captcha manually
  45. def login(driver):
  46. #wait for login page
  47. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  48. (By.XPATH, "/html/body/div[2]/div/div[1]/div/div/div[2]/form/div[5]/div/button")))
  49. #entering username and password into input boxes
  50. usernameBox = driver.find_element(by=By.XPATH, value='//*[@id="username"]')
  51. #Username here
  52. usernameBox.send_keys('holyre')
  53. passwordBox = driver.find_element(by=By.XPATH, value='//*[@id="password"]')
  54. #Password here
  55. passwordBox.send_keys('PlatinumBorn2')
  56. '''
  57. # wait for captcha page show up
  58. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  59. (By.XPATH, '//*[@id="captcha_img"]')))
  60. # save captcha to local
  61. driver.find_element(by=By.XPATH, value='//*[@id="captcha_img"]').screenshot(r'..\Helium\captcha.png')
  62. # This method will show image in any image viewer
  63. im = Image.open(r'..\Helium\captcha.png')
  64. im.show()
  65. # wait until input space show up
  66. inputBox = driver.find_element(by=By.XPATH, value='//*[@id="captcha"]')
  67. # ask user input captcha solution in terminal
  68. userIn = input("Enter solution: ")
  69. # send user solution into the input space
  70. inputBox.send_keys(userIn)
  71. # click the verify(submit) button
  72. driver.find_element(by=By.XPATH, value="/html/body/div[2]/div/div[1]/div/div/div[2]/form/div[5]/div/button").click()
  73. '''
  74. input("Press ENTER when CAPTCHA is completed\n")
  75. # wait for listing page show up (This Xpath may need to change based on different seed url)
  76. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  77. (By.XPATH, '/html/body/div[2]/div/p')))
  78. # Returns the name of the website
  79. def getForumName():
  80. name = 'Helium'
  81. return name
  82. # Return the link of the website
  83. def getFixedURL():
  84. url = 'http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/login'
  85. return url
  86. # Closes Tor Browser
  87. def closetor(driver):
  88. global pid
  89. # os.system("taskkill /pid " + str(pro.pid))
  90. os.system("taskkill /t /f /im tor.exe")
  91. print('Closing Tor...')
  92. driver.close()
  93. time.sleep(3)
  94. return
  95. # Creates FireFox 'driver' and configure its 'Profile'
  96. # to use Tor proxy and socket
  97. def createFFDriver():
  98. file = open('../../path.txt', 'r')
  99. lines = file.readlines()
  100. ff_binary = FirefoxBinary(lines[0].strip())
  101. ff_prof = FirefoxProfile(lines[1].strip())
  102. ff_prof.set_preference("places.history.enabled", False)
  103. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  104. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  105. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  106. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  107. ff_prof.set_preference("signon.rememberSignons", False)
  108. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  109. ff_prof.set_preference("network.dns.disablePrefetch", True)
  110. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  111. # ff_prof.set_preference("permissions.default.image", 2)
  112. ff_prof.set_preference("browser.download.folderList", 2)
  113. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  114. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  115. ff_prof.set_preference('network.proxy.type', 1)
  116. ff_prof.set_preference("network.proxy.socks_version", 5)
  117. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  118. ff_prof.set_preference('network.proxy.socks_port', 9150)
  119. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  120. ff_prof.set_preference("javascript.enabled", True)
  121. ff_prof.update_preferences()
  122. service = Service(lines[2].strip())
  123. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  124. return driver
  125. def getAccess():
  126. url = getFixedURL()
  127. driver = createFFDriver()
  128. try:
  129. driver.get(url)
  130. return driver
  131. except:
  132. return 'down'
  133. # Saves the crawled html page
  134. def savePage(page, url):
  135. cleanPage = cleanHTML(page)
  136. filePath = getFullPathName(url)
  137. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  138. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  139. return
  140. # Gets the full path of the page to be saved along with its appropriate file name
  141. def getFullPathName(url):
  142. fileName = getNameFromURL(url)
  143. if isDescriptionLink(url):
  144. fullPath = r'..\Helium\HTML_Pages\\' + str(
  145. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  146. "%04d" % date.today().year) + r'\\' + r'Description\\' + fileName + '.html'
  147. else:
  148. fullPath = r'..\Helium\HTML_Pages\\' + str(
  149. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  150. "%04d" % date.today().year) + r'\\' + r'Listing\\' + fileName + '.html'
  151. return fullPath
  152. # Creates the file name from passed URL
  153. def getNameFromURL(url):
  154. global counter
  155. name = ''.join(e for e in url if e.isalnum())
  156. if name == '':
  157. name = str(counter)
  158. counter = counter + 1
  159. return name
  160. def getInterestedLinks():
  161. links = []
  162. # General Discussion
  163. links.append('http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/board/6')
  164. # Anonymity and Security
  165. links.append('http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/board/8')
  166. # Programming
  167. links.append('http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/board/9')
  168. # Carding Discussions
  169. links.append('http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/board/10')
  170. # Hacked Database (free)
  171. links.append('http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/board/11')
  172. # Hacking tools, exploits and POC
  173. links.append('http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/board/17')
  174. # Hacked Database
  175. links.append('http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/board/12')
  176. # Hacking and other Services
  177. links.append('http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/board/13')
  178. # Selling/Buying Malware, Exploits etc
  179. links.append('http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/board/22')
  180. # General Tutorials
  181. links.append('http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/board/18')
  182. # Hacking Tutorials
  183. links.append('http://fahue6hb7odzns36vfoi2dqfvqvjq4btt7vo52a67jivmyz6a6h3vzqd.onion/board/19')
  184. return links
  185. def crawlForum(driver):
  186. print("Crawling the Helium forum")
  187. linksToCrawl = getInterestedLinks()
  188. # visited = set(linksToCrawl)
  189. # initialTime = time.time()
  190. i = 0
  191. count = 0
  192. while i < len(linksToCrawl):
  193. link = linksToCrawl[i]
  194. print('Crawling :', link)
  195. try:
  196. try:
  197. driver.get(link)
  198. except:
  199. driver.refresh()
  200. html = driver.page_source
  201. savePage(html, link)
  202. has_next_page = True
  203. while has_next_page:
  204. list = topicPages(html)
  205. for item in list:
  206. itemURL = urlparse.urljoin(baseURL, str(item))
  207. try:
  208. driver.get(itemURL)
  209. except:
  210. driver.refresh()
  211. savePage(driver.page_source, item)
  212. driver.back()
  213. # comment out
  214. break
  215. # comment out
  216. if count == 1:
  217. count = 0
  218. break
  219. try:
  220. bar = driver.find_element(by=By.XPATH, value=
  221. '/html/body/div[2]/div/div[3]/ul')
  222. li = bar.find_elements(By.TAG_NAME, 'li')[-1]
  223. link = li.find_element(By.TAG_NAME, 'a').get_attribute('href')
  224. if link == "":
  225. raise NoSuchElementException
  226. try:
  227. driver.get(link)
  228. except:
  229. driver.refresh()
  230. html = driver.page_source
  231. savePage(html, link)
  232. count += 1
  233. except NoSuchElementException:
  234. has_next_page = False
  235. except Exception as e:
  236. print(link, e.message)
  237. i += 1
  238. # finalTime = time.time()
  239. # print finalTime - initialTime
  240. input("Crawling Helium forum done successfully. Press ENTER to continue\n")
  241. # Returns 'True' if the link is Topic link
  242. def isDescriptionLink(url):
  243. if 'topic' in url:
  244. return True
  245. return False
  246. # Returns True if the link is a listingPage link
  247. def isListingLink(url):
  248. if 'board' in url:
  249. return True
  250. return False
  251. # calling the parser to define the links
  252. def topicPages(html):
  253. soup = BeautifulSoup(html, "html.parser")
  254. return helium_links_parser(soup)
  255. def crawler():
  256. startCrawling()
  257. # print("Crawling and Parsing BestCardingWorld .... DONE!")