-
Notifications
You must be signed in to change notification settings - Fork 450
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
+81
−1
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |
"net/http" | ||
_ "net/http/pprof" | ||
"os" | ||
"regexp" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
@@ -2090,10 +2091,89 @@ func (a *agent) runLiveQuery(query string) (results []map[string]string, status | |
ss := fleet.OsqueryStatus(1) | ||
return []map[string]string{}, &ss, ptr.String("live query failed with error foobar"), nil | ||
} | ||
ss := fleet.OsqueryStatus(0) | ||
if a.liveQueryNoResultsProb > 0.0 && rand.Float64() <= a.liveQueryNoResultsProb { | ||
ss := fleet.OsqueryStatus(0) | ||
return []map[string]string{}, &ss, nil, nil | ||
} | ||
|
||
// Switch based on contents of the query. | ||
lcQuery := strings.ToLower(query) | ||
switch { | ||
case strings.Contains(lcQuery, "from yara") && strings.Contains(lcQuery, "sigurl"): | ||
return a.runLiveYaraQuery(query) | ||
default: | ||
return a.runLiveMockQuery(query) | ||
} | ||
} | ||
|
||
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=(["'])([^"']*)["']`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
matches := urlRegex.FindStringSubmatch(query) | ||
var url string | ||
if len(matches) > 2 { | ||
url = matches[2] | ||
} else { | ||
ss := fleet.OsqueryStatus(1) | ||
return []map[string]string{}, &ss, ptr.String("live yara query failed because a valid sigurl could not be found"), nil | ||
} | ||
|
||
// Osquery validates that the sigurl is one of a configured set, so that it's not | ||
// sending requests to just anywhere. We'll check that it's at least the same host | ||
// as the Fleet server. | ||
if !strings.HasPrefix(url, a.serverAddress) { | ||
ss := fleet.OsqueryStatus(1) | ||
return []map[string]string{}, &ss, ptr.String("live yara query failed because sigurl host did not match server address"), nil | ||
} | ||
|
||
// Make the request. | ||
body := []byte(`{"node_key": "` + a.nodeKey + `"}`) | ||
request, err := http.NewRequest("POST", url, bytes.NewReader(body)) | ||
if err != nil { | ||
ss := fleet.OsqueryStatus(1) | ||
return []map[string]string{}, &ss, ptr.String("live yara query failed due to error creating request"), nil | ||
} | ||
request.Header.Add("Content-type", "application/json") | ||
|
||
// Make the request. | ||
response, err := http.DefaultClient.Do(request) | ||
if err != nil { | ||
ss := fleet.OsqueryStatus(1) | ||
return []map[string]string{}, &ss, ptr.String(fmt.Sprintf("yara request failed to run: %v", err)), nil | ||
} | ||
defer response.Body.Close() | ||
|
||
// For load testing purposes we don't actually care about the response, but check that we at least got one. | ||
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 | ||
} | ||
|
||
// Return a response indicating that the file is clean. | ||
ss := fleet.OsqueryStatus(0) | ||
return []map[string]string{ | ||
{ | ||
"count": "0", | ||
"matches": "", | ||
"strings": "", | ||
"tags": "", | ||
"sig_group": "", | ||
"sigfile": "", | ||
"sigrule": "", | ||
"sigurl": url, | ||
// Could pull this from the query, but not necessary for load testing. | ||
"path": "/some/path", | ||
}, | ||
}, &ss, nil, &fleet.Stats{ | ||
WallTimeMs: uint64(rand.Intn(1000) * 1000), | ||
UserTime: uint64(rand.Intn(1000)), | ||
SystemTime: uint64(rand.Intn(1000)), | ||
Memory: uint64(rand.Intn(1000)), | ||
} | ||
} | ||
|
||
func (a *agent) runLiveMockQuery(query string) (results []map[string]string, status *fleet.OsqueryStatus, message *string, stats *fleet.Stats) { | ||
ss := fleet.OsqueryStatus(0) | ||
return []map[string]string{ | ||
{ | ||
"admindir": "/var/lib/dpkg", | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.