Eval Injection
Overview
Before exploiting our finding, we need to understand code injection, specifically eval injection in JavaScript.
Code Injection Fundamentals
What is Code Injection?
Attacker uses user input to modify how an application executes by injecting code that becomes part of the application.
SQL Injection
SQL query
Command Injection
System command
Code Injection
Application code
Key Points
Only affects interpreted/scripted languages (JavaScript, Python, PHP)
Code is dynamically executed at runtime
XSS is a form of code injection (client-side)
Backend code injection can lead to RCE
Dangerous Functions by Language
Code Injection Functions (Highlighted)
eval
eval
eval
Function
exec
exec
setInterval
setTimeout
constructor.constructor
Command Injection Functions
child_process.exec
subprocess.open
proc_open
execlp
System.Diagnostics.Process.Start
Runtime.getRuntime().exec
child_process.spawn
subprocess.run
popen
execvp
os.system
shell_exec
ShellExecute
os.popen
passthru
system
system
popen
Eval Injection Basics
Functions That Evaluate Strings as Code
Basic Vulnerable Example
Exploitation
Payload:
Resulting Code:
Executed as:
β Code injection achieved!
Analyzing validateString
How It's Called
The eval Call
Condition Analysis
Not admin
throw({message: 'Invalid input', statusCode: 403})
β None
admin
throw({message: 'The input "${text}" contains...
β
${text} included
Key Insight
Must have role === "admin" to reach code injection!
When role is admin, our text input is placed directly into the eval string via string interpolation (${text}).
String Interpolation in JavaScript
Backtick Strings (Template Literals)
Vulnerability
When user input is interpolated into a string that gets eval()'d:
If text contains code-breaking characters, injection occurs!
Testing the Vulnerability
Setup
Run app in debug mode
Add breakpoint on line 20 (generateQR)
Get auth token
Get Token
Send Payload
Modify Role in Debugger
Breakpoint hits
In VARIABLES pane, change
rolefromusertoadminPress Continue
Response
β We reached the admin branch and got the verbose error message!
What the Payload Does
Payload: ";// `
";// `"
Close the string
`
Close the template literal
;
End the statement
//
Comment out rest of line
Resulting eval String
After injection, becomes:
The Filter Problem
validateString Checks
Blocked characters: ', ", `, ;
Challenge
Our injection payload requires these characters to:
Break out of strings (
"or')End statements (
;)Comment out code (
//)
BUT: If input contains these characters, validateString returns false BEFORE eval is called...
Wait, What?
Actually, looking at the logic:
If input has bad chars β
eval(onError)is calledonErrorcontains our input via${text}So we want to trigger the validation failure!
β The filter helps us reach eval!
Next Steps
Get admin role - How to authenticate as admin?
Bypass character filter - Find payload without
['";]`Achieve code execution - Execute arbitrary JS
Escalate to RCE - Run system commands
Key Takeaways
eval() is dangerous - Any user input reaching it = vulnerability
Template literals -
${var}interpolation can enable injectionFilters can backfire - Triggering validation = reaching eval
Role matters - Must be admin to get verbose error with our input
Debug to verify - Modify variables to test different code paths
Last updated