예제 #1
0
def foobar(input_file, file_list, option, buffer_size, count):
    """
        Sample method showing some usage examples.
    """

    # First of all, check if the paramters have the correct type
    pv.param_type_list([[input_file, "input file", str],
                        [file_list, "file list",
                         list], [option, "option", str],
                        [buffer_size, "buffer size", int],
                        [count, "count", int]])

    # Ensure that 'input_file' exists
    pv.path(input_file, "input file", True, True)

    # Ensure that 'input_file' is not identical with any from 'file_list'
    pv.compfile(input_file, "input file", file_list)

    # Ensure that the file name of 'input_file' does not contain any wildcards
    # and also that it does not contain brackets or exlamation marks
    pv.string(input_file, "input file", False, ['(', ')', '!'])

    # Ensure 'option' is one of the options from the list
    pv.compstr(option, "option", ["print", "read", "write"])

    # Ensure that 'buffer_size' is a postive integer between 1 and 4096
    pv.intrange(buffer_size, "buffer size", 1, 4096, False)

    # Finally, ensure that 'count' is either zero or a positive integer
    pv.intvalue(count, "count", True, True, False)

    print("Parameter validation successful.")
예제 #2
0
def get_task_file(task_id):
    """
        Get the task file path for the Erfr encryption or decryption
        process with the given task ID.
    """
    pv.intrange(task_id, "task ID", 1, get_max_tasks(), False)

    return os.path.join(tempfile.gettempdir(), "erfr_" + \
                        str(task_id).rjust(4, "0") + ".tmp")
예제 #3
0
def replace_chars(config_file,
                  string="",
                  remove_spaces=False,
                  remove_chars=False,
                  number=1,
                  sort_length=False):
    """
        Replace characters in a string based on the given config file.
    """
    pv.string(string, "string to modify")
    pv.intrange(number, "number of strings", 1, None)
    try:
        pv.path(config_file, "config", True, True)
    except:
        config_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
        config_file = \
            os.path.join(config_dir, "cfg", os.path.basename(config_file))
        pv.path(config_file, "config", True, True)

    config_file = os.path.abspath(config_file)
    number = int(number)

    if remove_spaces:
        string = string.replace(" ", "")

    if remove_chars:
        chars = __read_option(config_file, "Remove", "Characters")
        chars = chars.replace(" ", "")

        for char in chars:
            string = string.replace(char, "")

    dict_chars = {}
    for char in string:
        if char in ("[", "]", "=", ";", ","):
            continue

        value = __read_option(config_file, "Replace", char.upper())
        if char not in dict_chars:
            replace_chars = value.strip(", ").replace(" ", "").split(",")
            dict_chars.update({char: replace_chars})

    output = []
    for n in range(number):
        string_new = __transform(string, output, dict_chars)
        output.append(string_new)

    if sort_length:
        output = sorted(output, key=len, reverse=True)

    return output
예제 #4
0
def delete_temp_files(task_id):
    """
        Delete the temporary files of an Erfr process using the corresponding
        Erfr task ID.
    """
    if task_id == None:
        return

    pv.intrange(task_id, "task ID", 1, get_max_tasks(), False)

    task_file = get_task_file(task_id)
    file_key = task_file.rstrip(".tmp") + ".key"

    delete_file(file_key, "temporary key")
    delete_file(task_file, "temporary task")
예제 #5
0
def status(task_id, process, status):
    """
        Print a status message on the terminal.
    """
    if task_id == None:
        return

    pv.intrange(task_id, "task ID", 1, get_max_tasks(), False)

    message = "%sed %s process with task ID %s." % \
              (str(status).capitalize(), str(process), str(task_id))

    while (" " * 2) in message:
        message = message.replace((" " * 2), " ")

    print message
