如何从 C# process.start 修复操作系统命令行注入?

问题描述

我正在编写一个控制台应用程序,它接受一个命令行参数并运行一个 bat 文件。 我使用 process.start() 来运行 bat 文件。下面的示例代码片段

 static void Main(string[] args)
        {
            string Folder = @"C:\Program Files\Somefolder";
            string filename = "someBatFile.bat";
            Process proc = null;
            try
            {
                string complexityRules = @"^(?=.*[0-9])(?=.*[A-Za-z])(?=.*[!@#$%&*-+=^]).{12,}$";
               
                if (Regex.IsMatch(args[0],complexityRules))
                {
                    string batDir = string.Format(Folder);
                    proc = new Process();
                    proc.StartInfo.WorkingDirectory = batDir;
                    proc.StartInfo.FileName = filename;
                    if (args[0] != null)
                    {
                        proc.StartInfo.Arguments = args[0];
                    }
                    proc.StartInfo.CreateNowindow = false;
                    proc.StartInfo.UseShellExecute = false;
                    proc.Start();
                    proc.WaitForExit();
                }


            }
            catch (Exception ex)
            {
                //will be catching exceptions if any 

            }
        }

我已经阅读了 web 中的一些链接,因此使用正则表达式来验证参数,然后再发送它具有 process.start() 的参数以避免攻击。即便如此,我还是会收到操作系统命令行注入攻击(来自第三方安全测试扫描仪)。我错过了什么吗?任何帮助表示赞赏。提前致谢:)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)