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 YARA queries to osquery-perf #25272

Merged
merged 5 commits into from
Jan 9, 2025
Merged

Conversation

sgress454
Copy link
Contributor

@sgress454 sgress454 commented Jan 8, 2025

Overview

This PR adds support for remote YARA queries to osquery-perf, so that remote YARA queries can be load-tested.

Details

The existing runLiveQuery() is updated to branch off into different query running functions based on the content of the query. If the query contains from yara and sigurl, then the new runLiveYaraQuery() function is run which makes a request to the Fleet "get yara rules" API before returning an appropriate response. Otherwise, the new RunLiveMockQuery() function is run which includes the previous logic for sending a mock response.

Testing

I don't see any automated testing for osquery-perf, but I manually tested in the following way:

  1. Started osquery-perf with go run agent.go
  2. Ran a live query on the new host using
SELECT * FROM yara where sigurl="https://localhost:8080/api/osquery/yara/rule1.yar"

and verified that the result was as-expected:
image
I also used a log in Fleet to verify that the "get yara rules" API was really being called.

  1. Ran another live query on the host using:
SELECT * FROM system_info"

and verified that the result was as expected:
image

I also tested that sending a sigurl with the wrong host returns a live yara query failed because sigurl host did not match server address error

Checklist for submitter

  • Added support on fleet's osquery simulator cmd/osquery-perf for new osquery data ingestion features.

@sgress454 sgress454 requested a review from a team as a code owner January 8, 2025 22:09
// Switch based on contents of the query.
lcQuery := strings.ToLower(query)
switch {
case strings.Contains(lcQuery, "from yara") && strings.Contains(lcQuery, "sigurl"):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the query isn't using sigurl then it's not a remote Yara rule, so it won't cause any extra load on the Fleet server.


func (a *agent) runLiveYaraQuery(query string) (results []map[string]string, status *fleet.OsqueryStatus, message *string, stats *fleet.Stats) {
// Get the URL of the YARA rule to request (i.e. the sigurl).
urlRegex := regexp.MustCompile(`sigurl=(["'])([^"']*)["']`)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Technically this would allow mismatched single and double quotes but Go regex doesn't support backreferences and I don't think it's worth validating.

Copy link

codecov bot commented Jan 8, 2025

Codecov Report

Attention: Patch coverage is 0% with 65 lines in your changes missing coverage. Please review.

Project coverage is 63.82%. Comparing base (721b732) to head (5cc6717).
Report is 23 commits behind head on main.

Files with missing lines Patch % Lines
cmd/osquery-perf/agent.go 0.00% 65 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25272      +/-   ##
==========================================
- Coverage   63.84%   63.82%   -0.03%     
==========================================
  Files        1616     1616              
  Lines      153829   153893      +64     
  Branches     3975     3975              
==========================================
- Hits        98218    98217       -1     
- Misses      47797    47861      +64     
- Partials     7814     7815       +1     
Flag Coverage Δ
backend 64.69% <0.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

}
request.Header.Add("Content-type", "application/json")

// Make the request. For load testing purposes we don't actually care about the response.
Copy link
Member

Choose a reason for hiding this comment

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

We should still read the body to better simulate clients:

if _, err := io.Copy(io.Discard, response.Body); err != nil {
  ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 done

Copy link
Member

@lucasmrod lucasmrod left a comment

Choose a reason for hiding this comment

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

Looks great. Left a nit comment about reading the response body.

if _, err := io.Copy(io.Discard, response.Body); err != nil {
ss := fleet.OsqueryStatus(1)
return []map[string]string{}, &ss, ptr.String(fmt.Sprintf("error reading response from yara API: %v", err)), nil
}
defer response.Body.Close()
Copy link
Member

Choose a reason for hiding this comment

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

defer response.Body.Close() should be before the io.Copy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch, fixed

@sgress454 sgress454 merged commit 9ad2468 into main Jan 9, 2025
15 of 16 checks passed
@sgress454 sgress454 deleted the sgress454/25220-add-yara-to-load-test branch January 9, 2025 16:00
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.

2 participants