{"id":594,"cve":"CVE-2018-6012","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2018-6012","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":"2018-11-01","owasp":"A03 Injection","odc":"Function","cdc":"Incorrect Functionality","category":"Input Validation and Sanitization","subcategory":"Command Injection","scope":"Remote","impact":"Arbitrary Code Execution","summary":"Python code injection via 'Add new weather data source' upload function in Green Electronics RainMachine Mini-8 (2nd generation)","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2018-6012\/","description":"The 'Weather Service' feature of the Green Electronics RainMachine Mini-8 (2nd generation) allows an attacker to inject arbitrary Python code via the 'Add new weather data source' upload function.","solution":"Update RainMachine Mini-8 (2nd generation) firmware to the latest version with the security patch.","affected_component":"Python","fixed_by_upgrading":true,"vulnerable_code":"import json\n\n# Function to handle the upload of a new weather data source\ndef add_new_weather_data_source(data_source_json):\n    # Directly executing the input without validation\n    data_source = json.loads(data_source_json)\n    \n    # Arbitrary code execution vulnerability\n    exec(data_source.get('python_code', ''))\n\n    print(\"Weather data source added successfully.\")\n    # Here you would add logic to save the data source\n\n# Example usage\ndata_source_json = '{\"python_code\": \"__import__(\\'os\\').system(\\'ls\\')\"}'\nadd_new_weather_data_source(data_source_json)","patched_code":"import os\nimport json\nimport re\n\n# Function to validate and sanitize the uploaded weather data source\ndef validate_weather_data_source(data_source):\n    # Allow only specific keys and types to prevent arbitrary code execution\n    allowed_keys = {'name', 'url', 'api_key'}\n    if not all(key in allowed_keys for key in data_source.keys()):\n        raise ValueError(\"Invalid data source keys.\")\n\n    # Example of validating URL format\n    url_pattern = re.compile(r'^(http|https):\/\/[^\\s\/$.?#].[^\\s]*$')\n    if not url_pattern.match(data_source['url']):\n        raise ValueError(\"Invalid URL format.\")\n\n    # Ensure no executable code is present in the input\n    for key, value in data_source.items():\n        if isinstance(value, str):\n            if re.search(r'[\\(\\)\\{\\}\\[\\];]', value):\n                raise ValueError(\"Invalid characters in input.\")\n\n    return True\n\n# Function to handle the upload of a new weather data source\ndef add_new_weather_data_source(data_source_json):\n    try:\n        data_source = json.loads(data_source_json)\n        if validate_weather_data_source(data_source):\n            # Proceed with adding the validated data source\n            print(\"Weather data source added successfully.\")\n            # Here you would add logic to save the data source\n    except ValueError as e:\n        print(f\"Error: {e}\")\n\n# Example usage\ndata_source_json = '{\"","payload_needed":false,"payload":null,"payload_status":"none","published_fix":[],"references":null}