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

Enable descriptions for statement nodes #209

Merged
merged 6 commits into from
Jul 28, 2020
Merged

Enable descriptions for statement nodes #209

merged 6 commits into from
Jul 28, 2020

Conversation

Ana06
Copy link
Member

@Ana06 Ana06 commented Jul 27, 2020

Enable descriptions for statement nodes such as and and or. Includes some refactoring to facilitate the implementation.

Use of case in: mandiant/capa-rules/pull/51

Closes #194

@Ana06 Ana06 added the enhancement New feature or request label Jul 27, 2020
capa/engine.py Outdated
super(And, self).__init__()
self.children = list(children)
def __init__(self, children, description=None):
super(And, self).__init__(description)
Copy link
Collaborator

Choose a reason for hiding this comment

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

since description is a kwarg, i prefer specifying this via description=description

Copy link
Member Author

Choose a reason for hiding this comment

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

why is it a kward? From Python documentation:

**kwargs allows you to pass keyworded variable length of arguments to a function

Isn't this used to allow the function to receive an arbitrary number of parameters? This has a fixed number of parameters, but one is optional. Or has I understood something wrong? 😕

Copy link
Collaborator

Choose a reason for hiding this comment

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

kwarg is the name for a parameter like foo=bar, aka a parameter with a default value. kwargs don't have to be passed in order (but typically are) when they have their keyword provided. when a function supports arbitrary kwargs, that is, when not all of the possible keywords are pre-declared, then it accepts a positional argument **kwargs that collects all of these into a dict. but, thats not whats happening here. I'm just suggesting that we use the description= prefix so that its clear we're referencing the kwarg, not a position arg (which cannot be referenced by key, at least in py2.7).

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I get it now. I should used the key=value format when calling parameters with a default value, right? Should we also update this in other places for consistency? I am concretely thinking about the description implementation in features (which looks the same as here).

capa/rules.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@williballenthin williballenthin left a comment

Choose a reason for hiding this comment

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

outstanding!

would you add a couple tests showing rules with statement descriptions getting parsed and extracted ok? then, let's merge!

Copy link
Collaborator

@mr-tz mr-tz left a comment

Choose a reason for hiding this comment

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

lgtm, thanks!

"type": "subscope",
"subscope": statement.scope,
}
else:
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice simplification here, should we keep the RuntimeError though?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think it is needed. We would have to add an if with the rest Statement subclasses (or checking that statement is an instance of Statement), but this function is called at https://github.com/Ana06/capa/blob/statement-description/capa/render/__init__.py#L94 after checking that node is an instance of Statement. So, I don't see how the error could be trigger. It would be anyway a bug as there is no input the user can provide that should run in an error like this.

@mr-tz
Copy link
Collaborator

mr-tz commented Jul 27, 2020

Please also add 1-2 sentences in the documentation, e.g. here https://github.com/fireeye/capa-rules/blob/master/doc/format.md#features-block

@Ana06
Copy link
Member Author

Ana06 commented Jul 28, 2020

@mr-tz

Please also add 1-2 sentences in the documentation, e.g. here fireeye/capa-rules@master/doc/format.md#features-block

I'll do 😉 I though I had written this in the PR description... It seems I forgot... 👀

Avoid repeating code and make easier to modify.
They are not needed and complicate the code and make more difficult to
add more parameters to the initialization of Statements.

This produces many changes in the tests. The alternative would be to add
a parameter None in all of them, which are also a lot of changes.
Enable descriptions for statement nodes such as and and or.

Use of case in: mandiant/capa-rules/pull/51

Documentation should be added in capa-rules.
Avoid code repetition to make modifying this code easier.
Implement it similarly as how it is rendered for features.
Add statement descriptions to `test_rule_yaml_descriptions` to ensure
rules with statement descriptions are parsed and extracted correctly.
@Ana06
Copy link
Member Author

Ana06 commented Jul 28, 2020

Documentation in: mandiant/capa-rules#57

@williballenthin williballenthin merged commit 137c0ca into mandiant:master Jul 28, 2020
@williballenthin williballenthin added this to the v1.1.0 milestone Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable descriptions for statement nodes
3 participants