Mastering the Art of Logic Flaws: Unraveling Cyber Mysteries !!!

RootKid 🤖 🤖 🤖
Nest InfoSec
Published in
12 min readJul 26, 2023

Hey there, awesome people! How’s life treating you?

Today, we’ve got something exciting in store for you. We’re going to explore the intriguing world of Logic Flaw Vulnerabilities! Don’t worry if you’re not a tech guru; we’ll break it down in simple terms. We’ll discuss what Logic Flaws are, what causes them, and how to get into the mindset of a savvy flaw hunter. Plus, to spice things up, we’ll dig into some real-life examples of logic flaw vulnerabilities. So, sit back, relax, and let’s embark on this adventure together! 🕵️‍♂️💡

What is Logic Flaws ?

Logic flaws in application security refer to vulnerabilities that occur due to incorrect or flawed logic in the design or implementation of an application. These flaws can allow attackers to exploit the application’s behavior and gain unauthorized access or perform malicious actions on the application.

Think of it this way: Imagine you have a house with a front door and a back door, and you put a lock on each to keep intruders out. But, there’s a problem. The lock on the back door can be opened with any key, not just the right one. So, even if you have a different key, any key can still open it. This flaw in the lock’s design makes it easier for burglars to break in.

In the same way, with apps, logic flaws happen when the people making the app assume things will always be true or that only certain users can do certain things. But sometimes, those assumptions are wrong. Attackers can take advantage of these mistakes to get into the app and access sensitive information or do things they’re not supposed to do.

For example, let’s say there’s an online shopping app that lets you use discount codes to get discounts on your orders. But, because of a logic flaw, the app gives the discount to the whole shopping cart, even if the code is only supposed to apply to specific items. So, an attacker could use a fake discount code and get a big discount on their whole order, which is not fair.

Logic flaws are often challenging to identify because they are not caused by technical vulnerabilities but rather flaws in the underlying design or business logic of the application. They require a careful analysis of the application’s behavior and how different components interact to identify potential vulnerabilities.

Root Causes of Logic Flaws:

  • Incorrect Assumptions: Logic flaws can occur when developers make incorrect assumptions about how certain parts of the application should behave. For example, they might assume that only authorized users can access a certain feature, but they fail to consider other ways someone could bypass those restrictions.
  • Insufficient Validation: When input from users or external sources is not properly validated, logic flaws can arise. This means that the application doesn’t check if the data being provided is valid or expected. Attackers can take advantage of this by inputting unexpected or malicious data that the application can’t handle correctly.
  • Poor Authorization and Access Control: Logic flaws can also occur when the application doesn’t properly enforce who can access certain resources or perform specific actions. For instance, the application might not verify if a user has the necessary permissions to perform a certain task, allowing unauthorized users to gain access to sensitive information or perform actions they shouldn’t be able to.
  • Poor Business Logic Design: Sometimes, logic flaws arise from errors or oversights in the design of the application’s business logic. This refers to how the application handles specific processes or workflows to achieve its intended functionality. If the logic is flawed, attackers can exploit gaps or inconsistencies in the design to manipulate the application’s behaviour.
  • Failure to Consider Unintended Interactions: Logic flaws can occur when developers overlook the potential interactions between different parts of an application. For example, a developer may implement a feature that allows users to upload profile pictures, but fail to consider the possibility of someone uploading a malicious file that can exploit other parts of the application. By not thinking about these unintended interactions, vulnerabilities can be introduced.
  • Improper Error Handling: When errors or exceptions occur during the execution of an application, it’s important to handle them properly. However, if developers don’t handle errors consistently and securely, logic flaws can arise. For instance, an application might display error messages that provide too much information, which could help attackers understand how the application works and exploit its weaknesses.
  • Poor Session Management: Logic flaws can also stem from poor session management. Sessions are used to keep track of a user’s interactions with the application over a period of time. If sessions are not properly managed or protected, attackers may be able to hijack or manipulate them. For example, if an application relies solely on the session ID for authentication without verifying other factors, an attacker could steal someone else’s session ID and gain unauthorized access.

Mindset required to find Logic Flaws:

  • Critical Thinking: Approach the application with a skeptical and questioning mindset. Don’t take anything for granted and be open to exploring various scenarios.
  • Creativity: Think outside the box to envision how attackers might exploit unconventional paths or misuse application features.
  • Attention to Detail: Pay close attention to the application’s behavior, interactions, and any inconsistencies that could indicate potential logic flaws.
  • Methodical Approach: Testing for logic flaws needs a step-by-step and organized approach. First, divide the app into smaller parts and test each one separately. Then, look at all the different ways users can interact with it and the data they input. Also, consider how the app behaves under different conditions. This methodical approach helps you find more logic flaws and makes sure you test the app thoroughly.
  • Be Persistent: When looking for logic flaws, it’s important to be persistent. You might not find all the weaknesses right away, and that’s normal. Keep trying different methods, situations, and data inputs. Treat it like solving a challenging puzzle that you won’t give up on. Logic flaws can be tricky to find, but with persistent efforts, you increase your chances of discovering hidden vulnerabilities and making the app more secure.

