Exemple #1
0
        public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
        {
                                    context.Asm($"mov eax, [ebp+{(meth.ParamsCount * 4) - (4 * 2) + 4}]");
                        context.Asm("push eax");

        }
Exemple #2
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm($"pop eax");
     context.Asm($"mov ebx, [eax]");
     context.Asm($"push ebx");
 }
Exemple #3
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm("push 2");
 }
Exemple #4
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm(
         $"jmp _{Utils.MD5(meth.Namespace)}_{(instruction.Operand as Instruction).Offset}");
 }
Exemple #5
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     Logger.Debug($"Not Implemented: {instruction.OpCode.Code}");
 }
Exemple #6
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Comment("NOP");
 }
Exemple #7
0
        public static IRAssembly Build(ModuleDefMD module, UskrContext context, bool rk = false)
        {
            Logger.Log($"Building IR for module: {module.Name}");

            var re = new IRAssembly();

            if (!rk) //dont imbed resource in the kernel assembly
            {
                foreach (var resource in module.Resources)
                {
                    if (resource.ResourceType == ResourceType.Embedded &&
                        resource is EmbeddedResource er)
                    {
                        re.EmbeddedResources.Add(new IREmbedded()
                        {
                            Namespace = resource.Name,
                            Data      = er.CreateReader().ToArray()
                        });
                    }
                }
            }

            foreach (var type in module.Types)
            {
                if (type.IsGlobalModuleType)
                {
                    continue;
                }
                if (type.CustomAttributes.Any(x =>
                                              x.AttributeType.Name.ToString() == typeof(UskrIgnoreAttribute).Name))
                {
                    continue;
                }


                if (rk)
                {
                    if (type.CustomAttributes.All(x => x.AttributeType.Name.ToString() != typeof(RKAttribute).Name))
                    {
                        /*Logger.Debug(
                         *  $"Found Type {type.Name} but it does not have RK Attribute, and is there for being skipped");*/
                        continue;
                    }
                    else
                    {
                    }
                }

                Logger.Log($"Found Valid Type: {type.Name}");


                if (type.IsClass)
                {
                    foreach (var method in type.Methods)
                    {
                        if (!method.HasBody)
                        {
                            continue;
                        }

                        if (rk)
                        {
                            if (method.IsConstructor && !method.IsStaticConstructor)
                            {
                                Logger.Warn($"RK METH IS CTOR {method.FullName}");
                                continue;
                            }

                            if (!method.IsStatic)
                            {
                                Logger.Error($"RK METH NOT STATIC {method.FullName}");
                                Environment.Exit(0);
                            }
                        }

                        if (method.CustomAttributes.Any(x =>
                                                        x.AttributeType.Name.ToString() == typeof(UskrIgnoreAttribute).Name))
                        {
                            continue;
                        }


                        if (method.IsVirtual && !context.VirtualTypes.Contains(type))
                        {
                            context.VirtualTypes.Add(type);
                        }


                        var irMeth = new IRMethod();
                        irMeth.IsVirtual = (method.Attributes &
                                            MethodAttributes.VtableLayoutMask) == MethodAttributes.VtableLayoutMask;

                        irMeth.Namespace = method.FullName;
                        irMeth.IsFunc    = method.HasReturnType;
                        irMeth.IsStatic  = method.IsStatic;
                        irMeth.BaseType  = type.Name;
                        if (method.CustomAttributes.Any(x =>
                                                        x.AttributeType.Name.ToString() == typeof(PlugAttribute).Name))
                        {
                            var att = method.CustomAttributes
                                      .First(x => x.AttributeType.Name.ToString() == typeof(PlugAttribute).Name);
                            irMeth.Namespace = att.ConstructorArguments.First().Value.ToString();
                        }

                        if (method.CustomAttributes.Any(x =>
                                                        x.AttributeType.Name.ToString() == typeof(CCallAttribute).Name))
                        {
                            irMeth.CCall = true;
                        }

                        irMeth.Identifier  = method.Name;
                        irMeth.ParamsCount = method.Parameters.Count;
                        irMeth.Body        = method.Body;

                        re.Methods.Add(irMeth);
                    }

                    foreach (FieldDef member in type.Fields)
                    {
                        var irMem = new IRMember();
                        irMem.Namespace  = member.FullName;
                        irMem.Identifier = member.Name;
                        irMem.Static     = member.IsStatic;
                        irMem.IsField    = true;

                        irMem.Size = member.GetFieldSize();

                        irMem.InitValue = member.ResolveFieldDef().InitialValue;

                        re.Members.Add(irMem);
                    }

                    foreach (var member in type.Properties)
                    {
                        var irMem = new IRMember();
                        irMem.Namespace  = member.FullName;
                        irMem.Identifier = member.Name;
                        // Prop cant be static i think @Confirm
                        irMem.IsField = false;


                        re.Members.Add(irMem);
                    }
                }
            }

            return(re);
        }
