2017-06-06 123 views
1

我試圖從代碼運行Microsoft Rdp應用程序。Java中的命令注入漏洞

我有以下的僞代碼和SonarQube抱怨Command Injection Vulnerability

String rdpFilePath = myObject.getRdpFilePath() // get path of .rdp settings file 
ProcessBuilder processBuilder = new ProcessBuilder(); 
processBuilder.command("mstsc", rdpFilePath).start(); 

SonarQube問題解釋如下:

-Potential Command Injection- 
The highlighted API is used to execute a system command. 
If unfiltered input is passed to this API, it can lead to arbitrary command execution. 

如何過濾我的輸入和如何解決這個安全問題?

回答

1

你的樣品很安全。在執行mstsc之前,您應該添加的一件事是檢查,即存在rdpFilePath文件。

如果將未過濾的用戶輸入作爲command方法的第一個參數(有時候下一個參數也容易受到攻擊,如果您想運行的程序也允許運行命令),則存在安全問題。在這種情況下,用戶可以在系統上執行任意命令。

+0

嗨,我已經添加了rdpFilePath一致性檢查和可讀性檢查。但即使在這種情況下,SonarQube也會警告有關命令注入的漏洞。我正在尋找的是解決這個問題的最安全的解決方案。 –

+0

您絕對沒有所有這些檢查的安全問題。但SonarQube無法理解您的過濾代碼,並會繼續警告潛在的安全風險。您可以禁用代碼塊的特定警告。檢查這[回答](https://stackoverflow.com/questions/10971968/turning-sonar-off-for-certain-code)。 – berserkk