2019-05-20 16:40:47 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from ..exceptions import InvalidMISPObject
|
|
|
|
from .abstractgenerator import AbstractMISPObjectGenerator
|
|
|
|
from io import StringIO
|
|
|
|
import logging
|
2020-01-23 10:27:40 +01:00
|
|
|
from typing import Optional, Union
|
|
|
|
from pathlib import Path
|
2019-05-20 16:40:47 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger('pymisp')
|
|
|
|
|
|
|
|
|
|
|
|
class SSHAuthorizedKeysObject(AbstractMISPObjectGenerator):
|
|
|
|
|
2020-07-28 20:05:42 +02:00
|
|
|
def __init__(self, authorized_keys_path: Optional[Union[Path, str]] = None, authorized_keys_pseudofile: Optional[StringIO] = None, **kwargs):
|
2019-10-08 09:28:33 +02:00
|
|
|
# PY3 way:
|
|
|
|
# super().__init__('file')
|
2020-06-30 12:40:08 +02:00
|
|
|
super(SSHAuthorizedKeysObject, self).__init__('ssh-authorized-keys', **kwargs)
|
2019-05-20 16:40:47 +02:00
|
|
|
if authorized_keys_path:
|
|
|
|
with open(authorized_keys_path, 'r') as f:
|
|
|
|
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()
|
|
|
|
|
|
|
|
def generate_attributes(self):
|
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)
|