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.

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