0% found this document useful (0 votes)
19 views20 pages

Gnu Debugger

This report outlines the process of testing and mitigating integer and buffer overflow vulnerabilities using GNU Debugger (GDB) with a vulnerable C program as an example. It details the setup requirements, the vulnerabilities present in the code, and the necessary modifications to secure the program. Additionally, it emphasizes the importance of secure coding practices and the role of GDB in network security.

Uploaded by

talha.ali9ctn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views20 pages

Gnu Debugger

This report outlines the process of testing and mitigating integer and buffer overflow vulnerabilities using GNU Debugger (GDB) with a vulnerable C program as an example. It details the setup requirements, the vulnerabilities present in the code, and the necessary modifications to secure the program. Additionally, it emphasizes the importance of secure coding practices and the role of GDB in network security.

Uploaded by

talha.ali9ctn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

NAME: TALHA ALI

ID: F2022408046
SECTION: Y2
Testing Integer Overflow and Buffer Overflow Using GDB
Introduction
This report demonstrates how to test and mitigate integer and buffer overflow
vulnerabilities using GNU Debugger (GDB). By analyzing a vulnerable C program,
exploiting its weaknesses, and applying secure coding practices, the exercise
highlights the importance of detecting and preventing these common security
issues.

Prerequisites
 A Linux system (Ubuntu recommended).
 GCC (GNU Compiler Collection).
 GDB (GNU Debugger).

1. Set Up Environment
Required Tools
1. Linux system:
Ubuntu is recommended for its user-friendliness, but Kali Linux
will also work effectively for security-related tasks.
2. GCC (GNU Compiler Collection):
Essential for compiling C programs.
3. GDB (GNU Debugger):
A powerful tool for debugging and analyzing programs.
Run this command
sudo apt-get install gcc gdb
2. Write a Vulnerable C Program
 Create a file named vulnerable.c

 Run a command
nano vulnerable.c
Now we can write the vulnerable code inside it
And write the following code in it:
Vulnerable code :
#include <stdio.h>
#include <string.h>

void vulnerable_function(char *input) {


char buffer[10];
strcpy(buffer, input);
printf("You entered: %s\n", buffer);
}

int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number + 1 < number) {
printf("Integer overflow detected!\n");
} else {
printf("Number is safe: %d\n", number);
}
char input[100];
printf("Enter a string: ");
scanf("%s", input);
vulnerable_function(input);
return 0;
}
Written below in the screenshot
The program has two vulnerabilities:
1. Integer Overflow: The if (number + 1 < number) condition exploits an
overflow scenario.
2. Buffer Overflow: Using strcpy in vulnerable_function causes overflow when
the input exceeds the buffer size.

Compile the Program


Use the following command to compile the code with debugging symbols:
gcc -g vulnerable.c -o vulnerable
Launch GDB
Run the compiled program in GDB:
gdb ./vulnerable
Set Breakpoints
Set a breakpoint at the main function:
break main

Test Integer Overflow


Run the program:
Enter a very large number to trigger integer overflow.
Test Buffer Overflow
Restart the program:
Enter a very long string (e.g., TALHAAAAAAAAAAAAAAAAAAA) to trigger a buffer
overflow.
Observe if the program crashes or shows a stack-smashing error.

Fix the Vulnerabilities


Mitigating Vulnerabilities
Code Fixes
1. Replace strcpy with strncpy in vulnerable_function to limit input size:
strncpy(buffer, input, sizeof(buffer) - 1);
buffer[sizeof(buffer) - 1] = '\0';
2. Add bounds checks for the integer input:
if (number > INT_MAX - 1) {
printf("Input too large!\n");
return 1;
}
Modify the code:
Replace strcpy with strncpy in vulnerable_function. Apps
This is a modifyed code
#include <stdio.h>
#include <string.h>
#include <limits.h> // Include for INT MAX
void vulnerable_function(char *input) {
char buffer[10];
// Use snprintf instead of strncpy to prevent buffer overflow
snprintf(buffer, sizeof(buffer), "%s", input);
printf("You entered: %s\n", buffer);
}
int main() {
int number;
printf("Enter a number: ");
// Use %d with proper bounds to avoid overflow from the input
if (scanf("%d", &number) != 1) {
printf("Invalid input\n");
return 1;
}
// Integer overflow check using proper comparison
if (number > INT MAX 1) { printf("Input too large!\n");
return 1;
}
printf("Number is safe: %d\n", number);
char input[100];
printf("Enter a string: ");
// Use a format specifier with a limit to prevent buffer overflow
if (scanf("%99s", input) != 1) { printf("Invalid input\n");
return 1;
}
vulnerable_function(input);
return 0;
}
Add bounds checks for the integer input.
Recompile the program:
gcc -g vulnerable.c -o vulnerablefixed