Hunt for Logic Flaws:

  • Understand the Application: Before you begin testing, make sure you really understand the app you’re working with. Learn how it’s supposed to work, how different parts connect, and what it’s expected to do. This understanding will help you identify potential gaps or inconsistencies in the application’s logic.
  • Identify Assumptions: Logic flaws happen when developers assume things about how the app should work, but those assumptions are wrong. For example, assumptions about user permissions, input validation, or workflow sequences. By questioning these assumptions, you can uncover potential vulnerabilities.
  • Improper Input Validation: Focus on how the application validates user input. Look for places where the application accepts input without proper validation or sanitization. Test different types of input, including unexpected or malicious values, to see if the application handles them correctly. Improper input validation is a common source of logic flaws.
  • Consider Alternative Scenarios: Go beyond the usual ways people use the app and think like an attacker. Try to find ways to break or trick the app’s logic. Test different sequences of actions, various inputs, or unusual situations that developers might not have thought about. By doing this, you can discover logic flaws that might not show up in regular use of the app. It helps to find vulnerabilities that others might miss.
  • Check Authorization and Access Control: Make sure the app has the right security measures for authorization and access control. Test different user roles or permissions to see if they work correctly. Try to access resources or perform actions that only authorized users should do. If the app doesn’t properly restrict access, it can lead to logic flaws and unauthorized entry.
  • Review Business Logic: Carefully review the application’s business logic, which guides how certain processes or workflows are set up. Look for possible differences, areas where checks are missing, or spots where the logic can be manipulated. Pay attention to how the app manages important tasks and information flow, and identify any potential logic flaws in those parts.
  • Privilege Escalation: Check if a user can exploit the app’s logic to gain higher privileges or access resources they are not authorized for. For instance, examine if a regular user can manipulate a URL or input parameter to execute administrative actions they shouldn’t be able to

Logic Flaws in Real Life: Examples and Analysis

In this section, we’ll look at real-life situations that show logic flaws. It will make it easier for you to understand this concept and see how it happens in practical ways.

Scenario 1:

Imagine there’s an e-commerce website that’s offering a special discount for its new customers. If you’re a first-time customer, you can enjoy a 15% discount on your very first purchase. However, there are a couple of conditions to be eligible for the discount:

  • The total value of items in your shopping cart must be above $200.
  • The discount is only applicable to your very first order on the website.

So, if you’re a new customer and you have items worth more than $200 in your cart, you’ll get a 15% discount on that order.

Now, let’s take a look at some logic flaws commonly found in this kind of e-commerce websites.

Logic Flaw 1 — Getting a 15% discount on cart values below $200:

Attack Flow:

  • Step 1: Register as a new customer on the e-commerce website.
  • Step 2: Add items worth $300 into your cart.
  • Step 3: Apply the discount coupon meant for new customers on the cart, which gives a 15% discount on the first order.
  • Step 4: Observe that the discount coupon is successfully applied, and the total cart value is now reduced to $255 (after the 15% discount).
  • Step 5: Remove some items from your cart, which brings the total cart value below $200. For example, you remove $70 worth of items.
  • Step 6: Despite the cart value being below $200 now, the discount coupon remains valid, and you can proceed to checkout with the discounted price.

Vulnerable Parameter and Code Snippet:

In the application code, there might be a validation check for the discount coupon that only ensures the cart value is above $200 when the coupon is applied, but it doesn’t check if the cart value remains above $200 after applying the discount. The vulnerable code could look like this (Example code in Python):

def apply_discount_coupon(cart_value, coupon_code):
if cart_value >= 200:
if is_valid_coupon(coupon_code):
return calculate_discounted_price(cart_value, 0.15) # 15% discount applied
else:
return cart_value
else:
return cart_value
def is_valid_coupon(coupon_code):
# Code to check if the coupon code is valid for new customers
# (omitted for simplicity)
return True
def calculate_discounted_price(price, discount_rate):
return price - (price * discount_rate)

The problem lies in the apply_discount_coupon function, which only checks if the initial cart value is above $200. Once the coupon is applied and the price reduced, it doesn't re-validate whether the new discounted price is still above $200.

Logic Flaw 2 — Using a one-time-use coupon code multiple times:

Attack Flow:

  • Step 1: Register again as a new customer on the web application.
  • Step 2: Add items worth $300 into your cart.
  • Step 3: Apply the discount coupon meant for new customers on the cart and complete the checkout process successfully.
  • Step 4: Add another set of items worth $300 into your cart.
  • Step 5: Attempt to use the same coupon code (meant for one-time use by new customers) on this second order.
  • Step 6: If the application accepts the coupon code and applies the discount, it indicates a logic flaw vulnerability.

Vulnerable Parameter and Code Snippet:

In this case, the vulnerability might be due to the application not properly tracking the usage of one-time use coupon codes. The code could look like this (Example code in Python):

