{"id":511,"cve":"CVE-2018-16168","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2018-16168","cwe":"CWE-94 Improper Control of Generation of Code ('Code Injection')","cwe_number":94,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/94","cvss_score":9.8,"published":"2019-01-09","owasp":"A03 Injection","odc":"Function","cdc":"Incorrect Functionality","category":"Input Validation and Sanitization","subcategory":"Command Injection","scope":"Remote","impact":"Arbitrary Code Execution","summary":"Remote Python code injection in LogonTracer 1.2.0 and earlier","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2018-16168\/","description":"LogonTracer 1.2.0 and earlier allows remote attackers to conduct Python code injection attacks via unspecified vectors.","solution":"Update LogonTracer to version 1.2.1 or higher.","affected_component":"Python","fixed_by_upgrading":true,"vulnerable_code":"def execute_user_input(user_input):\n    # Vulnerable code that directly evaluates user input\n    result = eval(user_input)\n    return result\n\n# Example usage\nuser_input = \"__import__('os').system('ls')\"  # This input could be provided by an attacker\nresult = execute_user_input(user_input)\nprint(result)","patched_code":"import ast\n\ndef safe_eval(expression):\n    # Only allow specific nodes in the expression to prevent arbitrary code execution\n    allowed_nodes = {\n        ast.Expression,\n        ast.BinOp,\n        ast.UnaryOp,\n        ast.Num,\n        ast.Str,\n        ast.Name,\n        ast.Load,\n        ast.Add,\n        ast.Sub,\n        ast.Mult,\n        ast.Div,\n        ast.Mod,\n        ast.Pow,\n        ast.BitXor,\n        ast.USub,\n        ast.UAdd,\n        ast.Compare,\n        ast.Eq,\n        ast.NotEq,\n        ast.Lt,\n        ast.LtE,\n        ast.Gt,\n        ast.GtE,\n        ast.BoolOp,\n        ast.And,\n        ast.Or,\n    }\n\n    def _check_node(node):\n        if type(node) not in allowed_nodes:\n            raise ValueError(\"Disallowed expression\")\n\n    # Parse the expression and check for disallowed nodes\n    tree = ast.parse(expression, mode='eval')\n    for node in ast.walk(tree):\n        _check_node(node)\n\n    # Safely evaluate the expression\n    return eval(compile(tree, filename='', mode='eval'))\n\n# Example usage\nuser_input = \"2 + 2\"  # This should be a safe mathematical expression\nresult = safe_eval(user_input)\nprint(result)  # Output: 4","payload_needed":false,"payload":null,"payload_status":"none","published_fix":[],"references":null}