Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System version more plugins #1317

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ src/analysis/signatures/
src/plugins/*/*/bin
src/plugins/analysis/crypto_hints/signatures/crypto_signatures.yar
src/plugins/analysis/cve_lookup/internal/database/cve_cpe.db
src/plugins/analysis/cve_lookup/internal/database/version.json
src/plugins/analysis/qemu_exec/test/data/test_tmp_dir
src/plugins/analysis/qemu_exec/test/data/test_tmp_dir_2
src/plugins/analysis/users_and_passwords/internal/passwords/10k-most-common.txt
Expand Down
9 changes: 9 additions & 0 deletions src/helperFunctions/fileSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ def get_template_dir() -> Path:
return Path(get_src_dir()) / 'web_interface' / 'templates'


def get_bin_dir() -> Path:
"""
Retrieves the absolute path of the bin directory.
:return: The (absolute) path of the bin directory.
"""
return Path(get_src_dir()) / 'bin'


def get_relative_object_path(path: Path, offset_path: Path) -> str:
"""
FACT extraction unpacks files into a temporary directory. These files have to be offset to get the path relative
Expand Down
2 changes: 2 additions & 0 deletions src/install/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import subprocess
from contextlib import suppress
Expand Down Expand Up @@ -85,6 +86,7 @@ def _install_fw_magic(version: str = 'v0.2.2'):
# compile the magic files (results in .mgc suffix) so that we don't get warnings when using them
run_cmd_with_logging('file -C -m firmware')
run_cmd_with_logging('file -C -m internal_symlink_magic')
Path('fw_magic_version.json').write_text(json.dumps({'version': version.lstrip('v')}))


def _update_submodules():
Expand Down
10 changes: 8 additions & 2 deletions src/plugins/analysis/cve_lookup/code/cve_lookup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import json
import sys
from contextlib import suppress
from pathlib import Path
from typing import TYPE_CHECKING

Expand All @@ -20,7 +22,9 @@
from database.db_connection import DbConnection
from lookup import Lookup

DB_PATH = str(Path(__file__).parent / '../internal/database/cve_cpe.db')
DB_DIR = Path(__file__).parent.parent / 'internal/database'
DB_PATH = str(DB_DIR / 'cve_cpe.db')
VERSION_PATH = DB_DIR / 'version.json'


class AnalysisPlugin(AnalysisBasePlugin):
Expand All @@ -32,10 +36,12 @@ class AnalysisPlugin(AnalysisBasePlugin):
DESCRIPTION = 'lookup CVE vulnerabilities'
MIME_BLACKLIST = MIME_BLACKLIST_NON_EXECUTABLE
DEPENDENCIES = ['software_components'] # noqa: RUF012
VERSION = '0.2.0'
VERSION = '0.2.1'
FILE = __file__

def additional_setup(self):
with suppress(json.JSONDecodeError, FileNotFoundError):
self.SYSTEM_VERSION = json.loads(VERSION_PATH.read_text()).get('version')
self.min_crit_score = getattr(config.backend.plugin.get(self.NAME, {}), 'min-critical-score', 9.0)

def process_object(self, file_object: FileObject) -> FileObject:
Expand Down
25 changes: 24 additions & 1 deletion src/plugins/analysis/cve_lookup/internal/data_parsing.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations

import datetime
import json
import lzma
import re
from http import HTTPStatus
from pathlib import Path
from typing import TYPE_CHECKING

import requests
Expand All @@ -14,7 +17,10 @@
from ..internal.helper_functions import CveEntry

FILE_NAME = 'CVE-all.json.xz'
CVE_URL = f'https://github.com/fkie-cad/nvd-json-data-feeds/releases/latest/download/{FILE_NAME}'
VERSION_FILE = Path(__file__).parent / 'database' / 'version.json'
REPO = 'fkie-cad/nvd-json-data-feeds'
CVE_URL = f'https://github.com/{REPO}/releases/latest/download/{FILE_NAME}'
API_URL = f'https://api.github.com/repos/{REPO}/releases/latest'


def _retrieve_url(download_url: str) -> Response:
Expand All @@ -24,6 +30,22 @@ def _retrieve_url(download_url: str) -> Response:
return session.get(download_url)


def _retrieve_latest_version() -> str | None:
response = requests.get(API_URL)
if response.status_code == HTTPStatus.OK:
data = response.json()
return data['tag_name']
return None


def _store_release_data():
data = {
'version': _retrieve_latest_version(),
'last_updated': datetime.datetime.now().isoformat(),
}
Path(VERSION_FILE).write_text(json.dumps(data))


def download_and_decompress_data() -> bytes:
"""
Downloads data from a URL, saves it to a file, decompresses it, and returns the decompressed data.
Expand Down Expand Up @@ -93,3 +115,4 @@ def parse_data() -> list[CveEntry]:

if __name__ == '__main__':
parse_data()
_store_release_data()
12 changes: 11 additions & 1 deletion src/plugins/analysis/file_type/code/file_type.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from __future__ import annotations

import json
import typing
from pathlib import Path
from typing import List

import pydantic
from pydantic import Field
from semver import Version

from analysis.plugin import AnalysisPluginV0
from analysis.plugin.compat import AnalysisBasePluginAdapterMixin
from helperFunctions import magic
from helperFunctions.fileSystem import get_bin_dir

if typing.TYPE_CHECKING:
import io
Expand All @@ -24,11 +28,17 @@ class Schema(pydantic.BaseModel):
)

def __init__(self):
try:
version_file = Path(get_bin_dir()) / 'version.json'
fw_magic_db_version = json.loads(version_file.read_text()).get('version')
except (json.JSONDecodeError, FileNotFoundError):
fw_magic_db_version = None
super().__init__(
metadata=AnalysisPluginV0.MetaData(
name='file_type',
description='identify the file type',
version='1.0.0',
version=Version(1, 0, 1),
system_version=fw_magic_db_version,
Schema=AnalysisPlugin.Schema,
),
)
Expand Down
6 changes: 6 additions & 0 deletions src/test/unit/helperFunctions/test_file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from helperFunctions.fileSystem import (
file_is_empty,
get_bin_dir,
get_config_dir,
get_relative_object_path,
get_src_dir,
Expand Down Expand Up @@ -39,6 +40,11 @@ def test_get_template_dir():
assert '.html' in file_suffixes_in_template_dir


def test_get_bin_dir():
bin_dir = get_bin_dir()
assert bin_dir.is_dir(), 'bin dir not found'


@pytest.mark.parametrize(
('base', 'offset', 'result', 'message'),
[
Expand Down
Loading