{
char buf[8];
sprintf(buf,"AAAA%3s","XXXXXXXX");
printf("%s/n",buf);
}
what will happen?
The buffer have 8 characters space and only 3 free characters left, however, "XXXXXXXX" is 8 characters long.
It makes a lot of sense to consider what happens in your and, more importantly, similar, cases. As other posters have noted, it invokes UB. That's probably true. However, the world does not stop simply because someone did not define what exactly should happen next. And what physically happens next, may well be a major security hole.
If your string XXX...
comes from uncontrolled sources, you are very close to generating a buffer overflow vulnerability.
(1) Your stack typically "grows" backwards, i.e. the smaller the addresses, the more the stack is filled.
(2) Strings expect the characters belonging to that string to be stored so that character n+1 is stored after character n.
(3) When you call a function, the return address, i.e. the address of the instruction that is to be executed after the function returns, is pushed to the stack (among other things, typically).
Now consider a stack frame of your function.
|----------------|
| buf [size 8] |
|----------------|
| (func args) |
|----------------|
| (other stuff) |
|----------------|
| return address |
|----------------|
By finding out what exactly the offset between buf
and the return address on the stack is, a malicious user may manipulate input to your application in a way that the XXX...
string contains an address of the attacker's choosing at just the point where the uncontrolled sprintf
function will overwrite the return address on the stack. (NB: Better use snprintf
if it's available to you). Thereby the attacker mounted a buffer overflow attack. He might use something like the NOP sled technique to have your application start a shell for him. If you were writing an application that ran under a privileged user account, you'd just have provided an attacker with a first-grade entry to your costumer's system, an ACE hole, if you will.
Update
The run-time error you experience may well be due to an overwritten return address. Since you filled it with, basically, gargabe, the address the CPU jumped to did probably contain byte sequences that, interpreted as program text, cause an invalid memory access (or the address itself was already bad).
It should be noted that some compilers can help against these kinds of errors. GCC, for example, has the -fstack-protector
. I'm not familiar with how good those features are.
分享到:
相关推荐
**Buffer Overflow:概念与原理** Buffer Overflow(缓冲区溢出)是计算机安全领域的一个常见漏洞,它发生在程序尝试写入超过内存分配空间的数据时。这种现象可能导致数据丢失、程序崩溃,甚至恶意攻击者利用该漏洞...
**Buffer Overflow攻击与防御** Buffer Overflow(缓冲区溢出)是一种常见的计算机安全漏洞,它发生在程序尝试向固定大小的内存缓冲区写入超出其实际容量的数据时。这种攻击利用了编程语言(如C和C++)对内存管理的...
Buffer Overflow是一种常见的软件安全漏洞,它发生在程序试图在内存缓冲区之外写入数据时,超出其分配的空间,可能覆盖相邻的数据区域,导致程序崩溃或执行恶意代码。复旦大学的软件安全SEED labs系列实验旨在让学生...
"Buffer Overflow基础知识" Buffer Overflow是一种常见的安全漏洞,指的是当程序向缓冲区写入数据时超过缓冲区的容量,从而导致缓冲区溢出,影响程序的正常运行或使得攻击者获取非法访问权限。下面是 Buffer ...
buffer overflow 介绍软件漏洞的基本原理 如何通过反汇编进行缓冲区溢出漏洞的发现
这本书《Buffer Overflow Attacks: DETECT, EXPLOIT, PREVENT》深入探讨了这一主题,旨在帮助读者理解和防御这种攻击。 缓冲区是计算机内存中用于临时存储数据的一段连续空间。当程序员在处理数据时,如果没有正确...
《Windows缓冲区溢出之道》是一本深入探讨Windows系统中缓冲区溢出漏洞的经典著作。在信息技术领域,安全是至关重要的,而缓冲区溢出作为一类常见的安全问题,经常被黑客利用来攻击系统,获取非法权限。...
**缓冲区溢出攻击** 缓冲区溢出攻击是网络安全领域中的一个重要话题,它涉及到计算机程序在处理内存时的潜在漏洞。这种攻击方式利用了程序在处理数据时对内存空间分配和管理不当的情况,可能导致程序崩溃,更严重的...
这篇文章由David Litchfield撰写,他是一位在安全领域享有盛誉的专家。文章主题集中在破解由Microsoft Windows 2003 Server内置的基于栈的缓冲区溢出保护机制。文章首先声明了Microsoft对安全的承诺,并提到了Code ...
PDF格式的英文电子版。详细介绍了缓冲溢出攻击的原理和如何发现缓冲溢出。最后给出了诸多实例。
Buffer Overflow(处理方案).md
Buffer Overflow(解决方案).md
buffer overflow(解决方案).md
Buffer Overflow(亲测可用).md
The Tao of Windows Buffer Overflow 缓冲区溢出之道 中文翻译版
Packet Buffer Overflow(亲测可用).md
### 缓冲区溢出攻击(Buffer Overflow Attack) 缓冲区溢出攻击是计算机安全领域中的一个常见威胁,它被广泛认为是最重要的安全漏洞之一。这种类型的攻击利用了软件中的漏洞,特别是缺乏对用户输入的有效验证时更为...