C# 调用System.Diagnostics.Process.Start from SQL CLR函数,该函数包含带xml的参数
我在SQLServer2008CLR项目中有一个存储过程。此函数使用包含一些xml的参数调用控制台应用程序C# 调用System.Diagnostics.Process.Start from SQL CLR函数,该函数包含带xml的参数,c#,sql-server,clr,clrstoredprocedure,C#,Sql Server,Clr,Clrstoredprocedure,我在SQLServer2008CLR项目中有一个存储过程。此函数使用包含一些xml的参数调用控制台应用程序 [Microsoft.SqlServer.Server.SqlProcedure()] public static SqlInt32 ExecuteApp(SqlString utilPath, SqlString arguments, ref SqlString result) { ProcessStartInfo info = new ProcessStartInfo("
[Microsoft.SqlServer.Server.SqlProcedure()]
public static SqlInt32 ExecuteApp(SqlString utilPath, SqlString arguments, ref SqlString result)
{
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
Process p = new Process();
try
{
//ProcessStartInfo to run the DOS command attrib
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.UseShellExecute = false;
info.CreateNoWindow = true;
info.Arguments = string.Format(@"/c ""{0}"" {1}", utilPath, arguments);
SqlContext.Pipe.Send(info.Arguments);
p.StartInfo = info;
p.Start();
String processResults = p.StandardOutput.ReadToEnd();
SqlContext.Pipe.Send(processResults);
result = processResults;
if (String.IsNullOrEmpty((String)result))
result = p.StandardError.ReadToEnd();
return 1;
}
finally
{
p.Close();
}
}
这里的问题是包含xml的param。
这是执行存储过程的表达式:
declare @Result nvarchar(max)
exec ExecuteApp 'C:\Console.exe', 'send "<messages><message>my message</message></messages>"', @Result out
select @Result
declare@Result-nvarchar(最大值)
exec ExecuteApp'C:\Console.exe','send“my message”',@Result out
选择@Result
执行存储过程后,我遇到了如下错误:
这在当时是出人意料的
我想注意到,没有xml,一切都正常工作 XML包含由命令shell解释的特殊字符(
和您有严重的安全漏洞。为什么不使用xp\u cmdshell
?xp\u cmdshell也需要在服务器级别启用,然后为注入攻击打开一条全新的途径。感谢您的快速响应。但真正的执行字符串似乎是:exec ExecuteApp'C:\Console.exe',send“my message”“,@Result out选择@Result.So,使用“我不知道你在说什么。请阅读我的答案;它解释了你的问题。实际上,当我从控制台运行时,我没有问题:“C:\console.exe”发送“我的消息”。那么,这真的是我的问题吗(>并且您的引号正在被cmd/c
解析。您应该直接启动程序。而且,这对任意XML都不起作用。