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.

305 lines
10 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. '''
  5. from selenium import webdriver
  6. from selenium.common.exceptions import NoSuchElementException
  7. from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
  8. from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
  9. from selenium.webdriver.firefox.service import Service
  10. from selenium.webdriver.common.by import By
  11. from selenium.webdriver.support import expected_conditions as EC
  12. from selenium.webdriver.support.ui import WebDriverWait
  13. from PIL import Image
  14. import urllib.parse as urlparse
  15. import os, re, time
  16. import configparser
  17. from datetime import date
  18. import subprocess
  19. from bs4 import BeautifulSoup
  20. from Forums.Initialization.prepare_parser import new_parse
  21. from Forums.OnniForums.parser import onniForums_links_parser
  22. from Forums.Utilities.utilities import cleanHTML
  23. config = configparser.ConfigParser()
  24. config.read('../../setup.ini')
  25. counter = 1
  26. baseURL = 'http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/'
  27. # Opens Tor Browser, crawls the website
  28. def startCrawling():
  29. # opentor()
  30. forumName = getForumName()
  31. # driver = getAccess()
  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=False)
  40. # Opens Tor Browser
  41. def opentor():
  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. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  89. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  90. ff_prof.set_preference("places.history.enabled", False)
  91. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  92. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  93. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  94. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  95. ff_prof.set_preference("signon.rememberSignons", False)
  96. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  97. ff_prof.set_preference("network.dns.disablePrefetch", True)
  98. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  99. ff_prof.set_preference("permissions.default.image", 3)
  100. ff_prof.set_preference("browser.download.folderList", 2)
  101. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  102. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  103. ff_prof.set_preference('network.proxy.type', 1)
  104. ff_prof.set_preference("network.proxy.socks_version", 5)
  105. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  106. ff_prof.set_preference('network.proxy.socks_port', 9150)
  107. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  108. ff_prof.set_preference("javascript.enabled", True)
  109. ff_prof.update_preferences()
  110. service = Service(config.get('TOR', 'geckodriver_path'))
  111. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  112. return driver
  113. def getAccess():
  114. url = getFixedURL()
  115. driver = createFFDriver()
  116. try:
  117. driver.get(url)
  118. return driver
  119. except:
  120. driver.close()
  121. return 'down'
  122. # Saves the crawled html page
  123. def savePage(page, url):
  124. cleanPage = cleanHTML(page)
  125. filePath = getFullPathName(url)
  126. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  127. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  128. return
  129. # Gets the full path of the page to be saved along with its appropriate file name
  130. def getFullPathName(url):
  131. fileName = getNameFromURL(url)
  132. if isDescriptionLink(url):
  133. #..\CryptBB\HTML_Pages\\
  134. fullPath = r'..\OnniForums\HTML_Pages\\' + str(
  135. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  136. "%04d" % date.today().year) + r'\\' + r'Description\\' + fileName + '.html'
  137. else:
  138. fullPath = r'..\OnniForums\HTML_Pages\\' + str(
  139. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  140. "%04d" % date.today().year) + r'\\' + r'Listing\\' + fileName + '.html'
  141. return fullPath
  142. # Creates the file name from passed URL
  143. def getNameFromURL(url):
  144. global counter
  145. name = ''.join(e for e in url if e.isalnum())
  146. if (name == ''):
  147. name = str(counter)
  148. counter = counter + 1
  149. return name
  150. def getInterestedLinks():
  151. links = []
  152. # Hacking & Cracking tutorials
  153. links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Hacking-Cracking-tutorials')
  154. # Hacking & Cracking questions
  155. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Hacking-Cracking-questions')
  156. # # Exploit PoCs
  157. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Exploit-PoCs')
  158. # # Cracked software
  159. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Cracked-software')
  160. # # Malware-development
  161. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Malware-development')
  162. # # Carding & Fraud
  163. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Carding-Fraud')
  164. # # Darknet Discussions
  165. # links.append('http://cryptbbtg65gibadeeo2awe3j7s6evg7eklserehqr4w4e2bis5tebid.onion/forumdisplay.php?fid=88')
  166. # # OPSEC
  167. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-OPSEC')
  168. # # Databases
  169. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Databases')
  170. # # Proxies
  171. # links.append('http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/Forum-Proxies')
  172. return links
  173. def crawlForum(driver):
  174. print("Crawling the OnniForums forum")
  175. linksToCrawl = getInterestedLinks()
  176. visited = set(linksToCrawl)
  177. initialTime = time.time()
  178. i = 0
  179. count = 0
  180. while i < len(linksToCrawl):
  181. link = linksToCrawl[i]
  182. print('Crawling :', link)
  183. try:
  184. try:
  185. driver.get(link)
  186. except:
  187. driver.refresh()
  188. html = driver.page_source
  189. savePage(html, link)
  190. has_next_page = True
  191. while has_next_page:
  192. list = topicPages(html)
  193. for item in list:
  194. itemURL = urlparse.urljoin(baseURL, str(item))
  195. try:
  196. driver.get(itemURL)
  197. except:
  198. driver.refresh()
  199. savePage(driver.page_source, item)
  200. driver.back()
  201. # comment out, one topic per page
  202. break
  203. # comment out, go through all pages
  204. if count == 1:
  205. count = 0
  206. break
  207. try:
  208. temp = driver.find_element(by=By.XPATH, value=
  209. '/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. try:
  214. driver.get(link)
  215. except:
  216. driver.refresh()
  217. html = driver.page_source
  218. savePage(html, link)
  219. count += 1
  220. except NoSuchElementException:
  221. has_next_page = False
  222. except Exception as e:
  223. print(link, e)
  224. i += 1
  225. # finalTime = time.time()
  226. # print finalTime - initialTime
  227. input("Crawling OnniForums forum done sucessfully. Press ENTER to continue\n")
  228. # Returns 'True' if the link is Topic link
  229. def isDescriptionLink(url):
  230. if 'Thread' in url:
  231. return True
  232. return False
  233. # Returns True if the link is a listingPage link
  234. def isListingLink(url):
  235. if 'Forum' in url:
  236. return True
  237. return False
  238. # calling the parser to define the links
  239. def topicPages(html):
  240. soup = BeautifulSoup(html, "html.parser")
  241. #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)
  242. return onniForums_links_parser(soup)
  243. def crawler():
  244. startCrawling()
  245. # print("Crawling and Parsing BestCardingWorld .... DONE!")