You are on page 1of 8

RPB -TEAM

Secure coding

COMMON C
FUNCTIONS USED
TO CARRY OUT
BUFFER
OVERFLOW
ATTACKS

RPB - TEAM

RA'AD M. ABU JAISH


Common C Functions Used to Carry Out
Buffer Overflow Attacks RPB -TEAM

Buffer overflow attacks in IoT devices are often carried out by common C library functions that are
designed to copy or manipulate data in memory buffers. These include:

VULNERABLE C
DESCRIPTION EN DESCRIPTION AR
FUNCTION

This function copies data from one memory location ‫ ال يقوم‬.‫تقوم هذه الوظيفة بنسخ البيانات من موقع ذاكرة إلى آخر‬
to another. It does not perform any size checking, so ‫ لذلك يمكن استخدامه لنسخ المزيد من البيانات‬، ‫بأي فحص للحجم‬
memcpy it can be used to copy more data into a buffer than ، ‫في المخزن المؤقت أكثر من المخزن المؤقت المصمم لالحتفاظ به‬
the buffer is designed to hold, potentially leading to a
.‫مما قد يؤدي إلى ثغرة أمنية في تجاوز سعة المخزن المؤقت‬
buffer overflow vulnerability.

This function copies a string from one buffer to ‫ مثل‬.‫تقوم هذه الوظيفة بنسخ سلسلة من مخزن مؤقت إلى آخر‬
another. Like memcpy, it does not perform any size
‫ لذلك يمكن استخدامه لنسخ‬، ‫ ال يقوم بأي فحص للحجم‬، memcpy
strcpy checking, so it can be used to copy a string larger
‫ مما‬، ‫سلسلة أكبر من المخزن المؤقت الوجهة في المخزن المؤقت‬
than the destination buffer into the buffer, potentially
leading to a buffer overflow vulnerability. .‫قد يؤدي إلى ضعف سعة المخزن المؤقت‬

This function is similar to strcpy, but it allows the


programmer to specify the maximum number of ‫ لكنها تسمح للمبرمج بتحديد‬، strcpy ‫هذه الوظيفة مشابهة لـ‬
characters to be copied. While this can help to ‫ في حين أن هذا يمكن أن‬.‫الحد األقصى لعدد األحرف المراد نسخها‬
strncpy prevent buffer overflows, it is important to ensure ‫ فمن المهم التأكد من‬، ‫يساعد في منع فيضان المخزن المؤقت‬
that the maximum number of characters is set ‫تعيين الحد األقصى لعدد األحرف بشكل صحيح وأن المخزن المؤقت‬
correctly and that the destination buffer is large ‫للوجهة كبير بما يكفي الحتواء السلسلة المنسوخة‬
enough to hold the copied string

This function formats and stores a string in a ‫تعمل هذه الوظيفة على تنسيق سلسلة وتخزينها في مخزن‬
buffer. Like strcpy, it does not perform any size ‫ لذلك يمكن‬، ‫ ال يقوم بأي فحص للحجم‬، strcpy ‫ مثل‬.‫مؤقت‬
sprintf checking, so it can be used to write a string ‫استخدامه لكتابة سلسلة أكبر من المخزن المؤقت في المخزن‬
larger than the buffer into the buffer, potentially .‫ مما قد يؤدي إلى ضعف سعة المخزن المؤقت‬، ‫المؤقت‬
leading to a buffer overflow vulnerability.

This function is similar to sprintf, but it allows the


programmer to specify the maximum number of ‫ لكنها تسمح للمبرمج بتحديد‬، sprintf ‫هذه الوظيفة مشابهة لـ‬
characters to be written to the buffer. While this can ‫ في‬.‫الحد األقصى لعدد األحرف المراد كتابتها في المخزن المؤقت‬
snprintf help to prevent buffer overflows, it is important to ‫ فمن‬، ‫حين أن هذا يمكن أن يساعد في منع فيضان المخزن المؤقت‬
ensure that the maximum number of characters is set ‫المهم التأكد من تعيين الحد األقصى لعدد األحرف بشكل صحيح وأن‬
correctly and that the destination buffer is large .‫المخزن المؤقت للوجهة كبير بدرجة كافية‬
enough.

This function reads data from a string and


stores it in a buffer. Like sprintf, it does not ‫تقوم هذه الوظيفة بقراءة البيانات من سلسلة وتخزينها في مخزن‬
sscanf perform any size checking, so it can be used to ‫ لذلك يمكن‬، ‫ ال يقوم بأي فحص للحجم‬، sprintf ‫ مثل‬.‫مؤقت‬

read a string larger than the buffer into the ‫استخدامه لقراءة سلسلة أكبر من المخزن المؤقت في المخزن‬
buffer, potentially leading to a buffer overflow .‫ مما قد يؤدي إلى ضعف سعة المخزن المؤقت‬، ‫المؤقت‬
vulnerability.

RPB -TEAM
Buffer Copy without Checking Size of Input

