Generic.Detection.RapidHunt
Type: Client
Answers “which machines have seen this?” quickly, for one indicator.
Kind says what the indicator is (the console picks it for you):
web(a domain, URL or IP address): see below.hash(MD5, SHA1 or SHA256): running process executables, and files under the user, temp and ProgramData folders (up to 100 MB each).file(a file name or full path): the path itself, files with that name in the same folders, running processes and Windows Prefetch (evidence the program ran).process(a program name such asrclone.exe): running processes, Prefetch and Sysmon process start events (event 1).user(a user name, with or withoutDOMAIN\): processes running as the user, a profile folder, and Windows logons and failed logons (Security events 4624⁄4625).
For web, each endpoint checks, in one pass:
- Browser history (Chrome, Edge, Brave, Vivaldi, Opera and Firefox) for visits to the domain or any of its subdomains, or to the IP.
- The DNS client cache (Windows).
- Live network connections to the IP, or to any address the domain currently resolves to in the DNS cache, with the owning process.
- Sysmon DNS (event 22) and network (event 3) events, when Sysmon is installed.
- The Windows DNS client event log (event 3008), when it is enabled.
Only matching rows are returned, so the hunt is quick to read.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
Indicator |
string | A domain, URL, IP address, hash, file name or path, process name or user name. | |
Kind |
choices | web |
What the indicator is. |
DaysBack |
int | 30 |
How far back to look in browser history and event logs. |
IncludeEventLogs |
bool | Y |
Also search Sysmon and DNS client event logs (slower on large logs). |
Source
Defined in artifacts/definitions/Generic/Detection/RapidHunt.yaml.
name: Generic.Detection.RapidHunt
description: |
Answers "which machines have seen this?" quickly, for one indicator.
`Kind` says what the indicator is (the console picks it for you):
- `web` (a domain, URL or IP address): see below.
- `hash` (MD5, SHA1 or SHA256): running process executables, and files
under the user, temp and ProgramData folders (up to 100 MB each).
- `file` (a file name or full path): the path itself, files with that
name in the same folders, running processes and Windows Prefetch
(evidence the program ran).
- `process` (a program name such as `rclone.exe`): running processes,
Prefetch and Sysmon process start events (event 1).
- `user` (a user name, with or without `DOMAIN\`): processes running
as the user, a profile folder, and Windows logons and failed logons
(Security events 4624/4625).
For `web`, each endpoint checks, in one pass:
- Browser history (Chrome, Edge, Brave, Vivaldi, Opera and Firefox) for
visits to the domain or any of its subdomains, or to the IP.
- The DNS client cache (Windows).
- Live network connections to the IP, or to any address the domain
currently resolves to in the DNS cache, with the owning process.
- Sysmon DNS (event 22) and network (event 3) events, when Sysmon is
installed.
- The Windows DNS client event log (event 3008), when it is enabled.
Only matching rows are returned, so the hunt is quick to read.
type: CLIENT
parameters:
- name: Indicator
description: A domain, URL, IP address, hash, file name or path, process name or user name.
- name: Kind
description: What the indicator is.
type: choices
default: web
choices:
- web
- hash
- file
- process
- user
- name: DaysBack
description: How far back to look in browser history and event logs.
type: int
default: 30
- name: IncludeEventLogs
description: Also search Sysmon and DNS client event logs (slower on large logs).
type: bool
default: Y
sources:
- query: |
// linter: symbol_mask_warn:timestamp
LET Info <= SELECT OS FROM info()
LET ThisOS <= Info[0].OS
LET IsWindows <= ThisOS =~ "windows"
LET Since <= timestamp(epoch=now() - DaysBack * 86400)
LET Clean <= lowcase(string=regex_replace(source=Indicator, re='''^\s+|\s+$''', replace=""))
LET HostPart <= if(condition=Clean =~ "://",
then=url(parse=Clean).Hostname,
else=regex_replace(source=Clean, re='''[/:].*$''', replace=""))
LET IsIP <= HostPart =~ '''^[0-9]{1,3}(\.[0-9]{1,3}){3}$''' OR Clean =~ '''^[0-9a-f:]+:[0-9a-f:]+$'''
LET Target <= if(condition=Clean =~ '''^[0-9a-f:]+:[0-9a-f:]+$''', then=Clean, else=HostPart)
LET Escaped <= regex_replace(source=Target, re='''([.*+?^${}()|\[\]\\])''', replace='''\$1''')
// A URL whose host is the target or a subdomain of it.
LET UrlRe <= "(?i)^[a-z][a-z0-9+.-]*://([^/@]*@)?([^/]*\\.)?" + Escaped + "(:[0-9]+)?([/?#]|$)"
// A DNS name equal to the target or a subdomain of it.
LET NameRe <= "(?i)^([^ ]*\\.)?" + Escaped + "\\.?$"
LET HomeGlobs <= if(condition=IsWindows,
then="C:/Users/*",
else=if(condition=ThisOS =~ "darwin", then="/Users/*", else="/home/*"))
LET ChromiumHistory = SELECT OSPath, OSPath[2] AS User
FROM glob(globs=[
HomeGlobs + "/AppData/Local/{Google/Chrome,Microsoft/Edge,BraveSoftware/Brave-Browser,Vivaldi}/User Data/*/History",
HomeGlobs + "/AppData/Roaming/Opera Software/Opera*Stable/History",
HomeGlobs + "/.config/{google-chrome,chromium,microsoft-edge,BraveSoftware/Brave-Browser}/*/History",
HomeGlobs + "/Library/Application Support/{Google/Chrome,Microsoft Edge,BraveSoftware/Brave-Browser}/*/History"])
LET FirefoxHistory = SELECT OSPath, OSPath[2] AS User
FROM glob(globs=[
HomeGlobs + "/AppData/Roaming/Mozilla/Firefox/Profiles/*/places.sqlite",
HomeGlobs + "/.mozilla/firefox/*/places.sqlite",
HomeGlobs + "/Library/Application Support/Firefox/Profiles/*/places.sqlite"])
LET ChromiumHits = SELECT * FROM foreach(row=ChromiumHistory, query={
SELECT "Browser history" AS Source,
timestamp(winfiletime=last_visit_time * 10) AS Time,
User, "" AS Process, VisitedUrl AS Match,
format(format="%v (visits: %v)", args=[title, visit_count]) AS Detail,
OSPath AS Path
FROM sqlite(file=OSPath,
query="SELECT url AS VisitedUrl, title, visit_count, last_visit_time FROM urls WHERE url LIKE ?",
args=["%" + Target + "%"])
WHERE VisitedUrl =~ UrlRe AND Time > Since
})
LET FirefoxHits = SELECT * FROM foreach(row=FirefoxHistory, query={
SELECT "Browser history" AS Source,
timestamp(epoch=last_visit_date / 1000000) AS Time,
User, "" AS Process, VisitedUrl AS Match,
format(format="%v (visits: %v)", args=[title, visit_count]) AS Detail,
OSPath AS Path
FROM sqlite(file=OSPath,
query="SELECT url AS VisitedUrl, title, visit_count, last_visit_date FROM moz_places WHERE url LIKE ?",
args=["%" + Target + "%"])
WHERE VisitedUrl =~ UrlRe AND Time > Since
})
LET DnsCache = SELECT * FROM if(condition=IsWindows, then={
SELECT Name, Record, RecordType
FROM Artifact.Windows.System.DNSCache()
})
LET DnsHits = SELECT "DNS cache" AS Source, timestamp(epoch=now()) AS Time, "" AS User, "" AS Process,
Name AS Match, format(format="%v record: %v", args=[RecordType, Record]) AS Detail, "" AS Path
FROM DnsCache
WHERE if(condition=IsIP, then=Record = Target, else=Name =~ NameRe)
// The IPs to look for in live connections.
LET ResolvedIPs <= SELECT Record FROM if(condition=Kind = "web", then={
SELECT Record FROM DnsCache WHERE Name =~ NameRe AND RecordType =~ "^(A|AAAA)$"
})
LET TargetIPs <= if(condition=IsIP, then=[Target], else=ResolvedIPs.Record)
LET ProcName(P) = SELECT Name FROM pslist(pid=P)
LET ConnectionHits = SELECT "Live connection" AS Source, timestamp(epoch=now()) AS Time, "" AS User,
join(array=ProcName(P=Pid).Name, sep=",") AS Process,
Raddr.IP AS Match,
format(format="%v %v:%v -> %v:%v (pid %v)", args=[Status, Laddr.IP, Laddr.Port, Raddr.IP, Raddr.Port, Pid]) AS Detail,
"" AS Path
FROM netstat()
WHERE Raddr.IP AND Raddr.IP in TargetIPs
LET SysmonLog = "C:/Windows/System32/winevt/Logs/Microsoft-Windows-Sysmon%4Operational.evtx"
LET DnsClientLog = "C:/Windows/System32/winevt/Logs/Microsoft-Windows-DNS-Client%4Operational.evtx"
LET SysmonHits = SELECT * FROM if(condition=IsWindows AND IncludeEventLogs AND stat(filename=SysmonLog).Size, then={
SELECT if(condition=System.EventID.Value = 22, then="Sysmon DNS", else="Sysmon network") AS Source,
System.TimeCreated.SystemTime AS Time,
EventData.User AS User,
EventData.Image AS Process,
if(condition=System.EventID.Value = 22, then=EventData.QueryName, else=EventData.DestinationIp) AS Match,
if(condition=System.EventID.Value = 22,
then=format(format="answer: %v", args=EventData.QueryResults),
else=format(format="%v -> %v:%v", args=[EventData.SourceIp, EventData.DestinationIp, EventData.DestinationPort])) AS Detail,
SysmonLog AS Path
FROM parse_evtx(filename=SysmonLog)
WHERE System.EventID.Value in (3, 22)
AND System.TimeCreated.SystemTime > Since.Unix
AND if(condition=System.EventID.Value = 22,
then=EventData.QueryName =~ NameRe OR (IsIP AND EventData.QueryResults =~ Escaped),
else=EventData.DestinationIp in TargetIPs OR EventData.DestinationHostname =~ NameRe)
})
LET DnsClientHits = SELECT * FROM if(condition=IsWindows AND IncludeEventLogs AND NOT IsIP AND stat(filename=DnsClientLog).Size, then={
SELECT "DNS client log" AS Source,
System.TimeCreated.SystemTime AS Time,
"" AS User, "" AS Process,
EventData.QueryName AS Match,
format(format="query type %v, status %v, results: %v",
args=[EventData.QueryType, EventData.QueryStatus, EventData.QueryResults]) AS Detail,
DnsClientLog AS Path
FROM parse_evtx(filename=DnsClientLog)
WHERE System.EventID.Value = 3008
AND System.TimeCreated.SystemTime > Since.Unix
AND EventData.QueryName =~ NameRe
})
LET WebHits = SELECT * FROM chain(
a=ChromiumHits, b=FirefoxHits, c=DnsHits, d=ConnectionHits,
e=SysmonHits, f=DnsClientHits, async=TRUE)
// ----------------------------------------- hash, file, process, user
LET Raw <= regex_replace(source=Indicator, re='''^\s+|\s+$''', replace="")
LET HashType <= if(condition=Clean =~ '''^[0-9a-f]{64}$''', then="SHA256",
else=if(condition=Clean =~ '''^[0-9a-f]{40}$''', then="SHA1", else="MD5"))
LET IsPath <= Raw =~ '''[\\/]'''
LET BaseName <= lowcase(string=regex_replace(source=Raw, re='''^.*[\\/]''', replace=""))
LET ExeName <= if(condition=IsWindows AND NOT BaseName =~ '''\.[a-z0-9]{1,5}$''',
then=BaseName + ".exe", else=BaseName)
LET EscapedExe <= regex_replace(source=ExeName, re='''([.*+?^${}()|\[\]\\])''', replace='''\$1''')
LET UserName <= lowcase(string=regex_replace(source=Raw, re='''^.*\\|@.*$''', replace=""))
LET FileGlobs <= if(condition=IsWindows,
then=["C:/Users/*/{Downloads,Desktop,Documents,AppData/Local/Temp,AppData/Roaming,AppData/Local/Programs}/**",
"C:/ProgramData/**", "C:/Windows/Temp/**"],
else=if(condition=ThisOS =~ "darwin",
then=["/Users/*/{Downloads,Desktop,Documents}/**", "/private/tmp/**"],
else=["/home/*/**", "/root/**", "/tmp/**", "/var/tmp/**", "/dev/shm/**"]))
LET Procs = SELECT Pid, Name, Exe, CommandLine, Username, CreateTime FROM pslist()
LET DiskFiles = SELECT OSPath, Name, Size, Mtime FROM glob(globs=FileGlobs, nosymlink=TRUE)
WHERE NOT IsDir AND Size > 0 AND Size < 104857600
LET ProcHit(P) = SELECT "Running process" AS Source, CreateTime AS Time, Username AS User, Name AS Process,
Exe AS Match, format(format="pid %v: %v", args=[Pid, CommandLine]) AS Detail, Exe AS Path
FROM P
LET ProcHashHits = SELECT * FROM ProcHit(P={
SELECT * FROM Procs WHERE Exe AND lowcase(string=get(item=hash(path=Exe), field=HashType)) = Clean })
LET DiskHashHits = SELECT "File on disk" AS Source, Mtime AS Time, "" AS User, "" AS Process,
OSPath.Basename AS Match, format(format="%v bytes, %v %v", args=[Size, HashType, Clean]) AS Detail,
OSPath AS Path
FROM DiskFiles
WHERE lowcase(string=get(item=hash(path=OSPath), field=HashType)) = Clean
LET HashHits = SELECT * FROM chain(a=ProcHashHits, b=DiskHashHits, async=TRUE)
LET PrefetchHits = SELECT * FROM if(condition=IsWindows, then={
SELECT "Executed (Prefetch)" AS Source, Mtime AS Time, "" AS User, ExeName AS Process,
Name AS Match, "Prefetch file modified when the program last ran" AS Detail, OSPath AS Path
FROM glob(globs="C:/Windows/Prefetch/*.pf")
WHERE Name =~ ("(?i)^" + EscapedExe + "-[0-9a-f]{8}\\.pf$")
})
LET ExactPathHits = SELECT * FROM if(condition=IsPath, then={
SELECT "File exists" AS Source, Mtime AS Time, "" AS User, "" AS Process,
OSPath.Basename AS Match, format(format="%v bytes", args=Size) AS Detail, OSPath AS Path
FROM glob(globs=regex_replace(source=Raw, re='''\\''', replace="/"), nosymlink=TRUE)
LIMIT 50
})
LET NameOnDiskHits = SELECT "File on disk" AS Source, Mtime AS Time, "" AS User, "" AS Process,
Name AS Match, format(format="%v bytes", args=Size) AS Detail, OSPath AS Path
FROM DiskFiles
WHERE lowcase(string=Name) = BaseName
LET ProcNameHits = SELECT * FROM ProcHit(P={
SELECT * FROM Procs WHERE lowcase(string=Name) = BaseName OR lowcase(string=Name) = ExeName })
LET FileHits = SELECT * FROM chain(
a=ExactPathHits, b=NameOnDiskHits, c=ProcNameHits, d=PrefetchHits, async=TRUE)
LET SysmonProcHits = SELECT * FROM if(condition=IsWindows AND IncludeEventLogs AND stat(filename=SysmonLog).Size, then={
SELECT "Sysmon process start" AS Source, System.TimeCreated.SystemTime AS Time,
EventData.User AS User, EventData.Image AS Process, EventData.Image AS Match,
EventData.CommandLine AS Detail, SysmonLog AS Path
FROM parse_evtx(filename=SysmonLog)
WHERE System.EventID.Value = 1
AND System.TimeCreated.SystemTime > Since.Unix
AND EventData.Image =~ ("(?i)[\\\\/]" + EscapedExe + "$")
})
LET ProcessHits = SELECT * FROM chain(a=ProcNameHits, b=PrefetchHits, c=SysmonProcHits, async=TRUE)
LET SecurityLog = "C:/Windows/System32/winevt/Logs/Security.evtx"
LET UserProcHits = SELECT * FROM ProcHit(P={
SELECT * FROM Procs WHERE Exe AND lowcase(string=regex_replace(source=Username, re='''^.*\\''', replace="")) = UserName })
LET ProfileHits = SELECT "User profile" AS Source, Mtime AS Time, Name AS User, "" AS Process,
Name AS Match, "Profile folder exists" AS Detail, OSPath AS Path
FROM glob(globs=HomeGlobs)
WHERE IsDir AND lowcase(string=Name) = UserName
LET LogonHits = SELECT * FROM if(condition=IsWindows AND IncludeEventLogs AND stat(filename=SecurityLog).Size, then={
SELECT if(condition=System.EventID.Value = 4624, then="Logon", else="Failed logon") AS Source,
System.TimeCreated.SystemTime AS Time,
format(format="%v\\%v", args=[EventData.TargetDomainName, EventData.TargetUserName]) AS User,
EventData.ProcessName AS Process,
EventData.TargetUserName AS Match,
format(format="logon type %v from %v (%v)",
args=[EventData.LogonType, EventData.IpAddress, EventData.WorkstationName]) AS Detail,
SecurityLog AS Path
FROM parse_evtx(filename=SecurityLog)
WHERE System.EventID.Value in (4624, 4625)
AND System.TimeCreated.SystemTime > Since.Unix
AND lowcase(string=EventData.TargetUserName) = UserName
})
LET UserHits = SELECT * FROM chain(a=UserProcHits, b=ProfileHits, c=LogonHits, async=TRUE)
SELECT * FROM if(condition=NOT Raw, then={
SELECT log(message="No indicator given") AS Source FROM scope() WHERE FALSE
}, else={
SELECT * FROM if(condition=Kind = "hash", then=HashHits,
else={ SELECT * FROM if(condition=Kind = "file", then=FileHits,
else={ SELECT * FROM if(condition=Kind = "process", then=ProcessHits,
else={ SELECT * FROM if(condition=Kind = "user", then=UserHits,
else=WebHits) }) }) })
})