-
Notifications
You must be signed in to change notification settings - Fork 278
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
add a more flexible example for adding file type object #1313
Open
Delta-Sierra
wants to merge
5
commits into
MISP:main
Choose a base branch
from
Delta-Sierra:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79e3af0
fix print in sharing_groups.py
Delta-Sierra c37f53c
merge resolve
Delta-Sierra 8071158
Merge https://github.com/MISP/PyMISP
Delta-Sierra 23637d6
merge
Delta-Sierra 399d65f
add a more flexible example for adding file type object
Delta-Sierra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
import csv | ||
from pymisp import ExpandedPyMISP, MISPObject | ||
from keys import misp_url, misp_key, misp_verifycert | ||
import argparse | ||
|
||
|
||
""" | ||
|
||
Sample usage: | ||
|
||
python3 ./add_filetype_object_from_csv_v2.py -e event_id/event_uuid -f files_attributes.csv | ||
|
||
files_attributes.csv have at least 2 lines | ||
First line as header containing at least one of [filename;md5;sha1;sha256] | ||
Each other line will be used to create a file MISP Object | ||
Uses ; as delimiter | ||
|
||
Note : also works if there are multiple filename columns associated with a unique hash (each column must be named), | ||
""" | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser(description='Create a file type MISP Object starting from attributes in a csv file') | ||
parser.add_argument("-e", "--event_uuid", required=True, help="Event UUID to update") | ||
parser.add_argument("-f", "--attr_file", required=True, help="Attribute CSV file path") | ||
args = parser.parse_args() | ||
|
||
pymisp = ExpandedPyMISP(misp_url, misp_key, misp_verifycert) | ||
|
||
f = open(args.attr_file, newline='', encoding="utf-8-sig") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
csv_reader = csv.reader(f, delimiter=";") | ||
|
||
header = next(csv_reader) | ||
normalized_header = [col.strip().lower() for col in header] | ||
expected_columns = {"filename", "md5", "sha1", "sha256"} | ||
|
||
matching_columns = { | ||
index: col for index, col in enumerate(normalized_header) if col in expected_columns | ||
} | ||
if not matching_columns: | ||
raise ValueError(f"File must have at least one of those fields: {', '.join(expected_columns)}") | ||
|
||
print(matching_columns) | ||
count = 0 | ||
|
||
for line, row in enumerate(csv_reader, start=2): | ||
misp_object = MISPObject(name='file') | ||
for idx, col in matching_columns.items(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not using csv.DictReader instead? |
||
value = row[idx] | ||
misp_object.add_attribute(col, value = value) | ||
r = pymisp.add_object(args.event_uuid, misp_object) | ||
count = count+1 | ||
print(f'\n{count} Objects created :)') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can (should) use
PyMISP
and notExpandedPyMISP
. They're the same andExpandedPyMISP
is deprecated.