|
| 1 | +import os |
| 2 | +import warnings |
| 3 | +from typing import Any, Optional, Union |
| 4 | + |
| 5 | +import click |
| 6 | +from appdirs import AppDirs |
| 7 | +import logging |
| 8 | +from rich.logging import RichHandler |
| 9 | +from rich.console import Console |
| 10 | + |
| 11 | +from ciphey import iface |
| 12 | + |
| 13 | +warnings.filterwarnings("ignore") |
| 14 | + |
| 15 | +console = Console() |
| 16 | + |
| 17 | + |
| 18 | +def decrypt(config: iface.Config, ctext: Any) -> Union[str, bytes]: |
| 19 | + """A simple alias for searching a ctext and makes the answer pretty""" |
| 20 | + res: Optional[iface.SearchResult] = config.objs["searcher"].search(ctext) |
| 21 | + if res is None: |
| 22 | + return "Failed to crack" |
| 23 | + if config.verbosity < 0: |
| 24 | + return res.path[-1].result.value |
| 25 | + else: |
| 26 | + return iface.pretty_search_results(res) |
| 27 | + |
| 28 | + |
| 29 | +def print_help(ctx): |
| 30 | + # prints help menu |
| 31 | + # if no arguments are passed |
| 32 | + click.echo(ctx.get_help()) |
| 33 | + ctx.exit() |
| 34 | + |
| 35 | + |
| 36 | +@click.command() |
| 37 | +@click.option( |
| 38 | + "-t", |
| 39 | + "--text", |
| 40 | + help="The ciphertext you want to decrypt.", |
| 41 | + type=str, |
| 42 | +) |
| 43 | +@click.option( |
| 44 | + "-q", "--quiet", help="Decrease verbosity", type=int, count=True, default=None |
| 45 | +) |
| 46 | +@click.option( |
| 47 | + "-g", |
| 48 | + "--greppable", |
| 49 | + help="Only print the answer (useful for grep)", |
| 50 | + is_flag=True, |
| 51 | + default=None, |
| 52 | +) |
| 53 | +@click.option("-v", "--verbose", count=True, type=int) |
| 54 | +@click.option("-C", "--checker", help="Use the given checker", default=None) |
| 55 | +@click.option( |
| 56 | + "-c", |
| 57 | + "--config", |
| 58 | + help="Uses the given config file. Defaults to appdirs.user_config_dir('ciphey', 'ciphey')/'config.yml'", |
| 59 | +) |
| 60 | +@click.option("-w", "--wordlist", help="Uses the given wordlist") |
| 61 | +@click.option( |
| 62 | + "-p", |
| 63 | + "--param", |
| 64 | + help="Passes a parameter to the language checker", |
| 65 | + multiple=True, |
| 66 | +) |
| 67 | +@click.option( |
| 68 | + "-l", |
| 69 | + "--list-params", |
| 70 | + help="List the parameters of the selected module", |
| 71 | + is_flag=True, |
| 72 | +) |
| 73 | +@click.option( |
| 74 | + "--searcher", |
| 75 | + help="Select the searching algorithm to use", |
| 76 | +) |
| 77 | +@click.option( |
| 78 | + "-b", |
| 79 | + "--bytes", |
| 80 | + help="Forces Ciphey to use binary mode for the input", |
| 81 | + is_flag=True, |
| 82 | + default=None, |
| 83 | +) |
| 84 | +@click.option( |
| 85 | + "--default-dist", |
| 86 | + help="Sets the default character/byte distribution", |
| 87 | + type=str, |
| 88 | + default=None, |
| 89 | +) |
| 90 | +@click.option( |
| 91 | + "-m", |
| 92 | + "--module", |
| 93 | + help="Adds a module from the given path", |
| 94 | + type=click.Path(), |
| 95 | + multiple=True, |
| 96 | +) |
| 97 | +@click.option( |
| 98 | + "-A", |
| 99 | + "--appdirs", |
| 100 | + help="Print the location of where Ciphey wants the settings file to be", |
| 101 | + is_flag=True, |
| 102 | +) |
| 103 | +@click.option("-f", "--file", type=click.File("rb"), required=False) |
| 104 | +@click.argument("text_stdin", callback=iface.get_input_from_stdin, required=False) |
| 105 | +def main(**kwargs): |
| 106 | +# Initialize the console and logging |
| 107 | + console = Console() |
| 108 | + logging.basicConfig( |
| 109 | + level=logging.INFO, |
| 110 | + format="%(message)s", |
| 111 | + datefmt="[%X]", |
| 112 | + handlers=[RichHandler(console=console, rich_tracebacks=True)], |
| 113 | + ) |
| 114 | + |
| 115 | + # Create the configuration object |
| 116 | + config = iface.Config() |
| 117 | + |
| 118 | + # Load the settings file into the config |
| 119 | + config.load_settings_file(kwargs["config"]) |
| 120 | + |
| 121 | + # Set verbosity level |
| 122 | + verbosity = kwargs["verbose"] - kwargs["quiet"] if kwargs["verbose"] is not None else None |
| 123 | + config.set_verbosity(verbosity) |
| 124 | + |
| 125 | + # Load additional options into the config |
| 126 | + config.set_option("checker", kwargs["checker"]) |
| 127 | + config.set_option("searcher", kwargs["searcher"]) |
| 128 | + config.set_option("default_dist", kwargs["default_dist"]) |
| 129 | + config.set_option("wordlist", kwargs["wordlist"]) |
| 130 | + |
| 131 | + # Load additional modules |
| 132 | + config.load_modules(kwargs["module"]) |
| 133 | + |
| 134 | + # Update parameters |
| 135 | + config.update_parameters(kwargs["param"]) |
| 136 | + |
| 137 | + # Complete the configuration setup |
| 138 | + config.complete_config() |
| 139 | + |
| 140 | + # Process the input text |
| 141 | + input_text = kwargs["text"] or kwargs["text_stdin"] |
| 142 | + if isinstance(input_text, bytes) and not kwargs["bytes"]: |
| 143 | + input_text = input_text.decode("utf-8") |
| 144 | + elif isinstance(input_text, str) and kwargs["bytes"]: |
| 145 | + input_text = input_text.encode("utf-8") |
| 146 | + |
| 147 | + # Perform decryption |
| 148 | + result = None |
| 149 | + with console.status("[bold green]Thinking...", spinner="moon") as status: |
| 150 | + config.set_spinner(status) |
| 151 | + result = decrypt(config, input_text) |
| 152 | + |
| 153 | + # Print the result |
| 154 | + console.print(result) |
| 155 | + |
| 156 | + |
| 157 | +if __name__ == "__main__": |
| 158 | + main() |
0 commit comments