Exemple #8
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm("pop eax");
     context.Asm("mov [ebp-4], eax");
 }
Exemple #9
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm($"push _{Utils.MD5((instruction.Operand as MethodDef).FullName)}");
 }
Exemple #10
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm($"mov eax, [_{Utils.MD5((instruction.Operand as FieldDef).FullName)}]");
     context.Asm($"push eax");
 }
Exemple #11
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm($"push {instruction.GetLdcI4Value()}");
 }
Exemple #12
0
        private void EmmitStatic(IRAssembly assembly, UskrContext context)
        {
            context.Comment();
            foreach (var method in assembly.Methods)
            {
                context.Comment(method.Identifier);
                context.Comment($"{method.Namespace}: ");


                if (method.CCall)
                {
                    context.Asm($"Global _{method.Identifier}");
                    context.Asm($"_{method.Identifier}: ");
                }

                context.Asm($"Global _{Utils.MD5(method.Namespace)}");
                context.Asm($"_{Utils.MD5(method.Namespace)}: ");

                context.Asm("push ebp");
                context.Asm("mov ebp,esp");
                context.Asm(
                    $"sub esp, {16 + (method.Body.Variables.Count * 4 > 16 ? (method.Body.Variables.Count * 4) - 16 : 0)}");

                //vmt logic
                if (method.IsVirtual)
                {
                    context.Asm($"cmp ebx, 0");
                    context.Asm($"je _{Utils.MD5(method.Namespace)}_start");

                    for (var i = 0; i < context.VirtualTypes.Count; i++)
                    {
                        var type = context.VirtualTypes[i];
                        context.Comment(type.Name);
                        context.Comment();
                        if (type.BaseType.Name == method.BaseType)
                        {
                            context.Asm($"mov eax, [ebp+8]");
                            context.Asm($"mov ebx, [eax]");
                            context.Asm($"cmp ebx, {i}");
                            context.Asm($"jne _{Utils.MD5(method.Namespace)}_{type.Name}_false");
                            context.Asm($"_{Utils.MD5(method.Namespace)}_{type.Name}_true: ");

                            foreach (var def in type.Methods)
                            {
                                if (def.Name == method.Identifier)
                                {
                                    context.Comment(def.FullName);
                                    context.Asm($"mov ebx, 0");
                                    context.Asm($"call _{Utils.MD5(def.FullName)}");
                                    break;
                                }
                            }

                            context.Asm($"jmp _{Utils.MD5(method.Namespace)}_exit");
                            context.Asm($"_{Utils.MD5(method.Namespace)}_{type.Name}_false: ");
                        }
                    }


                    context.Asm($"jmp _{Utils.MD5(method.Namespace)}_exit");

                    context.Asm($"_{Utils.MD5(method.Namespace)}_start: ");
                }

                EmmitStaticIL(assembly, method, context);

                if (method.IsVirtual)
                {
                    context.Asm($"_{Utils.MD5(method.Namespace)}_exit: ");
                    context.Asm($"leave");
                    context.Asm($"ret");
                }

                context.Comment();
            }
        }
Exemple #13
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     Logger.Debug($"Not Implemented: {instruction.OpCode.Code}");
     //@Reflections this is used by reflections and is stupidly compicated for now
 }
Exemple #14
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm("pop eax");             //value
     context.Asm("pop ebx");             //adress
     context.Asm("mov [ebx],  eax");
 }
Exemple #15
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     // context.Asm($"mov eax, [ebp+{instruction.}]");
     //     context.Asm("push eax");
     throw new Exception(); //@need an test case to see wtf
 }
Exemple #16
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm($"push {instruction.Operand.ToString()}");
 }
Exemple #17
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     throw new Exception();                         //@need an test case to see wtf
 }
Exemple #18
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm($"mov eax, [ebp-{4 * 2}]");
     context.Asm("push eax");
 }
Exemple #19
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm($"mov eax, ebp");
     context.Asm($"sub eax, {4 * ((instruction.Operand as Local).Index + 1)}");
     context.Asm($"push eax");
 }
Exemple #20
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     context.Asm($"mov eax, [ebp-{4 + (4 * (instruction.Operand as Local).Index)}]");
     context.Asm("push eax");
 }
Exemple #21
0
 public void Handel(IRAssembly assembly, IRMethod meth, UskrContext context, Instruction instruction)
 {
     Logger.Debug($"Not Implemented: {instruction.OpCode.Code}");
     //@Incomplete need to create an embeded resource that stores this information to return
 }