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.

342 lines
12 KiB

1 year ago
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. 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 forum")
  173. linksToCrawl = getInterestedLinks()
  174. visited = set(linksToCrawl)
  175. initialTime = time.time()
  176. i = 0
  177. count = 0
  178. while i < len(linksToCrawl):
  179. link = linksToCrawl[i]
  180. print('Crawling :', link)
  181. try:
  182. try:
  183. driver.get(link)
  184. except:
  185. driver.refresh()
  186. html = driver.page_source
  187. savePage(html, link)
  188. has_next_page = True
  189. while has_next_page:
  190. list = topicPages(html)
  191. for item in list:
  192. itemURL = urlparse.urljoin(baseURL, str(item))
  193. try:
  194. driver.get(itemURL)
  195. except:
  196. driver.refresh()
  197. savePage(driver.page_source, item)
  198. #next page for topic
  199. # variable to check if there is a next page for the topic
  200. has_next_topic_page = True
  201. counter = 1
  202. # check if there is a next page for the topics
  203. while has_next_topic_page:
  204. # try to access next page of th topic
  205. itemURL = urlparse.urljoin(baseURL, str(item))
  206. try:
  207. driver.get(itemURL)
  208. except:
  209. driver.refresh()
  210. savePage(driver.page_source, item)
  211. # if there is a next page then go and save....
  212. # next page in the topic?
  213. try:
  214. temp = driver.find_element(By.XPATH,
  215. '/html/body/div/div[2]/div/div[3]/div') # /html/body/div/div[2]/div/div[2]/div/
  216. item = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute(
  217. 'href') # /html/body/div/div[2]/div/div[2]/div
  218. if item == "":
  219. raise NoSuchElementException
  220. has_next_topic_page = False
  221. else:
  222. counter += 1
  223. except NoSuchElementException:
  224. has_next_topic_page = False
  225. # end of loop
  226. for i in range(counter):
  227. driver.back()
  228. # comment out, one topic per page
  229. # break
  230. # comment out, go through all pages
  231. # if count == 1:
  232. # count = 0
  233. # break
  234. try:
  235. temp = driver.find_element(by=By.XPATH, value=
  236. '/html/body/div/div[2]/div/div[3]/div') # /html/body/div/div[2]/div/div[3]/div
  237. link = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href')
  238. if link == "":
  239. raise NoSuchElementException
  240. try:
  241. driver.get(link)
  242. except:
  243. driver.refresh()
  244. html = driver.page_source
  245. savePage(html, link)
  246. count += 1
  247. except NoSuchElementException:
  248. has_next_page = False
  249. except Exception as e:
  250. print(link, e)
  251. i += 1
  252. # finalTime = time.time()
  253. # print finalTime - initialTime
  254. input("Crawling OnniForums forum done sucessfully. Press ENTER to continue\n")
  255. # Returns 'True' if the link is Topic link
  256. def isDescriptionLink(url):
  257. if 'Thread' in url:
  258. return True
  259. return False
  260. # Returns True if the link is a listingPage link
  261. def isListingLink(url):
  262. if 'Forum' in url:
  263. return True
  264. return False
  265. # calling the parser to define the links
  266. def topicPages(html):
  267. soup = BeautifulSoup(html, "html.parser")
  268. #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)
  269. return onniForums_links_parser(soup)
  270. def crawler():
  271. startCrawling()
  272. # print("Crawling and Parsing BestCardingWorld .... DONE!")