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.

342 lines
12 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 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. Kingdom Market 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.common.exceptions import TimeoutException
  11. from selenium.webdriver.firefox.options import Options
  12. from selenium.webdriver.common.by import By
  13. from selenium.webdriver.support import expected_conditions as EC
  14. from selenium.webdriver.support.ui import WebDriverWait
  15. from selenium.webdriver.support.ui import Select
  16. from PIL import Image
  17. import base64
  18. from io import BytesIO
  19. import urllib.parse as urlparse
  20. import os, re, time
  21. from datetime import date
  22. import subprocess
  23. from bs4 import BeautifulSoup
  24. from MarketPlaces.Initialization.prepare_parser import new_parse
  25. from MarketPlaces.Kingdom.parser import kingdom_links_parser
  26. from MarketPlaces.Utilities.utilities import cleanHTML
  27. counter = 1
  28. baseURL = 'http://kingdomm7v6yed55o2rbspvs4exn5bzfxdizqaav27tw6gw4zc65vdad.onion'
  29. # Opens Tor Browser, crawls the website
  30. def startCrawling():
  31. # marketName = getMarketName()
  32. driver = getAccess()
  33. if driver != 'down':
  34. try:
  35. captcha(driver)
  36. login(driver)
  37. crawlForum(driver)
  38. except Exception as e:
  39. print(driver.current_url, e)
  40. closeDriver(driver)
  41. # new_parse(marketName, False)
  42. def captcha(driver):
  43. '''
  44. # wait for captcha page
  45. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  46. (By.XPATH, "/html/body/div/div[1]")))
  47. # save captcha to local
  48. driver.find_element(by=By.XPATH, value='/html/body/div/div[2]').screenshot(
  49. r'..\Kingdom\captcha1.png')
  50. # This method will show image in any image viewer
  51. im = Image.open(r'..\Kingdom\captcha1.png')
  52. im.show()
  53. iframes = driver.find_elements(by=By.TAG_NAME, value='iframe')
  54. # ask user input captcha solution in terminal
  55. print("Enter squares from smallest to largest (squares are numbered 1-9 left to right)")
  56. for order in ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']:
  57. id = input(f"{order}: ")
  58. iframes[int(id)-1].click()
  59. '''
  60. input("Press ENTER when CAPTCHA is completed\n")
  61. # wait for login page
  62. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  63. (By.XPATH, "/html/body/div/div/div[3]/div[1]/div/div/form/div[3]/div/div[1]/button")))
  64. # Login using premade account credentials and do login captcha manually
  65. def login(driver):
  66. # wait for login page
  67. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  68. (By.XPATH, "/html/body/div/div/div[3]/div[1]/div/div/form/div[3]/div/div[1]/button")))
  69. # entering username and password into input boxes
  70. usernameBox = driver.find_element(by=By.XPATH, value='//*[@id="loginformwithcaptcha-name"]')
  71. # Username here
  72. usernameBox.send_keys('blabri')
  73. passwordBox = driver.find_element(by=By.XPATH, value='//*[@id="loginformwithcaptcha-passwd"]')
  74. # Password here
  75. passwordBox.send_keys('fishowal')
  76. select = Select(driver.find_element(by=By.XPATH, value='//*[@id="loginformwithcaptcha-sessiontime"]'))
  77. select.select_by_visible_text('24 hours')
  78. '''
  79. # wait for captcha page show up
  80. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  81. (By.XPATH, '//*[@id="captcha"]')))
  82. # save captcha to local
  83. driver.find_element(by=By.XPATH, value='//*[@id="captcha"]').screenshot(r'..\Kingdom\captcha2.png')
  84. # This method will show image in any image viewer
  85. im = Image.open(r'..\Kingdom\captcha2.png')
  86. im.show()
  87. # wait until input space show up
  88. inputBox = driver.find_element(by=By.XPATH, value='//*[@id="loginformwithcaptcha-captcha"]')
  89. # ask user input captcha solution in terminal
  90. userIn = input("Enter solution: ")
  91. # send user solution into the input space
  92. inputBox.send_keys(userIn)
  93. # click the verify(submit) button
  94. driver.find_element(by=By.XPATH, value="/html/body/div/div/div[3]/div[1]/div/div/form/div[3]/div/div[1]/button").click()
  95. '''
  96. input("Press ENTER when CAPTCHA is completed\n")
  97. # wait for listing page show up (This Xpath may need to change based on different seed url)
  98. WebDriverWait(driver, 50).until(EC.visibility_of_element_located(
  99. (By.XPATH, '/html/body/div/div/div[3]/div[2]')))
  100. # Returns the name of the website
  101. def getMarketName():
  102. name = 'Kingdom'
  103. return name
  104. # Return the link of the website
  105. def getFixedURL():
  106. url = 'http://kingdomm7v6yed55o2rbspvs4exn5bzfxdizqaav27tw6gw4zc65vdad.onion'
  107. return url
  108. # Closes Tor Browser
  109. def closeDriver(driver):
  110. # global pid
  111. # os.system("taskkill /pid " + str(pro.pid))
  112. # os.system("taskkill /t /f /im tor.exe")
  113. print('Closing Tor...')
  114. driver.close()
  115. time.sleep(3)
  116. return
  117. # Creates FireFox 'driver' and configure its 'Profile'
  118. # to use Tor proxy and socket
  119. def createFFDriver():
  120. from MarketPlaces.Initialization.markets_mining import config
  121. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  122. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  123. ff_prof.set_preference("places.history.enabled", False)
  124. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  125. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  126. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  127. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  128. ff_prof.set_preference("signon.rememberSignons", False)
  129. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  130. ff_prof.set_preference("network.dns.disablePrefetch", True)
  131. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  132. ff_prof.set_preference("permissions.default.image", 3)
  133. ff_prof.set_preference("browser.download.folderList", 2)
  134. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  135. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  136. ff_prof.set_preference('network.proxy.type', 1)
  137. ff_prof.set_preference("network.proxy.socks_version", 5)
  138. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  139. ff_prof.set_preference('network.proxy.socks_port', 9150)
  140. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  141. ff_prof.set_preference("javascript.enabled", False)
  142. ff_prof.update_preferences()
  143. service = Service(config.get('TOR', 'geckodriver_path'))
  144. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  145. driver.maximize_window()
  146. return driver
  147. def getAccess():
  148. url = getFixedURL()
  149. driver = createFFDriver()
  150. try:
  151. driver.get(url)
  152. return driver
  153. except:
  154. driver.close()
  155. return 'down'
  156. # Saves the crawled html page
  157. def savePage(driver, page, url):
  158. cleanPage = cleanHTML(driver, page)
  159. filePath = getFullPathName(url)
  160. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  161. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  162. return
  163. # Gets the full path of the page to be saved along with its appropriate file name
  164. def getFullPathName(url):
  165. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  166. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMKTName() + "/HTML_Pages")
  167. fileName = getNameFromURL(url)
  168. if isDescriptionLink(url):
  169. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  170. else:
  171. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  172. return fullPath
  173. # Creates the file name from passed URL
  174. def getNameFromURL(url):
  175. global counter
  176. name = ''.join(e for e in url if e.isalnum())
  177. if (name == ''):
  178. name = str(counter)
  179. counter = counter + 1
  180. return name
  181. def getInterestedLinks():
  182. links = []
  183. # Software and Malware
  184. links.append('http://kingdomm7v6yed55o2rbspvs4exn5bzfxdizqaav27tw6gw4zc65vdad.onion/offers?filter_category=127&t=c298a77d9e93ad32')
  185. # # Services
  186. # links.append('http://kingdomm7v6yed55o2rbspvs4exn5bzfxdizqaav27tw6gw4zc65vdad.onion/offers?filter_category=45&t=c298a77d9e93ad32')
  187. # # Exploits
  188. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=45')
  189. # # Tools
  190. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=46')
  191. # # Malware
  192. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=47')
  193. # # Cryptography
  194. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=48')
  195. # # Others
  196. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=49')
  197. # # Hacking Tutorials
  198. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=50')
  199. # # Hacked Accounts and Database Dumps
  200. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=30')
  201. # # Android Moded pak
  202. # links.append('http://bestteermb42clir6ux7xm76d4jjodh3fpahjqgbddbmfrgp4skg2wqd.onion/viewforum.php?f=53')
  203. return links
  204. def crawlForum(driver):
  205. print("Crawling the Kingdom market")
  206. linksToCrawl = getInterestedLinks()
  207. i = 0
  208. while i < len(linksToCrawl):
  209. link = linksToCrawl[i]
  210. print('Crawling :', link)
  211. try:
  212. has_next_page = True
  213. count = 0
  214. while has_next_page:
  215. try:
  216. driver.get(link)
  217. except:
  218. driver.refresh()
  219. html = driver.page_source
  220. savePage(driver, html, link)
  221. list = productPages(html)
  222. for item in list:
  223. itemURL = urlparse.urljoin(baseURL, str(item))
  224. try:
  225. driver.get(itemURL)
  226. except:
  227. driver.refresh()
  228. savePage(driver, driver.page_source, item)
  229. driver.back()
  230. # comment out
  231. break
  232. # comment out
  233. if count == 1:
  234. break
  235. try:
  236. temp = driver.find_element(by=By.XPATH, value=
  237. '/html/body/div/div/div[3]/div[2]/div[2]/div/div/ul')
  238. next = temp.find_element_by_class_name("next")
  239. link = link.find_element_by_tag_name('a').get_attribute('href')
  240. if link == "":
  241. raise NoSuchElementException
  242. count += 1
  243. except NoSuchElementException:
  244. has_next_page = False
  245. except Exception as e:
  246. print(link, e)
  247. i += 1
  248. input("Crawling Kingdom Market done sucessfully. Press ENTER to continue\n")
  249. # Returns 'True' if the link is Topic link
  250. def isDescriptionLink(url):
  251. if 'view' in url:
  252. return True
  253. return False
  254. # Returns True if the link is a listingPage link
  255. def isListingLink(url):
  256. if 'category' in url:
  257. return True
  258. return False
  259. # calling the parser to define the links
  260. def productPages(html):
  261. soup = BeautifulSoup(html, "html.parser")
  262. #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)
  263. return kingdom_links_parser(soup)
  264. def crawler():
  265. startCrawling()
  266. # print("Crawling and Parsing BestCardingWorld .... DONE!")