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

Add CIDR function as custom SQLite function #7563

Merged
merged 6 commits into from
May 2, 2022

Conversation

iko1
Copy link
Contributor

@iko1 iko1 commented Apr 22, 2022

Add custom SQLite function to check if IP address v4/v6 is within supplied CIDR block.
The function gets two arguments. The first is the actual CIDR block, and the second is the IP address.
First, it parses the IP address and determines whether it's valid, and then checks, if the CIDR block has matched the IP address version, otherwise an error is returned.
If the IP address is within the CIDR block, the function returns 1 otherwise 0.

usage examples:
image

Closes #7555

@iko1 iko1 requested review from a team as code owners April 22, 2022 14:37
@iko1 iko1 changed the title Add CIDR function Add CIDR function as custom SQLite function Apr 22, 2022
@iko1 iko1 force-pushed the feature/add-cidr-function branch from a0b722f to d536c6a Compare April 24, 2022 13:51
Copy link
Member

@sharvilshah sharvilshah left a comment

Choose a reason for hiding this comment

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

Can this be added to the documentation along with other SQL additions here -- https://github.com/osquery/osquery/blob/master/docs/wiki/introduction/sql.md#sql-additions

@iko1 iko1 requested a review from sharvilshah April 27, 2022 10:47
Copy link
Member

@directionless directionless left a comment

Choose a reason for hiding this comment

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

I love the intent. A couple of questions...

@@ -480,6 +480,25 @@ There are also encoding functions available, to process query results.
</p>
</details>

#### Network functions

- `in_cidr_range(CIDR_RANGE, IP_ADDRESS)`: return 1 if the IP address is within the CIDR range, otherwise 0.
Copy link
Member

Choose a reason for hiding this comment

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

My inclination is to name this in_cidr_block, but maybe that doesn't matter.

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've totally agreed with your inclination. I have amended it accordingly.

osquery/sql/sqlite_network.cpp Outdated Show resolved Hide resolved
osquery/sql/sqlite_network.cpp Show resolved Hide resolved
EXPECT_EQ(sql.rows()[0], r);

sql = SQL(
"SELECT in_cidr_range('198.51.100.14/24', '198.51.100.14') AS result;");
Copy link
Member

Choose a reason for hiding this comment

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

<3 I wanted exactly this test

@directionless
Copy link
Member

Would you expect SELECT in_cidr_range('2001:db8::/0', '2001:db8:0:ffff:ffff:ffff:ffff:ffff'); to be true or false?

@iko1
Copy link
Contributor Author

iko1 commented May 2, 2022

Would you expect SELECT in_cidr_range('2001:db8::/0', '2001:db8:0:ffff:ffff:ffff:ffff:ffff'); to be true or false?

in_cidr_range('2001:db8::/0', '2001:db8:0:ffff:ffff:ffff:ffff:ffff') returns false. You've specified 0 bits after the slash notation, so the address before the slash must be valid, but it's not the case here, this IPv6 is less than 16 bytes.

@iko1 iko1 force-pushed the feature/add-cidr-function branch from f311c65 to b685c83 Compare May 2, 2022 21:22
Copy link
Member

@directionless directionless left a comment

Choose a reason for hiding this comment

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

Nice! thank you for this

@directionless directionless merged commit a349d3b into osquery:master May 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add CIDR functions
4 participants