9. Re-Test
 Repeat the integer overflow and buffer overflow tests after fixing the
vulnerabilities.
break main

This output simply indicates that you have successfully set a breakpoint at
the main function. The debugger is notifying you about the memory
address (0x11df) where the breakpoint is set, as well as the source file
(vulnerable.c) and line number (15).
GNU: Overview and Explanation
GNU, which stands for "GNU's Not Unix," is a free software project
that was launched in 1983 by Richard Stallman. The goal of the
GNU project is to provide a complete operating system made
entirely of free software, meaning that users have the freedom to
run, study, modify, and share the software.

Key Features of GNU


1. Free Software Philosophy:
o GNU emphasizes user freedom over proprietary restrictions.
o Software under the GNU project is typically licensed under
the GNU General Public License (GPL), which ensures users
can freely share and modify the code.
2. Component-Based Approach:
o GNU includes a wide range of tools, libraries, and utilities for
programming, software development, and system
management.
o Examples include:
 GNU Compiler Collection (GCC): A suite of compilers
for various programming languages.
 GNU Debugger (GDB): A powerful debugging tool.
 GNU Bash: A widely used shell for Unix-like operating
systems.
3. Open Source Collaboration:
o Many developers worldwide contribute to GNU projects,
making it one of the largest and most influential free
software ecosystems.

Importance of GNU in Modern Computing


1. Foundation of Linux Distributions:
o The Linux kernel, when combined with GNU utilities, forms a
fully functional operating system often referred to as
GNU/Linux.
o Popular distributions like Ubuntu, Fedora, and Debian are
built on GNU principles.
2. Support for Innovation:
o GNU tools provide a robust foundation for developers to
build and test software, fostering innovation and
collaboration.
3. Advocacy for Digital Freedom:
o The GNU project has been a key voice in advocating for user
rights in the digital age, challenging restrictive software
practices.

GNU Debugger and Its Role in Network Security


What is GNU Debugger (GDB)?
GNU Debugger (GDB) is an open-source tool designed to assist
developers in debugging and analyzing programs. It allows users
to:
 Examine the behavior of programs during runtime.
 Detect and analyze errors such as segmentation faults and
crashes.
 Step through code line by line or set breakpoints to observe
specific behaviors.
Key Features of GDB
1. Breakpoints: Pause execution at specific points in the program to
inspect variables and execution flow.
2. Stepping Through Code: Execute code line by line to identify the
exact line causing an issue.
3. Variable Inspection: View and modify the values of variables
during runtime.
4. Stack Traces: Analyze the function call stack when an error occurs.
5. Core Dumps: Load and analyze core dump files to debug post-
mortem crashes.
Uses of GDB in Network Security
1. Exploitation Analysis:
o Debuggers like GDB are essential for analyzing exploits in
networked applications.
o Security researchers use GDB to understand how
vulnerabilities such as buffer overflows and format string
exploits behave at runtime.
2. Reverse Engineering:
o GDB is frequently used to reverse engineer software binaries
to understand their functionality.
o This can be critical when analyzing malware or unknown
software in a network environment.
3. Protocol Debugging:
o Network applications often implement complex protocols.
Debuggers help verify that these protocols are functioning
correctly and securely.
4. Post-Attack Analysis:
o After a network attack, GDB can be used to inspect and
understand how an attacker exploited vulnerabilities in a
program.
5. Secure Development:
o Developers use GDB to test and validate security measures
in software.
o Ensuring bounds checking, input validation, and other
security features work as intended.
Real-World Implications
1. Integer Overflow: Can lead to bypassing authentication mechanisms or
causing logical errors in critical systems.
2. Buffer Overflow: Often exploited for executing malicious code, leading to
system compromise.

Case Study: Heartbleed


The infamous Heartbleed bug exploited a buffer overflow in OpenSSL, leaking
sensitive information from server memory. This highlights the critical need for
secure coding practices.

Secure Coding Practices


1. Input Validation: Always sanitize and validate user inputs to prevent invalid
data from being processed.
2. Safe Functions: Use safe string functions (e.g., strncpy instead of strcpy).
3. Compiler Protections: Enable stack canaries, ASLR (Address Space Layout
Randomization), and other compiler security features.
4. Testing and Debugging: Regularly test code using debugging tools like GDB
to identify and fix vulnerabilities early.
Conclusion
This exercise highlights:
 The ease with which vulnerabilities like integer and buffer overflow can
occur.
 The importance of using secure coding practices to mitigate these risks.
 The effectiveness of tools like GDB in detecting and analyzing
vulnerabilities.
By adopting these practices, developers can build robust applications resistant to
common exploits.

You might also like