예제 #6
0
def get_task_id(task_id):
    """
        Get a numeric task ID for an Erfr process or validate a given one.
    """
    disabled = \
        bool(int(global_config(["TaskID"], ["disable_permanently"], "0")))
    explicit = \
        bool(int(global_config(["TaskID"], ["explicit"], "0")))

    if disabled:
        return None
    elif explicit:
        if task_id == None:
            return None
        elif int(task_id) == 0:
            return None

    if task_id == None:
        for task in range(1, get_max_tasks() + 1):
            task_file = get_task_file(task)

            if file_exists(task_file):
                task_id = 0
            else:
                task_id = task
                break

        if task_id == 0:
            exception("All task IDs already are in use. Please wait for an " \
                      "Erfr process to complete before starting a new one.")
    elif int(task_id) == 0:
        task_id = None
    else:
        pv.intrange(task_id, "task ID", 1, get_max_tasks(), False)

        task_file = get_task_file(task_id)
        if file_exists(task_file):
            exception("The task file for the given task ID already exists.")

    return task_id
예제 #7
0
def validate_range(range_value_min=0, range_value_max=0, range_value_step=0):
    """
        Validate byte range values.
    """
    pv.intvalue(range_value_max, "maximum byte range", True, False, False)
    range_value_max = int(range_value_max)
    pv.intrange(range_value_min, "minimum byte range", 1,
                (int(range_value_max) - 1), False)
    pv.intvalue(range_value_step, "step byte range", True, False, True)

    range_value_min = int(range_value_min)
    range_value_step = int(range_value_step)
    range_length = range_value_max - range_value_min

    if range_length < 4:
        exception("The range must at least have a length of 4.")

    range_max_step = int(range_length / 2)
    pv.intrange(range_value_step, "step byte range", (range_max_step * -1),
                range_max_step, False)

    return range_value_min, range_value_max, range_value_step
예제 #8
0
def rename_files(directory, rename_mode, separator=" ", recursive=False,
                 padding=0, exclude=None, pattern=None, ignore_case=True,
                 regex_syntax=False, report_file=None, ignore_symlinks=False,
                 ignore_file_ext=False, custom_name=None, step=1,
                 order_by=None):
    """
        Rename the base name of files based on the name of the directory where
        they are stored in and add a numeric ID.
    """
    pv.path(directory, "given", False, True)
    pv.compstr(rename_mode, "rename mode",
               ["fill-gaps", "increase", "keep-order", "rename-new"])
    pv.intrange(padding, "padding", 0, 12, True)
    pv.string(separator, "seperator", False, common.get_invalid_chars())
    pv.intvalue(step, "step", True, False, False)

    if not order_by == None:
        pv.compstr(order_by, "order by", ["accessed", "created", "modified"])
        if not rename_mode == "keep-order":
            raise Exception("The order-by argument can only be used in " \
                            "combination with keep-order mode.")

    step = int(step)
    rename_mode = rename_mode.lower()
    directory = os.path.abspath(directory)
    if not directory.endswith(os.path.sep):
        directory += os.path.sep

    if report_file == None:
        simulate = False
    else:
        pv.path(report_file, "report", True, False)
        report_file = os.path.abspath(report_file)
        simulate = True

    if not custom_name == None:
        pv.string(custom_name, "custom file name", False,
                  common.get_invalid_chars())

    time_start = dt.now()

    list_content = []
    list_excluded = []
    list_renamed = []
    list_skipped = []
    regex = None
    if not pattern == None:
        regex = common.compile_regex(pattern, ignore_case, regex_syntax)

    list_content, list_excluded = \
        common.get_files(directory, recursive, ignore_case, regex, exclude,
                         ignore_symlinks, order_by)

    for item in list_content:
        list_files = item[1]
        if rename_mode == "fill-gaps":
            list_renamed, list_skipped = \
                __rename_files_fill(list_files, list_renamed, list_skipped,
                                    separator, padding, True, ignore_file_ext,
                                    custom_name, step)
        elif rename_mode == "rename-new":
            list_renamed, list_skipped = \
                __rename_files_fill(list_files, list_renamed, list_skipped,
                                    separator, padding, False,
                                    ignore_file_ext, custom_name, step)
        elif rename_mode == "keep-order":
            list_renamed, list_skipped = \
                __rename_files_keep_order(list_files, list_renamed,
                                          list_skipped, separator, padding,
                                          ignore_file_ext, custom_name, step,
                                          order_by)
        else:
            raise Exception("An invalid rename mode was given.")

    if simulate:
        if padding == 0:
            padding = "Set automatically"
        else:
            padding = str(padding)

        explicit = None
        if exclude == None:
            exclude = False
            explicit = False
        elif exclude:
            explicit = False
        else:
            explicit = True

        if order_by == "accessed":
            order_by = "Access time"
        elif order_by == "created":
            order_by = "Creation time"
        elif order_by == "modified":
            order_by = "Modification time"
        else:
            order_by = "False"

        list_header = []
        list_header.append("Nomen File Renamer simulation report")
        list_header.append(["Report file name:", report_file])
        list_header.append(["Directory:", directory])
        list_header.append(["Recursive:", recursive])
        list_header.append(["Ignore symlinks:", ignore_symlinks])
        list_header.append(["Rename mode:", rename_mode.capitalize()])
        list_header.append(["Order by time:", order_by])
        list_header.append(["Separator:", "\"" + separator + "\" " \
                            "(without double quotes)"])
        list_header.append(["Numeric padding:", padding])
        list_header.append(["Step size:", step])
        list_header.append(["Exclude files:", exclude])
        list_header.append(["Explicit files:", explicit])
        list_header.append(["Pattern:", pattern])
        list_header.append(["Ignore case:", ignore_case])
        list_header.append(["Regex syntax:", regex_syntax])

        common.report(report_file, list_header, list_renamed, list_excluded,
                      list_skipped, time_start)
    else:
        common.rename(list_renamed)
