<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Avoiding Shell Injection in Ruby, Python and PHP.</title>
	<atom:link href="http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/</link>
	<description>Things that have more than zero impact (on my live)</description>
	<pubDate>Wed, 08 Sep 2010 04:04:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Ron</title>
		<link>http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-2115</link>
		<dc:creator>Ron</dc:creator>
		<pubDate>Wed, 17 Dec 2008 22:17:04 +0000</pubDate>
		<guid isPermaLink="false">http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-2115</guid>
		<description>@Henryk
&#62; This is the desired behavior if you want shell expansion ...

Agreed.  I was faced with a couple inherited binaries which wouldn't work in subprocess without the shell, but I still needed to prevent injection.  Your lsopts example is a good one.
Take care,

Ron</description>
		<content:encoded><![CDATA[<p>@Henryk<br />
&gt; This is the desired behavior if you want shell expansion &#8230;</p>
<p>Agreed.  I was faced with a couple inherited binaries which wouldn&#039;t work in subprocess without the shell, but I still needed to prevent injection.  Your lsopts example is a good one.<br />
Take care,</p>
<p>Ron</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henryk</title>
		<link>http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-2110</link>
		<dc:creator>Henryk</dc:creator>
		<pubDate>Wed, 17 Dec 2008 17:36:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-2110</guid>
		<description>@Ron
&gt; However, this provides an opportunity for injection.
&gt; subprocess.call(r’echo arg0 arg1 arg2 arg3; echo 123; echo $HOME’, shell = True)
&gt;...
&gt;/home/username
This is the desired behavior if you want shell expansion (well, almost, as shell expansion allows you to chain commands other ways than by semicolon too). Only if you know what you are doing and you really want shell expansion then you can put everything in one string.

But your tip still seems helpfull, if you want to mix expanded and unexpanded parameters:
Like in
lsopts="-l"; subprocess.call(["ls $0 *.py",lsopts], shell = True)
the parameter lsopts is protected against injection, while "*.py" will correctly expand to all py-files in the working directory.

Thank you.</description>
		<content:encoded><![CDATA[<p>@Ron<br />
> However, this provides an opportunity for injection.<br />
> subprocess.call(r’echo arg0 arg1 arg2 arg3; echo 123; echo $HOME’, shell = True)<br />
>&#8230;<br />
>/home/username<br />
This is the desired behavior if you want shell expansion (well, almost, as shell expansion allows you to chain commands other ways than by semicolon too). Only if you know what you are doing and you really want shell expansion then you can put everything in one string.</p>
<p>But your tip still seems helpfull, if you want to mix expanded and unexpanded parameters:<br />
Like in<br />
lsopts=&#034;-l&#034;; subprocess.call(["ls $0 *.py",lsopts], shell = True)<br />
the parameter lsopts is protected against injection, while &#034;*.py&#034; will correctly expand to all py-files in the working directory.</p>
<p>Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ron</title>
		<link>http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-2108</link>
		<dc:creator>Ron</dc:creator>
		<pubDate>Wed, 17 Dec 2008 15:16:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-2108</guid>
		<description>Thanks a lot for this post... your writeup of Python's subprocess gave me the right perspective to figure out a problem that's been bugging me for the better part of a day.

I found subprocess to be weird, then I figured out that the "shell = True" keyword argument really messes everything up.  However, my target commands didn't function properly without it.  

Security becomes a big concern when setting shell = True.  Taking a closer look at the subprocess security promise: "this implementation will never call /bin/sh implicitly" (http://docs.python.org/library/subprocess.html#security).  As you point out in the subprocess source, the executable is now /bin/sh.  We are now explicitly calling /bin/sh ... !

Your suggestion is useful for subprocess calls; "If you really want to use shell expansion, it is probably best to put the whole command in one string."  This helps avoid inserting numerous positional parameters.  However, this provides an opportunity for injection.

subprocess.call(r'echo arg0 arg1 arg2 arg3; echo 123; echo $HOME', shell = True)

output:
arg0 arg1 arg2 arg3
123
/home/username

A workaround is: keep things in a list (not a string) and use positional parameters.  For example:

subprocess.call([r'echo $0', r'arg0 arg1 arg2 arg3; echo 123; echo $HOME'], shell = True)

Here, the output is:
arg0 arg1 arg2 arg3; echo 123; echo $HOME

That looks to be sanitized.  The $0 trick with a 2 element list allows you to use subprocess with the shell (some calls will need it) while ignoring the exact number of arguments (to avoid extra work/mistakes in listing a series of positional parameters).

This is now a ridiculously long comment, but I hope it helps to avoid shell injection!
Thanks!

-Ron</description>
		<content:encoded><![CDATA[<p>Thanks a lot for this post&#8230; your writeup of Python&#039;s subprocess gave me the right perspective to figure out a problem that&#039;s been bugging me for the better part of a day.</p>
<p>I found subprocess to be weird, then I figured out that the &#034;shell = True&#034; keyword argument really messes everything up.  However, my target commands didn&#039;t function properly without it.  </p>
<p>Security becomes a big concern when setting shell = True.  Taking a closer look at the subprocess security promise: &#034;this implementation will never call /bin/sh implicitly&#034; (http://docs.python.org/library/subprocess.html#security).  As you point out in the subprocess source, the executable is now /bin/sh.  We are now explicitly calling /bin/sh &#8230; !</p>
<p>Your suggestion is useful for subprocess calls; &#034;If you really want to use shell expansion, it is probably best to put the whole command in one string.&#034;  This helps avoid inserting numerous positional parameters.  However, this provides an opportunity for injection.</p>
<p>subprocess.call(r&#039;echo arg0 arg1 arg2 arg3; echo 123; echo $HOME&#039;, shell = True)</p>
<p>output:<br />
arg0 arg1 arg2 arg3<br />
123<br />
/home/username</p>
<p>A workaround is: keep things in a list (not a string) and use positional parameters.  For example:</p>
<p>subprocess.call([r'echo $0', r'arg0 arg1 arg2 arg3; echo 123; echo $HOME'], shell = True)</p>
<p>Here, the output is:<br />
arg0 arg1 arg2 arg3; echo 123; echo $HOME</p>
<p>That looks to be sanitized.  The $0 trick with a 2 element list allows you to use subprocess with the shell (some calls will need it) while ignoring the exact number of arguments (to avoid extra work/mistakes in listing a series of positional parameters).</p>
<p>This is now a ridiculously long comment, but I hope it helps to avoid shell injection!<br />
Thanks!</p>
<p>-Ron</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henryk</title>
		<link>http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-18</link>
		<dc:creator>Henryk</dc:creator>
		<pubDate>Wed, 20 Aug 2008 10:25:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-18</guid>
		<description>@web design.
I read your argument alreay on reddit and wanted to write a post about it - at some time. 

In short: I don't agree.
It's a bit like saying "to protect yourself from remote exploits, turn off your computer". It's true, but it does not really help. You call other programs, because they solve a problem for you. To turn these into longrunning 'deamons' may take quite some effort.
The spawning of a subprocess for a certain task is quite common in unix, including noteble examples like &lt;a href='http://cr.yp.to/qmail.html' rel="nofollow"&gt;qmail&lt;/a&gt; by Daniel J. Bernstein. In qmail it is an explicit security feature.

While I know very little about &lt;a href='http://developer.apple.com/macosx/launchd.html' rel="nofollow"&gt;launchd&lt;/a&gt; reading the "Some background—why launchd?" and "Benefits of launchctl" sections it seems to me that you misunderstood the purpose of it. Though there might reasons to run subprocess through it, e.g. if you want to replace one program by another, or you want to fine-control the amount of memory that a certain subprocess may consume without stopping your main application. If this is worth the overhead (startup, configuration, programming, ...) depends on the speciffic situation.

I still plan to write more about it. Some day.</description>
		<content:encoded><![CDATA[<p>@web design.<br />
I read your argument alreay on reddit and wanted to write a post about it - at some time. </p>
<p>In short: I don&#039;t agree.<br />
It&#039;s a bit like saying &#034;to protect yourself from remote exploits, turn off your computer&#034;. It&#039;s true, but it does not really help. You call other programs, because they solve a problem for you. To turn these into longrunning &#039;deamons&#039; may take quite some effort.<br />
The spawning of a subprocess for a certain task is quite common in unix, including noteble examples like <a href='http://cr.yp.to/qmail.html' rel="nofollow">qmail</a> by Daniel J. Bernstein. In qmail it is an explicit security feature.</p>
<p>While I know very little about <a href='http://developer.apple.com/macosx/launchd.html' rel="nofollow">launchd</a> reading the &#034;Some background—why launchd?&#034; and &#034;Benefits of launchctl&#034; sections it seems to me that you misunderstood the purpose of it. Though there might reasons to run subprocess through it, e.g. if you want to replace one program by another, or you want to fine-control the amount of memory that a certain subprocess may consume without stopping your main application. If this is worth the overhead (startup, configuration, programming, &#8230;) depends on the speciffic situation.</p>
<p>I still plan to write more about it. Some day.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: web design</title>
		<link>http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-16</link>
		<dc:creator>web design</dc:creator>
		<pubDate>Tue, 19 Aug 2008 23:21:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-16</guid>
		<description>The best way to avoid shell injection is to not rely on any shell functionality whatsoever. If you need to communicate with another program, that program should be running already, under the appropriate security restrictions and with its own IPC endpoint set up. If your process is quick to start but expensive to keep running, then you can go the route Apple went with launchd: build a shell that listens on a pipe or Unix socket or..., and when an inbound requests comes in, `fork` and `exec`.</description>
		<content:encoded><![CDATA[<p>The best way to avoid shell injection is to not rely on any shell functionality whatsoever. If you need to communicate with another program, that program should be running already, under the appropriate security restrictions and with its own IPC endpoint set up. If your process is quick to start but expensive to keep running, then you can go the route Apple went with launchd: build a shell that listens on a pipe or Unix socket or&#8230;, and when an inbound requests comes in, `fork` and `exec`.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jedai</title>
		<link>http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-10</link>
		<dc:creator>Jedai</dc:creator>
		<pubDate>Mon, 11 Aug 2008 16:47:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.littleimpact.de/index.php/2008/08/11/avoiding-shell-injection-in-ruby-python-and-php/#comment-10</guid>
		<description>Perl has more or less the same solutions as Ruby. Except you can use open in a safe manner too (open my($process), '-&#124;', 'ls', @args), and the taint mode is really helpful to avoid most stupid security flaws.</description>
		<content:encoded><![CDATA[<p>Perl has more or less the same solutions as Ruby. Except you can use open in a safe manner too (open my($process), &#039;-|&#039;, &#039;ls&#039;, @args), and the taint mode is really helpful to avoid most stupid security flaws.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