def apply_discount_coupon(cart_value, coupon_code, is_new_customer):
if cart_value >= 200:
if is_valid_coupon(coupon_code, is_new_customer):
return calculate_discounted_price(cart_value, 0.15) # 15% discount applied
else:
return cart_value
else:
return cart_value
def is_valid_coupon(coupon_code, is_new_customer):
# Code to check if the coupon code is valid for new customers
# and if it has already been used (omitted for simplicity)
return is_new_customer and not has_coupon_been_used(coupon_code)
def has_coupon_been_used(coupon_code):
# Code to check if the coupon code has already been used (omitted for simplicity)
return False
def calculate_discounted_price(price, discount_rate):
return price - (price * discount_rate)

The issue here is that the has_coupon_been_used function might not properly track the usage of the coupon code for new customers, allowing it to be reused multiple times even though it's meant for one-time use.

Scenario 2:

There is a profile page on a website that displays all your personal information, such as email ID, mobile number, username, first and last name, and your avatar. You can also update this information on the same page. To access this page, a specific ID parameter is used in the URL, which can be guessed by someone, for example, 76840. However, if you try to change this ID to any other number, like 76839, the website will display an error message saying “You are not authorized for this action.”

Now, let’s take a look at some logic flaws commonly found in this kind of Scenario.

Logic Flaw 1 — Unrestricted Access: To View and Edit any User data on the Application:

Attack Flow:

  • Step 1: Access the profile page using a guessable ID in the URL, for example, “https://example.com/profile?id=76840".
  • Step 2: Observe that if you try to change the ID parameter in the URL to any other ID, for example, “https://example.com/profile?id=76839", the application responds with an error message saying “You are not authorized for this action”. This message indicates that the application is trying to prevent unauthorized access to other users’ profiles.
  • Step 3: Use Burp Suite Proxy Tool to intercept and analyze the requests and responses between your browser and the application server.
  • Step 4: While analyzing the responses, you find that the response is in a guessable form, meaning it provides a clue or indication whether you are authorized to access the requested profile or not.
  • Step 5: In the response, you notice that there are two parameters — “User_Id” and “Requested_Id”. “User_Id” represents your original ID, and “Requested_Id” represents the ID you are trying to access.
  • Step 6: Now comes the logic flaw. You discover that when you manipulate the “User_Id” in the response to be the same as the “Requested_Id” (e.g., changing “User_Id=76678” to “User_Id=76629”), the application allows you to access the data of that particular user, even though you are not authorized to do so.
  • Step 7: By exploiting this flaw, you can access the data of other users by simply modifying the “User_Id” in the response to match the “Requested_Id”. For example, you can access the profile of the user with ID 76629 by changing your “User_Id” to 76629 in the response.
  • Step 8: Additionally, since you can modify the “User_Id” in the response, you can edit the data of any user by changing your “User_Id” to match the ID of the user whose data you want to modify.

Vulnerable Parameter and Code Snippet:

The vulnerability lies in the application’s failure to properly check the authorization for accessing other users’ profiles and update information. The code snippet for handling the profile page might look like this (Example code in Python):

def get_profile_data(user_id, requested_id):
# Check if the user is authorized to access the requested profile
is_authorized = check_authorization(user_id, requested_id)

if is_authorized:
# Fetch and return the profile data
profile_data = fetch_profile_data(requested_id)
return profile_data
else:
return "You are not authorized for this action."
def check_authorization(user_id, requested_id):
# Code to check if the user is authorized to access the requested profile
return user_id == requested_id
def fetch_profile_data(user_id):
# Code to fetch profile data from the database based on the user_id
# (omitted for simplicity)
return profile_data

In this code, the function check_authorization compares the "User_Id" and "Requested_Id" to determine if the user is authorized to access the requested profile. However, this logic is flawed, as changing the "User_Id" in the response to match the "Requested_Id" allows unauthorized access to other users' profiles. Additionally, the application doesn't properly authenticate the user before allowing them to modify data, which is another vulnerability.

Now that we have explored the explanations, analyzed root causes, discussed the mindset required to identify logic flaws, and examined real-life examples showcasing these issues, I hope the concept of logic flaw vulnerability is much clearer to you. Understanding these aspects will help you identify and address such vulnerabilities in various scenarios.

I recently conducted a session titled “The Art of Exploiting Logic Flaws” at The Hackers MeetUp — Ahmedabad. During the session, I demonstrated labs based on real-world scenarios. These labs are now available on my GitHub Page, and you can use them to practice identifying and addressing logic flaw vulnerabilities. The link to the labs is provided below for your reference.

Certificate of Appreciation by The Hackers MeetUp — India

Connect With R00tKid on Social Media:

GitHub: @im-rootkid || Twitter: @im_rootkid || Instagram: @im_rootkid/ || LinkedIn: @im-rootkid || Youtube: @im_rootkid || Website: rootkid.in

Happy learning !!!! Happy Hacking !!!!

--

--

RootKid 🤖 🤖 🤖
Nest InfoSec

Learner 📖 | Ethical Hacker 💻 | Penetration Tester 🧑 | Bug Hunter 🐞 |