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

Add stix-extract tool #48

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open

Add stix-extract tool #48

wants to merge 8 commits into from

Conversation

felddy
Copy link
Member

@felddy felddy commented Jun 9, 2023

Add stix-extract tool

🗣 Description

This PR introduces a new tool, stix-extract, to the ioc-scanner project. This tool extracts indicators of compromise (IoCs) from STIX files and outputs them in a format that can be used by the existing ioc-scan tool. Additionally the tool can extract IPs, FQDNs, and URLs for use in other parts of the IoC scanning process.

💭 Motivation and Context

The motivation for adding this tool is to allow for the use of STIX files as a source of IoCs. STIX files are a widely-adopted standard for threat intelligence sharing, and by adding the ability to directly process these files, we increase the utility and flexibility of the ioc-scanner project.

🧪 Testing

Testing for this change included manual usage of the tool to verify its functionality. The tool was used with different STIX files and combined with ioc-scan in various ways to ensure compatibility and functionality. Further automated testing can be added in the future to verify the robustness of this addition.

✅ Pre-approval Checklist

  • This PR has an informative and human-readable title.
  • Changes are limited to a single goal - eschew scope creep!
  • All relevant type-of-change labels have been added.
  • I have read the CONTRIBUTING document.
  • These code changes follow cisagov code standards.
  • All relevant repo and/or project documentation has been updated to reflect the changes in this PR.
  • All new and existing tests pass.

@felddy felddy added documentation This issue or pull request improves or adds to documentation improvement This issue or pull request will add or improve functionality, maintainability, or ease of use python Pull requests that update Python code labels Jun 9, 2023
@felddy felddy requested review from dav3r and jasonodoom as code owners June 9, 2023 21:28
@felddy felddy self-assigned this Jun 9, 2023
@felddy felddy requested review from jsf9k and mcdonnnj as code owners June 9, 2023 21:28
@felddy felddy enabled auto-merge June 9, 2023 21:28
@felddy felddy force-pushed the improvement/stix-extract branch 3 times, most recently from 205b2a0 to 65f6c34 Compare June 9, 2023 22:57
@felddy felddy force-pushed the improvement/stix-extract branch from 65f6c34 to 62afafc Compare June 9, 2023 23:04
@felddy felddy force-pushed the improvement/stix-extract branch from 62afafc to 5320414 Compare June 9, 2023 23:17
Copy link
Member

@jsf9k jsf9k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pointed out a few places for improvement, but this is nice work.

src/ioc_scan/stix_extract.py Outdated Show resolved Hide resolved
src/ioc_scan/stix_extract.py Outdated Show resolved Hide resolved
@jsf9k jsf9k added the test This issue or pull request adds or otherwise modifies test code label Jun 10, 2023
@jsf9k
Copy link
Member

jsf9k commented Jun 10, 2023

I think adding this script may warrant a bump of the patch portion of the version as well.

@felddy
Copy link
Member Author

felddy commented Jun 10, 2023

I think adding this script may warrant a bump of the patch portion of the version as well.

Bumped in: 1654c50

Copy link
Member

@jsf9k jsf9k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, but please make sure to create a release after merging. (I see that you removed that checkbox item from the PR description, but now that the version is bumped it is necessary.)

@jsf9k
Copy link
Member

jsf9k commented Jun 10, 2023

Also please note for the future that we generally add a co-author for commits created in response to reviewer suggestion by appending a line like this to the commit comment:

Co-authored-by: Shane Frasier <jeremy.frasier@gwe.cisa.dhs.gov>

Comment on lines +1 to +3
#!/usr/bin/env python3

"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be included in a file that is part of a package. The ioc_scanner.py file is an unpleasant hacky thing that does not conform to our general Python package guidelines and I see no rationale for this module to be configured similarly.

Suggested change
#!/usr/bin/env python3
"""
"""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mcdonnnj , Is the suggested change being made? I still notice in the file stix_extract.py the first line reads as #!/usr/bin/env python3.

Comment on lines +141 to +143

if __name__ == "__main__":
sys.exit(main())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same rationale as the shebang comment.

Suggested change
if __name__ == "__main__":
sys.exit(main())

Comment on lines +128 to +130
print(f"\n{'#' * 20}\n# IP Addresses\n{'#' * 20}\n")
for ip in ip_addresses:
print(ip)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to have four instances of the same logic I think we should DRY this out into a function. All four data sources are iterables and we're printing the headers with identical formatting.

best_priority = hash_priority[h.type_.value]
if best_hash is not None:
hashes.append(best_hash)
elif object_type == "DomainNameObjectType":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor but would you adjust this elif so that the elif checks are alphabetical? I think that would improve maintainability since we're just checking a bunch of strings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if you'd want to make those lists alphabetical or not considering we've done this before. LGTM tho

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if this file had typehints.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With all of the mocking and patching there is no true end-to-end test of this module's functionality. I would almost prefer crafted STIX documents that would naturally be parsed to provide the expected results instead. Using so much intentionally crafted data runs the risk of missing breaking changes in the dependency package (especially since there is no version pin around the package). Additionally there is nothing to test providing the STIX document through stdin that I see.

@jsf9k jsf9k requested a review from a team June 12, 2023 17:22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good, most of the "errors" relate to long lines.

image

@dav3r
Copy link
Member

dav3r commented Nov 3, 2023

@felddy Are you planning to return to this PR any time soon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This issue or pull request improves or adds to documentation improvement This issue or pull request will add or improve functionality, maintainability, or ease of use python Pull requests that update Python code test This issue or pull request adds or otherwise modifies test code
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

6 participants