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.

304 lines
10 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. forumName = getForumName()
  29. driver = getAccess()
  30. if driver != 'down':
  31. try:
  32. login(driver)
  33. crawlForum(driver)
  34. except Exception as e:
  35. print(driver.current_url, e)
  36. closeDriver(driver)
  37. new_parse(forum=forumName, url=baseURL, createLog=True)
  38. # Login using premade account credentials and do login captcha manually
  39. def login(driver):
  40. #click login button
  41. login_link = driver.find_element(
  42. by=By.XPATH, value='/html/body/div/div[2]/div/table/tbody/tr[2]/td/center/pre/strong/a').\
  43. get_attribute('href')
  44. driver.get(login_link)
  45. #entering username and password into input boxes
  46. usernameBox = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/table/tbody/tr[2]/td[2]/input')
  47. #Username here
  48. usernameBox.send_keys('purely_cabbage')
  49. passwordBox = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/table/tbody/tr[3]/td[2]/input')
  50. #Password here
  51. passwordBox.send_keys('$ourP@tchK1ds')
  52. clicker = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/div/input')
  53. clicker.click()
  54. # wait for listing page show up (This Xpath may need to change based on different seed url)
  55. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  56. (By.XPATH, '//*[@id="content"]')))
  57. # Returns the name of the website
  58. def getForumName():
  59. name = 'OnniForums'
  60. return name
  61. # Return the link of the website
  62. def getFixedURL():
  63. url = 'http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/'
  64. return url
  65. # Closes Tor Browser
  66. def closeDriver(driver):
  67. # global pid
  68. # os.system("taskkill /pid " + str(pro.pid))
  69. # os.system("taskkill /t /f /im tor.exe")
  70. print('Closing Tor...')
  71. driver.close()
  72. time.sleep(3)
  73. return
  74. # Creates FireFox 'driver' and configure its 'Profile'
  75. # to use Tor proxy and socket
  76. def createFFDriver():
  77. from Forums.Initialization.forums_mining import config
  78. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  79. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  80. ff_prof.set_preference("places.history.enabled", False)
  81. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  82. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  83. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  84. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  85. ff_prof.set_preference("signon.rememberSignons", False)
  86. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  87. ff_prof.set_preference("network.dns.disablePrefetch", True)
  88. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  89. ff_prof.set_preference("permissions.default.image", 3)
  90. ff_prof.set_preference("browser.download.folderList", 2)
  91. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  92. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  93. ff_prof.set_preference('network.proxy.type', 1)
  94. ff_prof.set_preference("network.proxy.socks_version", 5)
  95. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  96. ff_prof.set_preference('network.proxy.socks_port', 9150)
  97. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  98. ff_prof.set_preference("javascript.enabled", True)
  99. ff_prof.update_preferences()
  100. service = Service(config.get('TOR', 'geckodriver_path'))
  101. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  102. driver.maximize_window()
  103. return driver
  104. def getAccess():
  105. url = getFixedURL()
  106. driver = createFFDriver()
  107. try:
  108. driver.get(url)
  109. return driver
  110. except:
  111. driver.close()
  112. return 'down'
  113. # Saves the crawled html page
  114. def savePage(driver, page, url):
  115. cleanPage = cleanHTML(driver, page)
  116. filePath = getFullPathName(url)
  117. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  118. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  119. return
  120. # Gets the full path of the page to be saved along with its appropriate file name
  121. def getFullPathName(url):
  122. from Forums.Initialization.forums_mining import config, CURRENT_DATE
  123. mainDir = os.path.join(config.get('Project', 'shared_folder'), "Forums/" + getForumName() + "/HTML_Pages")
  124. fileName = getNameFromURL(url)
  125. if isDescriptionLink(url):
  126. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  127. else:
  128. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  129. return fullPath
  130. # Creates the file name from passed URL
  131. def getNameFromURL(url):
  132. global counter
  133. name = ''.join(e for e in url if e.isalnum())
  134. if (name == ''):
  135. name = str(counter)
  136. counter = counter + 1
  137. return name
  138. def getInterestedLinks():
  139. links = []
  140. # # Hacking & Cracking tutorials
  141. links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Hacking-Cracking-tutorials')
  142. # Hacking & Cracking questions
  143. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Hacking-Cracking-questions')
  144. # # Exploit PoCs
  145. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Exploit-PoCs')
  146. # # Cracked software
  147. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Cracked-software')
  148. # # Malware-development
  149. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Malware-development')
  150. # # Carding & Fraud
  151. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Carding-Fraud')
  152. # # Darknet Discussions
  153. # links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=88')
  154. # # OPSEC
  155. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-OPSEC')
  156. # # Databases
  157. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Databases')
  158. # # Proxies
  159. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Proxies')
  160. return links
  161. def crawlForum(driver):
  162. print("Crawling the OnniForums forum")
  163. linksToCrawl = getInterestedLinks()
  164. i = 0
  165. while i < len(linksToCrawl):
  166. link = linksToCrawl[i]
  167. print('Crawling :', link)
  168. try:
  169. has_next_page = True
  170. count = 0
  171. while has_next_page:
  172. try:
  173. driver.get(link)
  174. except:
  175. driver.refresh()
  176. html = driver.page_source
  177. savePage(driver, html, link)
  178. topics = topicPages(html)
  179. for topic in topics:
  180. has_next_topic_page = True
  181. counter = 1
  182. page = topic
  183. while has_next_topic_page:
  184. itemURL = urlparse.urljoin(baseURL, str(page))
  185. try:
  186. driver.get(itemURL)
  187. except:
  188. driver.refresh()
  189. savePage(driver, driver.page_source, topic + f"page{counter}") # very important
  190. # comment out
  191. if counter == 2:
  192. break
  193. try:
  194. temp = driver.find_element(By.XPATH,'/html/body/div/div[2]/div/div[3]/div') # /html/body/div/div[2]/div/div[2]/div/
  195. page = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href') # /html/body/div/div[2]/div/div[2]/div
  196. if page == "":
  197. raise NoSuchElementException
  198. counter += 1
  199. except NoSuchElementException:
  200. has_next_topic_page = False
  201. for i in range(counter):
  202. driver.back()
  203. # comment out
  204. # break
  205. # comment out
  206. if count == 1:
  207. break
  208. try:
  209. 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
  210. link = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href')
  211. if link == "":
  212. raise NoSuchElementException
  213. count += 1
  214. except NoSuchElementException:
  215. has_next_page = False
  216. except Exception as e:
  217. print(link, e)
  218. i += 1
  219. print("Crawling the OnniForums forum done.")
  220. # Returns 'True' if the link is Topic link
  221. def isDescriptionLink(url):
  222. if 'Thread' in url:
  223. return True
  224. return False
  225. # Returns True if the link is a listingPage link
  226. def isListingLink(url):
  227. if 'Forum' in url:
  228. return True
  229. return False
  230. # calling the parser to define the links
  231. def topicPages(html):
  232. soup = BeautifulSoup(html, "html.parser")
  233. #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)
  234. return onniForums_links_parser(soup)
  235. def crawler():
  236. startCrawling()
  237. # print("Crawling and Parsing BestCardingWorld .... DONE!")