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

dns: add dns.rcode keyword v1 #10087

Closed

Conversation

hadiqaalamdar
Copy link
Contributor

@hadiqaalamdar hadiqaalamdar commented Dec 21, 2023

Feature #6621

Link to redmine ticket: https://redmine.openinfosecfoundation.org/issues/6621

Describe changes:

  • added the dns.rcode field

SV_BRANCH=OISF/suricata-verify#1563

@@ -189,3 +263,43 @@ mod test {
));
}
}

#[cfg(test)]
mod test {
Copy link
Member

Choose a reason for hiding this comment

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

You can put your tests in the existing test block, in fact this will fail to compile. From the rust/ directory you can do cargo test or make check to run the tests before CI.

Copy link
Contributor

Choose a reason for hiding this comment

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

And you will not need tests if you reuse DetectUintData<u16>

Comment on lines +137 to +138
let rcode = ((flags >> 11) & 0xf) as u16;
detect.rcode == rcode
Copy link
Member

@jasonish jasonish Dec 21, 2023

Choose a reason for hiding this comment

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

This is looking at the opcode, not the rcode. As the rcode is the last 4 bits we can drop the shift and just mask off the first 12 bits to get our rcode. See pub fn rcode in dns.rs for an example.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh seeing the comment Support for OPT RR from RFC6891 will be needed to parse RCODE values over 15

Copy link
Contributor Author

Choose a reason for hiding this comment

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

would it be better if I used something like this instead:
let rcode = (flags & 0x000f) as DetectUintData<u16>;

&DetectDnsRcode {
rcode: 4,
},
0b0010_0000_0000_0000,
Copy link
Member

Choose a reason for hiding this comment

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

rcode still be the last 4 bits. So an rcode of 4 will be 0b0000_0000_0000_0100.

Copy link
Member

@jasonish jasonish left a comment

Choose a reason for hiding this comment

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

I recommend a few small steps first.

