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

added support for SecRuleUpdateTargetByTag and SecRuleUpdateTargetById #46

Merged
merged 4 commits into from
Jun 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/secrules_parsing/model/secrules.tx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ SecRule
*/
Rule:
SecAction | SecRuleScript | SecRule | SecMarker | SecComponentSignature |
SecRuleRemoveById | SecRuleRemoveByTag;
SecRuleRemoveById | SecRuleRemoveByTag | SecRuleUpdateTargetById | SecRuleUpdateTargetByTag;

SecAction:
'SecAction' '"' actions+=Action[','] '"';
Expand All @@ -32,15 +32,21 @@ SecRuleRemoveById:
SecRuleRemoveByTag:
'SecRuleRemoveBytag' tag=Tag;

SecRuleUpdateTargetById:
'SecRuleUpdateTargetById' id=INT targets+=TARGET[',']?;

SecRuleUpdateTargetByTag:
'SecRuleUpdateTargetByTag' '"'? tag=Tag '"'? targets+=TARGET[',']?;

Copy link
Contributor

Choose a reason for hiding this comment

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

Comment for both new rule above:

Here are the references of these directives, both for mod_security2 and libmodsecurity3:

https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual-(v2.x)#secruleupdatetargetbyid
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual-(v2.x)#secruleupdatetargetbytag
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual-(v3.x)#secruleupdatetargetbyid
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual-(v3.x)#secruleupdatetargetbytag

The syntax of two directives are almost the same, so we can take a look only for the first, for example.

Here are the syntax for mod_security2:

SecRuleUpdateTargetById RULEID TARGET1[,TARGET2,TARGET3] REPLACED_TARGET

and for libmodsecurity3:

SecRuleUpdateTargetById RULEID TARGET1[,TARGET2,TARGET3]

As you can see, the REPLACED_TARGET argument is gone.

IMHO without that this syntax makes no sense:

SecRuleUpdateTargetById 1234 "ARGS:foo"

Because you want to update a target - but for what? I mean there is no exclusion mark - with that, it makes sense:

SecRuleUpdateTargetById 1234 "!ARGS:foo"

This means you just want to remove this target.

If you want to be strict, you should allow one of these syntax:

SecRuleUpdateTargetById 1234 "!ARGS:foo"
SecRuleUpdateTargetById 1234 "ARGS:foo" "ARGS:bar"

But I'm sure this is not a critical thing, you can keep this as it is.

Also, both directives allows the list of TARGET:

... TARGET1[,TARGET2,TARGET3]

which is not allowed by this implementation. I suggest that you should add this syntax. Both mod_security2 and libmodsecurity3 allows this syntax.

Copy link
Member Author

@Xhoenix Xhoenix May 17, 2023

Choose a reason for hiding this comment

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

I've come up with this.

First, I'll create a new variable like this:-

TARGET: '"'? negated='!'? variables=Variable '"'?;

Then I'll change the rules based upon this. For e.g,

SecRuleUpdateTargetById:
    'SecRuleUpdateTargetById' id=INT targets+=TARGET[',']?;

What do you think about this? I tested this and it works.

image

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you pull this modification to your repository? Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, I pulled down, tested, and looks now it works as we expect.

// ActionList can be empty if is the last rule in chain
SecRule:
'SecRule' variables+=Variable['|'] '"' negated='!'? '@'?- operator=Operator '"' '"'? actions+=Action[',']? '"'?;

IDRangeList:
idlist+=ID | range=IDRange;
idlist+=INT | range=IDRange;

/*
There is no chech against collections existance, or typying between variables and value (e.g: TIME_DAY and its referred value must be equal to some integer)
There is no check against collections existance, or typying between variables and value (e.g: TIME_DAY and its referred value must be equal to some integer)
FILES is a collection, so it doesn't belong here
*/
Variable:
Expand Down Expand Up @@ -222,6 +228,7 @@ For operations, use RegExp as these have \"
RegExp: /((\\")|[^"])*/;

SlashedRegExp: /[^\/]+/;

/*
A rules ID Range: 920000-920010
*/
Expand All @@ -230,6 +237,8 @@ IDRange: /"[0-9]+-[0-9]+"/;
// URI path, tipically used in beginsWith, contains, etc.
URI: /\/[a-zA-Z0-9-_\.\/]+/;

TARGET: '"'? negated='!'? variables=Variable '"'?;

Hostname: /[a-z][a-zA-Z0-9-_\.\/]+/;

ParameterName: /[a-zA-Z0-9\-\-]+/;
Expand Down