Exemplo n.º 1
0
 def test_validate_unsupported_version(self):
     password = b"fooobarr"
     data = b"#bellavita"
     version = len(crypto.pack_versions).to_bytes(1, "big")
     pack = crypto.encode(password, data, b"")
     offset = crypto.PackVersion.hash_len
     pack = pack[:offset] + version + pack[offset + 1 :]
     with self.assertRaises(ValueError):
         crypto.validate(pack)
def crypt_file_main():
    parser = argparse.ArgumentParser(
        description='(De)Crypt a file according to a given password')
    parser.add_argument('-d', '--decrypt', action='store_true')
    parser.add_argument('-m', '--metadata', help='metadata input file')
    parser.add_argument('zip_password', help='hexified zip password')
    parser.add_argument('input_file',
                        help='input file',
                        nargs='?',
                        default='-')
    parser.add_argument('output_file',
                        help='output file',
                        nargs='?',
                        default='-')
    args = parser.parse_args()
    input_file = open(args.input_file,
                      'rb') if args.input_file != '-' else sys.stdin.buffer
    metadata_file = open(args.metadata,
                         'rb') if args.metadata is not None else None
    output_file = open(args.output_file,
                       'wb') if args.output_file != '-' else sys.stdout.buffer
    zip_password = bytes.fromhex(args.zip_password)
    input_data = input_file.read()
    if args.decrypt:
        assert validate(input_data)
        output_file.write(decode(zip_password, input_data))
    else:
        metadata_in = metadata_file.read(
        ) if metadata_file is not None else b""
        output_file.write(encode(zip_password, input_data, metadata_in))
Exemplo n.º 3
0
 def test_validate(self):
     for version in range(len(crypto.pack_versions)):
         with self.subTest():
             password = b"fooobarr"
             data = b"#bellavita"
             encrypted = crypto.encode(password, data, b"metadata", version=version)
             self.assertTrue(crypto.validate(encrypted))
Exemplo n.º 4
0
def crypt_file_main():
    parser = argparse.ArgumentParser(
        description="(De)Crypt a file according to a given password"
    )
    parser.add_argument("-d", "--decrypt", action="store_true")
    parser.add_argument("-m", "--metadata", help="metadata input file")
    parser.add_argument("zip_password", help="hexified zip password")
    parser.add_argument("input_file", help="input file", nargs="?", default="-")
    parser.add_argument("output_file", help="output file", nargs="?", default="-")
    args = parser.parse_args()
    input_file = (
        open(args.input_file, "rb") if args.input_file != "-" else sys.stdin.buffer
    )
    metadata_file = open(args.metadata, "rb") if args.metadata is not None else None
    output_file = (
        open(args.output_file, "wb") if args.output_file != "-" else sys.stdout.buffer
    )
    zip_password = bytes.fromhex(args.zip_password)
    input_data = input_file.read()
    if args.decrypt:
        assert validate(input_data)
        output_file.write(decode(zip_password, input_data))
    else:
        metadata_in = metadata_file.read() if metadata_file is not None else b""
        output_file.write(encode(zip_password, input_data, metadata_in))
Exemplo n.º 5
0
def get_metadata_main():
    parser = argparse.ArgumentParser(description="Validate and get a file metadata")
    parser.add_argument("input_file", help="input file", nargs="?", default="-")
    parser.add_argument("output_file", help="output file", nargs="?", default="-")
    args = parser.parse_args()
    input_file = (
        open(args.input_file, "rb") if args.input_file != "-" else sys.stdin.buffer
    )
    output_file = (
        open(args.output_file, "wb") if args.output_file != "-" else sys.stdout.buffer
    )
    input_data = input_file.read()
    assert validate(input_data)
    output_file.write(metadata(input_data).strip(b"\x00"))
Exemplo n.º 6
0
 def upload_pack(self, file):
     """
     POST /admin/upload_pack
     """
     if Database.get_meta("admin_token"):
         BaseHandler.raise_exc(Forbidden, "FORBIDDEN",
                               "The pack has already been extracted")
     elif os.path.exists(Config.encrypted_file):
         BaseHandler.raise_exc(Forbidden, "FORBIDDEN",
                               "The pack has already been uploaded")
     if not crypto.validate(file["content"]):
         self.raise_exc(Forbidden, "BAD_FILE", "The uploaded file is "
                        "not valid")
     StorageManager.save_file(os.path.realpath(Config.encrypted_file),
                              file["content"])
     return {}
Exemplo n.º 7
0
def main(args):
    with open(args.pack, "rb") as pack:
        pack = pack.read()
    if not validate(pack):
        raise AssertionError("Corrupted pack")
    meta = ruamel.yaml.safe_load(metadata(pack).strip(b"\x00"))
    if meta.get("deletable"):
        print(Fore.YELLOW, "WARNING: The pack is marked as deletable",
              Fore.RESET)
    if not meta.get("name"):
        print(Fore.YELLOW,
              "WARNING: The pack metadata does not include 'name'", Fore.RESET)
    if not meta.get("description"):
        print(Fore.YELLOW,
              "WARNING: The pack metadata does not include 'description'",
              Fore.RESET)
    decoded = decode(bytes.fromhex(args.password), pack)

    tasks = args.tasks.split(",")
    if args.solutions:
        solutions = [
            list(map(os.path.abspath, s.split(",")))
            for s in args.solutions.split(";")
        ]
    else:
        solutions = [[]] * len(tasks)

    extract_dir = tempfile.mkdtemp()
    os.chdir(extract_dir)
    print("Working in %s" % extract_dir)

    with open("pack.zip", "wb") as f:
        f.write(decoded)
    with zipfile.ZipFile("pack.zip") as zip_file:
        zip_file.extractall(".")
    if args.sedi:
        validate_sedi(args.sedi)
    if args.admin:
        validate_admin(args.admin, args.password)
    for i, task in enumerate(tasks):
        if not os.path.exists(task):
            raise AssertionError("Task %s not included in the pack" % task)
        sols = solutions[i] if i < len(solutions) else []
        validate_task(task, args.fuzz, args.iterations, sols)

    shutil.rmtree(extract_dir)
def get_metadata_main():
    parser = argparse.ArgumentParser(
        description='Validate and get a file metadata')
    parser.add_argument('input_file',
                        help='input file',
                        nargs='?',
                        default='-')
    parser.add_argument('output_file',
                        help='output file',
                        nargs='?',
                        default='-')
    args = parser.parse_args()
    input_file = open(args.input_file,
                      'rb') if args.input_file != '-' else sys.stdin.buffer
    output_file = open(args.output_file,
                       'wb') if args.output_file != '-' else sys.stdout.buffer
    input_data = input_file.read()
    assert validate(input_data)
    output_file.write(metadata(input_data).strip(b'\x00'))
Exemplo n.º 9
0
 def test_validate_invalid(self):
     password = b"fooobarr"
     data = b"#bellavita"
     encrypted = crypto.encode(password, data, b"metadata")
     encrypted += b'('
     self.assertFalse(crypto.validate(encrypted))
Exemplo n.º 10
0
 def test_validate(self):
     password = b"fooobarr"
     data = b"#bellavita"
     encrypted = crypto.encode(password, data, b"metadata")
     self.assertTrue(crypto.validate(encrypted))