示例#1
0
        protected T Parse <T>(AugmentrexContext context, string[] args)
            where T : class
        {
            return(new Parser(opts =>
            {
                opts.AutoHelp = false;
                opts.AutoVersion = false;
                opts.CaseInsensitiveEnumValues = true;
                opts.CaseSensitive = false;
            }).ParseArguments <T>(args).MapResult(
                       result => result,
                       errors =>
            {
                var builder = SentenceBuilder.Create();

                foreach (var error in errors)
                {
                    context.ErrorLine("{0}", builder.FormatError(error));
                }

                context.Line();
                PrintHelp(context, this, true);
                context.Line();

                return null;
            }));
        }
示例#2
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            context.Ipc.Channel.Clear();
            Console.Clear();

            return(null);
        }
示例#3
0
#pragma warning restore IDE1006 // Naming Styles

            internal Globals(AugmentrexContext context)
            {
                ctx  = context;
                host = context.Host;
                game = context.Game;
                mem  = context.Memory;
                cfg  = context.Configuration;
                keys = context.HotKeys;
            }
示例#4
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            PluginManager.UnloadPlugins();

            GC.Collect(GC.MaxGeneration);
            GC.WaitForPendingFinalizers();

            PluginManager.LoadPlugins(context);

            return(null);
        }
示例#5
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            if (!Debugger.IsAttached)
            {
                Debugger.Launch();
            }

            Debugger.Break();

            return(null);
        }
示例#6
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            var opts = Parse <HelpOptions>(context, args);

            if (opts == null)
            {
                return(null);
            }

            if (opts.Commands.Any())
            {
                var cmds    = opts.Commands.Select(x => (x, CommandInterpreter.GetCommand(x)));
                var unknown = false;

                foreach (var(name, cmd) in cmds)
                {
                    if (cmd == null)
                    {
                        unknown = true;
                        context.ErrorLine("Unknown command '{0}'.", name);
                    }
                }

                if (unknown)
                {
                    return(null);
                }

                context.Line();

                foreach (var(_, cmd) in cmds)
                {
                    PrintHelp(context, cmd, true);
                    context.Line();
                }
            }
            else
            {
                context.Line();
                context.InfoLine("Available commands:");
                context.Line();

                foreach (var cmd in CommandInterpreter.Commands)
                {
                    PrintHelp(context, cmd, false);
                    context.Line();
                }
            }

            return(null);
        }
示例#7
0
        protected static void PrintHelp(AugmentrexContext context, Command command, bool details)
        {
            context.SuccessLine("  {0} {1}", string.Join("|", command.Names), command.Syntax);
            context.InfoLine("    {0}", command.Description);

            if (details && command.Details.Count != 0)
            {
                foreach (var detail in command.Details)
                {
                    context.Line();
                    context.InfoLine("    {0}", detail);
                }
            }
        }
示例#8
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            var opts = Parse <DisassembleOptions>(context, args);

            if (opts == null)
            {
                return(null);
            }

            var parsed  = (int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromString(opts.Offset);
            var address = opts.Absolute ? (MemoryAddress)parsed : context.Memory.ToAddress((MemoryOffset)parsed);

            using var disasm = new Disassembler(address, (int)opts.Length, ArchitectureMode.x86_32, (uint)address, true);
            var         insns = new List <Instruction>();
            Instruction insn;
            string      error = null;

            while ((insn = disasm.NextInstruction()) != null)
            {
                if (insn.Error)
                {
                    error = insn.ErrorMessage;
                    break;
                }

                insns.Add(insn);
            }

            var length = insns.Max(x => x.Bytes.Length);

            foreach (var i in insns)
            {
                i.PrintColored(context, length);
            }

            if (error != null)
            {
                if (insns.Count != 0)
                {
                    context.Line();
                }

                context.WarningLine("Could not disassemble further: {0}", error);
            }

            return(null);
        }
