From 8d755546b866ea23756ec0b9bbcc7f64d0d1156b Mon Sep 17 00:00:00 2001 From: raylu <90059+raylu@users.noreply.github.com> Date: Fri, 6 Sep 2024 23:02:20 -0700 Subject: [PATCH] cryptolyze --- bot.py | 4 ++-- commands.py | 3 +++ cryptolyze.py | 34 ++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 cryptolyze.py diff --git a/bot.py b/bot.py index 64032e6..2a31fd3 100644 --- a/bot.py +++ b/bot.py @@ -588,11 +588,11 @@ def __init__(self, d, args: str, bot: Bot): # 'discriminator': '8396', # 'avatar': '464d73d2ca17733636282ab58b8cc3f5', # } - self.sender = d['author'] + self.sender: dict = d['author'] self.args = args self.bot = bot - def reply(self, message, embed=None, files=None): + def reply(self, message: str, embed: dict | None=None, files=None) -> None: self.bot.send_message(self.channel_id, message, embed, files) def react(self, emoji): diff --git a/commands.py b/commands.py index 96aae93..554514b 100644 --- a/commands.py +++ b/commands.py @@ -1,6 +1,7 @@ import animal_crossing import canned import code_eval +import cryptolyze import eve import friend_code import management @@ -37,6 +38,8 @@ 'py2': code_eval.python2, 'py3': code_eval.python3, + 'cryptolyze': cryptolyze.cryptolyze, + 'join': management.join, 'leave': management.leave, 'roles': management.list_roles, diff --git a/cryptolyze.py b/cryptolyze.py new file mode 100644 index 0000000..18effad --- /dev/null +++ b/cryptolyze.py @@ -0,0 +1,34 @@ +from __future__ import annotations + +import subprocess +import typing + +if typing.TYPE_CHECKING: + from bot import CommandEvent + + +def cryptolyze(cmd: CommandEvent) -> None: + if not cmd.args: + return + base_cmd = ['cryptolyze', '--output-format', 'markdown', 'tls'] + try: + versions = subprocess.run([*base_cmd, 'versions', cmd.args], check=True, capture_output=True, text=True) + output_lines = versions.stdout.splitlines() + + vulns = subprocess.run([*base_cmd, 'vulns', cmd.args], check=True, capture_output=True, text=True) + vuln_lines = vulns.stdout.splitlines() + assert vuln_lines[0] == '* Target:' + for start, line in enumerate(vuln_lines[1:], 1): + if line.startswith('* '): + break + else: + raise AssertionError("couldn't find second heading in vulns output") + for line in vuln_lines[start:]: + if line.endswith('yes'): + line = line.removesuffix('yes') + '**yes**' + output_lines.append(line) + + output = '\n'.join(line[2:] if line.startswith(' ') else line for line in output_lines) + cmd.reply('', embed={'description': output}) + except subprocess.CalledProcessError as e: + cmd.reply(f'{cmd.sender['pretty_name']}: {e.stderr}') diff --git a/requirements.txt b/requirements.txt index e7f249f..83d39e8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +cryptolyzer pillow python-dateutil PyYAML