Repository: mandiant/capa
File: scripts/capa2sarif.py (Python)

Code before:
# Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
# You may obtain a copy of the License at: [package root]/LICENSE.txt
# Unless required by applicable law or agreed to in writing, software distributed under the License
#  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
"""
Convert capa json output to sarif schema
    usage: capa2sarif.py [-h] [-g] [-r] [-t TAG] [--version] capa_output
    
Capa to SARIF analysis file
positional arguments:
    capa_output         Path to capa JSON output file
optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -t TAG, --tag TAG     filter on rule meta field values (ruleid)
"""
import logging
import sys
import json
import argparse
from pathlib import Path

from sarif_om import Tool, SarifLog, Run, ToolComponent
from jschema_to_python.to_json import to_json

from capa.version import __version__

from typing import Optional, List


logger = logging.getLogger('capa2sarif')


def _parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Capa to SARIF analysis file"
    )

    # Positional argument
    parser.add_argument(
        'capa_output',
        help='Path to capa JSON output file'
    )

    # Optional arguments
    parser.add_argument(
        '-g', '--ghidra-compat',
        action="store_true",
        help='Compatibility for Ghidra 11.0.X'

Review comment (suggestion):
```suggestion
try:
    from sarif_om import Tool, SarifLog, Run, ToolComponent
    from jschema_to_python.to_json import to_json
except ImportError as e:
    print("this script requires `sarif_om` and `jschema_to_python`, please install those first", file=sys.stderr)
    raise
```

Code after:
# Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
# You may obtain a copy of the License at: [package root]/LICENSE.txt
# Unless required by applicable law or agreed to in writing, software distributed under the License
#  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
"""
Convert capa json output to sarif schema
    usage: capa2sarif.py [-h] [-g] [-r] [-t TAG] [--version] capa_output

Capa to SARIF analysis file
positional arguments:
    capa_output         Path to capa JSON output file
optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -t TAG, --tag TAG     filter on rule meta field values (ruleid)

Requires:
    - sarif_om 1.0.4
    - jschema_to_python 1.2.3
"""
import sys
import json
import logging
import argparse
from typing import List, Optional
from pathlib import Path

from capa.version import __version__

logger = logging.getLogger("capa2sarif")

# Dependencies
try:
    from sarif_om import Run, Tool, SarifLog, ToolComponent
except ImportError as e:
    logger.error(
        "Required import `sarif_om` is not installed. This is solved by installing `python3 -m pip install sarif_om>=1.0.4`. %s",
        e,
    )
    exit(-4)

try:
    from jschema_to_python.to_json import to_json
except ImportError as e:
    logger.error(
        "Required import `jschema_to_python` is not installed. This is solved by installing `python3 -m pip install jschema_to_python>=1.2.3`, %s",
        e,
    )
    exit(-4)