コード例 #1
0
ファイル: erfr-cleaner.py プロジェクト: urbanware-org/erfr
def main():
    from core import clap
    from core import common

    try:
        p = clap.Parser()
    except Exception as e:
        print("%s: error: %s" % (os.path.basename(sys.argv[0]), e))
        sys.exit(1)

    p.set_description("Delete remaining temporary files of the Erfr " \
                      "components.")
    p.set_epilog("Further information and usage examples can be found " \
                 "inside the documentation file for this script.")

    # Define optional arguments
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print(common.get_version())
        sys.exit(0)

    try:
        print("Started cleaning up temporary files.")
        max_tasks = int(common.get_max_tasks())
        for task in range(1, max_tasks + 1):
            common.delete_temp_files(task)
        print("Finished cleaning up temporary files.")
    except Exception as e:
        print("cancelled cleaning up temporary files.")
        p.error(e)
コード例 #2
0
def main():
    from core import common
    from core import main as core
    from core import keyfile
    from core import mainargs
    from datetime import datetime as dt

    try:
        p = mainargs.Parser()
    except Exception as e:
        print "%s: error: %s" % (os.path.basename(sys.argv[0]), e)
        sys.exit(1)

    p.set_description("Encrypt or decrypt a file using the one-time pad " \
                      "encryption method with optional features.")
    p.set_epilog("Further information and usage examples can be found " \
                 "inside the documentation file for this script.")

    # Define required arguments
    p.add_predef("-a", "--action", "action to perform", "action",
                 ["encrypt", "decrypt"], True, "required")
    p.add_avalue("-i", "--input-file", "input file path", "input_file", None,
                 True, "required")
    p.add_avalue("-k", "--key-file", "key file path", "key_file", None, True,
                 "required")
    p.add_avalue("-o", "--output-file", "output file path", "output_file",
                 None, True, "required")

    # Define optional arguments (general)
    p.add_switch(None, "--base64", "use a Base64 key string instead of a " \
                 "binary key file", "base64", True, False)
    p.add_avalue("-b", "--buffer-size", "buffer size in bytes", "buffer_size",
                 4096, False)
    p.add_switch(None, "--dev-random", "use \"/dev/random\" as random " \
                 "number generator (Unix-like systems only)", "dev_random",
                 True, False)
    p.add_avalue("-e", "--export", "Export encryption parameters into a file",
                 "export_file", None, False)
    p.add_switch(None, "--fortuna", "use Fortuna as random number generator",
                 "fortuna", True, False)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_switch(None, "--no-task-id", "disable the task ID", "no_task_id",
                 True, False)
    p.add_avalue(None, "--obfuscate-enc", "obfuscate the encrypted file by " \
                 "adding random bytes", "obfuscate_enc", 0, False)
    p.add_avalue(None, "--obfuscate-key", "obfuscate the key file by " \
                 "adding random bytes", "obfuscate_key", 0, False)
    p.add_switch(None, "--overwrite", "overwrite existing files", "overwrite",
                 True, False)
    p.add_avalue("-t", "--task-id", "user-defined task ID", "task_id", None,
                 False)
    p.add_switch(None, "--use-existing-key", "use an already existing key " \
                 "for encryption", "use_existing_key", True, False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    # Define optional arguments for the rotate feature
    p.add_avalue(None, "--rotate-max", "maximum rotation value (0-255)",
                 "rotate_max", None, False, "rotate")
    p.add_avalue(None, "--rotate-min", "minimum rotation value (0-255)",
                 "rotate_min", None, False, "rotate")
    p.add_switch(None, "--rotate-modulo", "use modulo operation when using " \
                 "byte rotation", "rotate_mod", True, False, "rotate")
    p.add_avalue(None, "--rotate-step", "rotation step value", "rotate_step",
                 None, False, "rotate")

    # Define optional arguments for the reverse feature
    p.add_avalue("-r", "--reverse", "reverse certain amount of bytes",
                 "reverse_bytes", None, False, "reverse")

    # Define optional arguments for the substitution-box feature
    p.add_switch("-s", "--use-sbox", "use the Rijndael substitution-box to " \
                 "obscure the relationship between the key and the " \
                 "ciphertext", "sbox", True, False, "sbox")

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print core.get_version()
        sys.exit(0)

    args = p.parse_args()
    if args.action == None:
        p.error("The action argument is missing.")
    elif args.action.lower() == "encrypt":
        encrypt = True
    elif args.action.lower() == "decrypt":
        encrypt = False
    else:
        p.error("An unsupported action was given.")

    key_file = args.key_file
    use_existing_key = args.use_existing_key
    base64 = False

    if encrypt:
        if args.base64 and use_existing_key:
            p.error("The argument for using an existing key cannot be used " \
                    "together with the optional Base64 key argument.")

    if args.no_task_id:
        if not args.task_id == None:
            p.error("The argument for disabling the task ID cannot be used " \
                    "together with the user-defined task ID argument.")
        task_id = None
    else:
        try:
            task_id = common.get_task_id(args.task_id)
            if args.base64:
                key_file = keyfile.base64_key(task_id, args.key_file)
                if encrypt:
                    use_existing_key = True
                base64 = True
            else:
                key_file = args.key_file
        except Exception as e:
            task_id = args.task_id
            p.error(e)

    try:
        timestamp = dt.now()
        erfr = core.ErfrCrypt()

        if encrypt:
            if args.export_file == None:
                action = "encryption"
            else:
                action = "export"
            common.status(task_id, action, "start")
            erfr.encrypt_file(task_id, args.input_file, key_file,
                              args.output_file, args.buffer_size,
                              use_existing_key, args.overwrite,
                              args.obfuscate_enc, args.obfuscate_key,
                              args.fortuna, args.dev_random, args.rotate_min,
                              args.rotate_max, args.rotate_step,
                              args.rotate_mod, args.reverse_bytes,
                              args.sbox, args.export_file)
            common.status(task_id, action, "finish")
        else:
            if not args.export_file == None:
                p.error("The argument for exporting the encryption " \
                        "parameters into a file can only be used when " \
                        "encrypting a file.")

            if use_existing_key:
                p.error("The argument for using an existing key can " \
                        "only be used when encrypting a file.")
            elif args.dev_random or args.fortuna:
                p.error("Giving a random number generator only makes " \
                        "sense when encrypting a file.")
            else:
                common.status(task_id, "decryption", "start")
                erfr.decrypt_file(task_id, args.input_file, key_file,
                                  args.output_file, args.buffer_size,
                                  args.overwrite, args.obfuscate_enc,
                                  args.obfuscate_key, args.rotate_min,
                                  args.rotate_max, args.rotate_step,
                                  args.rotate_mod, args.reverse_bytes,
                                  args.sbox)
                common.status(task_id, "decryption", "finish")
        print "Elapsed time: %s" % (dt.now() - timestamp)
    except Exception as e:
        if encrypt:
            common.status(task_id, action, "cancel")
        else:
            common.status(task_id, "decryption", "cancel")
        p.error(e)
    finally:
        if base64:
            common.delete_file(key_file, "temporary key")
        common.delete_temp_files(task_id)
コード例 #3
0
def main():
    from core import clap
    from core import common
    from core import main as core
    from datetime import datetime as dt

    try:
        p = clap.Parser()
    except Exception as e:
        print("%s: error: %s" % (os.path.basename(sys.argv[0]), e))
        sys.exit(1)

    p.set_description("Process a parameter file generated by the main Erfr " \
                      "script containing encryption related information.")
    p.set_epilog("Further information and usage examples can be found " \
                 "inside the documentation file for this script.")

    # Define required arguments
    p.add_predef("-a", "--action", "action to perform", "action",
                 ["encrypt", "decrypt"], True)
    p.add_avalue("-f", "--file", "parameter file to process", "file", None,
                 True)

    # Define optional arguments (general)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_avalue("-s", "--suffix", "add additional suffix to the decrypted " \
                 "file", "suffix", None, False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print(core.get_version())
        sys.exit(0)

    args = p.parse_args()
    if args.action == None:
        p.error("The action argument is missing.")
    elif args.action.lower() == "encrypt":
        encrypt = True
    elif args.action.lower() == "decrypt":
        encrypt = False
    else:
        p.error("An unsupported action was given.")

    try:
        params = common.process_params(args.file)

        buffer_size = params.get("buffer_size")
        dev_random = params.get("dev_random")
        fortuna = params.get("fortuna")
        input_file = params.get("input_file")
        key_file = params.get("key_file")
        obfuscate_enc = params.get("obfuscate_enc")
        obfuscate_key = params.get("obfuscate_key")
        output_file = params.get("output_file")
        overwrite = params.get("overwrite")
        rotate_max = params.get("rotate_max")
        rotate_min = params.get("rotate_min")
        rotate_mod = params.get("rotate_mod")
        rotate_step = params.get("rotate_step")
        reverse_bytes = params.get("reverse_bytes")
        sbox = params.get("sbox")
        task_id = params.get("task_id")
        use_existing_key = params.get("use_existing_key")

        timestamp = dt.now()
        erfr = core.ErfrCrypt()

        if task_id == 0:
            task_id = None

        if encrypt:
            if not args.suffix == None:
                p.error("The argument for an additional suffix can only be " \
                        "used when decrypting a file.")
            common.status(task_id, "encryption", "start")
            erfr.encrypt_file(task_id, input_file, key_file, output_file,
                              buffer_size, use_existing_key, overwrite,
                              obfuscate_enc, obfuscate_key, fortuna,
                              dev_random, rotate_min, rotate_max, rotate_step,
                              rotate_mod, reverse_bytes, sbox)
            common.status(task_id, "encryption", "finish")
        else:
            if not args.suffix == None:
                input_file += ".%s" % args.suffix
            common.status(task_id, "decryption", "start")
            erfr.decrypt_file(task_id, output_file, key_file, input_file,
                              buffer_size, overwrite, obfuscate_enc,
                              obfuscate_key, rotate_min, rotate_max,
                              rotate_step, rotate_mod, reverse_bytes, sbox)
            common.status(task_id, "decryption", "finish")
        print("Elapsed time: %s" % (dt.now() - timestamp))
    except Exception as e:
        if encrypt:
            common.status(task_id, "encryption", "cancel")
        else:
            common.status(task_id, "decryption", "cancel")
        p.error(e)
    finally:
        try:
            common.delete_temp_files(task_id)
        except:
            pass
コード例 #4
0
def main():
    from core import clap
    from core import common
    from core import keyfile
    from datetime import datetime as dt

    try:
        p = clap.Parser()
    except Exception as e:
        print "%s: error: %s" % (os.path.basename(sys.argv[0]), e)
        sys.exit(1)

    p.set_description("Generate key files which can either be used for " \
                      "encryption or obfuscation purposes (as fake key " \
                      "files).")
    p.set_epilog("Further information and usage examples can be found " \
                 "inside the documentation file for this script.")

    # Define required arguments
    p.add_avalue("-s", "--key-size", "key size in bytes", "key_size", None,
                 True)

    # Define optional arguments
    p.add_switch(None, "--base64", "generate Base64 key string", "base64",
                 True, False)
    p.add_avalue("-b", "--buffer-size", "buffer size in bytes", "buffer_size",
                 4096, False)
    p.add_switch(None, "--dev-random", "use \"/dev/random\" as random " \
                 "number generator (Unix-like systems only)", "dev_random",
                 True, False)
    p.add_switch(None, "--fortuna", "use Fortuna as random number generator",
                 "fortuna", True, False)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_avalue("-k", "--key-file", "key file path", "key_file", None, False)
    p.add_switch(None, "--overwrite", "overwrite existing file", "overwrite",
                 True, False)
    p.add_avalue("-p", "--parts", "split key into separate parts", "parts", 1,
                 False)
    p.add_avalue("-t", "--task-id", "user-defined task ID", "task_id", None,
                 False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print keyfile.get_version()
        sys.exit(0)

    args = p.parse_args()
    if not args.base64 and args.key_file == None:
        p.error("The argument to either generate a key file or a Base64 " \
                "encoded key string is missing.")
    elif args.base64 and not args.key_file == None:
        p.error("The arguments to generate a key file and a Base64 key " \
                "string cannot be given at the same time.")
    elif args.base64 and args.overwrite:
        p.error("The overwrite argument does not make any sense when " \
                "generating a Base64 string.")
    elif args.base64 and args.parts > 1:
        p.error("The parts argument does not make any sense when " \
                "generating a Base64 string.")

    if args.base64:
        if not args.task_id == None:
            p.error("No task ID can be given when creating a Base64 key " \
                    "string.")
        try:
            print keyfile.generate_key_string(args.key_size, args.dev_random,
                                              args.fortuna)
        except Exception as e:
            p.error(e)
    else:
        try:
            task_id = common.get_task_id(args.task_id)
        except Exception as e:
            task_id = args.task_id
            p.error(e)

        try:
            timestamp = dt.now()
            common.status(task_id, "key generation", "start")
            keyfile.generate_key_file(task_id, args.key_file, args.key_size,
                                      args.buffer_size, 0, False,
                                      args.dev_random, args.fortuna,
                                      args.overwrite, args.parts)
            common.status(task_id, "key generation", "finish")
            print "Elapsed time: %s" % (dt.now() - timestamp)
        except Exception as e:
            common.status(task_id, "key generation", "cancel")
            p.error(e)
        finally:
            common.delete_temp_files(task_id)
コード例 #5
0
def main():
    from core import clap
    from core import common
    from core import obfuscator
    from datetime import datetime as dt

    try:
        p = clap.Parser()
    except Exception as e:
        print("%s: error: %s" % (os.path.basename(sys.argv[0]), e))
        sys.exit(1)

    p.set_description("Obfuscate existing encrypted and key files by " \
                      "adding random bytes.")
    p.set_epilog("Further information and usage examples can be found " \
                 "inside the documentation file for this script.")

    # Define required arguments
    p.add_avalue("-f", "--file", "target file path", "file", None, True)
    p.add_avalue("-r", "--random-bytes", "amount of random bytes to add to " \
                 "the given file", "random_bytes", 0, True)

    # Define optional arguments
    p.add_avalue("-b", "--buffer-size", "buffer size in bytes", "buffer_size",
                 4096, False)
    p.add_switch(None, "--dev-random", "use \"/dev/random\" as random " \
                 "number generator (Unix-like systems only)", "dev_random",
                 True, False)
    p.add_switch(None, "--fortuna", "use Fortuna as random number generator",
                 "fortuna", True, False)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_avalue("-t", "--task-id", "user-defined task ID", "task_id", None,
                 False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print(obfuscator.get_version())
        sys.exit(0)

    args = p.parse_args()
    try:
        task_id = common.get_task_id(args.task_id)
    except Exception as e:
        task_id = args.task_id
        p.error(e)

    try:
        timestamp = dt.now()
        common.status(task_id, "file obfuscation", "start")
        obfuscator.obfuscate_file(task_id, args.file, args.buffer_size,
                                  args.random_bytes, args.dev_random,
                                  args.fortuna)
        common.status(task_id, "file obfuscation", "finish")
        print("Elapsed time: %s" % (dt.now() - timestamp))
    except Exception as e:
        common.status(task_id, "file obfuscation", "cancel")
        p.error(e)
    finally:
        common.delete_temp_files(task_id)