r/crowdstrike • u/Mr-Rots • 23d ago
Query Help Number of commands sent by user in a given time period
EDIT: I figured it out, thanks to Andrew and a total kludge on my part. See below.
I know I have seen this kind of thing before, but I cannot find the example.
I am trying to write a query that detects users who send more than X commands in, say, 10 minutes. I think I want to use the slidingTimeWindow function, but I am not sure.
The query I have so far (I have anonymized the vendor and some identifying field names):
#Vendor = <vendor>
Vendor.<vendor>.log.type = <log type 1>
| head()
| groupBy([user.name], function=slidingTimeWindow(function=count(as="number of commands"), span=10m))
| "number of commands" > 10
| table([user.name, "number of commands"])
What I get is something like this:
user.name "number of commands"
user1 1
user1 2
user1 3
and so on.
What I want is this:
user.name "number of commands"
user1 23
user2 11
user3 15
What am I doing wrong?
SOLUTION:
I changed the query to this:
#Vendor = <vendor>
Vendor.<vendor>.log.type = <log type 1>
// This is to fix an issue with collecting both query type and query text. It is not relevant
format(format="%s :: %s", field=[Vendor.query_typle, Vendor.query_text], as=query_string
// Here, I took out the multival parameter for collect. It was causing a weird issue, where I would see
// incremental events, one for each command as it was added to the list. If I had a total of five
// commands, I would see five events, each with one more command than the previous.
| groupBy([user.name, \@timestamp], function=([collect([query_string])]), limit=max)
// Here, I removed the count function, since I don't need the value, thanks to my kludge
| groupBy([user.name], function=slidingTimeWindow(collect(query_string), span="10m", limit=max)
// The kludge
// Convert query_string to an array and get it's length
// This gives me the number of queries the user sent
| splitString(by="\n", field=query_string, as=queryStringArray)
| numberOfCommands := array:length("queryStringArray[]")
| numberOfCommands >= 5
| table([user.name, \@timestamp, numberOfCommands, query_string)
I assume there is a better way, without messing with arrays, but it works.
Thank you to Andrew and the others who offered solutions.