예제 #9
0
    def __prepare_process(self, task_id, file_input, file_key, file_output,
                          buffer_size, existing_key, overwrite, obfuscate_enc,
                          obfuscate_key, fortuna, dev_random, rotate_min,
                          rotate_max, rotate_step, rotate_mod, reverse_bytes,
                          sbox, skip_checks):
        """
            Prepare the requested process by checking the given parameters
            and execute the appropriate method.
        """
        if not skip_checks:
            pv.path(file_input, "input", True, True)
            if not overwrite:
                if self.__encrypt:
                    pv.path(file_key, "key", True, existing_key)
                pv.path(file_output, "output", True, False)
            pv.compfile(file_input, "input",
                        [[file_key, "key"], [file_output, "output"]])

        pv.intvalue(buffer_size, "buffer size", True, False, False)
        pv.intvalue(obfuscate_enc, "encrypted file obfuscation byte", True,
                    True, False)
        pv.intvalue(obfuscate_key, "key file obfuscation byte", True, True,
                    False)

        if rotate_min == None and rotate_max == None and rotate_step == None:
            self.__rotate = False
        else:
            pv.intvalue(rotate_min, "minimum rotation", True, True, False)
            pv.intvalue(rotate_max, "maximum rotation", True, True, False)
            pv.intvalue(rotate_min, "rotation step", True, False, True)
            range_value_min, range_value_max, range_value_step = \
                common.validate_range(rotate_min, rotate_max, rotate_step)
            self.__rotate = True

        if reverse_bytes == None:
            self.__reverse = False
        else:
            pv.intrange(reverse_bytes, "reverse byte", 2, None, False)
            self.__reverse = True

        self.__task_id = task_id
        self.__fortuna = fortuna
        self.__dev_random = dev_random
        self.__file_input = os.path.abspath(file_input)
        self.__file_key = os.path.abspath(file_key)
        self.__file_output = os.path.abspath(file_output)
        self.__buffer_size = int(buffer_size)
        self.__existing_key = existing_key
        self.__overwrite = overwrite
        self.__obfuscate_enc = int(obfuscate_enc)
        self.__obfuscate_key = int(obfuscate_key)
        self.__sbox = sbox

        if self.__rotate:
            self.__rotate_min = int(rotate_min)
            self.__rotate_max = int(rotate_max)
            self.__rotate_step = int(rotate_step)
            self.__rotate_mod = rotate_mod
            self.__rotate_obj = rt.Rotate(self.__rotate_min, self.__rotate_max,
                                          self.__rotate_step,
                                          self.__rotate_mod)
        if self.__reverse:
            self.__reverse_bytes = int(reverse_bytes)
