I am writing a console application , which accepts a command line argument and runs a bat file. I use process.start() for running a bat file. Example code snippet below
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
}
}
I have read some links in web , so used regex for validating an argument before sending it has parameter for process.start() to avoid attack. Even then I get OS command line injection attack(From third party security testing scanner). Am I missing something? Any help appreciated. Thanks in Advance:)