Hunt down social media accounts by username across social networks (Mirror repo)
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.

635 lines
26 KiB

  1. #! /usr/bin/env python3
  2. """
  3. Sherlock: Find Usernames Across Social Networks Module
  4. This module contains the main logic to search for usernames at social
  5. networks.
  6. """
  7. import csv
  8. import os
  9. import platform
  10. import re
  11. import sys
  12. from argparse import ArgumentParser, RawDescriptionHelpFormatter
  13. from time import monotonic
  14. import requests
  15. from requests_futures.sessions import FuturesSession
  16. from torrequest import TorRequest
  17. from result import QueryStatus
  18. from result import QueryResult
  19. from notify import QueryNotifyPrint
  20. from sites import SitesInformation
  21. module_name = "Sherlock: Find Usernames Across Social Networks"
  22. __version__ = "0.12.2"
  23. class SherlockFuturesSession(FuturesSession):
  24. def request(self, method, url, hooks={}, *args, **kwargs):
  25. """Request URL.
  26. This extends the FuturesSession request method to calculate a response
  27. time metric to each request.
  28. It is taken (almost) directly from the following StackOverflow answer:
  29. https://github.com/ross/requests-futures#working-in-the-background
  30. Keyword Arguments:
  31. self -- This object.
  32. method -- String containing method desired for request.
  33. url -- String containing URL for request.
  34. hooks -- Dictionary containing hooks to execute after
  35. request finishes.
  36. args -- Arguments.
  37. kwargs -- Keyword arguments.
  38. Return Value:
  39. Request object.
  40. """
  41. #Record the start time for the request.
  42. start = monotonic()
  43. def response_time(resp, *args, **kwargs):
  44. """Response Time Hook.
  45. Keyword Arguments:
  46. resp -- Response object.
  47. args -- Arguments.
  48. kwargs -- Keyword arguments.
  49. Return Value:
  50. N/A
  51. """
  52. resp.elapsed = monotonic() - start
  53. return
  54. #Install hook to execute when response completes.
  55. #Make sure that the time measurement hook is first, so we will not
  56. #track any later hook's execution time.
  57. try:
  58. if isinstance(hooks['response'], list):
  59. hooks['response'].insert(0, response_time)
  60. elif isinstance(hooks['response'], tuple):
  61. #Convert tuple to list and insert time measurement hook first.
  62. hooks['response'] = list(hooks['response'])
  63. hooks['response'].insert(0, response_time)
  64. else:
  65. #Must have previously contained a single hook function,
  66. #so convert to list.
  67. hooks['response'] = [response_time, hooks['response']]
  68. except KeyError:
  69. #No response hook was already defined, so install it ourselves.
  70. hooks['response'] = [response_time]
  71. return super(SherlockFuturesSession, self).request(method,
  72. url,
  73. hooks=hooks,
  74. *args, **kwargs)
  75. def get_response(request_future, error_type, social_network):
  76. #Default for Response object if some failure occurs.
  77. response = None
  78. error_context = "General Unknown Error"
  79. expection_text = None
  80. try:
  81. response = request_future.result()
  82. if response.status_code:
  83. #status code exists in response object
  84. error_context = None
  85. except requests.exceptions.HTTPError as errh:
  86. error_context = "HTTP Error"
  87. expection_text = str(errh)
  88. except requests.exceptions.ProxyError as errp:
  89. error_context = "Proxy Error"
  90. expection_text = str(errp)
  91. except requests.exceptions.ConnectionError as errc:
  92. error_context = "Error Connecting"
  93. expection_text = str(errc)
  94. except requests.exceptions.Timeout as errt:
  95. error_context = "Timeout Error"
  96. expection_text = str(errt)
  97. except requests.exceptions.RequestException as err:
  98. error_context = "Unknown Error"
  99. expection_text = str(err)
  100. return response, error_context, expection_text
  101. def sherlock(username, site_data, query_notify,
  102. tor=False, unique_tor=False,
  103. proxy=None, timeout=None):
  104. """Run Sherlock Analysis.
  105. Checks for existence of username on various social media sites.
  106. Keyword Arguments:
  107. username -- String indicating username that report
  108. should be created against.
  109. site_data -- Dictionary containing all of the site data.
  110. query_notify -- Object with base type of QueryNotify().
  111. This will be used to notify the caller about
  112. query results.
  113. tor -- Boolean indicating whether to use a tor circuit for the requests.
  114. unique_tor -- Boolean indicating whether to use a new tor circuit for each request.
  115. proxy -- String indicating the proxy URL
  116. timeout -- Time in seconds to wait before timing out request.
  117. Default is no timeout.
  118. Return Value:
  119. Dictionary containing results from report. Key of dictionary is the name
  120. of the social network site, and the value is another dictionary with
  121. the following keys:
  122. url_main: URL of main site.
  123. url_user: URL of user on site (if account exists).
  124. status: QueryResult() object indicating results of test for
  125. account existence.
  126. http_status: HTTP status code of query which checked for existence on
  127. site.
  128. response_text: Text that came back from request. May be None if
  129. there was an HTTP error when checking for existence.
  130. """
  131. #Notify caller that we are starting the query.
  132. query_notify.start(username)
  133. # Create session based on request methodology
  134. if tor or unique_tor:
  135. #Requests using Tor obfuscation
  136. underlying_request = TorRequest()
  137. underlying_session = underlying_request.session
  138. else:
  139. #Normal requests
  140. underlying_session = requests.session()
  141. underlying_request = requests.Request()
  142. #Limit number of workers to 20.
  143. #This is probably vastly overkill.
  144. if len(site_data) >= 20:
  145. max_workers=20
  146. else:
  147. max_workers=len(site_data)
  148. #Create multi-threaded session for all requests.
  149. session = SherlockFuturesSession(max_workers=max_workers,
  150. session=underlying_session)
  151. # Results from analysis of all sites
  152. results_total = {}
  153. # First create futures for all requests. This allows for the requests to run in parallel
  154. for social_network, net_info in site_data.items():
  155. # Results from analysis of this specific site
  156. results_site = {}
  157. # Record URL of main site
  158. results_site['url_main'] = net_info.get("urlMain")
  159. # A user agent is needed because some sites don't return the correct
  160. # information since they think that we are bots (Which we actually are...)
  161. headers = {
  162. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0',
  163. }
  164. if "headers" in net_info:
  165. # Override/append any extra headers required by a given site.
  166. headers.update(net_info["headers"])
  167. # URL of user on site (if it exists)
  168. url = net_info["url"].format(username)
  169. # Don't make request if username is invalid for the site
  170. regex_check = net_info.get("regexCheck")
  171. if regex_check and re.search(regex_check, username) is None:
  172. # No need to do the check at the site: this user name is not allowed.
  173. results_site['status'] = QueryResult(username,
  174. social_network,
  175. url,
  176. QueryStatus.ILLEGAL)
  177. results_site["url_user"] = ""
  178. results_site['http_status'] = ""
  179. results_site['response_text'] = ""
  180. query_notify.update(results_site['status'])
  181. else:
  182. # URL of user on site (if it exists)
  183. results_site["url_user"] = url
  184. url_probe = net_info.get("urlProbe")
  185. if url_probe is None:
  186. # Probe URL is normal one seen by people out on the web.
  187. url_probe = url
  188. else:
  189. # There is a special URL for probing existence separate
  190. # from where the user profile normally can be found.
  191. url_probe = url_probe.format(username)
  192. if (net_info["errorType"] == 'status_code' and
  193. net_info.get("request_head_only", True) == True):
  194. #In most cases when we are detecting by status code,
  195. #it is not necessary to get the entire body: we can
  196. #detect fine with just the HEAD response.
  197. request_method = session.head
  198. else:
  199. #Either this detect method needs the content associated
  200. #with the GET response, or this specific website will
  201. #not respond properly unless we request the whole page.
  202. request_method = session.get
  203. if net_info["errorType"] == "response_url":
  204. # Site forwards request to a different URL if username not
  205. # found. Disallow the redirect so we can capture the
  206. # http status from the original URL request.
  207. allow_redirects = False
  208. else:
  209. # Allow whatever redirect that the site wants to do.
  210. # The final result of the request will be what is available.
  211. allow_redirects = True
  212. # This future starts running the request in a new thread, doesn't block the main thread
  213. if proxy is not None:
  214. proxies = {"http": proxy, "https": proxy}
  215. future = request_method(url=url_probe, headers=headers,
  216. proxies=proxies,
  217. allow_redirects=allow_redirects,
  218. timeout=timeout
  219. )
  220. else:
  221. future = request_method(url=url_probe, headers=headers,
  222. allow_redirects=allow_redirects,
  223. timeout=timeout
  224. )
  225. # Store future in data for access later
  226. net_info["request_future"] = future
  227. # Reset identify for tor (if needed)
  228. if unique_tor:
  229. underlying_request.reset_identity()
  230. # Add this site's results into final dictionary with all of the other results.
  231. results_total[social_network] = results_site
  232. # Open the file containing account links
  233. # Core logic: If tor requests, make them here. If multi-threaded requests, wait for responses
  234. for social_network, net_info in site_data.items():
  235. # Retrieve results again
  236. results_site = results_total.get(social_network)
  237. # Retrieve other site information again
  238. url = results_site.get("url_user")
  239. status = results_site.get("status")
  240. if status is not None:
  241. # We have already determined the user doesn't exist here
  242. continue
  243. # Get the expected error type
  244. error_type = net_info["errorType"]
  245. # Retrieve future and ensure it has finished
  246. future = net_info["request_future"]
  247. r, error_text, expection_text = get_response(request_future=future,
  248. error_type=error_type,
  249. social_network=social_network)
  250. #Get response time for response of our request.
  251. try:
  252. response_time = r.elapsed
  253. except AttributeError:
  254. response_time = None
  255. # Attempt to get request information
  256. try:
  257. http_status = r.status_code
  258. except:
  259. http_status = "?"
  260. try:
  261. response_text = r.text.encode(r.encoding)
  262. except:
  263. response_text = ""
  264. if error_text is not None:
  265. result = QueryResult(username,
  266. social_network,
  267. url,
  268. QueryStatus.UNKNOWN,
  269. query_time=response_time,
  270. context=error_text)
  271. elif error_type == "message":
  272. error = net_info.get("errorMsg")
  273. # Checks if the error message is in the HTML
  274. if not error in r.text:
  275. result = QueryResult(username,
  276. social_network,
  277. url,
  278. QueryStatus.CLAIMED,
  279. query_time=response_time)
  280. else:
  281. result = QueryResult(username,
  282. social_network,
  283. url,
  284. QueryStatus.AVAILABLE,
  285. query_time=response_time)
  286. elif error_type == "status_code":
  287. # Checks if the status code of the response is 2XX
  288. if not r.status_code >= 300 or r.status_code < 200:
  289. result = QueryResult(username,
  290. social_network,
  291. url,
  292. QueryStatus.CLAIMED,
  293. query_time=response_time)
  294. else:
  295. result = QueryResult(username,
  296. social_network,
  297. url,
  298. QueryStatus.AVAILABLE,
  299. query_time=response_time)
  300. elif error_type == "response_url":
  301. # For this detection method, we have turned off the redirect.
  302. # So, there is no need to check the response URL: it will always
  303. # match the request. Instead, we will ensure that the response
  304. # code indicates that the request was successful (i.e. no 404, or
  305. # forward to some odd redirect).
  306. if 200 <= r.status_code < 300:
  307. result = QueryResult(username,
  308. social_network,
  309. url,
  310. QueryStatus.CLAIMED,
  311. query_time=response_time)
  312. else:
  313. result = QueryResult(username,
  314. social_network,
  315. url,
  316. QueryStatus.AVAILABLE,
  317. query_time=response_time)
  318. else:
  319. #It should be impossible to ever get here...
  320. raise ValueError(f"Unknown Error Type '{error_type}' for "
  321. f"site '{social_network}'")
  322. #Notify caller about results of query.
  323. query_notify.update(result)
  324. # Save status of request
  325. results_site['status'] = result
  326. # Save results from request
  327. results_site['http_status'] = http_status
  328. results_site['response_text'] = response_text
  329. # Add this site's results into final dictionary with all of the other results.
  330. results_total[social_network] = results_site
  331. #Notify caller that all queries are finished.
  332. query_notify.finish()
  333. return results_total
  334. def timeout_check(value):
  335. """Check Timeout Argument.
  336. Checks timeout for validity.
  337. Keyword Arguments:
  338. value -- Time in seconds to wait before timing out request.
  339. Return Value:
  340. Floating point number representing the time (in seconds) that should be
  341. used for the timeout.
  342. NOTE: Will raise an exception if the timeout in invalid.
  343. """
  344. from argparse import ArgumentTypeError
  345. try:
  346. timeout = float(value)
  347. except:
  348. raise ArgumentTypeError(f"Timeout '{value}' must be a number.")
  349. if timeout <= 0:
  350. raise ArgumentTypeError(f"Timeout '{value}' must be greater than 0.0s.")
  351. return timeout
  352. def main():
  353. version_string = f"%(prog)s {__version__}\n" + \
  354. f"{requests.__description__}: {requests.__version__}\n" + \
  355. f"Python: {platform.python_version()}"
  356. parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter,
  357. description=f"{module_name} (Version {__version__})"
  358. )
  359. parser.add_argument("--version",
  360. action="version", version=version_string,
  361. help="Display version information and dependencies."
  362. )
  363. parser.add_argument("--verbose", "-v", "-d", "--debug",
  364. action="store_true", dest="verbose", default=False,
  365. help="Display extra debugging information and metrics."
  366. )
  367. parser.add_argument("--rank", "-r",
  368. action="store_true", dest="rank", default=False,
  369. help="Present websites ordered by their Alexa.com global rank in popularity.")
  370. parser.add_argument("--folderoutput", "-fo", dest="folderoutput",
  371. help="If using multiple usernames, the output of the results will be saved to this folder."
  372. )
  373. parser.add_argument("--output", "-o", dest="output",
  374. help="If using single username, the output of the result will be saved to this file."
  375. )
  376. parser.add_argument("--tor", "-t",
  377. action="store_true", dest="tor", default=False,
  378. help="Make requests over Tor; increases runtime; requires Tor to be installed and in system path.")
  379. parser.add_argument("--unique-tor", "-u",
  380. action="store_true", dest="unique_tor", default=False,
  381. help="Make requests over Tor with new Tor circuit after each request; increases runtime; requires Tor to be installed and in system path.")
  382. parser.add_argument("--csv",
  383. action="store_true", dest="csv", default=False,
  384. help="Create Comma-Separated Values (CSV) File."
  385. )
  386. parser.add_argument("--site",
  387. action="append", metavar='SITE_NAME',
  388. dest="site_list", default=None,
  389. help="Limit analysis to just the listed sites. Add multiple options to specify more than one site."
  390. )
  391. parser.add_argument("--proxy", "-p", metavar='PROXY_URL',
  392. action="store", dest="proxy", default=None,
  393. help="Make requests over a proxy. e.g. socks5://127.0.0.1:1080"
  394. )
  395. parser.add_argument("--json", "-j", metavar="JSON_FILE",
  396. dest="json_file", default=None,
  397. help="Load data from a JSON file or an online, valid, JSON file.")
  398. parser.add_argument("--timeout",
  399. action="store", metavar='TIMEOUT',
  400. dest="timeout", type=timeout_check, default=None,
  401. help="Time (in seconds) to wait for response to requests. "
  402. "Default timeout of 60.0s."
  403. "A longer timeout will be more likely to get results from slow sites."
  404. "On the other hand, this may cause a long delay to gather all results."
  405. )
  406. parser.add_argument("--print-found",
  407. action="store_true", dest="print_found_only", default=False,
  408. help="Do not output sites where the username was not found."
  409. )
  410. parser.add_argument("--no-color",
  411. action="store_true", dest="no_color", default=False,
  412. help="Don't color terminal output"
  413. )
  414. parser.add_argument("username",
  415. nargs='+', metavar='USERNAMES',
  416. action="store",
  417. help="One or more usernames to check with social networks."
  418. )
  419. parser.add_argument("--browse", "-b",
  420. action="store_true", dest="browse", default=False,
  421. help="Browse to all results on default bowser.")
  422. args = parser.parse_args()
  423. # Argument check
  424. # TODO regex check on args.proxy
  425. if args.tor and (args.proxy is not None):
  426. raise Exception("Tor and Proxy cannot be set at the same time.")
  427. # Make prompts
  428. if args.proxy is not None:
  429. print("Using the proxy: " + args.proxy)
  430. if args.tor or args.unique_tor:
  431. print("Using Tor to make requests")
  432. print("Warning: some websites might refuse connecting over Tor, so note that using this option might increase connection errors.")
  433. # Check if both output methods are entered as input.
  434. if args.output is not None and args.folderoutput is not None:
  435. print("You can only use one of the output methods.")
  436. sys.exit(1)
  437. # Check validity for single username output.
  438. if args.output is not None and len(args.username) != 1:
  439. print("You can only use --output with a single username")
  440. sys.exit(1)
  441. #Create object with all information about sites we are aware of.
  442. try:
  443. sites = SitesInformation(args.json_file)
  444. except Exception as error:
  445. print(f"ERROR: {error}")
  446. sys.exit(1)
  447. #Create original dictionary from SitesInformation() object.
  448. #Eventually, the rest of the code will be updated to use the new object
  449. #directly, but this will glue the two pieces together.
  450. site_data_all = {}
  451. for site in sites:
  452. site_data_all[site.name] = site.information
  453. if args.site_list is None:
  454. # Not desired to look at a sub-set of sites
  455. site_data = site_data_all
  456. else:
  457. # User desires to selectively run queries on a sub-set of the site list.
  458. # Make sure that the sites are supported & build up pruned site database.
  459. site_data = {}
  460. site_missing = []
  461. for site in args.site_list:
  462. for existing_site in site_data_all:
  463. if site.lower() == existing_site.lower():
  464. site_data[existing_site] = site_data_all[existing_site]
  465. if not site_data:
  466. # Build up list of sites not supported for future error message.
  467. site_missing.append(f"'{site}'")
  468. if site_missing:
  469. print(
  470. f"Error: Desired sites not found: {', '.join(site_missing)}.")
  471. sys.exit(1)
  472. if args.rank:
  473. # Sort data by rank
  474. site_dataCpy = dict(site_data)
  475. ranked_sites = sorted(site_data, key=lambda k: ("rank" not in k, site_data[k].get("rank", sys.maxsize)))
  476. site_data = {}
  477. for site in ranked_sites:
  478. site_data[site] = site_dataCpy.get(site)
  479. #Create notify object for query results.
  480. query_notify = QueryNotifyPrint(result=None,
  481. verbose=args.verbose,
  482. print_found_only=args.print_found_only,
  483. color=not args.no_color)
  484. # Run report on all specified users.
  485. for username in args.username:
  486. print()
  487. results = sherlock(username,
  488. site_data,
  489. query_notify,
  490. tor=args.tor,
  491. unique_tor=args.unique_tor,
  492. proxy=args.proxy,
  493. timeout=args.timeout)
  494. if args.output:
  495. result_file = args.output
  496. elif args.folderoutput:
  497. # The usernames results should be stored in a targeted folder.
  498. # If the folder doesn't exist, create it first
  499. os.makedirs(args.folderoutput, exist_ok=True)
  500. result_file = os.path.join(args.folderoutput, f"{username}.txt")
  501. else:
  502. result_file = f"{username}.txt"
  503. with open(result_file, "w", encoding="utf-8") as file:
  504. exists_counter = 0
  505. for website_name in results:
  506. dictionary = results[website_name]
  507. if dictionary.get("status").status == QueryStatus.CLAIMED:
  508. exists_counter += 1
  509. file.write(dictionary["url_user"] + "\n")
  510. file.write(f"Total Websites Username Detected On : {exists_counter}")
  511. if args.csv:
  512. with open(username + ".csv", "w", newline='', encoding="utf-8") as csv_report:
  513. writer = csv.writer(csv_report)
  514. writer.writerow(['username',
  515. 'name',
  516. 'url_main',
  517. 'url_user',
  518. 'exists',
  519. 'http_status',
  520. 'response_time_s'
  521. ]
  522. )
  523. for site in results:
  524. response_time_s = results[site]['status'].query_time
  525. if response_time_s is None:
  526. response_time_s = ""
  527. writer.writerow([username,
  528. site,
  529. results[site]['url_main'],
  530. results[site]['url_user'],
  531. str(results[site]['status'].status),
  532. results[site]['http_status'],
  533. response_time_s
  534. ]
  535. )
  536. if __name__ == "__main__":
  537. main()