示例#9
0
 public override int?Run(AugmentrexContext context, string[] args)
 {
     if (_hook == null)
     {
示例#10
0
        public static void PrintColored(this Instruction instruction, AugmentrexContext context, int length)
        {
            const ConsoleColor TriviaColor   = ConsoleColor.DarkGray;
            const ConsoleColor AddressColor  = ConsoleColor.White;
            const ConsoleColor BinaryColor   = ConsoleColor.DarkCyan;
            const ConsoleColor MnemonicColor = ConsoleColor.DarkYellow;
            const ConsoleColor CastColor     = ConsoleColor.DarkMagenta;
            const ConsoleColor RegisterColor = ConsoleColor.DarkGreen;
            const ConsoleColor LiteralColor  = ConsoleColor.DarkRed;

            context.Color(AddressColor, "{0:X8} ", instruction.Offset);
            context.Color(BinaryColor, $"{{0,-{length * 2 + length - 1}}} ",
                          string.Join(" ", instruction.Bytes.Select(x => x.ToString("X2"))));

            var dynIns = Exposed.From(instruction);

            string GetCast(Operand operand)
            {
                var value = dynIns.br_far != 0 ? "far " : string.Empty;

                switch (operand.Size)
                {
                case 8:
                    value += "byte ";
                    break;

                case 16:
                    value += "word ";
                    break;

                case 32:
                    value += "dword ";
                    break;

                case 64:
                    value += "qword ";
                    break;

                case 80:
                    value += "tword ";
                    break;

                case 128:
                    value += "oword ";
                    break;

                case 256:
                    value += "yword ";
                    break;

                case 512:
                    value += "zword ";
                    break;
                }

                return(value);
            }

            var trans = new HackyTranslator();

            void HandleOperand(Operand operand, int index)
            {
                var cast = false;

                switch (index)
                {
                case 0:
                    if (operand.Type == ud_type.UD_OP_MEM)
                    {
                        var next = instruction.Operands.Skip(1).FirstOrDefault();

                        if (next != null && next.Type != ud_type.UD_OP_IMM &&
                            next.Type != ud_type.UD_OP_CONST && next.Size == operand.Size)
                        {
                            if (next.Type == ud_type.UD_OP_REG && next.Base == ud_type.UD_R_CL)
                            {
                                switch (instruction.Mnemonic)
                                {
                                case ud_mnemonic_code.UD_Ircl:
                                case ud_mnemonic_code.UD_Irol:
                                case ud_mnemonic_code.UD_Iror:
                                case ud_mnemonic_code.UD_Ircr:
                                case ud_mnemonic_code.UD_Ishl:
                                case ud_mnemonic_code.UD_Ishr:
                                case ud_mnemonic_code.UD_Isar:
                                    cast = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            cast = true;
                        }
                    }
                    break;

                case 1:
                    if (operand.Type == ud_type.UD_OP_MEM &&
                        operand.Size != instruction.Operands[0].Size)
                    {
                        cast = true;
                    }
                    break;

                case 2:
                    if (operand.Type == ud_type.UD_OP_MEM &&
                        operand.Size != instruction.Operands[1].Size)
                    {
                        cast = true;
                    }
                    break;

                case 3:
                    break;
                }

                switch (operand.Type)
                {
                case ud_type.UD_OP_REG:
                    context.Color(RegisterColor, trans.GetRegisterName(operand.Base));
                    break;

                case ud_type.UD_OP_MEM:
                    if (cast)
                    {
                        context.Color(CastColor, GetCast(operand));
                    }

                    context.Color(TriviaColor, "[");

                    if (dynIns.pfx_seg != 0)
                    {
                        context.Color(RegisterColor, trans.GetRegisterName((ud_type)dynIns.pfx_seg));
                        context.Color(TriviaColor, ":");
                    }

                    if (operand.Base != ud_type.UD_NONE)
                    {
                        context.Color(RegisterColor, trans.GetRegisterName(operand.Base));
                    }

                    if (operand.Index != ud_type.UD_NONE)
                    {
                        if (operand.Base != ud_type.UD_NONE)
                        {
                            context.Color(TriviaColor, "+");
                        }

                        context.Color(RegisterColor, trans.GetRegisterName(operand.Index));

                        if (operand.Scale != 0)
                        {
                            context.Color(TriviaColor, "*");
                            context.Color(RegisterColor, "{0}", operand.Scale);
                        }
                    }

                    if (operand.Offset != 0)
                    {
                        if (operand.Base != ud_type.UD_NONE || operand.Index != ud_type.UD_NONE)
                        {
                            var value = operand.LvalSQWord;

                            if (value > 0)
                            {
                                context.Color(TriviaColor, "+");
                            }
                            else
                            {
                                context.Color(TriviaColor, "-");
                            }

                            context.Color(LiteralColor, "0x{0:X}", value);
                        }
                        else
                        {
                            context.Color(LiteralColor, "0x{0:X}", operand.LvalUQWord);
                        }
                    }

                    context.Color(TriviaColor, "]");
                    break;

                case ud_type.UD_OP_PTR:
                    if (operand.Size == 32)
                    {
                        context.Color(CastColor, "word ");
                        context.Color(LiteralColor, "0x{0:X}:0x{1:X}", operand.PtrSegment, operand.PtrOffset & 0xffff);
                    }
                    else if (operand.Size == 48)
                    {
                        context.Color(CastColor, "dword ");
                        context.Color(LiteralColor, "0x{0:X}:0x{1:X}", operand.PtrSegment, operand.PtrOffset);
                    }
                    break;

                case ud_type.UD_OP_IMM:
                    var imm = operand.LvalUQWord;

                    if (operand.Opcode == ud_operand_code.OP_sI &&
                        operand.Size != dynIns.opr_mode &&
                        dynIns.opr_mode < 64)
                    {
                        imm &= (1ul << dynIns.opr_mode) - 1;
                    }

                    context.Color(LiteralColor, "0x{0:X}", imm);
                    break;

                case ud_type.UD_OP_JIMM:
                    context.Color(LiteralColor, "0x{0:X}", trans.GetRelativeJumpTarget(instruction, operand));
                    break;

                case ud_type.UD_OP_CONST:
                    if (cast)
                    {
                        context.Color(CastColor, GetCast(operand));
                    }

                    context.Color(LiteralColor, "{0}", operand.LvalUDWord);
                    break;
                }
            }

            var dynBitOps = Exposed.From(typeof(Disassembler).Assembly.GetType("SharpDisasm.Udis86.BitOps"));

            if (dynBitOps.P_OSO((uint)dynIns.itab_entry.Prefix) == 0 && dynIns.pfx_opr != 0)
            {
                switch (instruction.dis_mode)
                {
                case ArchitectureMode.x86_16:
                    context.Color(MnemonicColor, "o32 ");
                    break;

                case ArchitectureMode.x86_64:
                    context.Color(MnemonicColor, "o16 ");
                    break;
                }
            }

            if (dynBitOps.P_ASO((uint)dynIns.itab_entry.Prefix) == 0 && dynIns.pfx_opr != 0)
            {
                switch (instruction.dis_mode)
                {
                case ArchitectureMode.x86_16:
                    context.Color(MnemonicColor, "a32 ");
                    break;

                case ArchitectureMode.x86_32:
                    context.Color(MnemonicColor, "a16 ");
                    break;

                case ArchitectureMode.x86_64:
                    context.Color(MnemonicColor, "a32 ");
                    break;
                }
            }

            if (dynIns.pfx_seg != 0 && instruction.Operands.Length >= 2 &&
                instruction.Operands[0].Type != ud_type.UD_OP_MEM &&
                instruction.Operands[1].Type != ud_type.UD_OP_MEM)
            {
                context.Color(RegisterColor, "{0} ", trans.GetRegisterName((ud_type)dynIns.pfx_seg));
            }

            if (dynIns.pfx_lock != 0)
            {
                context.Color(MnemonicColor, "lock ");
            }

            if (dynIns.pfx_rep != 0)
            {
                context.Color(MnemonicColor, "rep ");
            }
            else if (dynIns.pfx_repe != 0)
            {
                context.Color(MnemonicColor, "repe ");
            }
            else if (dynIns.pfx_repne != 0)
            {
                context.Color(MnemonicColor, "repne ");
            }

            context.Color(MnemonicColor, "{0}", udis86.ud_lookup_mnemonic(instruction.Mnemonic));

            for (var i = 0; i < instruction.Operands.Length; i++)
            {
                var op = instruction.Operands[i];

                if (i != 0)
                {
                    context.Color(TriviaColor, ",");
                }

                context.Info(" ");

                HandleOperand(op, i);
            }

            context.Line();
        }
示例#11
0
 public CommandInterpreter(AugmentrexContext context)
 {
     Context = context;
 }
示例#12
0
        public unsafe override int?Run(AugmentrexContext context, string[] args)
        {
            var opts = Parse <ReadOptions>(context, args);

            if (opts == null)
            {
                return(null);
            }

            var parsed = (int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromString(opts.Offset);
            var memory = context.Memory;
            var offset = opts.Absolute ? memory.ToOffset((MemoryAddress)parsed) : (MemoryOffset)parsed;

            switch (opts.Type)
            {
            case ReadType.I8:
                var i8 = memory.Read <sbyte>(offset);
                context.InfoLine("{0} (0x{0:X})", i8);
                break;

            case ReadType.U8:
                var u8 = memory.Read <byte>(offset);
                context.InfoLine("{0} (0x{0:X})", u8);
                break;

            case ReadType.I16:
                var i16 = memory.Read <short>(offset);
                context.InfoLine("{0} (0x{0:X})", i16);
                break;

            case ReadType.U16:
                var u16 = memory.Read <ushort>(offset);
                context.InfoLine("{0} (0x{0:X})", u16);
                break;

            case ReadType.I32:
                var i32 = memory.Read <int>(offset);
                context.InfoLine("{0} (0x{0:X})", i32);
                break;

            case ReadType.U32:
                var u32 = memory.Read <uint>(offset);
                context.InfoLine("{0} (0x{0:X})", u32);
                break;

            case ReadType.I64:
                var i64 = memory.Read <long>(offset);
                context.InfoLine("{0} (0x{0:X})", i64);
                break;

            case ReadType.U64:
                var u64 = memory.Read <ulong>(offset);
                context.InfoLine("{0} (0x{0:X})", u64);
                break;

            case ReadType.F32:
                var f32 = memory.Read <float>(offset);
                context.InfoLine("{0} (0x{0:X})", f32, Unsafe.Read <uint>(&f32));
                break;

            case ReadType.F64:
                var f64 = memory.Read <double>(offset);
                context.InfoLine("{0} (0x{0:X})", f64, Unsafe.Read <uint>(&f64));
                break;

            case ReadType.Ptr:
                var ptr = memory.ReadOffset(offset);
                context.InfoLine("{0}{1}={2}", memory.Address, ptr, memory.Address + ptr);
                break;
            }

            return(null);
        }
示例#13
0
 public abstract int?Run(AugmentrexContext context, string[] args);
示例#14
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            var opts = Parse <WriteOptions>(context, args);

            if (opts == null)
            {
                return(null);
            }

            var parsed = (int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromString(opts.Offset);

            long  ivalue = 0;
            ulong uvalue = 0;
            var   fvalue = 0.0f;
            var   dvalue = 0.0d;
            var   ovalue = default(MemoryOffset);

            switch (opts.Type)
            {
            case WriteType.I8:
            case WriteType.I16:
            case WriteType.I32:
            case WriteType.I64:
                ivalue = (long)TypeDescriptor.GetConverter(typeof(long)).ConvertFromString(opts.Value);
                break;

            case WriteType.U8:
            case WriteType.U16:
            case WriteType.U32:
            case WriteType.U64:
                uvalue = (ulong)TypeDescriptor.GetConverter(typeof(ulong)).ConvertFromString(opts.Value);
                break;

            case WriteType.F32:
                fvalue = (float)TypeDescriptor.GetConverter(typeof(float)).ConvertFromString(opts.Value);
                break;

            case WriteType.F64:
                dvalue = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromString(opts.Value);
                break;

            case WriteType.Ptr:
                ovalue = (MemoryOffset)(int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromString(opts.Value);
                break;
            }

            var memory = context.Memory;
            var offset = opts.Absolute ? memory.ToOffset((MemoryAddress)parsed) : (MemoryOffset)parsed;

            switch (opts.Type)
            {
            case WriteType.I8:
                memory.Write(offset, (sbyte)ivalue);
                break;

            case WriteType.U8:
                memory.Write(offset, (byte)uvalue);
                break;

            case WriteType.I16:
                memory.Write(offset, (short)ivalue);
                break;

            case WriteType.U16:
                memory.Write(offset, (ushort)uvalue);
                break;

            case WriteType.I32:
                memory.Write(offset, (int)ivalue);
                break;

            case WriteType.U32:
                memory.Write(offset, (uint)uvalue);
                break;

            case WriteType.I64:
                memory.Write(offset, ivalue);
                break;

            case WriteType.U64:
                memory.Write(offset, uvalue);
                break;

            case WriteType.F32:
                memory.Write(offset, fvalue);
                break;

            case WriteType.F64:
                memory.Write(offset, dvalue);
                break;

            case WriteType.Ptr:
                memory.WriteOffset(offset, ovalue);
                break;
            }

            return(null);
        }
示例#15
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            var opts = Parse <KeyOptions>(context, args);

            if (opts == null)
            {
                return(null);
            }

            if (!opts.Add && !opts.Delete)
            {
                foreach (var kvp in _bindings)
                {
                    context.InfoLine("{0} = {1}", kvp.Key, kvp.Value);
                }

                return(null);
            }

            var info = new HotKeyInfo(opts.Key, opts.Alt, opts.Control, opts.Shift);

            if (_handler == null)
            {
                void KeyHandler(HotKeyInfo info)
                {
                    var freq = context.Configuration.HotKeyBeepFrequency;

                    if (freq != 0)
                    {
                        context.Ipc.Channel.Beep(freq, context.Configuration.HotKeyBeepDuration);
                    }

                    if (_bindings.TryGetValue(info, out var command))
                    {
                        context.Interpreter.RunCommand(command, true);
                    }
                }

                _handler = new HotKeyHandler(KeyHandler);
            }

            if (opts.Add)
            {
                var command = string.Join(" ", opts.Fragments);

                if (string.IsNullOrWhiteSpace(command))
                {
                    context.ErrorLine("No command given.");

                    return(null);
                }

                if (_bindings.TryAdd(info, command))
                {
                    context.HotKeys.Add(info, _handler);

                    context.InfoLine("Added key binding: {0} = {1}", info, command);
                }
                else
                {
                    context.ErrorLine("Key binding already exists: {0} = {1}", info, _bindings[info]);
                }
            }

            if (opts.Delete)
            {
                if (_bindings.Remove(info, out var command))
                {
                    context.InfoLine("Deleted key binding: {0} = {1}", info, command);
                }
                else
                {
                    context.ErrorLine("Key binding not found: {0}", info);
                }
            }

            return(null);
        }
示例#16
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            var opts = Parse <EvaluateOptions>(context, args);

            if (opts == null)
            {
                return(null);
            }

            if (_script == null || opts.Clear)
            {
                _script = CSharpScript.Create(string.Empty,
                                              ScriptOptions.Default.WithFilePath("<hgl>")
                                              .WithImports(ScriptOptions.Default.Imports.AddRange(
                                                               Assembly.GetExecutingAssembly().DefinedTypes.Select(x => x.Namespace).Where(x => x != null).Distinct()))
                                              .WithLanguageVersion(LanguageVersion.CSharp8)
                                              .WithReferences(Assembly.GetExecutingAssembly()),
                                              typeof(Globals));
            }

            var code = string.Join(" ", opts.Fragments);

            if (string.IsNullOrWhiteSpace(code))
            {
                return(null);
            }

            Script script = _script.ContinueWith(code);

            Task <ScriptState> task;

            try
            {
                task = script.RunAsync(new Globals(context));
            }
            catch (CompilationErrorException ex)
            {
                foreach (var diag in ex.Diagnostics)
                {
                    context.ErrorLine("{0}", diag.ToString());
                }

                return(null);
            }

            try
            {
                task.Wait();
            }
            catch (AggregateException ex)
            {
                context.ErrorLine("{0}", ex.InnerException);

                return(null);
            }

            _script = script;

            var result = task.Result.ReturnValue;

            context.InfoLine("{0} it = {1}", result?.GetType() ?? typeof(object), result ?? "null");

            return(null);
        }
示例#17
0
 public override int?Run(AugmentrexContext context, string[] args)
 {
     return(0);
 }
示例#18
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            context.Ipc.Channel.KillRequested = true;

            return(0);
        }
示例#19
0
 public virtual void Start(AugmentrexContext context)
 {
 }
示例#20
0
 public override void Stop(AugmentrexContext context)
 {
     context.InfoLine("Simple test plugin stopped.");
 }