Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
public class X
{
public int b;
public int c;
}
public class A : X
{
public int b { get; set; }
public int c { get; set; }
}[FieldOffset(0)]
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication26 {
class Program {
static void Main(string[] args) {
var testString = "habrahabr";
Console.WriteLine(testString.Length); // out: 9
var shell = new StringShell();
shell.str = testString;
Console.WriteLine(shell.hack/*ссылка на Bytes*/.GetType());
// GetType врет ?) out: System.String
// ну и самое интересное, не является ли это уязвимостью?
shell.hack.b0 = 143;
Console.WriteLine(testString.Length); // Ахтунт! out: 143
// А если еще сделать так
Console.WriteLine(testString); // out: %содержимое кучи%
}
}
// интересная особенность, StructLayout работает для классов
[StructLayout(LayoutKind.Explicit)]
public class StringShell {
[FieldOffset(0)]
public String str;
[FieldOffset(0)]
public Bytes hack;
}
// сюда можно добавить столько полей,
// на сколько мы хотим пролезть "вглубь" кучи
// далее расположения "хакнутого" объкта
public class Bytes {
public byte b0;
public byte b1;
public byte b2;
public byte b3;
public byte b4;
public byte b5;
public byte b6;
public byte b7;
}
}
class A
{
public int b, c;
public A GetType()
{
return this;
}
public A GetField(string value)
{
return this;
}
public void SetValue(A instance, int value)
{
// Nothing here
}
}
class A
{
public int b { get { return 42; } set {} }
public int c { get { return 42; } set {} }
}
Задачка по C#