Exemplo n.º 1
0
        public override void Execute(string text)
        {
            base.Execute(text);

            char[]   sp   = new char[] { ' ', '\t' };
            string[] argv = text.Split(sp, 2, StringSplitOptions.RemoveEmptyEntries);

            if (argv.Length < 2)
            {
                Context.CONSOLE.Write(CommandHelp);
                return;
            }

            // check for optional parameters
            string strArgParam  = argv[0];
            string strArgFile   = "";
            string strArgSource = "";
            string strArgSize   = "";
            char   chrFlag      = '\0';

            if ('-' == strArgParam[0])
            {
                // max 2 characters for flags
                if (2 == strArgParam.Length)
                {
                    chrFlag = Char.ToLower(strArgParam[1]);
                }
                else
                {
                    Context.CONSOLE.Write("Specified flag: " + strArgParam + " is invalid!");
                    return;
                }
            }

            int reqNArg = ('\0' == chrFlag) ? 3 : 4;

            argv = text.Split(sp, reqNArg, StringSplitOptions.RemoveEmptyEntries);

            if (argv.Length != reqNArg)
            {
                Context.CONSOLE.Write(CommandHelp);
                return;
            }


            strArgFile   = ('\0' == chrFlag) ? argv[0] : argv[1];
            strArgSource = ('\0' == chrFlag) ? argv[1] : argv[2];
            strArgSize   = ('\0' == chrFlag) ? argv[2] : argv[3];

            // get file path
            string strPath = Path.GetDirectoryName(strArgFile);

            // check if we have full path
            if (strPath != string.Empty)
            {
                // create folder if it doesn`t exist
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }
            }
            // then this was meant to be in the working directory
            else
            {
                strArgFile = Path.Combine(Context.Settings.GeneralSettings.WorkingDirectory, strArgFile);

                strPath = Path.GetDirectoryName(strArgFile);

                // create folder if it doesn`t exist
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }
            }

            // if append or force is not specified, return error
            if (File.Exists(strArgFile) && !(tkn_force == chrFlag || tkn_append == chrFlag))
            {
                Context.CONSOLE.Write("Output file name: " + strArgFile + " is in use!");
                Context.CONSOLE.Write("Use -" + tkn_force + "/-" + tkn_append + " to overwrite/append");
                return;
            }

            var varArgSource = Context.IDE.Debugger.GetExpression(strArgSource, false, 100);
            var varArgSize   = Context.IDE.Debugger.GetExpression(strArgSize, false, 100);
            int processId    = Context.IDE.Debugger.CurrentProcess.ProcessID;

            if (!varArgSource.IsValidValue)
            {
                Context.CONSOLE.Write("Address: <" + strArgSource + "> is invalid!");
                return;
            }

            if (!varArgSize.IsValidValue)
            {
                Context.CONSOLE.Write("Size: <" + strArgSize + "> is invalid!");
                return;
            }

            long         startAddress   = 0;
            long         dataSize       = 0;
            NumberStyles numStyleSource = NumberHelpers.IsHexNumber(varArgSource.Value) ? NumberStyles.HexNumber : NumberStyles.Integer;
            NumberStyles numStyleSize   = NumberHelpers.IsHexNumber(varArgSize.Value) ? NumberStyles.HexNumber : NumberStyles.Integer;
            bool         bRet           = true;


            bRet = bRet && NumberHelpers.TryParseLong(varArgSource.Value, numStyleSource, out startAddress);
            bRet = bRet && NumberHelpers.TryParseLong(varArgSize.Value, numStyleSize, out dataSize);

            if (!bRet)
            {
                Context.CONSOLE.Write("Failed to evaluate command arguments!");
                return;
            }

            FileMode fileMode = FileMode.Create;

            // if force flag is specified, and the file exists, then delete it
            if (tkn_force == chrFlag && File.Exists(strArgFile))
            {
                File.Delete(strArgFile);
            }

            if (tkn_append == chrFlag && File.Exists(strArgFile))
            {
                fileMode = FileMode.Append;
            }

            if (!DebugHelpers.IsMiniDumpProcess(Context.IDE.Debugger.CurrentProcess))
            {
                int ntdbgStatus = NativeMethods.NTDBG_OK;
                if (NativeMethods.NTDBG_OK != (ntdbgStatus = MemoryHelpers.WriteMemoryToFile(strArgFile, processId, startAddress, dataSize, fileMode)))
                {
                    File.Delete(strArgFile);

                    Context.CONSOLE.Write("Couldn`t dump memory to file!");
                    Context.CONSOLE.Write("Error code:" + ntdbgStatus.ToString() + " - " + NativeMethods.GetStatusString(ntdbgStatus) + ".");
                    return;
                }
            }
            else
            {
                bRet = MemoryHelpers.WriteMemoryToFile(strArgFile,
                                                       DkmMethods.GetDkmProcess(Context.IDE.Debugger.CurrentStackFrame),
                                                       startAddress,
                                                       dataSize,
                                                       fileMode);
                if (!bRet)
                {
                    Context.CONSOLE.Write("Couldn`t dump memory to file!");
                    return;
                }
            }

            Context.CONSOLE.Write("Wrote: " + dataSize.ToString() + " bytes to: " + MiscHelpers.GetClickableFileName(strArgFile));
        }