Repository navigation
Match aliased imports in Python capability rules - #921
Open
talalkashar wants to merge 1 commit into
Open
talalkashar wants to merge 1 commit into
talalkashar wants to merge 1 commit into
Conversation
capability-network-outbound, capability-network-download and capability-process-spawn key on the literal module.attribute text (requests.get, urllib.request.urlopen, subprocess.run). After "import requests as _r" the call site reads "_r.get(...)", so the same code produces no findings. Add strings for "import X as Y" and "from X import f as Y" for these modules, the same way the JS strings already key on the require of child_process and on destructured imports. Fixes DataDog#913
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #913
What was wrong
capability-network-outbound,capability-network-downloadandcapability-process-spawnmatch the literalmodule.attributetext (requests.get,urllib.request.urlopen,subprocess.run). Once the module is imported under an alias, the call site reads_r.get(...)and none of the three rules fire.Reproduction
Scanning a directory with only this file on
maingives no findings:What changed
YARA can't tie an alias back to its call site, so these strings match the aliased import itself. That follows how
threat-setup-import-aliasingkeys onfrom X import f as Y, and how the JS strings incapability-process-spawnkey on thechild_processrequire and on destructured imports.import requests as,import urllib.request as, andfrom requests|urllib.request import <fn> asfromform limited toget,urlopenandurlretrieveimport subprocess as,from subprocess import <fn> asA plain
import requestswith no call still does not match, andimport requests_toolbelt as xdoesn't match either (the pattern needs whitespace after the module name). With this change, the file above gets one finding from each of the three rules.Testing
*.import-alias.pypositive fixtures for each rule. They fail onmainand pass with this change.pytest tests/analyzer/sourcecode: 136 passedpytest tests/core tests/reporters: 242 passed, 2 skippedmake lint(first flake8 pass): 0 errors