CS - SQL Injection (Discussion-1) ) & Buffer Overflow Attack
CS - SQL Injection (Discussion-1) ) & Buffer Overflow Attack
SQL Injection (SQLi) is a type of attack where an attacker can execute malicious SQL statements (also known as payloads)
that control a web application's database server. This is made possible by inserting or "injecting" arbitrary SQL code into
a query. The impact can range from unauthorized data viewing to complete control over the database server.
Vulnerable Code
Malicious Input
Resulting Query
SELECT * FROM users WHERE username = '' OR '1'='1' AND password = 'anything';
This query would return all rows from the users table because '1'='1' is always true.
' OR '1'='1
' AND (SELECT CASE WHEN (1=1) THEN 1 ELSE (SELECT 1 UNION SELECT 2) END)--
3. Error-based SQL Injection
o Description: The attacker uses database error messages to gather information about the structure of
the database.
o Example:
A buffer overflow attack occurs when an attacker sends more data to a buffer (a temporary storage area in memory)
than it can handle. This excess data can overwrite adjacent memory, potentially allowing the attacker to execute
arbitrary code or crash the system.
1. Identify Vulnerability:
o The attacker finds a vulnerable program where user input is copied into a fixed-size buffer without
proper bounds checking.
2. Craft Malicious Input:
o The attacker crafts input that exceeds the buffer's capacity. This input includes data that will overwrite
critical parts of memory, such as the return address of a function.
3. Inject Payload:
o The malicious input includes a payload (typically shellcode) that the attacker wants to execute. This
payload is placed in the overflowed buffer.
4. Trigger Overflow:
o The attacker sends the crafted input to the vulnerable program, causing the buffer overflow. The
overflowed data overwrites the return address on the stack with the address of the payload.
5. Gain Control:
o When the function returns, it uses the overwritten return address, redirecting execution to the
attacker's payload, granting control to the attacker.
#include <stdio.h>
#include <string.h>
Malicious Input:
o The overflow may overwrite adjacent heap data structures, potentially allowing control over the
program flow.
o
3. Integer Overflow
o Description: An integer overflow can lead to buffer overflow when arithmetic operations result in
unexpected values.
o Example:
o If len is calculated incorrectly due to an integer overflow, the allocated buffer might be too small,
leading to overflow.
4. Format String Vulnerability
o Description: Improper handling of format strings can lead to buffer overflows.
o Example:
printf(user_input);
o If user_input contains format specifiers, it can lead to arbitrary memory access and buffer overflow.
1. Bounds Checking:
o Always check the size of input data before copying it to a buffer.
o Example: