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.

292 lines
10 KiB

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. # crawlForum(driver)
  29. # new_parse(forumName, False)
  30. new_parse(forumName, False)
  31. # closetor(driver)
  32. # Opens Tor Browser
  33. #prompts for ENTER input to continue
  34. def opentor():
  35. global pid
  36. print("Connecting Tor...")
  37. path = open('../../path.txt').readline().strip()
  38. pro = subprocess.Popen(path)
  39. pid = pro.pid
  40. time.sleep(7.5)
  41. input('Tor Connected. Press ENTER to continue\n')
  42. return
  43. # Returns the name of the website
  44. #return: name of site in string type
  45. def getForumName():
  46. name = 'BestCardingWorld'
  47. return name
  48. # Return the base link of the website
  49. #return: url of base site in string type
  50. def getFixedURL():
  51. url = 'http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/'
  52. return url
  53. # Closes Tor Browser
  54. #@param: current selenium driver
  55. def closetor(driver):
  56. global pid
  57. # os.system("taskkill /pid " + str(pro.pid))
  58. os.system("taskkill /t /f /im tor.exe")
  59. print('Closing Tor...')
  60. driver.close()
  61. time.sleep(3)
  62. return
  63. # Creates FireFox 'driver' and configure its 'Profile'
  64. # to use Tor proxy and socket
  65. def createFFDriver():
  66. file = open('../../path.txt', 'r')
  67. lines = file.readlines()
  68. ff_binary = FirefoxBinary(lines[0].strip())
  69. ff_prof = FirefoxProfile(lines[1].strip())
  70. ff_prof.set_preference("places.history.enabled", False)
  71. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  72. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  73. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  74. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  75. ff_prof.set_preference("signon.rememberSignons", False)
  76. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  77. ff_prof.set_preference("network.dns.disablePrefetch", True)
  78. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  79. ff_prof.set_preference("permissions.default.image", 2)
  80. ff_prof.set_preference("browser.download.folderList", 2)
  81. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  82. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  83. ff_prof.set_preference('network.proxy.type', 1)
  84. ff_prof.set_preference("network.proxy.socks_version", 5)
  85. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  86. ff_prof.set_preference('network.proxy.socks_port', 9150)
  87. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  88. ff_prof.set_preference("javascript.enabled", True)
  89. ff_prof.update_preferences()
  90. service = Service(lines[2].strip())
  91. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  92. return driver
  93. #the driver 'gets' the url, attempting to get on the site, if it can't access return 'down'
  94. #return: return the selenium driver or string 'down'
  95. def getAccess():
  96. url = getFixedURL()
  97. driver = createFFDriver()
  98. try:
  99. driver.get(url)
  100. return driver
  101. except:
  102. return 'down'
  103. # Saves the crawled html page, makes the directory path for html pages if not made
  104. def savePage(page, url):
  105. cleanPage = cleanHTML(page)
  106. filePath = getFullPathName(url)
  107. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  108. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  109. return
  110. # Gets the full path of the page to be saved along with its appropriate file name
  111. #@param: raw url as crawler crawls through every site
  112. def getFullPathName(url):
  113. fileName = getNameFromURL(url)
  114. if isDescriptionLink(url):
  115. fullPath = r'C:\Users\fakeguy\Documents\threatIntelligence-main\DarkWebMining_Working\Forums\BestCardingWorld\HTML_Pages\\' + str(
  116. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  117. "%04d" % date.today().year) + r'\\' + r'Description\\' + fileName + '.html'
  118. else:
  119. fullPath = r'C:\Users\fakeguy\Documents\threatIntelligence-main\DarkWebMining_Working\Forums\BestCardingWorld\HTML_Pages\\' + str(
  120. "%02d" % date.today().month) + str("%02d" % date.today().day) + str(
  121. "%04d" % date.today().year) + r'\\' + 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. #General Discussion
  158. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=16&sid=6a4959d49be41e72944e5aa5684c187a')
  159. return links
  160. # gets links of interest to crawl through, iterates through list, where each link is clicked and crawled through
  161. #topic and description pages are crawled through here, where both types of pages are saved
  162. #@param: selenium driver
  163. def crawlForum(driver):
  164. print("Crawling the BestCardingWorld forum")
  165. linksToCrawl = getInterestedLinks()
  166. visited = set(linksToCrawl)
  167. initialTime = time.time()
  168. i = 0
  169. while i < len(linksToCrawl):
  170. link = linksToCrawl[i]
  171. print('Crawling :', link)
  172. try:
  173. try:
  174. driver.get(link)
  175. except:
  176. driver.refresh()
  177. html = driver.page_source
  178. savePage(html, link)
  179. has_next_page = True
  180. while has_next_page:
  181. list = topicPages(html)
  182. for item in list:
  183. itemURL = urlparse.urljoin(baseURL, str(item))
  184. try:
  185. driver.get(itemURL)
  186. except:
  187. driver.refresh()
  188. savePage(driver.page_source, item)
  189. driver.back()
  190. try:
  191. bar = driver.find_element(by=By.XPATH, value=
  192. '/html/body/div[1]/div[2]/div[2]/div[3]/ul')
  193. next = bar.find_element_by_class_name('next')
  194. link = next.find_element_by_tag_name('a').get_attribute('href')
  195. try:
  196. driver.get(link)
  197. except:
  198. driver.refresh()
  199. html = driver.page_source
  200. savePage(html, link)
  201. except NoSuchElementException:
  202. has_next_page = False
  203. except Exception as e:
  204. print(link, e.message)
  205. i += 1
  206. # finalTime = time.time()
  207. # print finalTime - initialTime
  208. input("Crawling BestCardingWorld forum done sucessfully. Press ENTER to continue\n")
  209. # Returns 'True' if the link is a description link
  210. #@param: url of any url crawled
  211. #return: true if is a description page, false if not
  212. def isDescriptionLink(url):
  213. if 'topic' in url:
  214. return True
  215. return False
  216. # Returns True if the link is a listingPage link
  217. #@param: url of any url crawled
  218. #return: true if is a Listing page, false if not
  219. def isListingLink(url):
  220. if 'forum' in url:
  221. return True
  222. return False
  223. # calling the parser to define the links, the html is the url of a link from the list of interested link list
  224. #@param: link from interested link list
  225. #return: list of description links that should be crawled through
  226. def topicPages(html):
  227. soup = BeautifulSoup(html, "html.parser")
  228. #print(soup.find('div', {"class": "forumbg"}).find('ul', {"class": "topiclist topics"}).find('li', {"class": "row bg1"}).find('a', {"class": "topictitle"}, href=True))
  229. return bestcardingworld_links_parser(soup)
  230. def crawler():
  231. startCrawling()
  232. # print("Crawling and Parsing BestCardingWorld .... DONE!")