  • Fixup so the tests compile locally
  • Update tests with the rcode in the correct place in the flags
  • Fix the match
  • Locally run cargo clippy --all-features and fix any issues found by that

As a side note, all the DNS files are rustfmt clean (not true for all our rust code though), so you can run rustfmt on the DNS files or let your editor do it if you like. No need while in-progress, but will have you do it as it looks more ready to merge.

Thanks!

@@ -25,6 +25,9 @@ pub struct DetectDnsOpcode {
negate: bool,
opcode: u8,
}
pub struct DetectDnsRcode {
rcode: u16,
Copy link
Contributor

Choose a reason for hiding this comment

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

You should reuse DetectUintData<u16>

@@ -53,6 +56,23 @@ fn parse_opcode(opcode: &str) -> Result<DetectDnsOpcode, ()> {
Err(())
}

fn parse_rcode(rcode: &str) -> Result<DetectDnsRcode, ()> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Reusing DetectUintData<u16> supplies you the parsing function rs_detect_u16_parse

Copy link
Contributor

Choose a reason for hiding this comment

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

(and this allows more features, like negation, lesser or greater than, etc...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was wondering if rcode can be negated? Should I add functions and tests to check that as well similar to opcode?

Copy link
Member

Choose a reason for hiding this comment

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

I think it makes sense to have negation on the rcode. I might want to alert on all non-zero rcodes without listing them all for example.

@@ -0,0 +1,86 @@
/* Copyright (C) 2019 Open Information Security Foundation
Copy link
Contributor

Choose a reason for hiding this comment

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

nit year is 2023


if (SigMatchAppendSMToList(
de_ctx, s, DETECT_AL_DNS_RCODE, (SigMatchCtx *)detect, dns_rcode_list_id) == NULL) {
goto error;
Copy link
Contributor

Choose a reason for hiding this comment

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

do not need a label if only used once

static int DetectDnsRcodeMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, void *state,
void *txv, const Signature *s, const SigMatchCtx *ctx)
{
return rs_dns_rcode_match(txv, (void *)ctx, flags);
Copy link
Contributor

Choose a reason for hiding this comment

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

Get the u16 value from the dns tx with a call to a new SCDnsTxGetRcode and then match it DetectU16Match

void DetectDnsRcodeRegister(void)
{
sigmatch_table[DETECT_AL_DNS_RCODE].name = "dns.rcode";
sigmatch_table[DETECT_AL_DNS_RCODE].desc = "Match the DNS header rcode flag.";
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing a doc entry, and by the way documentation in doc/userguide/rules/dns-keywords.rst

Copy link
Contributor

Choose a reason for hiding this comment

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

And related: the doc addition itself :P

@catenacyber
Copy link
Contributor

Looking good overall.
Main change is to reuse existing DetectU16Data (and this have less new code)

@catenacyber
Copy link
Contributor

Is code really u16, or is it u8 ?

@hadiqaalamdar
Copy link
Contributor Author

Is code really u16, or is it u8 ?

according to this it's supposed to be u16: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6

@jasonish
Copy link
Member

Is code really u16, or is it u8 ?

according to this it's supposed to be u16: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6

We can safely use u8 here as we only deal with 4 bit rcodes currently. Note to self: do we have to worry about EDNS with extension options for more rcodes? I don't think this has ever come up.

@hadiqaalamdar
Copy link
Contributor Author

hadiqaalamdar commented Dec 22, 2023

Is code really u16, or is it u8 ?

according to this it's supposed to be u16: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6

We can safely use u8 here as we only deal with 4 bit rcodes currently. Note to self: do we have to worry about EDNS with extension options for more rcodes? I don't think this has ever come up.

so should I use the DetectUintData<u8> here or simply u8 as done for opcode?

@jasonish
Copy link
Member

so should I use the DetectUintData<u8> here or simply u8 as done for opcode?

I think the end-goal would be to get to DetectUintData<u8>, but if you want to first use u8 to be as close as possible to opcode in the short term to get CI greener sooner for a small win, that is fine as well. Then migrate to DetectUintData<u8>. This PR would be useful as a template as it converts opcode, but its not in master yet.

@jufajardini
Copy link
Contributor

Edited the PR description to remove space before SV PR link, to ensure that our CI would pick the right branch for the tests (cf https://github.com/OISF/suricata/actions/runs/7290609564/job/19867785013#step:9:38)

Comment on lines +16 to +18
*/

#include "suricata-common.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

You can also add your info as author, if you want :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooo that would be cool to see when my branch gets merged. Right now my code is all sorts of messed up lol. I'm relearning bitwise operations from back when I studied them at uni

Copy link
Contributor

Choose a reason for hiding this comment

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

💪🏽
Happens a lot, for these things that we don't use much, after learning.

Copy link
Contributor

@jufajardini jufajardini left a comment

Choose a reason for hiding this comment

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

Hey there, good progress!

In addition to the existing comments, when you're working on the next version of the work, do make sure to check if the errors shown in https://github.com/OISF/suricata/actions/runs/7290609583/job/19867784386?pr=10087#step:12:126 have been addressed :)

@catenacyber
Copy link
Contributor

We can safely use u8 here as we only deal with 4 bit rcodes currently. Note to self: do we have to worry about EDNS with extension options for more rcodes? I don't think this has ever come up.

To keep track, Is there a ticket to handle more rcodes Jason ?

@jasonish
Copy link
Member

We can safely use u8 here as we only deal with 4 bit rcodes currently. Note to self: do we have to worry about EDNS with extension options for more rcodes? I don't think this has ever come up.

To keep track, Is there a ticket to handle more rcodes Jason ?

I guess that would be EDNS, not specifically rcodes. Is it even a thing that saw use?

@jasonish
Copy link
Member

We can safely use u8 here as we only deal with 4 bit rcodes currently. Note to self: do we have to worry about EDNS with extension options for more rcodes? I don't think this has ever come up.

To keep track, Is there a ticket to handle more rcodes Jason ?

I guess that would be EDNS, not specifically rcodes. Is it even a thing that saw use?

https://redmine.openinfosecfoundation.org/issues/6650

@hadiqaalamdar Please don't concern yourself with these comments. They are way out of scope for the actual work required in this PR.

@hadiqaalamdar
Copy link
Contributor Author

New PR: #10097

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants