<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jos%C3%A9+Luis+Escobar</id>
		<title>OWASP - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jos%C3%A9+Luis+Escobar"/>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php/Special:Contributions/Jos%C3%A9_Luis_Escobar"/>
		<updated>2026-04-28T12:48:31Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.2</generator>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Inyecci%C3%B3n_De_Comandos_En_Java&amp;diff=140663</id>
		<title>Inyección De Comandos En Java</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Inyecci%C3%B3n_De_Comandos_En_Java&amp;diff=140663"/>
				<updated>2012-12-04T14:27:10Z</updated>
		
		<summary type="html">&lt;p&gt;José Luis Escobar: Created page with &amp;quot;==Estado== Review  ==Introducción== Las vulnerabilidades de  este tipo permiten a un atacante inyectar arbitrariamente comandos del sistema en una aplicación. Estos comandos...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Estado==&lt;br /&gt;
Review&lt;br /&gt;
&lt;br /&gt;
==Introducción==&lt;br /&gt;
Las vulnerabilidades de  este tipo permiten a un atacante inyectar arbitrariamente comandos del sistema en una aplicación. Estos comandos se ejecutan al mismo nivel de privilegios que la aplicación Java y proveen al atacante una funcionalidad similar a la de una Shell de Sistema Operativo. En Java, Runtime.exec es comúnmente usado para invocar a un nuevo proceso, pero esto no invoca una nueva Shell de comandos, lo que significa que usualmente encadenando o entubando múltiples comandos juntos no funciona. La inyección de comandos sin embargo es posible si el proceso engendrado con Runtime.exec es una Shell como command.com, cmd.exe o /bin/sh..&lt;br /&gt;
&lt;br /&gt;
==Ejemplos ==&lt;br /&gt;
&lt;br /&gt;
===Ejemplo 1===&lt;br /&gt;
&lt;br /&gt;
El código detallado a continuación permite a un usuario el control de los argumentos del comando de Windows find. Mientras el usuario no posea control absoluto sobre los argumentos, no es posible inyectar comandos adicionales. Por ejemplo, ingresando “test &amp;amp; del file” no causará que el comando del se ejecute. Dado que Runtime.exec tokeniza la cadena de comando y luego invoca el comando find usando los parámetros “test”, “&amp;amp;”, “del” y “file”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class Example1 {&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	throws IOException {&lt;br /&gt;
		if(args.length != 1) {&lt;br /&gt;
			System.out.println(&amp;quot;No arguments&amp;quot;);&lt;br /&gt;
			System.exit(1);&lt;br /&gt;
		}&lt;br /&gt;
		Runtime runtime = Runtime.getRuntime();&lt;br /&gt;
		Process proc = runtime.exec(&amp;quot;find&amp;quot; + &amp;quot; &amp;quot; + args[0]);&lt;br /&gt;
		&lt;br /&gt;
		InputStream is = proc.getInputStream();&lt;br /&gt;
		InputStreamReader isr = new InputStreamReader(is);&lt;br /&gt;
		BufferedReader br = new BufferedReader(isr);&lt;br /&gt;
		&lt;br /&gt;
		String line;&lt;br /&gt;
		while ((line = br.readLine()) != null) {&lt;br /&gt;
			System.out.println(line);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Ejemplo 2===&lt;br /&gt;
&lt;br /&gt;
El código a continuación invoca una shell del sistema para ejecutar un commando no ejecutable usando lo ingresado por el usuario como parámetros. Comandos de Windows no ejecutables tales como dir y copy son parte del intérprete de comandos y por ende no pueden ser directamente invoados por Runtime.exec. En este caso, la inyección de comandos es posible y un atacante puede encadenar múltiples comandos juntos. Por ejemplo ingresando, “.&amp;amp; echo hello” causará que el comando dir liste los contenidos del directorio actual y el comando echo imprima un mensaje de saludo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class Example2 {&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	throws IOException {&lt;br /&gt;
		if(args.length != 1) {&lt;br /&gt;
			System.out.println(&amp;quot;No arguments&amp;quot;);&lt;br /&gt;
			System.exit(1);&lt;br /&gt;
		}&lt;br /&gt;
		Runtime runtime = Runtime.getRuntime();&lt;br /&gt;
		String[] cmd = new String[3];&lt;br /&gt;
		cmd[0] = &amp;quot;cmd.exe&amp;quot; ;&lt;br /&gt;
                cmd[1] = &amp;quot;/C&amp;quot;;&lt;br /&gt;
                cmd[2] = &amp;quot;dir &amp;quot; + args[0];&lt;br /&gt;
		Process proc = runtime.exec(cmd);&lt;br /&gt;
		&lt;br /&gt;
		InputStream is = proc.getInputStream();&lt;br /&gt;
		InputStreamReader isr = new InputStreamReader(is);&lt;br /&gt;
		BufferedReader br = new BufferedReader(isr);&lt;br /&gt;
		&lt;br /&gt;
		String line;&lt;br /&gt;
		while ((line = br.readLine()) != null) {&lt;br /&gt;
			System.out.println(line);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mejores Prácticas ==&lt;br /&gt;
&lt;br /&gt;
Los desarrolladores deben evitar invocar la shell usando Runtime.exec para llamar a comandos del sistema operativo y en su defecto deben usar la API de Java. Por ejemplo, en vez de llamar lsor dir desde la sell se debe usar la clase Java File para la función de listado. Si es necesario que el usuario deba ingresar datos y pasarlos hacía Runtime.exec, y luego usar expresiones regulares para validar el ingreso.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Java Project]]&lt;/div&gt;</summary>
		<author><name>José Luis Escobar</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Command_injection_in_Java&amp;diff=140662</id>
		<title>Command injection in Java</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Command_injection_in_Java&amp;diff=140662"/>
				<updated>2012-12-04T14:27:02Z</updated>
		
		<summary type="html">&lt;p&gt;José Luis Escobar: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Status==&lt;br /&gt;
Review&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Command injection vulnerabilities allow an attacker to inject arbitrary system commands into an application.  The commands execute at the same privilege level as the Java application and provides an attacker with functionality similar to a system shell.  In Java, Runtime.exec is often used to invoke a new process, but it does not invoke a new command shell, which means that chaining or piping multiple commands together does not usually work.  Command injection is still possible if the process spawned with Runtime.exec is a command shell like command.com, cmd.exe, or /bin/sh.&lt;br /&gt;
&lt;br /&gt;
==Examples ==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
The code below allows a user to control the arguments to the Window's ''find'' command.  While the user does have full control over the arguments, it is not possible to inject additional commands.  For example, inputting “test &amp;amp; del file” will not cause the ''del'' command to execute, since Runtime.exec tokenizes the command string and then invokes the ''find'' command using the parameters “test”, “&amp;amp;”, “del”, and “file.”&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class Example1 {&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	throws IOException {&lt;br /&gt;
		if(args.length != 1) {&lt;br /&gt;
			System.out.println(&amp;quot;No arguments&amp;quot;);&lt;br /&gt;
			System.exit(1);&lt;br /&gt;
		}&lt;br /&gt;
		Runtime runtime = Runtime.getRuntime();&lt;br /&gt;
		Process proc = runtime.exec(&amp;quot;find&amp;quot; + &amp;quot; &amp;quot; + args[0]);&lt;br /&gt;
		&lt;br /&gt;
		InputStream is = proc.getInputStream();&lt;br /&gt;
		InputStreamReader isr = new InputStreamReader(is);&lt;br /&gt;
		BufferedReader br = new BufferedReader(isr);&lt;br /&gt;
		&lt;br /&gt;
		String line;&lt;br /&gt;
		while ((line = br.readLine()) != null) {&lt;br /&gt;
			System.out.println(line);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
The code below invokes the system shell in order to execute a non-executable command using user input as parameters.  Non-executable Window's commands such as ''dir'' and ''copy'' are part of the command interpreter and therefore cannot be directly invoked by Runtime.exec.  In this case, command injection is possible and an attacker could chain multiple commands together.  For example, inputting “. &amp;amp; echo hello” will cause the ''dir'' command to list the contents of the current directory and the ''echo'' command to print a friendly message.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class Example2 {&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	throws IOException {&lt;br /&gt;
		if(args.length != 1) {&lt;br /&gt;
			System.out.println(&amp;quot;No arguments&amp;quot;);&lt;br /&gt;
			System.exit(1);&lt;br /&gt;
		}&lt;br /&gt;
		Runtime runtime = Runtime.getRuntime();&lt;br /&gt;
		String[] cmd = new String[3];&lt;br /&gt;
		cmd[0] = &amp;quot;cmd.exe&amp;quot; ;&lt;br /&gt;
                cmd[1] = &amp;quot;/C&amp;quot;;&lt;br /&gt;
                cmd[2] = &amp;quot;dir &amp;quot; + args[0];&lt;br /&gt;
		Process proc = runtime.exec(cmd);&lt;br /&gt;
		&lt;br /&gt;
		InputStream is = proc.getInputStream();&lt;br /&gt;
		InputStreamReader isr = new InputStreamReader(is);&lt;br /&gt;
		BufferedReader br = new BufferedReader(isr);&lt;br /&gt;
		&lt;br /&gt;
		String line;&lt;br /&gt;
		while ((line = br.readLine()) != null) {&lt;br /&gt;
			System.out.println(line);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Best Practices ==&lt;br /&gt;
&lt;br /&gt;
Developers should avoid invoking the shell using Runtime.exec in order to call operating system specific commands and should use Java APIs instead.  For example, instead of calling ''ls'' or ''dir'' from the shell use the Java File class and the list function.  If it is necessary to accept user input and pass it to Runtime.exec, then use regular expressions to validate the input.&lt;br /&gt;
&lt;br /&gt;
#'''Traduccion en Español: ''' [[Inyección De Comandos En Java]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Java Project]]&lt;/div&gt;</summary>
		<author><name>José Luis Escobar</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Command_injection_in_Java&amp;diff=140660</id>
		<title>Command injection in Java</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Command_injection_in_Java&amp;diff=140660"/>
				<updated>2012-12-04T14:25:43Z</updated>
		
		<summary type="html">&lt;p&gt;José Luis Escobar: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Status==&lt;br /&gt;
Review&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Command injection vulnerabilities allow an attacker to inject arbitrary system commands into an application.  The commands execute at the same privilege level as the Java application and provides an attacker with functionality similar to a system shell.  In Java, Runtime.exec is often used to invoke a new process, but it does not invoke a new command shell, which means that chaining or piping multiple commands together does not usually work.  Command injection is still possible if the process spawned with Runtime.exec is a command shell like command.com, cmd.exe, or /bin/sh.&lt;br /&gt;
&lt;br /&gt;
==Examples ==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
The code below allows a user to control the arguments to the Window's ''find'' command.  While the user does have full control over the arguments, it is not possible to inject additional commands.  For example, inputting “test &amp;amp; del file” will not cause the ''del'' command to execute, since Runtime.exec tokenizes the command string and then invokes the ''find'' command using the parameters “test”, “&amp;amp;”, “del”, and “file.”&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class Example1 {&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	throws IOException {&lt;br /&gt;
		if(args.length != 1) {&lt;br /&gt;
			System.out.println(&amp;quot;No arguments&amp;quot;);&lt;br /&gt;
			System.exit(1);&lt;br /&gt;
		}&lt;br /&gt;
		Runtime runtime = Runtime.getRuntime();&lt;br /&gt;
		Process proc = runtime.exec(&amp;quot;find&amp;quot; + &amp;quot; &amp;quot; + args[0]);&lt;br /&gt;
		&lt;br /&gt;
		InputStream is = proc.getInputStream();&lt;br /&gt;
		InputStreamReader isr = new InputStreamReader(is);&lt;br /&gt;
		BufferedReader br = new BufferedReader(isr);&lt;br /&gt;
		&lt;br /&gt;
		String line;&lt;br /&gt;
		while ((line = br.readLine()) != null) {&lt;br /&gt;
			System.out.println(line);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
The code below invokes the system shell in order to execute a non-executable command using user input as parameters.  Non-executable Window's commands such as ''dir'' and ''copy'' are part of the command interpreter and therefore cannot be directly invoked by Runtime.exec.  In this case, command injection is possible and an attacker could chain multiple commands together.  For example, inputting “. &amp;amp; echo hello” will cause the ''dir'' command to list the contents of the current directory and the ''echo'' command to print a friendly message.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class Example2 {&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	throws IOException {&lt;br /&gt;
		if(args.length != 1) {&lt;br /&gt;
			System.out.println(&amp;quot;No arguments&amp;quot;);&lt;br /&gt;
			System.exit(1);&lt;br /&gt;
		}&lt;br /&gt;
		Runtime runtime = Runtime.getRuntime();&lt;br /&gt;
		String[] cmd = new String[3];&lt;br /&gt;
		cmd[0] = &amp;quot;cmd.exe&amp;quot; ;&lt;br /&gt;
                cmd[1] = &amp;quot;/C&amp;quot;;&lt;br /&gt;
                cmd[2] = &amp;quot;dir &amp;quot; + args[0];&lt;br /&gt;
		Process proc = runtime.exec(cmd);&lt;br /&gt;
		&lt;br /&gt;
		InputStream is = proc.getInputStream();&lt;br /&gt;
		InputStreamReader isr = new InputStreamReader(is);&lt;br /&gt;
		BufferedReader br = new BufferedReader(isr);&lt;br /&gt;
		&lt;br /&gt;
		String line;&lt;br /&gt;
		while ((line = br.readLine()) != null) {&lt;br /&gt;
			System.out.println(line);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Best Practices ==&lt;br /&gt;
&lt;br /&gt;
Developers should avoid invoking the shell using Runtime.exec in order to call operating system specific commands and should use Java APIs instead.  For example, instead of calling ''ls'' or ''dir'' from the shell use the Java File class and the list function.  If it is necessary to accept user input and pass it to Runtime.exec, then use regular expressions to validate the input.&lt;br /&gt;
&lt;br /&gt;
#'''Traduccion en Español: ''' [[Traducción]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Java Project]]&lt;/div&gt;</summary>
		<author><name>José Luis Escobar</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Command_injection_in_Java&amp;diff=140659</id>
		<title>Command injection in Java</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Command_injection_in_Java&amp;diff=140659"/>
				<updated>2012-12-04T14:25:15Z</updated>
		
		<summary type="html">&lt;p&gt;José Luis Escobar: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Status==&lt;br /&gt;
Review&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Command injection vulnerabilities allow an attacker to inject arbitrary system commands into an application.  The commands execute at the same privilege level as the Java application and provides an attacker with functionality similar to a system shell.  In Java, Runtime.exec is often used to invoke a new process, but it does not invoke a new command shell, which means that chaining or piping multiple commands together does not usually work.  Command injection is still possible if the process spawned with Runtime.exec is a command shell like command.com, cmd.exe, or /bin/sh.&lt;br /&gt;
&lt;br /&gt;
==Examples ==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
The code below allows a user to control the arguments to the Window's ''find'' command.  While the user does have full control over the arguments, it is not possible to inject additional commands.  For example, inputting “test &amp;amp; del file” will not cause the ''del'' command to execute, since Runtime.exec tokenizes the command string and then invokes the ''find'' command using the parameters “test”, “&amp;amp;”, “del”, and “file.”&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class Example1 {&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	throws IOException {&lt;br /&gt;
		if(args.length != 1) {&lt;br /&gt;
			System.out.println(&amp;quot;No arguments&amp;quot;);&lt;br /&gt;
			System.exit(1);&lt;br /&gt;
		}&lt;br /&gt;
		Runtime runtime = Runtime.getRuntime();&lt;br /&gt;
		Process proc = runtime.exec(&amp;quot;find&amp;quot; + &amp;quot; &amp;quot; + args[0]);&lt;br /&gt;
		&lt;br /&gt;
		InputStream is = proc.getInputStream();&lt;br /&gt;
		InputStreamReader isr = new InputStreamReader(is);&lt;br /&gt;
		BufferedReader br = new BufferedReader(isr);&lt;br /&gt;
		&lt;br /&gt;
		String line;&lt;br /&gt;
		while ((line = br.readLine()) != null) {&lt;br /&gt;
			System.out.println(line);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
The code below invokes the system shell in order to execute a non-executable command using user input as parameters.  Non-executable Window's commands such as ''dir'' and ''copy'' are part of the command interpreter and therefore cannot be directly invoked by Runtime.exec.  In this case, command injection is possible and an attacker could chain multiple commands together.  For example, inputting “. &amp;amp; echo hello” will cause the ''dir'' command to list the contents of the current directory and the ''echo'' command to print a friendly message.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class Example2 {&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	throws IOException {&lt;br /&gt;
		if(args.length != 1) {&lt;br /&gt;
			System.out.println(&amp;quot;No arguments&amp;quot;);&lt;br /&gt;
			System.exit(1);&lt;br /&gt;
		}&lt;br /&gt;
		Runtime runtime = Runtime.getRuntime();&lt;br /&gt;
		String[] cmd = new String[3];&lt;br /&gt;
		cmd[0] = &amp;quot;cmd.exe&amp;quot; ;&lt;br /&gt;
                cmd[1] = &amp;quot;/C&amp;quot;;&lt;br /&gt;
                cmd[2] = &amp;quot;dir &amp;quot; + args[0];&lt;br /&gt;
		Process proc = runtime.exec(cmd);&lt;br /&gt;
		&lt;br /&gt;
		InputStream is = proc.getInputStream();&lt;br /&gt;
		InputStreamReader isr = new InputStreamReader(is);&lt;br /&gt;
		BufferedReader br = new BufferedReader(isr);&lt;br /&gt;
		&lt;br /&gt;
		String line;&lt;br /&gt;
		while ((line = br.readLine()) != null) {&lt;br /&gt;
			System.out.println(line);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Best Practices ==&lt;br /&gt;
&lt;br /&gt;
Developers should avoid invoking the shell using Runtime.exec in order to call operating system specific commands and should use Java APIs instead.  For example, instead of calling ''ls'' or ''dir'' from the shell use the Java File class and the list function.  If it is necessary to accept user input and pass it to Runtime.exec, then use regular expressions to validate the input.&lt;br /&gt;
&lt;br /&gt;
#'''Traduccion en Español: ''' [[Inyección de Comandos en Java]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Java Project]]&lt;/div&gt;</summary>
		<author><name>José Luis Escobar</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Command_injection_in_Java&amp;diff=140655</id>
		<title>Command injection in Java</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Command_injection_in_Java&amp;diff=140655"/>
				<updated>2012-12-04T14:19:30Z</updated>
		
		<summary type="html">&lt;p&gt;José Luis Escobar: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Status==&lt;br /&gt;
Review&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Command injection vulnerabilities allow an attacker to inject arbitrary system commands into an application.  The commands execute at the same privilege level as the Java application and provides an attacker with functionality similar to a system shell.  In Java, Runtime.exec is often used to invoke a new process, but it does not invoke a new command shell, which means that chaining or piping multiple commands together does not usually work.  Command injection is still possible if the process spawned with Runtime.exec is a command shell like command.com, cmd.exe, or /bin/sh.&lt;br /&gt;
&lt;br /&gt;
==Examples ==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
The code below allows a user to control the arguments to the Window's ''find'' command.  While the user does have full control over the arguments, it is not possible to inject additional commands.  For example, inputting “test &amp;amp; del file” will not cause the ''del'' command to execute, since Runtime.exec tokenizes the command string and then invokes the ''find'' command using the parameters “test”, “&amp;amp;”, “del”, and “file.”&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class Example1 {&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	throws IOException {&lt;br /&gt;
		if(args.length != 1) {&lt;br /&gt;
			System.out.println(&amp;quot;No arguments&amp;quot;);&lt;br /&gt;
			System.exit(1);&lt;br /&gt;
		}&lt;br /&gt;
		Runtime runtime = Runtime.getRuntime();&lt;br /&gt;
		Process proc = runtime.exec(&amp;quot;find&amp;quot; + &amp;quot; &amp;quot; + args[0]);&lt;br /&gt;
		&lt;br /&gt;
		InputStream is = proc.getInputStream();&lt;br /&gt;
		InputStreamReader isr = new InputStreamReader(is);&lt;br /&gt;
		BufferedReader br = new BufferedReader(isr);&lt;br /&gt;
		&lt;br /&gt;
		String line;&lt;br /&gt;
		while ((line = br.readLine()) != null) {&lt;br /&gt;
			System.out.println(line);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
The code below invokes the system shell in order to execute a non-executable command using user input as parameters.  Non-executable Window's commands such as ''dir'' and ''copy'' are part of the command interpreter and therefore cannot be directly invoked by Runtime.exec.  In this case, command injection is possible and an attacker could chain multiple commands together.  For example, inputting “. &amp;amp; echo hello” will cause the ''dir'' command to list the contents of the current directory and the ''echo'' command to print a friendly message.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class Example2 {&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	throws IOException {&lt;br /&gt;
		if(args.length != 1) {&lt;br /&gt;
			System.out.println(&amp;quot;No arguments&amp;quot;);&lt;br /&gt;
			System.exit(1);&lt;br /&gt;
		}&lt;br /&gt;
		Runtime runtime = Runtime.getRuntime();&lt;br /&gt;
		String[] cmd = new String[3];&lt;br /&gt;
		cmd[0] = &amp;quot;cmd.exe&amp;quot; ;&lt;br /&gt;
                cmd[1] = &amp;quot;/C&amp;quot;;&lt;br /&gt;
                cmd[2] = &amp;quot;dir &amp;quot; + args[0];&lt;br /&gt;
		Process proc = runtime.exec(cmd);&lt;br /&gt;
		&lt;br /&gt;
		InputStream is = proc.getInputStream();&lt;br /&gt;
		InputStreamReader isr = new InputStreamReader(is);&lt;br /&gt;
		BufferedReader br = new BufferedReader(isr);&lt;br /&gt;
		&lt;br /&gt;
		String line;&lt;br /&gt;
		while ((line = br.readLine()) != null) {&lt;br /&gt;
			System.out.println(line);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Best Practices ==&lt;br /&gt;
&lt;br /&gt;
Developers should avoid invoking the shell using Runtime.exec in order to call operating system specific commands and should use Java APIs instead.  For example, instead of calling ''ls'' or ''dir'' from the shell use the Java File class and the list function.  If it is necessary to accept user input and pass it to Runtime.exec, then use regular expressions to validate the input.&lt;br /&gt;
&lt;br /&gt;
#'''Traduccion en Español: ''' [[Traducción]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Java Project]]&lt;/div&gt;</summary>
		<author><name>José Luis Escobar</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Command_Injection&amp;diff=140654</id>
		<title>Command Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Command_Injection&amp;diff=140654"/>
				<updated>2012-12-04T14:19:14Z</updated>
		
		<summary type="html">&lt;p&gt;José Luis Escobar: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Attack}}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Category:OWASP ASDR Project]]&lt;br /&gt;
&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The purpose of the command injection attack is to inject and execute commands specified by the attacker in the vulnerable application. In situation like this, the application, which executes unwanted system commands, is like a pseudo system shell, and the attacker may use it as any authorized system user. However, commands are executed with the same privileges and environment as the application has. Command injection attacks are possible in most cases because of lack of correct input data validation, which can be manipulated by the attacker (forms, cookies, HTTP headers etc.).&lt;br /&gt;
&lt;br /&gt;
There is a variant of the [[Code Injection]] attack. The difference with code injection is that the attacker adds his own code to the existing code. In this way, the attacker extends the  default functionality of the application without the necessity of executing system commands. Injected code is executed with the same privileges and environment as the application has.&lt;br /&gt;
&lt;br /&gt;
An OS command injection attack occurs when an attacker attempts to execute system level commands through a vulnerable application. Applications are considered vulnerable to the OS command injection attack if they utilize user input in a system level command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--==Risk Factors==&lt;br /&gt;
TBD&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
==Examples ==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
The following code is a wrapper around the UNIX command ''cat'' which prints the contents of a file to standard output. It is also injectable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
 char cat[] = &amp;quot;cat &amp;quot;;&lt;br /&gt;
 char *command;&lt;br /&gt;
 size_t commandLength;&lt;br /&gt;
&lt;br /&gt;
 commandLength = strlen(cat) + strlen(argv[1]) + 1;&lt;br /&gt;
 command = (char *) malloc(commandLength);&lt;br /&gt;
 strncpy(command, cat, commandLength);&lt;br /&gt;
 strncat(command, argv[1], (commandLength - strlen(cat)) );&lt;br /&gt;
&lt;br /&gt;
 system(command);&lt;br /&gt;
 return (0);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Used normally, the output is simply the contents of the file requested:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$ ./catWrapper Story.txt&lt;br /&gt;
When last we left our heroes...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, if we add a semicolon and another command to the end of this line, the command is executed by catWrapper with no complaint:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ ./catWrapper &amp;quot;Story.txt; ls&amp;quot;&lt;br /&gt;
When last we left our heroes...&lt;br /&gt;
Story.txt               doubFree.c              nullpointer.c&lt;br /&gt;
unstosig.c              www*                    a.out*&lt;br /&gt;
format.c                strlen.c                useFree*&lt;br /&gt;
catWrapper*             misnull.c               strlength.c             useFree.c&lt;br /&gt;
commandinjection.c      nodefault.c             trunc.c                 writeWhatWhere.c&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If catWrapper had been set to have a higher privilege level than the standard user, arbitrary commands could be executed with that higher privilege.&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
The following simple program accepts a filename as a command line argument, and displays the contents of the file back to the user. The program is installed setuid root because it is intended for use as a learning tool to allow system administrators in-training to inspect privileged system files without giving them the ability to modify them or damage the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
       int main(char* argc, char** argv) {&lt;br /&gt;
               char cmd[CMD_MAX] = &amp;quot;/usr/bin/cat &amp;quot;;&lt;br /&gt;
               strcat(cmd, argv[1]);&lt;br /&gt;
               system(cmd);&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because the program runs with root privileges, the call to system() also executes with root privileges. If a user specifies a standard filename, the call works as expected. However, if an attacker passes a string of the form &amp;quot;;rm -rf /&amp;quot;, then the call to system() fails to execute cat due to a lack of arguments and then plows on to recursively delete the contents of the root partition.&lt;br /&gt;
&lt;br /&gt;
===Example 3===&lt;br /&gt;
&lt;br /&gt;
The following code from a privileged program uses the environment variable $APPHOME to determine the application's installation directory, and then executes an initialization script in that directory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
       ...&lt;br /&gt;
       char* home=getenv(&amp;quot;APPHOME&amp;quot;);&lt;br /&gt;
       char* cmd=(char*)malloc(strlen(home)+strlen(INITCMD));&lt;br /&gt;
       if (cmd) {&lt;br /&gt;
               strcpy(cmd,home);&lt;br /&gt;
               strcat(cmd,INITCMD);&lt;br /&gt;
               execl(cmd, NULL);&lt;br /&gt;
       }&lt;br /&gt;
       ...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in Example 2, the code in this example allows an attacker to execute arbitrary commands with the elevated privilege of the application. In this example, the attacker can modify the environment variable $APPHOME to specify a different path containing a malicious version of INITCMD. Because the program does not validate the value read from the environment, by controlling the environment variable, the attacker can fool the application into running malicious code.&lt;br /&gt;
&lt;br /&gt;
The attacker is using the environment variable to control the command that the program invokes, so the effect of the environment is explicit in this example. We will now turn our attention to what can happen when the attacker changes the way the command is interpreted.&lt;br /&gt;
&lt;br /&gt;
===Example 4===&lt;br /&gt;
&lt;br /&gt;
The code below is from a web-based CGI utility that allows users to change their passwords. The password update process under NIS includes running ''make'' in the /var/yp directory. Note that since the program updates password records, it has been installed setuid root.&lt;br /&gt;
&lt;br /&gt;
The program invokes make as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
       system(&amp;quot;cd /var/yp &amp;amp;&amp;amp; make &amp;amp;&amp;gt; /dev/null&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike the previous examples, the command in this example is hardcoded, so an attacker cannot control the argument passed to system(). However, since the program does not specify an absolute path for make, and does not scrub any environment variables prior to invoking the command, the attacker can modify their $PATH variable to point to a malicious binary named make and execute the CGI script from a shell prompt. And since the program has been installed setuid root, the attacker's version of make now runs with root privileges.&lt;br /&gt;
&lt;br /&gt;
The environment plays a powerful role in the execution of system commands within programs. Functions like system() and exec() use the environment of the program that calls them, and therefore attackers have a potential opportunity to influence the behavior of these calls.&lt;br /&gt;
&lt;br /&gt;
There are many sites that will tell you that Java's Runtime.exec is exactly the same as C's system function. This is not true. Both allow you to invoke a new program/process. However, C's system function passes its arguments to the shell (/bin/sh) to be parsed, whereas Runtime.exec tries to split the string into an array of words, then executes the first word in the array with the rest of the words as parameters. Runtime.exec does NOT try to invoke the shell at any point. The key difference is that much of the functionality provided by the shell that could be used for mischief (chaining commands using &amp;quot;&amp;amp;&amp;quot;, &amp;quot;&amp;amp;&amp;amp;&amp;quot;, &amp;quot;|&amp;quot;, &amp;quot;||&amp;quot;, etc, redirecting input and output) would simply end up as a parameter being passed to the first command, and likely causing a syntax error, or being thrown out as an invalid parameter.&lt;br /&gt;
&lt;br /&gt;
===Example 5===&lt;br /&gt;
The following trivial code snippets are vulnerable to OS command injection on the Unix/Linux platform:&lt;br /&gt;
&lt;br /&gt;
:* C:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char **argv)&lt;br /&gt;
 {&lt;br /&gt;
      char command[256];&lt;br /&gt;
 &lt;br /&gt;
      if(argc != 2) {&lt;br /&gt;
           printf(&amp;quot;Error: Please enter a program to time!\n&amp;quot;);&lt;br /&gt;
           return -1;&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      memset(&amp;amp;command, 0, sizeof(command));&lt;br /&gt;
 &lt;br /&gt;
      strcat(command, &amp;quot;time ./&amp;quot;);&lt;br /&gt;
      strcat(command, argv[1]);&lt;br /&gt;
 &lt;br /&gt;
      system(command);&lt;br /&gt;
      return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
:* If this were a suid binary, consider the case when an attacker enters the following: 'ls; cat /etc/shadow'. In the Unix environment, shell commands are separated by a semi-colon. We now can execute system commands at will!&lt;br /&gt;
&lt;br /&gt;
:* Java:&lt;br /&gt;
&lt;br /&gt;
'''There are many sites that will tell you that Java's Runtime.exec is exactly the same as C's system function. This is not true. Both allow you to invoke a new program/process. However, C's system function passes its arguments to the shell (/bin/sh) to be parsed, whereas Runtime.exec tries to split the string into an array of words, then executes the first word in the array with the rest of the words as parameters. Runtime.exec does NOT try to invoke the shell at any point. The key difference is that much of the functionality provided by the shell that could be used for mischief (chaining commands using &amp;quot;&amp;amp;&amp;quot;, &amp;quot;&amp;amp;&amp;amp;&amp;quot;, &amp;quot;|&amp;quot;, &amp;quot;||&amp;quot;, etc, redirecting input and output) would simply end up as a parameter being passed to the first command, and likely causing a syntax error, or being thrown out as an invalid parameter.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--==Related [[Threat Agents]]==&lt;br /&gt;
TBD&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
==Related [[Attacks]]==&lt;br /&gt;
* [[Code Injection]]&lt;br /&gt;
* [[Blind SQL Injection]]&lt;br /&gt;
* [[Blind XPath Injection]]&lt;br /&gt;
* [[LDAP injection]]&lt;br /&gt;
* [[Relative Path Traversal]]&lt;br /&gt;
* [[Absolute Path Traversal]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--==Related [[Vulnerabilities]]==&lt;br /&gt;
TBD&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
==Related [[Controls]]==&lt;br /&gt;
* [[:Category:Input Validation]]&lt;br /&gt;
&lt;br /&gt;
Ideally, a developer should use existing API for their language. For example (Java): Rather than use Runtime.exec() to issue a 'mail' command, use the available Java API located at javax.mail.*&lt;br /&gt;
&lt;br /&gt;
If no such available API exists, the developer should scrub all input for malicious characters. Implementing a positive security model would be most efficient. Typically, it is much easier to define the legal characters than the illegal characters.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/77.html CWE-77: Command Injection]&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/78.html CWE-78: OS Command Injection]&lt;br /&gt;
* http://blog.php-security.org/archives/76-Holes-in-most-preg_match-filters.html&lt;br /&gt;
&lt;br /&gt;
[[Category:Injection Attack]]&lt;br /&gt;
[[Category:Injection]]&lt;br /&gt;
[[Category:Attack]]&lt;br /&gt;
[[Category:Externally Linked Page]]&lt;/div&gt;</summary>
		<author><name>José Luis Escobar</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Command_Injection&amp;diff=140652</id>
		<title>Command Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Command_Injection&amp;diff=140652"/>
				<updated>2012-12-04T14:16:55Z</updated>
		
		<summary type="html">&lt;p&gt;José Luis Escobar: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Attack}}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Category:OWASP ASDR Project]]&lt;br /&gt;
&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The purpose of the command injection attack is to inject and execute commands specified by the attacker in the vulnerable application. In situation like this, the application, which executes unwanted system commands, is like a pseudo system shell, and the attacker may use it as any authorized system user. However, commands are executed with the same privileges and environment as the application has. Command injection attacks are possible in most cases because of lack of correct input data validation, which can be manipulated by the attacker (forms, cookies, HTTP headers etc.).&lt;br /&gt;
&lt;br /&gt;
There is a variant of the [[Code Injection]] attack. The difference with code injection is that the attacker adds his own code to the existing code. In this way, the attacker extends the  default functionality of the application without the necessity of executing system commands. Injected code is executed with the same privileges and environment as the application has.&lt;br /&gt;
&lt;br /&gt;
An OS command injection attack occurs when an attacker attempts to execute system level commands through a vulnerable application. Applications are considered vulnerable to the OS command injection attack if they utilize user input in a system level command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--==Risk Factors==&lt;br /&gt;
TBD&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
==Examples ==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
The following code is a wrapper around the UNIX command ''cat'' which prints the contents of a file to standard output. It is also injectable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
 char cat[] = &amp;quot;cat &amp;quot;;&lt;br /&gt;
 char *command;&lt;br /&gt;
 size_t commandLength;&lt;br /&gt;
&lt;br /&gt;
 commandLength = strlen(cat) + strlen(argv[1]) + 1;&lt;br /&gt;
 command = (char *) malloc(commandLength);&lt;br /&gt;
 strncpy(command, cat, commandLength);&lt;br /&gt;
 strncat(command, argv[1], (commandLength - strlen(cat)) );&lt;br /&gt;
&lt;br /&gt;
 system(command);&lt;br /&gt;
 return (0);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Used normally, the output is simply the contents of the file requested:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$ ./catWrapper Story.txt&lt;br /&gt;
When last we left our heroes...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, if we add a semicolon and another command to the end of this line, the command is executed by catWrapper with no complaint:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ ./catWrapper &amp;quot;Story.txt; ls&amp;quot;&lt;br /&gt;
When last we left our heroes...&lt;br /&gt;
Story.txt               doubFree.c              nullpointer.c&lt;br /&gt;
unstosig.c              www*                    a.out*&lt;br /&gt;
format.c                strlen.c                useFree*&lt;br /&gt;
catWrapper*             misnull.c               strlength.c             useFree.c&lt;br /&gt;
commandinjection.c      nodefault.c             trunc.c                 writeWhatWhere.c&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If catWrapper had been set to have a higher privilege level than the standard user, arbitrary commands could be executed with that higher privilege.&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
The following simple program accepts a filename as a command line argument, and displays the contents of the file back to the user. The program is installed setuid root because it is intended for use as a learning tool to allow system administrators in-training to inspect privileged system files without giving them the ability to modify them or damage the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
       int main(char* argc, char** argv) {&lt;br /&gt;
               char cmd[CMD_MAX] = &amp;quot;/usr/bin/cat &amp;quot;;&lt;br /&gt;
               strcat(cmd, argv[1]);&lt;br /&gt;
               system(cmd);&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because the program runs with root privileges, the call to system() also executes with root privileges. If a user specifies a standard filename, the call works as expected. However, if an attacker passes a string of the form &amp;quot;;rm -rf /&amp;quot;, then the call to system() fails to execute cat due to a lack of arguments and then plows on to recursively delete the contents of the root partition.&lt;br /&gt;
&lt;br /&gt;
===Example 3===&lt;br /&gt;
&lt;br /&gt;
The following code from a privileged program uses the environment variable $APPHOME to determine the application's installation directory, and then executes an initialization script in that directory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
       ...&lt;br /&gt;
       char* home=getenv(&amp;quot;APPHOME&amp;quot;);&lt;br /&gt;
       char* cmd=(char*)malloc(strlen(home)+strlen(INITCMD));&lt;br /&gt;
       if (cmd) {&lt;br /&gt;
               strcpy(cmd,home);&lt;br /&gt;
               strcat(cmd,INITCMD);&lt;br /&gt;
               execl(cmd, NULL);&lt;br /&gt;
       }&lt;br /&gt;
       ...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in Example 2, the code in this example allows an attacker to execute arbitrary commands with the elevated privilege of the application. In this example, the attacker can modify the environment variable $APPHOME to specify a different path containing a malicious version of INITCMD. Because the program does not validate the value read from the environment, by controlling the environment variable, the attacker can fool the application into running malicious code.&lt;br /&gt;
&lt;br /&gt;
The attacker is using the environment variable to control the command that the program invokes, so the effect of the environment is explicit in this example. We will now turn our attention to what can happen when the attacker changes the way the command is interpreted.&lt;br /&gt;
&lt;br /&gt;
===Example 4===&lt;br /&gt;
&lt;br /&gt;
The code below is from a web-based CGI utility that allows users to change their passwords. The password update process under NIS includes running ''make'' in the /var/yp directory. Note that since the program updates password records, it has been installed setuid root.&lt;br /&gt;
&lt;br /&gt;
The program invokes make as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
       system(&amp;quot;cd /var/yp &amp;amp;&amp;amp; make &amp;amp;&amp;gt; /dev/null&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike the previous examples, the command in this example is hardcoded, so an attacker cannot control the argument passed to system(). However, since the program does not specify an absolute path for make, and does not scrub any environment variables prior to invoking the command, the attacker can modify their $PATH variable to point to a malicious binary named make and execute the CGI script from a shell prompt. And since the program has been installed setuid root, the attacker's version of make now runs with root privileges.&lt;br /&gt;
&lt;br /&gt;
The environment plays a powerful role in the execution of system commands within programs. Functions like system() and exec() use the environment of the program that calls them, and therefore attackers have a potential opportunity to influence the behavior of these calls.&lt;br /&gt;
&lt;br /&gt;
There are many sites that will tell you that Java's Runtime.exec is exactly the same as C's system function. This is not true. Both allow you to invoke a new program/process. However, C's system function passes its arguments to the shell (/bin/sh) to be parsed, whereas Runtime.exec tries to split the string into an array of words, then executes the first word in the array with the rest of the words as parameters. Runtime.exec does NOT try to invoke the shell at any point. The key difference is that much of the functionality provided by the shell that could be used for mischief (chaining commands using &amp;quot;&amp;amp;&amp;quot;, &amp;quot;&amp;amp;&amp;amp;&amp;quot;, &amp;quot;|&amp;quot;, &amp;quot;||&amp;quot;, etc, redirecting input and output) would simply end up as a parameter being passed to the first command, and likely causing a syntax error, or being thrown out as an invalid parameter.&lt;br /&gt;
&lt;br /&gt;
===Example 5===&lt;br /&gt;
The following trivial code snippets are vulnerable to OS command injection on the Unix/Linux platform:&lt;br /&gt;
&lt;br /&gt;
:* C:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char **argv)&lt;br /&gt;
 {&lt;br /&gt;
      char command[256];&lt;br /&gt;
 &lt;br /&gt;
      if(argc != 2) {&lt;br /&gt;
           printf(&amp;quot;Error: Please enter a program to time!\n&amp;quot;);&lt;br /&gt;
           return -1;&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      memset(&amp;amp;command, 0, sizeof(command));&lt;br /&gt;
 &lt;br /&gt;
      strcat(command, &amp;quot;time ./&amp;quot;);&lt;br /&gt;
      strcat(command, argv[1]);&lt;br /&gt;
 &lt;br /&gt;
      system(command);&lt;br /&gt;
      return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
:* If this were a suid binary, consider the case when an attacker enters the following: 'ls; cat /etc/shadow'. In the Unix environment, shell commands are separated by a semi-colon. We now can execute system commands at will!&lt;br /&gt;
&lt;br /&gt;
:* Java:&lt;br /&gt;
&lt;br /&gt;
'''There are many sites that will tell you that Java's Runtime.exec is exactly the same as C's system function. This is not true. Both allow you to invoke a new program/process. However, C's system function passes its arguments to the shell (/bin/sh) to be parsed, whereas Runtime.exec tries to split the string into an array of words, then executes the first word in the array with the rest of the words as parameters. Runtime.exec does NOT try to invoke the shell at any point. The key difference is that much of the functionality provided by the shell that could be used for mischief (chaining commands using &amp;quot;&amp;amp;&amp;quot;, &amp;quot;&amp;amp;&amp;amp;&amp;quot;, &amp;quot;|&amp;quot;, &amp;quot;||&amp;quot;, etc, redirecting input and output) would simply end up as a parameter being passed to the first command, and likely causing a syntax error, or being thrown out as an invalid parameter.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--==Related [[Threat Agents]]==&lt;br /&gt;
TBD&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
==Related [[Attacks]]==&lt;br /&gt;
* [[Code Injection]]&lt;br /&gt;
* [[Blind SQL Injection]]&lt;br /&gt;
* [[Blind XPath Injection]]&lt;br /&gt;
* [[LDAP injection]]&lt;br /&gt;
* [[Relative Path Traversal]]&lt;br /&gt;
* [[Absolute Path Traversal]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--==Related [[Vulnerabilities]]==&lt;br /&gt;
TBD&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
==Related [[Controls]]==&lt;br /&gt;
* [[:Category:Input Validation]]&lt;br /&gt;
&lt;br /&gt;
Ideally, a developer should use existing API for their language. For example (Java): Rather than use Runtime.exec() to issue a 'mail' command, use the available Java API located at javax.mail.*&lt;br /&gt;
&lt;br /&gt;
If no such available API exists, the developer should scrub all input for malicious characters. Implementing a positive security model would be most efficient. Typically, it is much easier to define the legal characters than the illegal characters.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/77.html CWE-77: Command Injection]&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/78.html CWE-78: OS Command Injection]&lt;br /&gt;
* http://blog.php-security.org/archives/76-Holes-in-most-preg_match-filters.html&lt;br /&gt;
&lt;br /&gt;
[[Category:Injection Attack]]&lt;br /&gt;
[[Category:Injection]]&lt;br /&gt;
[[Category:Attack]]&lt;br /&gt;
[[Category:Externally Linked Page]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#'''Traduccion en Español: ''' [[Traducción]]&lt;/div&gt;</summary>
		<author><name>José Luis Escobar</name></author>	</entry>

	</feed>