RPB - TEAM

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {


char buffer1[10];
char buffer2[10];

// Copy user input into buffer1


without checking the size
strcpy(buffer1, argv[1]);

// Copy the contents of buffer1


into buffer2 without checking the
size
strcpy(buffer2, buffer1);

printf("%s\n", buffer2);

return 0;
}
@ RA'AD M. ABU JAISH

RPB - TEAM

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {


char buffer1[3];
char buffer2[3];

// Copy user input into buffer1 without


checking the size
strncpy(buffer1, argv[1],sizeof(buffer1)-1);

// Copy the contents of buffer1 into buffer2


without checking the size
strncpy(buffer2, argv[1],sizeof(buffer2)-1);

printf("%s\n", buffer2);

return 0;
}

@ RA'AD M. ABU JAISH


RPB -TEAM

VULNERABLE C
DESCRIPTION EN DESCRIPTION AR
FUNCTION

This function is a safe alternative to gets(). It ‫ وتخزينها في مخزن‬stdin ‫تقوم هذه الوظيفة بقراءة األحرف من‬
‫ ال يتحقق من حجم‬.‫مؤقت حتى يتم العثور على حرف سطر جديد‬
gets reads a line from the input stream and stores it
in a buffer, taking care not to exceed the ‫المخزن المؤقت ويمكن أن يؤدي إلى ثغرات تجاوز سعة المخزن‬
buffer's size. .‫المؤقت‬

This function reads input from stdin and tries to parse ‫ وتحاول تحليلها وفًق ا‬stdin ‫تقرأ هذه الوظيفة المدخالت من‬
it according to a format string. Like gets, it does not
‫ فإنه ال يتحقق من حجم المخزن‬، get ‫ مثل‬.‫لسلسلة التنسيق‬
scanf check the size of the input buffer and can cause
buffer overflow vulnerabilities.
‫المؤقت لإلدخال ويمكن أن يسبب ثغرات في تجاوز سعة المخزن‬
.‫المؤقت‬

This function reads input from stdin and tries to parse ‫ يقرأ سطًر ا من دفق‬.)( ‫هذه الوظيفة هي بديل آمن للحصول على‬
it according to a format string. Like gets, it does not
‫ مع الحرص على عدم تجاوز حجم‬، ‫اإلدخال ويخزنه في مخزن مؤقت‬
fgets check the size of the input buffer and can cause
buffer overflow vulnerabilities.
.‫المخزن المؤقت‬

his function is safe to use as long as you specify the


maximum number of characters to read. ‫هذه الوظيفة آمنة لالستخدام طالما أنك تحدد الحد األقصى لعدد‬
scanf

char str[21];
.‫األحرف للقراءة‬

scanf("%20s", str);

RPB - TEAM RPB - TEAM

#include <stdio.h> #include <stdio.h>


#include <string.h> #include <string.h>

int main() { int main() {


char name[10];
char name[10];
int age;
int age;

printf("Enter your name: ");


printf("Enter your name: ");

scanf(“%9s”, &name);
scanf(“%s”, &name);

printf("Enter your age: ");


printf("Enter your age: ");
scanf("%2d", &age);
scanf("%d", &age);
printf("Your name is %s and you are %d
printf("Your name is %s and you are %d years old.\n", name, age);
years old.\n", name, age);
return 0;
return 0; }
}

@ RA'AD M. ABU JAISH @ RA'AD M. ABU JAISH


RPB -TEAM

RPB - TEAM

#include <stdio.h>
#include <string.h>

int main() {
char name[10];
int age;

printf("Enter your name: ");

fgets(name, 10, stdin);

printf("Enter your age: ");


scanf("%d", &age);

printf("Your name is %s and you are %d


years old.\n", name, age);

return 0;
}

@ RA'AD M. ABU JAISH


VULNERABLE C
DESCRIPTION EN DESCRIPTION AR
FUNCTION

fprint Writes the printf to a file


‫ إلى ملف‬printf ‫يكتب‬

printf Output a formatted string



‫إخراج سلسلة منسقة‬

sprintf Prints into a string ‫يطبع في سلسلة‬


Prints into a string checking the length


snprintf

‫يطبع في سلسلة للتحقق من الطول‬

vfprintf Prints the a va_arg structure to a file ‫ إلى ملف‬va_arg ‫يطبع بنية‬

Below are some format parameters which can be used and their consequences:

•”%x” Read data from the stack

•”%s” Read character strings from the process’ memory

•”%n” Write an integer to locations in the process’ memory

%% % character (literal) Reference

External representation of a
%p pointer to void
Reference

%d Decimal Value

%c Character  

%u Unsigned decimal Value

%x Hexadecimal Value

%s String Reference

Writes the number of characters


%n Reference
into a pointer
RPB - TEAM

#include <stdio.h>
void main(int argc, char **argv)
{char buf[100];
snprintf(buf, sizeof buf, argv[1]);}

@ RA'AD M. ABU JAISH

RPB - TEAM

#include <stdio.h>
void main(int argc, char **argv)
{char buf[100];
snprintf(buf, sizeof buf,"%s", argv[1]);}

@ RA'AD M. ABU JAISH

You might also like