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.

301 lines
11 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. __author__ = 'DarkWeb'
  2. '''
  3. BestCardingWorld Forum 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. import urllib.parse as urlparse
  12. import os, time
  13. from datetime import date
  14. import subprocess
  15. from bs4 import BeautifulSoup
  16. from Forums.Initialization.prepare_parser import new_parse
  17. from Forums.BestCardingWorld.parser import bestcardingworld_links_parser
  18. from Forums.Utilities.utilities import cleanHTML
  19. counter = 1
  20. baseURL = 'http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/'
  21. # Opens Tor Browser, crawls the website, then parses, then closes tor
  22. #acts like the main method for the crawler, another function at the end of this code calls this function later
  23. def startCrawling():
  24. forumName = getForumName()
  25. driver = getAccess()
  26. if driver != 'down':
  27. try:
  28. crawlForum(driver)
  29. except Exception as e:
  30. print(driver.current_url, e)
  31. closeDriver(driver)
  32. new_parse(forumName, baseURL, True)
  33. # Returns the name of the website
  34. #return: name of site in string type
  35. def getForumName():
  36. name = 'BestCardingWorld'
  37. return name
  38. # Return the base link of the website
  39. #return: url of base site in string type
  40. def getFixedURL():
  41. url = 'http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/'
  42. return url
  43. # Closes Tor Browser
  44. #@param: current selenium driver
  45. def closeDriver(driver):
  46. # global pid
  47. # os.system("taskkill /pid " + str(pro.pid))
  48. # os.system("taskkill /t /f /im tor.exe")
  49. print('Closing Tor...')
  50. driver.close()
  51. time.sleep(3)
  52. return
  53. # Creates FireFox 'driver' and configure its 'Profile'
  54. # to use Tor proxy and socket
  55. def createFFDriver():
  56. from Forums.Initialization.forums_mining import config
  57. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  58. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  59. ff_prof.set_preference("places.history.enabled", False)
  60. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  61. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  62. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  63. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  64. ff_prof.set_preference("signon.rememberSignons", False)
  65. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  66. ff_prof.set_preference("network.dns.disablePrefetch", True)#might need to turn off
  67. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  68. ff_prof.set_preference("permissions.default.image", 3)
  69. ff_prof.set_preference("browser.download.folderList", 2)
  70. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  71. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  72. ff_prof.set_preference('network.proxy.type', 1)
  73. ff_prof.set_preference("network.proxy.socks_version", 5)
  74. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  75. ff_prof.set_preference('network.proxy.socks_port', 9150)
  76. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  77. ff_prof.set_preference("javascript.enabled", True)
  78. ff_prof.update_preferences()
  79. service = Service(config.get('TOR', 'geckodriver_path'))
  80. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  81. driver.maximize_window()
  82. return driver
  83. #the driver 'gets' the url, attempting to get on the site, if it can't access return 'down'
  84. #return: return the selenium driver or string 'down'
  85. def getAccess():
  86. url = getFixedURL()
  87. driver = createFFDriver()
  88. try:
  89. driver.get(url)
  90. return driver
  91. except:
  92. driver.close()
  93. return 'down'
  94. # Saves the crawled html page, makes the directory path for html pages if not made
  95. def savePage(driver, page, url):
  96. cleanPage = cleanHTML(driver, page)
  97. filePath = getFullPathName(url)
  98. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  99. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  100. return
  101. # Gets the full path of the page to be saved along with its appropriate file name
  102. #@param: raw url as crawler crawls through every site
  103. def getFullPathName(url):
  104. from Forums.Initialization.forums_mining import config, CURRENT_DATE
  105. mainDir = os.path.join(config.get('Project', 'shared_folder'), "Forums/" + getForumName() + "/HTML_Pages")
  106. fileName = getNameFromURL(url)
  107. if isDescriptionLink(url):
  108. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  109. else:
  110. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  111. return fullPath
  112. # Creates the file name from passed URL, gives distinct name if can't be made unique after cleaned
  113. #@param: raw url as crawler crawls through every site
  114. def getNameFromURL(url):
  115. global counter
  116. name = ''.join(e for e in url if e.isalnum())
  117. if (name == ''):
  118. name = str(counter)
  119. counter = counter + 1
  120. return name
  121. # returns list of urls, here is where you can list the different urls of interest, the crawler runs through this list
  122. #in this example, there are a couple of categories some threads fall under such as
  123. #exploits, malware, and hacking tutorials
  124. def getInterestedLinks():
  125. links = []
  126. # # Penetration Tests
  127. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=43')
  128. # # Social Engineering Tests
  129. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=44')
  130. # # Exploits
  131. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=45')
  132. # # Tools
  133. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=46')
  134. # Malware
  135. links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=47')
  136. # # Cryptography
  137. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=48')
  138. # # Others
  139. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=49')
  140. # # Hacking Tutorials
  141. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=50')
  142. # # Hacked Accounts and Database Dumps
  143. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=30')
  144. # # Android Moded pak
  145. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=53')
  146. return links
  147. # gets links of interest to crawl through, iterates through list, where each link is clicked and crawled through
  148. #topic and description pages are crawled through here, where both types of pages are saved
  149. #@param: selenium driver
  150. def crawlForum(driver):
  151. print("Crawling the BestCardingWorld forum")
  152. linksToCrawl = getInterestedLinks()
  153. i = 0
  154. while i < len(linksToCrawl):
  155. link = linksToCrawl[i]
  156. print('Crawling :', link)
  157. try:
  158. has_next_page = True
  159. count = 0
  160. while has_next_page:
  161. try:
  162. driver.get(link)
  163. except:
  164. driver.refresh()
  165. html = driver.page_source
  166. savePage(driver, html, link)
  167. topics = topicPages(html)
  168. for topic in topics:
  169. has_next_topic_page = True
  170. counter = 1
  171. page = topic
  172. while has_next_topic_page:
  173. itemURL = urlparse.urljoin(baseURL, str(page))
  174. try:
  175. driver.get(itemURL)
  176. except:
  177. driver.refresh()
  178. if isListingLink(driver.current_url):
  179. break
  180. savePage(driver, driver.page_source, topic + f"page{counter}") # very important
  181. # comment out
  182. if counter == 2:
  183. break
  184. try:
  185. nav = driver.find_element(by=By.XPATH, value='/html/body/div[1]/div[2]/div[2]/div[4]/ul')
  186. li = nav.find_element(by=By.CLASS_NAME, value='next')
  187. page = li.find_element(by=By.TAG_NAME, value='a').get_attribute('href')
  188. if page == "":
  189. raise NoSuchElementException
  190. counter += 1
  191. except NoSuchElementException:
  192. has_next_topic_page = False
  193. # making sure we go back to the listing page (browser back button simulation)
  194. try:
  195. driver.get(link)
  196. except:
  197. driver.refresh()
  198. # comment out
  199. # break
  200. # comment out
  201. if count == 1:
  202. break
  203. try:
  204. bar = driver.find_element(by=By.XPATH, value='/html/body/div[1]/div[2]/div[2]/div[3]/ul')
  205. next = bar.find_element(by=By.CLASS_NAME, value='next')
  206. link = next.find_element(by=By.TAG_NAME, value='a').get_attribute('href')
  207. if link == "":
  208. raise NoSuchElementException
  209. count += 1
  210. except NoSuchElementException:
  211. has_next_page = False
  212. except Exception as e:
  213. print(link, e)
  214. i += 1
  215. print("Crawling the BestCardingWorld forum done.")
  216. # Returns 'True' if the link is a description link
  217. #@param: url of any url crawled
  218. #return: true if is a description page, false if not
  219. def isDescriptionLink(url):
  220. if 'topic' in url:
  221. return True
  222. return False
  223. # Returns True if the link is a listingPage link
  224. #@param: url of any url crawled
  225. #return: true if is a Listing page, false if not
  226. def isListingLink(url):
  227. if '.onion/viewforum' in url:
  228. return True
  229. return False
  230. # calling the parser to define the links, the html is the url of a link from the list of interested link list
  231. #@param: link from interested link list
  232. #return: list of description links that should be crawled through
  233. def topicPages(html):
  234. soup = BeautifulSoup(html, "html.parser")
  235. #print(soup.find('div', {"class": "forumbg"}).find('ul', {"class": "topiclist topics"}).find('li', {"class": "row bg1"}).find('a', {"class": "topictitle"}, href=True))
  236. return bestcardingworld_links_parser(soup)
  237. def crawler():
  238. startCrawling()
  239. # print("Crawling and Parsing BestCardingWorld .... DONE!")