Chinaunix首页 | 论坛 | 博客
  • 博客访问: 845949
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:09:07

Using Reflection to Read/Write Variables
using System;
using System.Reflection;

class EvilCodeWithFullTrust
{
  static void CallPrivateMethod(object o, string methodName) {
    Type t = o.GetType();
    MethodInfo mi = t.GetMethod(methodName,
       BindingFlags.NonPublic |
       BindingFlags.Instance);
    mi.Invoke(o, null);
  }
  static void Main() {
    CallPrivateMethod(new NuclearReactor(), "Meltdown");
    }
}

Executing Code in Another AppDomain
// YourCode.cs --> YourCode.dll
using System;

public class Init : MarshalByRefObject  {
    // entry point for victim's AppDomain
    public Init() {
        Console.WriteLine("YourCode is running in {0}",
            AppDomain.CurrentDomain.FriendlyName);
    }
}

public class NuclearReactor {
    // some function we don't want an attacker to call
    private static void Meltdown() {
        Console.WriteLine("Reactor meltdown!");
    }
}

public class SecretData {
    private static string TheData = "555-55-5555";
}

// MyCode.cs --> MyCode.dll
using System;
using System.Reflection;

public class Init : MarshalByRefObject {
    // entry point for attacker's AppDomain
    public Init(AppDomain target) {
        Console.WriteLine("MyCode is running in {0}",
            AppDomain.CurrentDomain.FriendlyName);
        Console.WriteLine("Injecting code into {0}...",
            target.FriendlyName);

        // here's how we inject the code
        target.DoCallBack(
            new CrossAppDomainDelegate(InjectedAttackCode));
    }
    public static void InjectedAttackCode() {
        Console.WriteLine("InjectedAttackCode in {0}",
            AppDomain.CurrentDomain.FriendlyName);

        // time to melt down the nuclear reactor using reflection
        Type t = Type.GetType("NuclearReactor, YourCode");
        MethodInfo mi = t.GetMethod("Meltdown",
            BindingFlags.Static | BindingFlags.NonPublic);
        mi.Invoke(null, null);

        // steal secret data from the victim
        t = Type.GetType("SecretData, YourCode");
        FieldInfo fi = t.GetField("TheData",
            BindingFlags.Static | BindingFlags.NonPublic);
        Console.WriteLine("Found a secret: {0}", fi.GetValue(null));
    }
}

// host.cs --> host.exe
using System;
using System.Reflection;

class Host {
    static void Main() {
        AppDomain victim = AppDomain.CreateDomain("Victim's Domain");
        AppDomain attacker = AppDomain.CreateDomain("Attacker's Domain");

        victim.CreateInstance("YourCode", "Init");
        attacker.CreateInstance("MyCode", "Init", false,
          BindingFlags.Public | BindingFlags.Instance,
          null, new object[]{victim}, null, null, null);
    }
}


--------------------next---------------------

阅读(375) | 评论(0) | 转发(0) |
0

上一篇:高精度时间供应器--源代码

下一篇:Figures

给主人留下些什么吧!~~