|
| 1 | +import os |
| 2 | +import json |
| 3 | +import string |
| 4 | +import random |
| 5 | + |
| 6 | + |
| 7 | +import blessings |
| 8 | + |
| 9 | +t = blessings.Terminal() |
| 10 | + |
| 11 | + |
| 12 | +def random_file_name(acceptable=string.ascii_letters, length=7): |
| 13 | + """ |
| 14 | + create a random filename. |
| 15 | +
|
| 16 | + `note: this could potentially cause issues if there |
| 17 | + a lot of file in the directory` |
| 18 | + """ |
| 19 | + retval = set() |
| 20 | + for _ in range(length): |
| 21 | + retval.add(random.choice(acceptable)) |
| 22 | + return ''.join(list(retval)) |
| 23 | + |
| 24 | + |
| 25 | +def load_exploits(path, node="exploits"): |
| 26 | + """ |
| 27 | + load exploits from a given path, depending on how many files are loaded into |
| 28 | + the beginning `file_list` variable it will display a list of them and prompt |
| 29 | + or just select the one in the list |
| 30 | + """ |
| 31 | + retval = [] |
| 32 | + file_list = os.listdir(path) |
| 33 | + if len(file_list) != 1: |
| 34 | + print("\n[{}] total of {} files discovered select one".format( |
| 35 | + t.green("+"), len(file_list))) |
| 36 | + for i, f in enumerate(file_list, start=1): |
| 37 | + print("{}. {}".format(i, f[:-5])) |
| 38 | + action = raw_input("\n<" + t.cyan("AUTOSPLOIT") + ">$ ") |
| 39 | + selected_file = file_list[int(action) - 1] |
| 40 | + else: |
| 41 | + selected_file = file_list[0] |
| 42 | + |
| 43 | + selected_file_path = os.path.join(path, selected_file) |
| 44 | + |
| 45 | + with open(selected_file_path) as exploit_file: |
| 46 | + # loading it like this has been known to cause Unicode issues later on down |
| 47 | + # the road |
| 48 | + _json = json.loads(exploit_file.read()) |
| 49 | + for item in _json[node]: |
| 50 | + # so we'll reload it into a ascii string before we save it into the file |
| 51 | + retval.append(str(item)) |
| 52 | + return retval |
| 53 | + |
| 54 | + |
| 55 | +def text_file_to_dict(path): |
| 56 | + """ |
| 57 | + take a text file path, and load all of the information into a `dict` |
| 58 | + send that `dict` into a JSON format and save it into a file. it will |
| 59 | + use the same start node (`exploits`) as the `default_modules.json` |
| 60 | + file so that we can just use one node instead of multiple when parsing |
| 61 | + """ |
| 62 | + start_dict = {"exploits": []} |
| 63 | + with open(path) as exploits: |
| 64 | + for exploit in exploits.readlines(): |
| 65 | + # load everything into the dict |
| 66 | + start_dict["exploits"].append(exploit.strip()) |
| 67 | + filename_path = "{}/etc/json/{}.json".format(os.getcwd(), random_file_name()) |
| 68 | + with open(filename_path, "a+") as exploits: |
| 69 | + # sort and indent to make it look pretty |
| 70 | + _data = json.dumps(start_dict, indent=4, sort_keys=True) |
| 71 | + exploits.write(_data) |
| 72 | + return filename_path |
| 73 | + |
0 commit comments