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.

344 lines
12 KiB

1 year ago
1 year ago
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. config = configparser.ConfigParser()
  25. config.read('../../setup.ini')
  26. counter = 1
  27. baseURL = 'http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/'
  28. # Opens Tor Browser, crawls the website
  29. def startCrawling():
  30. opentor()
  31. # forumName = getForumName()
  32. driver = getAccess()
  33. if driver != 'down':
  34. try:
  35. login(driver)
  36. crawlForum(driver)
  37. except Exception as e:
  38. print(driver.current_url, e)
  39. closetor(driver)
  40. # new_parse(forumName, baseURL, False)
  41. # Opens Tor Browser
  42. def opentor():
  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. 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. fileName = getNameFromURL(url)
  133. if isDescriptionLink(url):
  134. #..\CryptBB\HTML_Pages\\
  135. fullPath = r'..\OnniForums\HTML_Pages\\' + str(
  136. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  137. "%04d" % date.today().year) + r'\\' + r'Description\\' + fileName + '.html'
  138. else:
  139. fullPath = r'..\OnniForums\HTML_Pages\\' + str(
  140. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  141. "%04d" % date.today().year) + r'\\' + r'Listing\\' + fileName + '.html'
  142. return fullPath
  143. # Creates the file name from passed URL
  144. def getNameFromURL(url):
  145. global counter
  146. name = ''.join(e for e in url if e.isalnum())
  147. if (name == ''):
  148. name = str(counter)
  149. counter = counter + 1
  150. return name
  151. def getInterestedLinks():
  152. links = []
  153. # # Hacking & Cracking tutorials
  154. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Hacking-Cracking-tutorials')
  155. # Hacking & Cracking questions
  156. links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Hacking-Cracking-questions')
  157. # # Exploit PoCs
  158. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Exploit-PoCs')
  159. # # Cracked software
  160. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Cracked-software')
  161. # # Malware-development
  162. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Malware-development')
  163. # # Carding & Fraud
  164. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Carding-Fraud')
  165. # # Darknet Discussions
  166. # links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=88')
  167. # # OPSEC
  168. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-OPSEC')
  169. # # Databases
  170. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Databases')
  171. # # Proxies
  172. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Proxies')
  173. return links
  174. def crawlForum(driver):
  175. print("Crawling the OnniForums forum")
  176. linksToCrawl = getInterestedLinks()
  177. visited = set(linksToCrawl)
  178. initialTime = time.time()
  179. i = 0
  180. count = 0
  181. while i < len(linksToCrawl):
  182. link = linksToCrawl[i]
  183. print('Crawling :', link)
  184. try:
  185. try:
  186. driver.get(link)
  187. except:
  188. driver.refresh()
  189. html = driver.page_source
  190. savePage(html, link)
  191. has_next_page = True
  192. while has_next_page:
  193. list = topicPages(html)
  194. for item in list:
  195. itemURL = urlparse.urljoin(baseURL, str(item))
  196. try:
  197. driver.get(itemURL)
  198. except:
  199. driver.refresh()
  200. savePage(driver.page_source, item)
  201. #next page for topic
  202. # variable to check if there is a next page for the topic
  203. has_next_topic_page = True
  204. counter = 1
  205. # check if there is a next page for the topics
  206. while has_next_topic_page:
  207. # try to access next page of th topic
  208. itemURL = urlparse.urljoin(baseURL, str(item))
  209. try:
  210. driver.get(itemURL)
  211. except:
  212. driver.refresh()
  213. savePage(driver.page_source, item)
  214. # if there is a next page then go and save....
  215. # next page in the topic?
  216. try:
  217. temp = driver.find_element(By.XPATH,
  218. '/html/body/div/div[2]/div/div[3]/div') # /html/body/div/div[2]/div/div[2]/div/
  219. item = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute(
  220. 'href') # /html/body/div/div[2]/div/div[2]/div
  221. if item == "":
  222. raise NoSuchElementException
  223. has_next_topic_page = False
  224. else:
  225. counter += 1
  226. except NoSuchElementException:
  227. has_next_topic_page = False
  228. # end of loop
  229. for i in range(counter):
  230. driver.back()
  231. # comment out, one topic per page
  232. # break
  233. #
  234. # # comment out, go through all pages
  235. # if count == 1:
  236. # count = 0
  237. # break
  238. try:
  239. temp = driver.find_element(by=By.XPATH, value=
  240. '/html/body/div/div[2]/div/div[3]/div') # /html/body/div/div[2]/div/div[3]/div
  241. link = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href')
  242. if link == "":
  243. raise NoSuchElementException
  244. try:
  245. driver.get(link)
  246. except:
  247. driver.refresh()
  248. html = driver.page_source
  249. savePage(html, link)
  250. count += 1
  251. except NoSuchElementException:
  252. has_next_page = False
  253. except Exception as e:
  254. print(link, e)
  255. i += 1
  256. # finalTime = time.time()
  257. # print finalTime - initialTime
  258. input("Crawling OnniForums forum done sucessfully. Press ENTER to continue\n")
  259. # Returns 'True' if the link is Topic link
  260. def isDescriptionLink(url):
  261. if 'Thread' in url:
  262. return True
  263. return False
  264. # Returns True if the link is a listingPage link
  265. def isListingLink(url):
  266. if 'Forum' in url:
  267. return True
  268. return False
  269. # calling the parser to define the links
  270. def topicPages(html):
  271. soup = BeautifulSoup(html, "html.parser")
  272. #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)
  273. return onniForums_links_parser(soup)
  274. def crawler():
  275. startCrawling()
  276. # print("Crawling and Parsing BestCardingWorld .... DONE!")