feat(csharp): add C# ChaCha20Poly1305 detection rules - #506
Conversation
Signed-off-by: Kaldesyvon <martinnovysedlak99@gmail.com>
Signed-off-by: Kaldesyvon <martinnovysedlak99@gmail.com>
e38a1a9 to
0d2d19b
Compare
|
Hi Martin, That looks good structure wise but we need to refine the detection rule a bit according to the comments above. Best regards, |
Signed-off-by: Kaldesyvon <martinnovysedlak99@gmail.com>
Signed-off-by: Kaldesyvon <martinnovysedlak99@gmail.com>
|
Hi Martin, that looks way better now, very good job! Also the unit test is comprehensive and covers properly. Best regards, |
Signed-off-by: Kaldesyvon <martinnovysedlak99@gmail.com>
| .createDetectionRule() | ||
| .forObjectTypes(MethodMatcher.ANY) | ||
| .forMethods("Encrypt") | ||
| .shouldBeDetectedAs(new ValueActionFactory<>("ENCRYPT")) |
There was a problem hiding this comment.
If you detect this as
.shouldBeDetectedAs(new CipherActionFactory<>(CipherAction.Action.ENCRYPT))
you would not have to mix this into the handling of the value actions in the translator.
There was a problem hiding this comment.
Thats a good change thanks, will also implement that in my local rules :)
| .forObjectTypes(MethodMatcher.ANY) | ||
| .forMethods("Encrypt") | ||
| .shouldBeDetectedAs(new ValueActionFactory<>("ENCRYPT")) | ||
| .withAnyParameters() // Byte[] or ReadOnlySpan<Byte> overloads |
There was a problem hiding this comment.
This depends on how specific you want to be. withAnyParameters() means what is says - anything is ok. If I read the C# docs correctly this Encrypt requires 5 parameters. Your test now uses 4 which is OK if you have ANY here, but is this correct C#?
There was a problem hiding this comment.
This is correct c# code because AAD in this function has a default value that will be taken if only 4 parameters are there. That is also the reason for that specific test case and the test before that covers the 5 parameter option.
HOWEVER i have tried to compile the file and it seems that new ReadOnlySpan<byte>[12]; creates an array of ReadOnlySpan<byte> which then fails once it gets used as parameter where it becomes non-convertible.
Also the file needs using System; for compilation, which doesnt matter for the test but just to not let it out.
For me this works better: Span<byte> nonce = stackalloc byte[12];
Did it compile for you Martin?
|
Hi @Kaldesyvon , hi @fynnth, I just learned that C# has the same concept of named parameters like python. Named parameters in a function call can occur in arbitrary order. Some of them are optional which makes the number of variants a valid function call can have even higher. Writing detection rules for pycryptodome was thus very difficult because you need a lot of rules to cover the most common call variants. To this end, I suggested to add another call to the detection rule interface .withNamedMethodParameter(String name, String, boolean optional);
For your reference, there is currently one detection rule in python/…/detection/pycrypto/hash/PycryptoCryptoHash.java for |
|
Hi @san-zrl, Thats true, I have not thought of that. But for csharp the issue is even worse because the parameter name was not in the parser interface that I have implemented, so i have to rework that too before anything like that works. (info is just not there for parameter names) My plan would be to finish detection rules like it is now and then afterwards start reworking the engine and implementing named parameters, then update the rules and also extend parameter detection in general (for example trying to detect certain key sizes that were instantiated in a variable before the method call) |
|
@fynnth - Sure. For the moment the new feature is python specific. We have to see where else it would make sense. |
Hello team,
After restoring C# I am adding new detection rules for ChaCha20Poly1305 algorithm.
This is my first PR in this project so I would appreciate any comments or help.
Thank you for reviewing.
Best,
Martin