欢迎光临 Rick 's BLOG
日志首页  | C# .Net编程  | 原创作品  | 生活点滴  | C\C++相关  | 多媒体相关※ERMP  | VB相关  | 其它运维与编程  |  留言簿
BBSFTP V2.8 FTP流量插件 FOR PHPWIND 5.3[20070609更新] .Net 反射脱壳机代码核心代码详解
未知  .Net 反射脱壳机核心源代码
[ 发布日期:18年前 (2007-06-30) ]   [ 来自:rick@博客园 ] [分类:原创作品]
入口函数 void DumpAssembly(Assembly ass,string path) 枚举所有type,
调用 void DumpType(Type tp, BinaryWriter sw) 枚举所有方法,
调用 void DumpMethod(MethodBase mb, BinaryWriter sw) { MethodBody mbd = mb.GetMethodBody(); if (mbd == null) return; SetOffset(sw, mb.MetadataToken); 
WriteHeader(sw, mbd); WriteILCode(sw, mbd); WriteSEH(sw, mbd); }

[复制到剪贴板]


sing System
;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
namespace 
testdd
{
    public class Class1
    
{
        private bool IsTiny(MethodBody mbd)
        {
            if(mbd.MaxStackSize>8)
                return false;//
            //if(mbd.LocalSignatureMetadataToken != 0)
            //    return false;
            if(mbd.LocalVariables.Count>0)
                return false;
            if(mbd.ExceptionHandlingClauses.Count>0)
                return false;
            if(mbd.GetILAsByteArray().Length>63)
                return false;
            return true;
        }

        private bool IsSEHTiny(MethodBody mb)
        {
            int n mb.ExceptionHandlingClauses.Count;
            int datasize 12 4;
            if (datasize 255)
                return false;
            foreach(ExceptionHandlingClause ehc in mb.ExceptionHandlingClauses)
            {
                if (ehc.HandlerLength 255)
                    return false;
                if (ehc.TryLength 255)
                    return false;
                if (ehc.TryOffset 65535)
                    return false;
                if (ehc.HandlerOffset 65535)
                    return false;
            }
            return true;
        }
        private void WriteHeader(BinaryWriter bw,MethodBody mb)
        {
            int codesize mb.GetILAsByteArray().Length;
            if(IsTiny(mb))
            {
                byte bt 2;
                bt = (byte)(bt codesize 4);
                bw.Write(bt);
                return;
            }
            //fat mode here
            byte fg 3;//fat flag
            if (mb.LocalVariables.Count && mb.InitLocals)
                fg |= 0x10;
            if (mb.ExceptionHandlingClauses.Count 0)
                fg |= 0x8;
            bw.Write(fg);// byte 1           
            bw.Write((byte)0x30);//byte 2
            bw.Write((ushort)mb.MaxStackSize);// byte 3, 4
            bw.Write(codesize);//byte 5-8
            bw.Write(mb.LocalSignatureMetadataToken);//byte 9-12
        }
        private void WriteILCode(BinaryWriter bw,MethodBody mb)
        {
            int codesize mb.GetILAsByteArray().Length;
            bw.Write(mb.GetILAsByteArray());

            //对齐 4 bytes
            int ig codesize 3;
            if (ig == 0)
                return;
            if (mb.ExceptionHandlingClauses.Count == 0)
                return;//无SEH;
            ig ig;
            for(int i=0i<ig;i++)
            {
                bw.Write((byte)0);
            }
        }
        private void WriteTinySEHHeader(BinaryWriter bw,MethodBody mb)
        {
            int n mb.ExceptionHandlingClauses.Count;
            int datasize 12 4;
            bw.Write((byte)1);
            bw.Write((byte)datasize);
            bw.Write((byte)0);
            bw.Write((byte)0);
        }
        private void WriteFatSEHHeader(BinaryWriter bwMethodBody mb)
        {
            int n mb.ExceptionHandlingClauses.Count;
            int datasize 24 4;
            datasize datasize 0x100 0x41;
            bw.Write(datasize);
        }
        private void WriteSeHTinyRow(BinaryWriter bw,ExceptionHandlingClause ehc)
        {
            ushort flag 0;
          
            
if (ehc.Flags == ExceptionHandlingClauseOptions.Filter)
                flag += 1;
            if (ehc.Flags == ExceptionHandlingClauseOptions.Fault)
                flag += 4;
            if (ehc.Flags == ExceptionHandlingClauseOptions.Finally)
                flag += 2;
            bw.Write(flag);

            bw.Write((ushort)ehc.TryOffset);
            bw.Write((byte)ehc.TryLength);

            bw.Write((ushort)ehc.HandlerOffset);
            bw.Write((byte)ehc.HandlerLength);
            object obj = new object();
            if (ehc.Flags == ExceptionHandlingClauseOptions.Clause /*|| ehc.CatchType != obj.GetType()*/)
                bw.Write(GetTypeToken(ehc.CatchType));
            else
                bw.Write(ehc.FilterOffset);

        }

        private void WriteSeHFatRow(BinaryWriter bwExceptionHandlingClause ehc)
        {
            int flag 0;
          
            
if (ehc.Flags == ExceptionHandlingClauseOptions.Filter)
                flag += 1;
            if (ehc.Flags == ExceptionHandlingClauseOptions.Fault)
                flag += 4;
            if (ehc.Flags == ExceptionHandlingClauseOptions.Finally)
                flag += 2;
            bw.Write(flag);//
           
            bw
.Write(ehc.TryOffset);
            bw.Write(ehc.TryLength);

            bw.Write(ehc.HandlerOffset);
            bw.Write(ehc.HandlerLength);
            object obj = new object();
            if (ehc.Flags == ExceptionHandlingClauseOptions.Clause /*|| ehc.CatchType != obj.GetType()*/)
                bw.Write(GetTypeToken(ehc.CatchType));
            else
                bw.Write(ehc.FilterOffset);
           

        
}
   
        
private void WriteSEH(BinaryWriter bw,MethodBody mb)
        {
            if (mb.ExceptionHandlingClauses.Count == 0)
                return;
            bool bTiny IsSEHTiny(mb);
            if (bTiny)
                WriteTinySEHHeader(bwmb);
            else
                WriteFatSEHHeader(bwmb);
            foreach (ExceptionHandlingClause ehc in mb.ExceptionHandlingClauses)
            {
                if (bTiny)
                    WriteSeHTinyRow(bwehc);
                else
                    WriteSeHFatRow(bwehc);
            }
        }

      
        
public static void Dump()
        {
            Class1 cls = new Class1();
            cls.DoIt();
        }
        public Class1()
        {
            //nil
            int i 0;
            try
            {
                string s "";
                if (== "")
                    i 2;

            }
            catch(Exception ex)
            {
                MessageBox.Show("err" ex.ToString());
            }
        }

        protected void DoIt()
        {
            Assembly ass Assembly.GetEntryAssembly();
            DumpAssembly(ass,@"D:\4.0.1.0\dumped.exe");
          
        
}

        /// <summary>
        /// Dump程序集的 IL字节代码到指定目录;
        /// </summary>
        /// <param name="ass"></param>
        /// <param name="path"></param>
        private void DumpAssembly(Assembly ass,string path)
        {
            //////////////////////////////////////////////////////////////////////////
            if(!testdd.com.WrapperClass.MetaInit(ass.Location))
            {
                MessageBox.Show("error meta");
                return;
            }
            FileStream fs = new FileStream(pathSystem.IO.FileMode.Open,FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);

            Type[] tps ass.GetTypes();
            for(int i=0itps.Lengthi++)
            {
                DumpType(tps[i], bw);
            }
            bw.Flush();
            bw.Close();
            bw null;
            fs.Close();
            fs null;
            MessageBox.Show("ok");
        }
        private void DumpType(Type tpBinaryWriter sw)
        {
            BindingFlags bf BindingFlags.NonPublic BindingFlags.DeclaredOnly |
               BindingFlags.Public | BindingFlags.Static
               BindingFlags.Instance;

           
            MemberInfo
[] mbis tp.GetMembers(bf);
            for (int i 0mbis.Lengthi++)
            {
                MemberInfo mbi mbis[i];               
               
                
try
                {
                    if (mbi.MemberType == MemberTypes.Method || mbi.MemberType == MemberTypes.Constructor)
                    {
                        DumpMethod((MethodBase)mbisw);
                    }
                }
                catch(Exception)
                {
                  
                
}

            }
          
        
}

        private void DumpMethod(MethodBase mbBinaryWriter sw)
        {
            MethodBody mbd mb.GetMethodBody();
            if (mbd == null)
                return;
            SetOffset(swmb.MetadataToken);

            WriteHeader(swmbd);

            WriteILCode(swmbd);

            WriteSEH(swmbd);  

        
}
        private int GetTypeToken(Type tp)
        {
            if (tp.Assembly == Assembly.GetEntryAssembly())
                return tp.MetadataToken;
            Assembly ass Assembly.GetEntryAssembly();
            uint tk testdd.com.WrapperClass.GetTypeToken(tp);
            if(tk == 0)
            {
                MessageBox.Show("error tk");
                return 0x100005f;
            }
            return (int)tk;
        }
        private void SetOffset(BinaryWriter bwint mbtk)
        {
            uint token = (uint)mbtk;
            uint offsetrva testdd.com.WrapperClass.GetMehodRVA(token);
            int offsetra = (int)(offsetrva 0x1000);
            bw.Seek(offsetraSeekOrigin.Begin);
        }
    }

   
}
 


引用通告地址 (0):
复制引用地址https://www.rickw.cn/trackback/168
复制引用地址https://www.rickw.cn/trackback/168/GBK
[ 分类:原创作品  | 查看:2878 ]

暂时没有评论,快来发表一个评论吧。
发表评论
作者:   用户:[访客] 
评论:

表  情
禁止表情 | 禁止UBB | 禁止图片 | 识别链接
对不起,你没有权限上传附件!
验证:
 
PoweredBy R-Blog V1.00 © 2004-2024 WWW.RICKW.CN, Processed in second(s) , 7 queries    京ICP备17058477号-5