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.

303 lines
10 KiB

  1. __author__ = 'DarkWeb'
  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. from datetime import date
  17. import subprocess
  18. from bs4 import BeautifulSoup
  19. from Forums.Initialization.prepare_parser import new_parse
  20. from Forums.OnniForums.parser import cryptBB_links_parser
  21. from Forums.Utilities.utilities import cleanHTML
  22. counter = 1
  23. baseURL = 'http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/'
  24. # Opens Tor Browser, crawls the website
  25. def startCrawling():
  26. opentor()
  27. # forumName = getForumName()
  28. driver = getAccess()
  29. if driver != 'down':
  30. try:
  31. login(driver)
  32. # crawlForum(driver)
  33. except Exception as e:
  34. print(driver.current_url, e)
  35. closetor(driver)
  36. # new_parse(forumName, False)
  37. # Opens Tor Browser
  38. def opentor():
  39. global pid
  40. print("Connecting Tor...")
  41. path = open('../../path.txt').readline().strip()
  42. pro = subprocess.Popen(path)
  43. pid = pro.pid
  44. time.sleep(7.5)
  45. input('Tor Connected. Press ENTER to continue\n')
  46. return
  47. # Login using premade account credentials and do login captcha manually
  48. def login(driver):
  49. #click login button
  50. login_link = driver.find_element(
  51. by=By.XPATH, value='/html/body/div/div[2]/div/table/tbody/tr[2]/td/center/pre/strong/a').\
  52. get_attribute('href')
  53. driver.get(login_link)
  54. #entering username and password into input boxes
  55. usernameBox = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/table/tbody/tr[2]/td[2]/input')
  56. #Username here
  57. usernameBox.send_keys('purely_cabbage')
  58. passwordBox = driver.find_element(by=By.XPATH, value='/html/body/div/div[2]/div/form/table/tbody/tr[3]/td[2]/input')
  59. #Password here
  60. passwordBox.send_keys('$ourP@tchK1ds')
  61. input("Press ENTER when log in is completed\n")
  62. # wait for listing page show up (This Xpath may need to change based on different seed url)
  63. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  64. (By.XPATH, '//*[@id="tab_content"]')))
  65. # Returns the name of the website
  66. def getForumName():
  67. name = 'OnniForums'
  68. return name
  69. # Return the link of the website
  70. def getFixedURL():
  71. url = 'http://onnii6niq53gv3rvjpi7z5axkasurk2x5w5lwliep4qyeb2azagxn4qd.onion/'
  72. return url
  73. # Closes Tor Browser
  74. def closetor(driver):
  75. # global pid
  76. # os.system("taskkill /pid " + str(pro.pid))
  77. # os.system("taskkill /t /f /im tor.exe")
  78. print('Closing Tor...')
  79. driver.close()
  80. time.sleep(3)
  81. return
  82. # Creates FireFox 'driver' and configure its 'Profile'
  83. # to use Tor proxy and socket
  84. def createFFDriver():
  85. file = open('../../path.txt', 'r')
  86. lines = file.readlines()
  87. ff_binary = FirefoxBinary(lines[0].strip())
  88. ff_prof = FirefoxProfile(lines[1].strip())
  89. ff_prof.set_preference("places.history.enabled", False)
  90. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  91. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  92. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  93. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  94. ff_prof.set_preference("signon.rememberSignons", False)
  95. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  96. ff_prof.set_preference("network.dns.disablePrefetch", True)#
  97. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  98. ff_prof.set_preference("permissions.default.image", 3)
  99. ff_prof.set_preference("browser.download.folderList", 2)
  100. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  101. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  102. ff_prof.set_preference('network.proxy.type', 1)
  103. ff_prof.set_preference("network.proxy.socks_version", 5)
  104. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  105. ff_prof.set_preference('network.proxy.socks_port', 9150)
  106. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  107. ff_prof.set_preference("javascript.enabled", True)
  108. ff_prof.update_preferences()
  109. service = Service(lines[2].strip())
  110. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  111. return driver
  112. def getAccess():
  113. url = getFixedURL()
  114. driver = createFFDriver()
  115. try:
  116. driver.get(url)
  117. return driver
  118. except:
  119. driver.close()
  120. return 'down'
  121. # Saves the crawled html page
  122. def savePage(page, url):
  123. cleanPage = cleanHTML(page)
  124. filePath = getFullPathName(url)
  125. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  126. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  127. return
  128. # Gets the full path of the page to be saved along with its appropriate file name
  129. def getFullPathName(url):
  130. fileName = getNameFromURL(url)
  131. if isDescriptionLink(url):
  132. fullPath = r'..\OnniForums\HTML_Pages\\' + str(
  133. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  134. "%04d" % date.today().year) + r'\\' + r'Description\\' + fileName + '.html'
  135. else:
  136. fullPath = r'..\OnniForums\HTML_Pages\\' + str(
  137. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  138. "%04d" % date.today().year) + r'\\' + 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. driver.back()
  199. # comment out
  200. break
  201. # comment out
  202. if count == 1:
  203. count = 0
  204. break
  205. try:
  206. temp = driver.find_element(by=By.XPATH, value=
  207. '/html/body/div/div[2]/div/div[2]/div')
  208. link = temp.find_element(by=By.CLASS_NAME, value='pagination_next').get_attribute('href')
  209. if link == "":
  210. raise NoSuchElementException
  211. try:
  212. driver.get(link)
  213. except:
  214. driver.refresh()
  215. html = driver.page_source
  216. savePage(html, link)
  217. count += 1
  218. except NoSuchElementException:
  219. has_next_page = False
  220. except Exception as e:
  221. print(link, e)
  222. i += 1
  223. # finalTime = time.time()
  224. # print finalTime - initialTime
  225. input("Crawling OnniForums forum done sucessfully. Press ENTER to continue\n")
  226. # Returns 'True' if the link is Topic link
  227. def isDescriptionLink(url):
  228. if 'Thread' in url:
  229. return True
  230. return False
  231. # Returns True if the link is a listingPage link
  232. def isListingLink(url):
  233. if 'Forum' in url:
  234. return True
  235. return False
  236. # calling the parser to define the links
  237. def topicPages(html):
  238. soup = BeautifulSoup(html, "html.parser")
  239. #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)
  240. return cryptBB_links_parser(soup)
  241. def crawler():
  242. startCrawling()
  243. # print("Crawling and Parsing BestCardingWorld .... DONE!")