The CSVLoader tool allows you to import data from CSV files into MISP events.
There are two ways to use it depending on your file structure:
Automatic Mapping: If your CSV header contains valid object relations defined in your MISP template, you can initialize CSVLoader with the template_name and csv_path. The load() method will yield dictionaries that can be passed directly to event.add_object(**o).
Manual Mapping: If your CSV header does not match MISP template relations, you must provide a list of fieldnames and set has_fieldnames=True to map the columns manually.
Key parameters for CSVLoader:
template_name: The name of the MISP template to use for mapping.csv_path: A pathlib.Path object pointing to the CSV file.fieldnames: (Optional) A list of strings representing the column names to use when the CSV header is not standard.has_fieldnames: (Optional) A boolean indicating if the CSV has a header row that should be used for mapping.
from pymisp.tools import CSVLoader
from pymisp import MISPEvent
from pathlib import Path
# Scenario 1: CSV header matches template
csv1 = CSVLoader(template_name='file', csv_path=Path('tests/csv_testfiles/valid_fieldnames.csv'))
event = MISPEvent()
event.info = 'Test event from CSV loader'
for o in csv1.load():
event.add_object(**o)
# Scenario 2: Manual fieldname mapping
csv2 = CSVLoader(template_name='file', csv_path=Path('tests/csv_testfiles/invalid_fieldnames.csv'),
fieldnames=['SHA1', 'fileName', 'size-in-bytes'], has_fieldnames=True)
for o in csv2.load():
event.add_object(**o)