예제 #10
0
def get_status(task_id, delay=0):
    """
        Get the status of the Erfr process with the given task ID.
    """
    task_file = common.get_task_file(task_id)
    pv.intrange(task_id, "task ID", 1, common.get_max_tasks(), False)
    pv.intvalue(delay, "delay", True, True, False)

    delay = int(delay)
    task_id = int(task_id)
    progress_key = True
    process_type = ""
    process_type_list = ["encryption", "decryption", "key generation"]
    file_input_path = ""
    file_input_size = 0
    file_key_path = ""
    file_key_size = 0
    file_output_path = ""
    file_output_size = 0
    valid_type = False

    if not common.file_exists(task_file):
        common.exception("No process is running with the given task ID.")

    dict_contents = __read_content(task_file)
    process_type = dict_contents["process_type"]
    if process_type == "":
        common.exception("The process type cannot be empty.")

    for item in process_type_list:
        if process_type == item:
            valid_type = True

    if not valid_type:
        common.exception("The process type '%s' is not supported." \
                         % process_type)

    file_input_path = dict_contents["file_input_path"]
    file_input_size = dict_contents["file_input_size"]

    if "crypt" in process_type:
        file_key_path = dict_contents["file_key_path"]
        file_key_size = dict_contents["file_key_size"]
        file_output_path = dict_contents["file_output_path"]
        file_output_size = dict_contents["file_output_size"]
        if process_type == "decryption":
            progress_key = False

    print
    print "Monitoring Erfr %s process with task ID %s." % \
              (process_type, task_id)
    if delay > 0:
        if delay == 1:
            print "Refreshing the process status every second."
        else:
            print "Refreshing the process status every %s seconds." % \
                  str(delay)

    print
    print "-" * 78
    if file_key_path == "" and file_output_path == "":
        __monitor_file(task_file, file_input_path, file_input_size,
                       "File name", delay, True)
    else:
        __monitor_file(task_file, file_input_path, file_input_size,
                       "Input file", delay, False)
        print
        __monitor_file(task_file, file_key_path, file_key_size, "Key file",
                       delay, progress_key)
        print
        __monitor_file(task_file, file_output_path, file_output_size,
                       "Output file", delay, True)
    print "-" * 78
    print

    if delay > 0:
        print "Process finished."
예제 #11
0
def build_task_file(task_id, file_input_path, file_input_size, file_key_path,
                    file_key_size, file_output_path, file_output_size,
                    process_type):
    """
        Build a temporary task file using the Erfr task ID containing the
        relevant information of the corresponding encryption or decryption
        ("crypt_process"), key generation or obfuscation process.
    """

    if task_id == None:
        return

    pv.intrange(task_id, "task ID", 1, get_max_tasks(), False)
    pv.intvalue(file_input_size, "input file size", True, False, False)

    if file_input_path == None:
        exception("The input file path is missing.")

    if "crypt" in process_type:
        pv.intvalue(file_key_size, "key file size", True, True, False)
        if file_key_path == None:
            exception("The key file path is missing.")

        pv.intvalue(file_output_size, "output file size", True, True, False)
        if file_output_path == None:
            exception("The output file path is missing.")

    task = __validate_files(file_input_path, file_key_path, file_output_path)
    if not task == 0:
        exception("At least one of the given files already is in use by " \
                  "another Erfr process with ID %s." % task)

    task_file = get_task_file(task_id)
    if file_exists(task_file):
        exception("The task file for the given ID already exists.")

    string = __build_content_string(file_input_path, file_input_size,
                                    file_key_path, file_key_size,
                                    file_output_path, file_output_size,
                                    process_type)

    encode_file = bool(int(global_config(["TaskID"], ["encode_base64"], 1)))

    # Check if the file shall be encoded and then run the appropriate code for
    # the Python framework used
    if encode_file:
        if sys.version_info[0] == 2:
            content = base64.encodestring(string)
        elif sys.version_info[0] > 2:
            temp = string.encode(sys.getdefaultencoding())
            content = base64.encodestring(temp)
    else:
        if sys.version_info[0] == 2:
            content = string
        elif sys.version_info[0] > 2:
            content = bytes(string, sys.getdefaultencoding())

    try:
        fh_task = open(task_file, "wb")
        fh_task.write(content)
        fh_task.close()
    except:
        print "Failed to create task file. Due to this, the monitor " + \
              "component will not work on this process."