flawopen.com/Command Injection/Java Runtime.exec()
क्या Java Runtime.exec() Command Injection रोकता है?
Runtime.exec(String) अपने-आप shell नहीं चलाता, इसलिए shell operators अपने-आप shell syntax नहीं बनते। फिर भी string को program और arguments में बाँटा जाता है और अविश्वसनीय text target program को मिलने वाले arguments बदल सकता है। ProcessBuilder argument boundaries स्पष्ट करता है; हर value की जाँच और executable तथा अनुमत options पर नियंत्रण फिर भी ज़रूरी है।
// User input को command string में जोड़ा गया है Runtime.getRuntime().exec( "approved-tool --input " + filename );
// Fixed executable और अलग arguments; पहले filename validate करें String safeName = validateFilename(filename); new ProcessBuilder( "/opt/app/bin/approved-tool", "--input", safeName ).start();
Shell injection और argument injection अलग हैं
Java 18 से Oracle ने त्रुटि-प्रवण होने के कारण Runtime.exec(String) को deprecated किया है और exec(String[]) या ProcessBuilder की सलाह दी है। String overload whitespace पर tokens बाँटता है, जिससे spaces वाले paths जैसे arguments विभाजित हो सकते हैं। Runtime.exec(String) अपने-आप sh या कोई दूसरा shell नहीं चलाता; Oracle docs के अनुसार single-string overload command string को argument array में बदलता है। इससे सामान्य shell metacharacters shell syntax की तरह नहीं चलते, पर input सुरक्षित नहीं हो जाता: tokenization अतिरिक्त arguments बना सकती है और target program किसी argument को option समझ सकता है। ProcessBuilder executable और arguments की string list लेता है; आम platforms पर हर element अलग argument boundary होता है। Executable तय रखें, target program के अपेक्षित grammar के अनुसार values जाँचें, और option जैसे दिखने वाले values अस्वीकार या सुरक्षित रूप से संभालें। -- तभी इस्तेमाल करें जब program का documentation इसकी गारंटी दे। अविश्वसनीय data के साथ sh -c जैसा shell स्पष्ट रूप से न चलाएँ।
अक्सर पूछे जाने वाले प्रश्न
नहीं। यह overload shell-injection जोखिम घटाता है, लेकिन argument injection संभव रहती है। Target program attacker-controlled input को option या filename समझ सकता है। ProcessBuilder में fixed executable और अलग arguments दें और हर value को target program के अनुसार validate करें।
नहीं। यह program और arguments की list बनाकर process शुरू करता है; सुरक्षा या वैधता नहीं जाँचता। Application में allowlist और validation लागू करें तथा target program के documented argument rules का पालन करें।