2011-01-10 3 views
0

SQL Server 2008 CLR 프로젝트에 저장 프로 시저가 있습니다. 이 함수는 일부 xml을 포함하는 매개 변수로 콘솔 응용 프로그램을 호출합니다.XML로 param을 포함하는 SQL CLR 함수에서 System.Diagnostics.Process.Start를 호출하십시오.

[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 

저장 프로 시저를 실행 한 후 :

<이 시간에 예상치 못한합니다.

xml이 모두 제대로 작동하지 않는 것을 알고 싶습니다.

+0

_ 심각한 보안 취약점이 있습니다. – SLaks

+1

왜 'xp_cmdshell'을 사용하지 않습니까? – SLaks

+0

xp_cmdshell도 서버 수준에서 사용하도록 설정 한 다음 주입 공격을위한 완전히 새로운 방법을 엽니 다. – Shiv

답변

1

XML에 명령 셸 (><)이 해석하는 특수 문자가 포함되어 있습니다.
매개 변수를 인용하고 ""으로 이스케이프 인용 부호를 사용해야합니다.

또한 프로그램을 직접 실행해야합니다 (cmd /c 제외). 그러면 문제가 해결 될 것입니다.

+0

빠른 응답을 보내 주셔서 감사합니다. 하지만 실제로 실행 문자열은 다음과 같습니다. – apros

+0

exec ExecuteApp 'C : \ Console.exe', '보내기' 내 메시지 '', @ 결과 선택 @ 결과. 그래서, "<"기호. 그래서 콘솔 앱 응답에 "<이 시간에 예상치 못한 결과"가 포함 된 이유는 무엇입니까? – apros

+0

당신이 무슨 말을하는지 잘 모르겠습니다. 내 대답 읽기; 그것은 당신의 문제를 설명합니다. – SLaks