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.

317 lines
11 KiB

1 year ago
1 year ago
  1. __author__ = 'Helium'
  2. '''
  3. OnniForums Crawler (Selenium)
  4. Now goes through multiple topic pages.
  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. import configparser
  18. from datetime import date
  19. import subprocess
  20. from bs4 import BeautifulSoup
  21. from Forums.Initialization.prepare_parser import new_parse
  22. from Forums.OnniForums.parser import onniForums_links_parser
  23. from Forums.Utilities.utilities import cleanHTML
  24. counter = 1
  25. baseURL = 'http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/'
  26. # Opens Tor Browser, crawls the website
  27. def startCrawling():
  28. opentor()
  29. forumName = getForumName()
  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(forum=forumName, url=baseURL, createLog=False)
  39. # Opens Tor Browser
  40. def opentor():
  41. from Forums.Initialization.forums_mining import config
  42. global pid
  43. print("Connecting Tor...")
  44. pro = subprocess.Popen(config.get('TOR', 'firefox_binary_path'))
  45. pid = pro.pid
  46. time.sleep(7.5)
  47. input('Tor Connected. Press ENTER to continue\n')
  48. return
  49. # Login using premade account credentials and do login captcha manually
  50. def login(driver):
  51. #click login button
  52. login_link = driver.find_element(
  53. by=By.XPATH, value='/html/body/div/div[2]/div/table/tbody/tr[2]/td/center/pre/strong/a').\
  54. get_attribute('href')
  55. driver.get(login_link)
  56. #entering username and password into input boxes
  57. usernameBox = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/table/tbody/tr[2]/td[2]/input')
  58. #Username here
  59. usernameBox.send_keys('purely_cabbage')
  60. passwordBox = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/table/tbody/tr[3]/td[2]/input')
  61. #Password here
  62. passwordBox.send_keys('$ourP@tchK1ds')
  63. clicker = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/div/input')
  64. clicker.click()
  65. # wait for listing page show up (This Xpath may need to change based on different seed url)
  66. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  67. (By.XPATH, '//*[@id="content"]')))
  68. # Returns the name of the website
  69. def getForumName():
  70. name = 'OnniForums'
  71. return name
  72. # Return the link of the website
  73. def getFixedURL():
  74. url = 'http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/'
  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()
  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. # # Hacking & Cracking tutorials
  151. links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Hacking-Cracking-tutorials')
  152. # Hacking & Cracking questions
  153. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Hacking-Cracking-questions')
  154. # # Exploit PoCs
  155. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Exploit-PoCs')
  156. # # Cracked software
  157. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Cracked-software')
  158. # # Malware-development
  159. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Malware-development')
  160. # # Carding & Fraud
  161. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Carding-Fraud')
  162. # # Darknet Discussions
  163. # links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=88')
  164. # # OPSEC
  165. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-OPSEC')
  166. # # Databases
  167. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Databases')
  168. # # Proxies
  169. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Proxies')
  170. return links
  171. def crawlForum(driver):
  172. print("Crawling the OnniForums")
  173. linksToCrawl = getInterestedLinks()
  174. i = 0
  175. while i < len(linksToCrawl):
  176. link = linksToCrawl[i]
  177. print('Crawling :', link)
  178. try:
  179. has_next_page = True
  180. count = 0
  181. while has_next_page:
  182. try:
  183. driver.get(link)
  184. except:
  185. driver.refresh()
  186. html = driver.page_source
  187. savePage(html, link)
  188. topics = topicPages(html)
  189. for topic in topics:
  190. has_next_topic_page = True
  191. counter = 1
  192. page = topic
  193. while has_next_topic_page:
  194. itemURL = urlparse.urljoin(baseURL, str(page))
  195. try:
  196. driver.get(itemURL)
  197. except:
  198. driver.refresh()
  199. savePage(driver.page_source, topic + f"page{counter}") # very important
  200. # comment out
  201. if counter == 2:
  202. break
  203. try:
  204. temp = driver.find_element(By.XPATH,'/html/body/div/div[2]/div/div[3]/div') # /html/body/div/div[2]/div/div[2]/div/
  205. page = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href') # /html/body/div/div[2]/div/div[2]/div
  206. if page == "":
  207. raise NoSuchElementException
  208. counter += 1
  209. except NoSuchElementException:
  210. has_next_topic_page = False
  211. for i in range(counter):
  212. driver.back()
  213. # comment out
  214. break
  215. # comment out
  216. if count == 1:
  217. break
  218. try:
  219. temp = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/div[3]/div') # /html/body/div/div[2]/div/div[3]/div
  220. link = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href')
  221. if link == "":
  222. raise NoSuchElementException
  223. count += 1
  224. except NoSuchElementException:
  225. has_next_page = False
  226. except Exception as e:
  227. print(link, e)
  228. i += 1
  229. input("Crawling OnniForums done successfully. Press ENTER to continue\n")
  230. # Returns 'True' if the link is Topic link
  231. def isDescriptionLink(url):
  232. if 'Thread' in url:
  233. return True
  234. return False
  235. # Returns True if the link is a listingPage link
  236. def isListingLink(url):
  237. if 'Forum' in url:
  238. return True
  239. return False
  240. # calling the parser to define the links
  241. def topicPages(html):
  242. soup = BeautifulSoup(html, "html.parser")
  243. #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)
  244. return onniForums_links_parser(soup)
  245. def crawler():
  246. startCrawling()
  247. # print("Crawling and Parsing BestCardingWorld .... DONE!")