2019-05-20 16:40:47 +02:00
|
|
|
#!/usr/bin/env python3
|
2024-01-17 13:13:14 +01:00
|
|
|
|
|
|
|
from __future__ import annotations
|
2019-05-20 16:40:47 +02:00
|
|
|
|
|
|
|
import logging
|
2024-02-01 14:40:12 +01:00
|
|
|
|
|
|
|
from io import StringIO
|
2020-01-23 10:27:40 +01:00
|
|
|
from pathlib import Path
|
2019-05-20 16:40:47 +02:00
|
|
|
|
2024-02-01 14:40:12 +01:00
|
|
|
from ..exceptions import InvalidMISPObject
|
|
|
|
from .abstractgenerator import AbstractMISPObjectGenerator
|
|
|
|
|
2019-05-20 16:40:47 +02:00
|
|
|
logger = logging.getLogger('pymisp')
|
|
|
|
|
|
|
|
|
|
|
|
class SSHAuthorizedKeysObject(AbstractMISPObjectGenerator):
|
|
|
|
|
2024-02-01 14:40:12 +01:00
|
|
|
def __init__(self, authorized_keys_path: Path | str | None = None, # type: ignore[no-untyped-def]
|
|
|
|
authorized_keys_pseudofile: StringIO | None = None, **kwargs):
|
2021-10-26 02:37:12 +02:00
|
|
|
super().__init__('ssh-authorized-keys', **kwargs)
|
2019-05-20 16:40:47 +02:00
|
|
|
if authorized_keys_path:
|
2024-01-17 13:13:14 +01:00
|
|
|
with open(authorized_keys_path) as f:
|
2019-05-20 16:40:47 +02:00
|
|
|
self.__pseudofile = StringIO(f.read())
|
|
|
|
elif authorized_keys_pseudofile and isinstance(authorized_keys_pseudofile, StringIO):
|
2020-01-23 10:27:40 +01:00
|
|
|
self.__pseudofile = authorized_keys_pseudofile
|
2019-05-20 16:40:47 +02:00
|
|
|
else:
|
|
|
|
raise InvalidMISPObject('File buffer (StringIO) or a path is required.')
|
|
|
|
self.__data = self.__pseudofile.getvalue()
|
|
|
|
self.generate_attributes()
|
|
|
|
|
2024-02-01 14:40:12 +01:00
|
|
|
def generate_attributes(self) -> None:
|
2020-05-12 11:34:38 +02:00
|
|
|
for line in self.__pseudofile:
|
|
|
|
if line.startswith('ssh') or line.startswith('ecdsa'):
|
|
|
|
key = line.split(' ')[1]
|
2019-05-20 16:40:47 +02:00
|
|
|
self.add_attribute('key', key)
|