用过C#的朋友可能认为它是一种十分安全的语言,其实C#也可以做到经典的缓冲区溢出。
本文章将用一个实例来描述C#究竟是如何发生缓冲区溢出的!
首先建立一个C# Console工程,并开启工程的“允许不安全代码”选项
键入代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
char ori = 'A';
StackOverflow();
Console.WriteLine(ori);
Console.ReadLine();
}
static unsafe void StackOverflow()
{
int* p = stackalloc int[1]; //声明一个int数组指针
int i = sizeof(int); //指针增量
while (*p != (int)'A') //找到Main函数中的ori变量
{
p += i;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Address: {0}", (int)p); //输出当前指针所指向的地址
Console.ForegroundColor = ConsoleColor.Gray;
try

本文揭示了一个鲜为人知的事实:即使在C#这种被认为是相对安全的语言中,也存在缓冲区溢出的现象。通过一个具体的实例,详细阐述了C#如何发生缓冲区溢出,以及利用不安全代码修改内存中的值,甚至可能影响函数地址,达到执行远程代码的风险。
最低0.47元/天 解锁文章
222

被折叠的 条评论
为什么被折叠?



