×

Buffer overflow attack with examples

You have undoubtedly faced the term buffer overflow in your programming journey. Many times it occurs when we try to run a piece of code with user input, but it gives some error like stack buffer overflow or heap buffer overflow or something like segmentation fault. So, let's try to understand this topic.

What is Buffer?

When we run a program, particular types of memory are used to store data and the program also. This memory is called a buffer. Buffer lives in RAM. It is a sequential section of memory to contain anything from character strings to integer numbers. Buffer improves the performance of a computer.

What is Buffer Overflow?

Some programming languages like C and C++ allow the programmers to allocate memory for the buffer. When a user puts an input beyond the memory of the buffer, inputs are stored nearby memory or buffers, which is not allowed. It happens because the programmer has not used anyway to check the input. Some built-in functions of C and CPP languages are also a way to create buffer overflow.

Example: -

Let’s take a piece of code as example

#include <bits/stdc++.h>
using namespace std;


int main() {
	vector<int> arr(2,0);
	arr[0] = 5;
	arr[1] = 6;
	arr[4] = arr[9];
	return 0;
}

Here firstly, we declare a vector arr of size 2. But after that, we are trying to allocate some data in arr[4]. As there does not exist any memory location for it so, there will occur buffer overflow because the information should be stored for this purpose; we need to modify the nearby memory.

Types of buffer overflow: -

Stack buffer overflow: Stack is an integral part of programming. In memory allocation of buffer, the stack is used. When we call a new function, a stack is created for that purpose. When the overflow occurs in the function, it is called a stack buffer overflow.

Heap buffer overflow: When overflow occurs in the open memory pool, it is called a heap buffer overflow.

Integer Overflow: This type of overflow frequently occurs in our programming practices. When we take two integer type values and multiply them and try to store them in an integer type variable, it causes an overflow.

Some Results of Buffer Overflow: -

  1. It causes the update of a memory location that is unwanted and not permissible.
  2. It may crash your system.
  3. It also creates a way for attackers to manipulate the private data.

Buffer Overflow attack: -

Buffer overflow is the weak point of any app or programmed system. Attackers target this point and manipulate the code. For this reason, a big problem in cybersecurity arises.

Example: In 2014, a cyber threat named "heartbled" was exposed to hundreds of millions of users due to a buffer overflow in SSL software.

The way of attack: -       

When attackers find buffer overflow, they try to inject their shellcode into the main code to manipulate the program. There are different parts of this malicious content which control the data. The parts are as follows:

  1. A chain of bytes which represent NOP instruction ( programs that are not executable).
  2. A new return address which points to the NOP bytes.
  3. Arbitrary code is located somewhere in the middle of the chain of bytes.

Types of Buffer Overflow attacks:

Stack buffer overflow attack: It is the most common buffer overflow attack because it is very easy. Here call stack is used.

Heap buffer overflow attack: When an overflow attack occurs in the available memory pool, it is called a heap buffer overflow.

Integer Overflow attack: When we take two integer-types values and multiply them and try to store them in an integer type variable, it causes overflow. It also may result in an attack.

Prevention methods: -

  1. Selection of language: Many programming languages are prone to buffer overflow, but the limit of such attacks varies on the language that is used to write the vulnerable program. Code written in Perl or JavaScript or python is generally not vulnerable to buffer overflows. Buffer overflow in a program which is written in C, CPP, Fortran or assembly could allow the attackers to compromise the targeted system fully.
  2. Use of safe library and functions: In many cases occurs because of using library functions which do not check any boundaries. Functions like gets, printf, scanf and strcpy are of this type. So, it is highly recommended not to use this type of library or function.
  3. Testing: It is one of the suitable measures to prevent buffer overflow. We can find the faulty code and patch the bug by edge testing. It is an advantageous method in software which are under development process.

Note: We can also use the methods like deep packet inspection, address space layout randomization, Executable space protection, Buffer overflow protection etc.


Related Topics

Convert binary tree to a doubly linked list

Implementation //creating a C++ program for the transition of a binary tree into a linked list. #include <iostream> using namespace std; /* Firstly, let’s create a binary tree that will help us in setting...

4 minutes read.

Given a Perfect Binary Tree, Reverse Alternate Levels

Implementation //writing a program in C++ language to see how to approach it. #include <bits/stdc++.h> using namespace std; // creating a tree node. struct Nod { char ky; struct Nod *Lft, *Rt; }; // creating a new utility function...

9 minutes read.

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Given a Binary Tree Swap Nodes at K Height

Implementation // Writing a C++ program that will help us exchange the nodes.  #include<bits/stdc++.h> using namespace std; // Creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a function that will help...

8 minutes read.

Dynamic memory allocation of structure in C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

5 minutes read.

Semi-Structured data

In this article, we will discuss the semi-structured data. Data can be defined as the distinct piece of information that is gathered and translated for some purpose. It can be...

5 minutes read.

Red Black Tree vs AVL Tree: Data Structure

Difference Between Red Black Tree vs AVL Tree Red Black Tree: A red-black tree is referred as self-balancing binary search tree. In red-black, each node stores an extra bit that determines...

4 minutes read.

What is the B+ Tree in Data Structures?

We all know that the B+ tree in data structures is nothing but just an extended version of the B tree. It allows the smooth working of all the operations...

7 minutes read.

Construction of B tree in Data Structure

A B-tree is a type of balanced tree data structure that is commonly used in file systems and databases to improve the efficiency of search, insert, and delete operations. The structure...

4 minutes read.

Dijkstra’s vs Bellman-Ford Algorithm

The Dijkstra Algorithm One of the SSSP (Single Source Shortest Path) algorithms is Dijkstra's. As a result, it finds the shortest path between a source node and all other nodes in...

7 minutes read.

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Heap Data Structure

In this article, we will learn in detail about Heap (Min heap and Max heap). Before going to the main topics, let’s have a look at what is complete binary...

19 minutes read.

Why is Binary Heap Preferred over BST for Priority Queue

A priority queue is a linear and ordered collection of elements in which each element has an attribute named priority and the priority attribute decides the order in which elements...

2 minutes read.

Application of 2D array - Sparse Matrix

2D Arrays Application - Sparse Matrix A matrix is a two-dimensional data item consisting of m rows and n columns, with a total of m x n values. A sparse matrix...

7 minutes read.

Delete N nodes after M nodes of a linked list

Delete N nodes after M nodes of a linked list In this problem, we have given a linked list and two integers M and N. We need to traverse the linked...

3 minutes read.

Big O Notations

What is Big O Notation, and why is it important? "Big O notation is a mathematical notation that depicts a function's limiting behaviour when the input tends towards a certain value...

10 minutes read.

Bubble Sort vs Selection Sort

In this article, we will discuss the basic differences between these two sorting algorithms. Let us have a quick overview of what these sorting algorithms are? And what are the...

6 minutes read.

Binary Tree vs Binary Search Tree: Data Structure

Difference Between Binary Tree and Binary Search Tree What is Binary Tree? A tree which each node can have utmost two children called binary tree. These children are referred as the ‘left...

3 minutes read.

Trim a binary search tree

Implementation //writing a C++ program will help us eliminate the keys that are out of the league.  #include<bits/stdc++.h> using namespace std; //we are now creating a binary search tree node consisting of key left...

8 minutes read.

AVL Tree

AVL Tree AVL Tree is referred to as self-balanced or height-balanced binary search tree where the difference between heights of its left subtree and right subtree (Balance Factor) can't more than...

25 minutes read.