| ID | Technique | Tactic |
|---|---|---|
| T1059.004 | Unix Shell | Execution |
Detection: Linux Auditd Shell from Web or Application Directory
Description
This analytic looks for a shell started with the "-c" argument for command line execution while its working directory is a web application directory. It joins auditd SYSCALL, EXECVE, and CWD records from the same event. A match may show that a web application ran a command, check the parent process and command before treating it as suspicious.
Search
1`linux_auditd`
2(
3 (
4 type=SYSCALL
5 key="process_creation"
6 success="yes"
7 exe IN (
8 "*/ash",
9 "*/bash",
10 "*/busybox",
11 "*/csh",
12 "*/dash",
13 "*/fish",
14 "*/ion",
15 "*/ksh",
16 "*/mksh",
17 "*/sh",
18 "*/tcsh",
19 "*/zsh"
20 )
21 )
22 OR
23 (
24 type=EXECVE
25 a0 IN (
26 "ash", "*/ash",
27 "bash", "*/bash",
28 "csh", "*/csh",
29 "dash", "*/dash",
30 "fish", "*/fish",
31 "ion", "*/ion",
32 "ksh", "*/ksh",
33 "mksh", "*/mksh",
34 "sh", "*/sh",
35 "tcsh", "*/tcsh",
36 "zsh", "*/zsh",
37 "busybox", "*/busybox"
38 )
39 )
40 OR
41 (
42 type=CWD
43 cwd IN (
44 "/app",
45 "/app/*",
46 "/dev/shm",
47 "/dev/shm/*",
48 "/home/*/*",
49 "/home/*/*/*",
50 "/opt/*",
51 "/srv/http",
52 "/srv/http/*",
53 "/srv/www",
54 "/srv/www/*",
55 "/usr/share/nginx/html",
56 "/usr/share/nginx/html/*",
57 "/var/lib/tomcat*/webapps",
58 "/var/lib/tomcat*/webapps/*",
59 "/var/www",
60 "/var/www/*"
61 )
62 )
63)
64
65
66| rex field=msg "audit\\((?<audit_event_id>[^)]+)\\)"
67
68
69| eval shell_exec=if(
70 type="SYSCALL"
71 AND key="process_creation"
72 AND success="yes"
73 AND match(
74 lower(exe),
75 "(^
76|/)(ash
77|bash
78|busybox
79|csh
80|dash
81|fish
82|ion
83|ksh
84|mksh
85|sh
86|tcsh
87|zsh)$"
88 ),
89 1,
90 0
91)
92
93
94| eval argv0=if(type="EXECVE",lower(a0),null())
95
96| eval argv1=if(type="EXECVE",a1,null())
97
98
99| eval shell_argv=if(
100 type="EXECVE"
101 AND (
102 match(
103 argv0,
104 "(^
105|/)(ash
106|bash
107|csh
108|dash
109|fish
110|ion
111|ksh
112|mksh
113|sh
114|tcsh
115|zsh)$"
116 )
117 OR (
118 match(argv0,"(^
119|/)busybox$")
120 AND match(lower(argv1),"^(ash
121|sh)$")
122 )
123 ),
124 1,
125 0
126)
127
128
129| eval exec_args=if(
130 type="EXECVE",
131 mvappend(a1,a2,a3,a4,a5,a6,a7,a8),
132 null()
133)
134
135
136| eval c_option_index=if(
137 shell_argv=1,
138 mvfind(exec_args,"(?i)^-[a-z]*c[a-z]*$"),
139 null()
140)
141
142
143| eval fish_command_index=if(
144 shell_argv=1 AND match(argv0,"(^
145|/)fish$"),
146 mvfind(exec_args,"(?i)^--command(?:=.*)?$"),
147 null()
148)
149
150
151| eval shell_command=if(
152 shell_argv=1
153 AND (
154 c_option_index>=0
155 OR fish_command_index>=0
156 ),
157 1,
158 0
159)
160
161
162| eval command_arg=case(
163 c_option_index>=0,
164 mvindex(exec_args,c_option_index+1),
165
166 fish_command_index>=0
167 AND match(mvindex(exec_args,fish_command_index),"(?i)^--command="),
168 replace(mvindex(exec_args,fish_command_index),"(?i)^--command=",""),
169
170 fish_command_index>=0,
171 mvindex(exec_args,fish_command_index+1)
172)
173
174
175| eval working_directory=if(type="CWD",cwd,null())
176
177| eval shell_path=if(type="SYSCALL",exe,null())
178
179| eval shell_pid=if(type="SYSCALL",pid,null())
180
181| eval parent_pid=if(type="SYSCALL",ppid,null())
182
183| eval syscall_name=if(type="SYSCALL",SYSCALL,null())
184
185| eval shell_argv0=if(type="EXECVE",a0,null())
186
187
188| stats min(_time) as firstTime
189 max(_time) as lastTime
190 max(shell_exec) as shell_exec
191 max(shell_command) as shell_command
192 values(working_directory) as working_directory
193 values(shell_path) as shell_path
194 values(shell_argv0) as shell_argv0
195 values(shell_pid) as pid
196 values(parent_pid) as ppid
197 values(syscall_name) as syscall_name
198 values(command_arg) as command_arg
199 by host audit_event_id
200
201
202| where shell_exec=1
203 AND shell_command=1
204 AND isnotnull(mvfind(
205 working_directory,
206 "(?i)^(/app
207|/dev/shm
208|/home/[^/]+/[^/]+
209|/opt/[^/]+
210|/srv/http
211|/srv/www
212|/usr/share/nginx/html
213|/var/lib/tomcat[^/]+/webapps
214|/var/www)(/.*)?$"
215 ))
216
217
218| where NOT (
219 (
220 isnotnull(mvfind(working_directory,"(?i)^/opt/nessus_agent/.+$"))
221 AND (
222 isnotnull(mvfind(shell_path,"(?i)(^
223|/)(sh
224|dash)$"))
225 OR isnotnull(mvfind(shell_argv0,"(?i)^/bin/sh$"))
226 )
227 )
228 OR
229 (
230 isnotnull(mvfind(working_directory,"(?i)^/home/[^/]+/jenkins$"))
231 AND (
232 isnotnull(mvfind(shell_path,"(?i)(^
233|/)(bash
234|rbash)$"))
235 OR isnotnull(mvfind(shell_argv0,"(?i)^/(bin
236|usr/bin)/bash$"))
237 )
238 )
239)
240
241
242| rename host as dest
243
244| `security_content_ctime(firstTime)`
245
246| `security_content_ctime(lastTime)`
247
248| `linux_auditd_shell_from_web_or_application_directory_filter`
Data Source
| Name | Platform | Sourcetype | Source |
|---|---|---|---|
| Linux Auditd Cwd | 'auditd' |
'auditd' |
|
| Linux Auditd Execve | 'auditd' |
'auditd' |
|
| Linux Auditd Syscall | 'auditd' |
'auditd' |
Macros Used
| Name | Value |
|---|---|
| security_content_ctime | convert timeformat="%Y-%m-%dT%H:%M:%S" ctime($field$) |
| linux_auditd_shell_from_web_or_application_directory_filter | search * |
linux_auditd_shell_from_web_or_application_directory_filter is an empty macro by default. It allows the user to filter out any results (false positives) without editing the SPL.
Annotations
CVE
Default Configuration
This detection is configured by default in Splunk Enterprise Security to run with the following settings:
| Setting | Value |
|---|---|
| Disabled | true |
| Cron Schedule | 0 * * * * |
| Earliest Time | -70m@m |
| Latest Time | -10m@m |
| Schedule Window | auto |
| Creates Finding (Notable) | No |
| Creates Intermediate Finding (Risk Event) | Yes |
Implementation
Install and enable auditd on Linux web servers and load the Neo23x0 audit.rules (https://github.com/Neo23x0/auditd/blob/master/audit.rules) ruleset, including the process_creation execve and execveat rules. Forward /var/log/audit/audit.log with the Splunk Universal Forwarder and install Splunk Add-on for Unix and Linux (https://splunkbase.splunk.com/app/833). Configure the existing linux_auditd macro in Splunk for the local index and sourcetype. Confirm that SYSCALL, EXECVE, and CWD records from one audit event are retained with the same msg identifier and that key, success, exe, pid, ppid, a1, cwd, and SYSCALL are extracted. Update the web-directory expression for actual application paths and tune approved maintenance commands. Correlate this finding with HTTP activity, application logs, and service ownership.
Known False Positives
Deployments, build steps, application jobs, and administrative scripts may run shells from a web application directory. Review the command argument, parent PID, service ownership, and nearby HTTP traffic. Hex-encoded EXECVE arguments may need decoding during investigation.
Associated Analytic Story
Intermediate Findings
| Message | Entity Field | Entity Type | Risk Score |
|---|---|---|---|
| Shell [$shell_path$] ran a command string while working in [$working_directory$] on [$dest$]. | dest | system | 20 |
Threat Objects
| Field | Type |
|---|---|
| command_arg | process |
| shell_path | process_name |
References
Detection Testing
| Test Type | Status | Dataset | Source | Sourcetype |
|---|---|---|---|---|
| Validation | ✅ Passing | N/A | N/A | N/A |
| Unit | ✅ Passing | Dataset | /var/log/audit/audit.log |
auditd |
| Integration | ✅ Passing | Dataset | /var/log/audit/audit.log |
auditd |
Replay any dataset to Splunk Enterprise by using our replay.py tool or the UI.
Alternatively you can replay a dataset into a Splunk Attack Range
Source: GitHub |
Version: 1