T09 · Insecure Skill Coding Practices
- Location
generate.ps1:37- Finding
PowerShell Command Injection Through Invoke-Expression
- Content
View full analysis
Vulnerability Details
File Location:
generate.ps1, lines 37-46 and execution sinks at lines 64 and 73
Vulnerability Type: PowerShell command injection
Risk Level: HighVulnerable Code
powershell # Build the Python command $pythonCmd = "python main.py --topic `"$Topic`" --style `"$Style`" --count $Count" if (-not [string]::IsNullOrWhiteSpace($Audience)) { $pythonCmd += " --audience `"$Audience`"" } if (-not [string]::IsNullOrWhiteSpace($Trending)) { $pythonCmd += " --trending `"$Trending`"" } # Execute the Python script Invoke-Expression $pythonCmd # Export results if ($Export) { if ([string]::IsNullOrWhiteSpace($ExportPath)) { $ExportPath = "douyin_titles_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt" } # Capture the output and save it $output = Invoke-Expression $pythonCmd $output | Out-File -FilePath $ExportPath -Encoding UTF8 }Technical Analysis
The script concatenates the user-controlled
$Topic,$Style,$Audience, and$Trendingparameters into a string intended to represent a command. It then passes that string toInvoke-Expression, which reparses the entire value as PowerShell source code.Escaped double quotes in the constructed string do not establish a security boundary. A crafted parameter can terminate the intended quoted argument and introduce PowerShell separators, expressions, or additional commands. Consequently, data supplied as a title-generation parameter can be interpreted as executable PowerShell syntax.
The same unsafe command is evaluated again when export mode is enabled, potentially executing an injected payload twice.
Attack Path
- An attacker prepares a malicious value for one of the string parameters, such as
Topic,Style,Audience, orTrending. - The victim or an automated wrapper invokes
generate.ps1with that attacker-controlled value. - The script interpolates the value into
$pythonCmdwithout preserving it as a discrete proce ...[truncated 887 chars]
- An attacker prepares a malicious value for one of the string parameters, such as
- Remediation
View remediation
Remediation Suggestions
Remove
Invoke-Expressionand invoke Python with a PowerShell argument array so every user value remains a single process argument:powershell $pythonArgs = @( "main.py", "--topic", $Topic, "--style", $Style, "--count", $Count ) if (-not [string]::IsNullOrWhiteSpace($Audience)) { $pythonArgs += @("--audience", $Audience) } if (-not [string]::IsNullOrWhiteSpace($Trending)) { $pythonArgs += @("--trending", $Trending) } $output = & python @pythonArgs if ($LASTEXITCODE -ne 0) { throw "The title generator failed with exit code $LASTEXITCODE." } $output if ($Export) { if ([string]::IsNullOrWhiteSpace($ExportPath)) { $ExportPath = "douyin_titles_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt" } $output | Out-File -LiteralPath $ExportPath -Encoding UTF8 }Additional hardening measures:
- Execute the generator only once and reuse the captured output for display and export.
- Use
-LiteralPathwhen writing to a caller-supplied export path. - Validate
Countagainst a reasonable positive upper bound to prevent excessive resource consumption. - Restrict
Style,Audience, andTrendingto documented allowlists when arbitrary values are unnecessary. - Resolve
main.pyrelative to$PSScriptRootso execution does not depend on the current working directory. - Add regression tests containing quotes, semicolons, subexpressions, and newline characters to verify that parameters are treated only as data.
