将引号字符(")作为C#控制台应用程序参数传递

时间:2016-09-25 19:58:33

标签: c# console-application

我有一个项目来演示类似于" echo" MS-DOS命令行中的命令。这是C#中的代码:

using System;

namespace arguments
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                switch (args[0])
                {
                    case "/?":
                        string location = System.Reflection.Assembly.GetEntryAssembly().Location;
                        string name = System.IO.Path.GetFileName(location);
                        Console.WriteLine("Displays messages\nSyntax: {0} [message]", name);
                        Environment.Exit(0);
                        break;
                }
                if (args.Length >= 0)
                {
                    string x = "";
                    foreach (var item in args)
                    {
                        x += item.ToString() + " ";
                    }
                    Console.WriteLine(Convert.ToString(x)); // this should eliminate vulnerabilities.
                }
            }
            catch
            {
                string location = System.Reflection.Assembly.GetEntryAssembly().Location;
                string name = System.IO.Path.GetFileName(location);
                Console.WriteLine("Displays messages\nSyntax: {0} [message]", name);
            }
        }
    }
}

这可以很有效地完成它应该做的事情。然后我开始尝试以任何方式利用它。

在命令提示符下,我运行arguments.exe ",这应该打印出"。但事实并非如此。然后,我通过运行echoecho "命令尝试了同样的操作,并且它应该打印出来"。这令人难以置信,因为我甚至不认为这会是一个问题。我无法让它构成一个巨大的威胁,只是困惑了我一分钟。

我的问题是,有没有办法将引号(")作为参数传递给此控制台应用程序?

这是一张更好地展示它的照片:http://prntscr.com/cm9yal

2 个答案:

答案 0 :(得分:2)

为了能够获得单引号,在填充args数组时,您需要绕过CLR执行的默认解析。你可以通过检查mutation AddComment($input_0:AddCommentInput!) { addComment(input:$input_0) { clientMutationId, ...F1 } } fragment F0 on Customer { id } fragment F1 on AddCommentPayload { commentEdge { cursor, __typename, node { id, createdAt, commentText } }, customer { id, ...F0 } } 来做到这一点,在上面描述的情况下,这将返回以下内容:

Environment.CommandLine

请注意,我传递的参数只是ConsoleApplication1.exe \"(不是显示的转义变体)。

答案 1 :(得分:2)

void Main(string[] args)
这里的

args数组包含已传递给您的应用程序的参数。因为参数可能有空格,所以它们可以用引号括起来。

出于这个原因,你不会得到你作为参数放置的字符串。您还将在引用的参数之间释放任意数量的空格。

如果您需要原始命令行字符串,请使用:

string cmdline = System.Environment.CommandLine;