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