Business Logic Vulnerabilities and Mitigation

Business Logic Vulnerabilities and Mitigation

1. Vulnerability Description
A Business Logic Vulnerability refers to design flaws in an application's business processing flow, where attackers achieve malicious objectives by abusing normal functionalities rather than exploiting technical vulnerabilities (such as SQL injection). These vulnerabilities are often difficult to detect with automated tools and require a deep understanding of the business context. Common examples include: order amount tampering, CAPTCHA bypass, race condition abuse, etc.

2. Vulnerability Principle
The core issue of business logic vulnerabilities is that the program fails to impose reasonable business rule constraints on user operations. For example:

  • Trusting data passed from the client (e.g., price, inventory);
  • Failing to validate the integrity of multi-step operations (e.g., skipping the payment step to directly generate an order);
  • Improper handling of concurrent requests (e.g., repeatedly claiming coupons).

3. Typical Scenarios and Attack Techniques
Scenario 1: Order Amount Tampering

  • Step 1: The user selects a product and proceeds to the checkout page, where the frontend displays a total price of 100 yuan.
  • Step 2: The user intercepts the payment request using a packet capture tool (e.g., Burp Suite) and modifies the amount to 1 yuan.
  • Step 3: The server does not re-validate the amount and completes the transaction with the modified value.

Scenario 2: CAPTCHA Bypass

  • Step 1: The user requests an SMS verification code, and the server generates and stores the code (e.g., 1234).
  • Step 2: The user intentionally enters an incorrect verification code but intercepts the request and changes the status code in the response packet to "success" (e.g., HTTP 200).
  • Step 3: The frontend relies solely on the response status to determine the verification result, leading to a bypass.

Scenario 3: Race Condition Vulnerability

  • Step 1: The user simultaneously initiates multiple "balance withdrawal" requests, with the server checking if the balance is sufficient before each withdrawal.
  • Step 2: The server processes the requests without locking, allowing all parallel validations to pass and resulting in an over-withdrawal.

4. Mitigation Strategies
Principle: Trust no user input; critical logic must be enforced server-side.

  • Data Integrity Validation:
    • Critical parameters such as order amount and quantity should be calculated server-side, not relied upon from the frontend.
    • Use digital signatures or tokens (e.g., HMAC) to ensure parameters are not tampered with.
  • Process State Control:
    • Use server-side sessions to track multi-step processes (e.g., order → payment → completion), preventing steps from being skipped or reverted.
  • Preventing Concurrent Attacks:
    • Apply distributed locks (e.g., Redis/MySQL locks) to critical operations (e.g., payment, inventory deduction) to ensure atomicity.
  • CAPTCHA Security:
    • The server should directly return explicit results after CAPTCHA validation (e.g., {"code": 400, "msg": "Incorrect CAPTCHA"}), rather than letting the frontend interpret the response status.

5. Case Study
Taking "Repeated Coupon Usage" as an example:

  • Vulnerable Code (pseudocode):
    def use_coupon(user_id, coupon_id):  
        coupon = db.query("SELECT * FROM coupons WHERE id = ?", coupon_id)  
        if coupon.is_used:  
            return "Coupon already used"  
        # No lock applied; concurrent requests may pass validation simultaneously  
        db.update("UPDATE coupons SET is_used=1 WHERE id=?", coupon_id)  
        grant_credit(user_id, coupon.amount)  # Grant benefits  
    
  • Fixed Solution:
    def use_coupon(user_id, coupon_id):  
        with redis.lock(f"lock_{coupon_id}", timeout=5):  # Apply distributed lock  
            coupon = db.query("SELECT * FROM coupons WHERE id = ?", coupon_id)  
            if coupon.is_used:  
                return "Coupon already used"  
            db.update("UPDATE coupons SET is_used=1 WHERE id=?", coupon_id)  
            grant_credit(user_id, coupon.amount)  
    

6. Summary
The key to mitigating business logic vulnerabilities lies in: Treating business rules as security rules, ensuring processes cannot be tampered with or bypassed through server-side enforcement, state tracking, resource locking, and other mechanisms.