File size: 1,891 Bytes
6960b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[
    {
        "id": "vuln_001",
        "type": "SQL Injection",
        "language": "python",
        "vulnerable_code": "def get_user(username):\n    query = \"SELECT * FROM users WHERE username = '\" + username + \"'\"\n    cursor.execute(query)\n    return cursor.fetchall()",
        "secure_code": "def get_user(username):\n    query = \"SELECT * FROM users WHERE username = %s\"\n    cursor.execute(query, (username,))\n    return cursor.fetchall()",
        "explanation": "The vulnerable code directly concatenates the user input into the SQL query string, allowing an attacker to manipulate the query (SQL Injection). The secure code uses parameterized queries (or prepared statements), which treat user input as data rather than executable code."
    },
    {
        "id": "vuln_002",
        "type": "XSS",
        "language": "python",
        "vulnerable_code": "@app.route('/hello')\ndef hello():\n    name = request.args.get('name')\n    return 'Hello, ' + name",
        "secure_code": "from flask import escape\n@app.route('/hello')\ndef hello():\n    name = request.args.get('name')\n    return 'Hello, ' + str(escape(name))",
        "explanation": "The vulnerable code reflects user input directly to the browser, allowing Cross-Site Scripting (XSS). The secure code escapes the input, converting special characters into HTML entities."
    },
    {
        "id": "vuln_003",
        "type": "Command Injection",
        "language": "python",
        "vulnerable_code": "import os\ndef ping_host(host):\n    os.system('ping ' + host)",
        "secure_code": "import subprocess\ndef ping_host(host):\n    subprocess.run(['ping', host], check=True)",
        "explanation": "Using os.system or concatenation in shell commands allows command injection. Using subprocess.run with a list of arguments facilitates safer execution by avoiding shell interpretation."
    }
]