<?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>nerthase 2.0 &#187; PHP &amp; MySql</title>
	<atom:link href="http://www.nerthase.com/blog/category/guide/php-mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.nerthase.com/blog</link>
	<description>Progettazione... Programmazione... Soluzione...</description>
	<lastBuildDate>Wed, 29 Jun 2011 07:11:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java: HTTP POST con Socket</title>
		<link>http://www.nerthase.com/blog/java-http-post-con-socket?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=java-http-post-con-socket</link>
		<comments>http://www.nerthase.com/blog/java-http-post-con-socket#comments</comments>
		<pubDate>Sat, 24 Apr 2010 21:39:16 +0000</pubDate>
		<dc:creator>nerthase</dc:creator>
				<category><![CDATA[Guide]]></category>
		<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[Informatica & co]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[crack]]></category>
		<category><![CDATA[Hack]]></category>
		<category><![CDATA[hacker]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[Password]]></category>
		<category><![CDATA[POST]]></category>
		<category><![CDATA[Socket]]></category>
		<category><![CDATA[username]]></category>

		<guid isPermaLink="false">http://www.nerthase.com/blog/?p=603</guid>
		<description><![CDATA[Come potete vedere nei precedenti articoli riguardanti il Brute Force in Java e i vari metodi di attacco per le password, la sicurezza informatica è uno degli aspetti dell&#8217;informatica che preferisco, soprattutto per quanto riguarda la sicurezza in Internet. Vi sembrerà strano, ma non potete immaginare quanta gente dia per scontato il fattore sicurezza mentre&#8230;]]></description>
			<content:encoded><![CDATA[<p>Come potete vedere nei precedenti articoli riguardanti il <a title="Brute Force in Java - Attacco - Crack - Hack" href="http://www.nerthase.com/blog/brute-force-in-java" target="_blank">Brute Force in Java</a> e i vari metodi di <a title="Attacco password - Crack - Hack" href="http://www.nerthase.com/blog/password-come-evitare-il-crack" target="_blank">attacco per le password</a>, la sicurezza informatica è uno degli aspetti dell&#8217;informatica che preferisco, soprattutto per quanto riguarda la sicurezza in Internet. Vi sembrerà strano, ma non potete immaginare quanta gente dia per scontato il fattore sicurezza mentre naviga in rete, crea account e soprattutto quanto <strong>sceglie la password</strong>.</p>
<p>Proprio sulla scelta della password vorrei soffermarmi un attimo con una piccola considerazione, basata su dati statistici. Infatti, secondo alcuni studi, la password usata più spesso dagli utenti è la stringa <em>&#8220;123456&#8243;</em>. Questo dato mi è sembrato sconvolgente, ma posso assicurarvi che è molto realistico. Su questo argomento credo di fare a breve un articolo più approfondito, quindi per ora consideriamo semplicemente l&#8217;aspetto che riguarda Java, ed in particolare <strong>come fare una richiesta http usando il metodo POST, sfruttando i Socket</strong>.</p>
<div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div>
<p>I due argomenti sopra citati sono strettamente collegati, in quanto le applicazioni web sono costruite essenzialmente sfruttando il metodo GET per le varie richieste. Il metodo POST viene usato per elementari requisiti di sicurezza per la trasmissione di dati sensibili, quali appunto <strong>username</strong> e <strong>password</strong> al momento del login su un servizio web. Il metodo che vi mostro quindi, può essere appositamente adattato per effettuare un login su un&#8217;applicazione web usando Java. Un esempio concreto di ciò, potrebbe essere la creazione di un&#8217;applicazione Java che effettua una connessione all&#8217;applicazione web per effettuare determinate operazioni.</p>
<p>Solitamente una applicazione web che richiede il login, ha una porzione della pagina che effettua una richiesta POST verso una specifica pagina che si occupa di verificare la correttezza dei dati forniti:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;login.php&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">target</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_top&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">maxlength</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">maxlength</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;login&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Login&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></div></td></tr></tbody></table></div>
<p>Per realizzare il nostro scopo, dobbiamo analizzare la pagina specifica, ed annotare la struttura del messaggio inviato tramite POST, e la pagina che riceve i dati, che è specificata nel parametro <strong>action</strong> dell&#8217;elemento <em>form</em>.</p>
<div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div>
<p>Per quanto riguarda la parte Java, questo è un esempio di come realizzare la richiesta POST usando solo le Socket.</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedReader</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedWriter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStreamReader</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.OutputStreamWriter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.InetAddress</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.Socket</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URLEncoder</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SocketPost <span style="color: #009900;">&#123;</span> &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> host &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;HOST-WEB-APP&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> port &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> form_action &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;PAGINA-DI-LOGIN&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> username &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;TUO-USERNAME&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> password &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;TUA-PASSWORD&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> result <span style="color: #339933;">=</span> login<span style="color: #009900;">&#40;</span>host, port, form_action, username, password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> login <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> host, <span style="color: #000066; font-weight: bold;">int</span> port, <span style="color: #003399;">String</span> form_action, <span style="color: #003399;">String</span> username, <span style="color: #003399;">String</span> password<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Dati da inviare tramite POST</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Copmosizione tipica: &nbsp; &nbsp;username=TUOUSER&amp;password=TUAPASSWORD</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> data <span style="color: #339933;">=</span> &nbsp; <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username&quot;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span>username, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span> <span style="color: #339933;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span>password, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Creo il Socket per la connessione all'host</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">InetAddress</span> addr <span style="color: #339933;">=</span> <span style="color: #003399;">InetAddress</span>.<span style="color: #006633;">getByName</span><span style="color: #009900;">&#40;</span>host<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Socket</span> socket <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Socket</span><span style="color: #009900;">&#40;</span>addr, port<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">BufferedWriter</span> wr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedWriter</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">OutputStreamWriter</span><span style="color: #009900;">&#40;</span>socket.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; wr.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;POST &quot;</span> <span style="color: #339933;">+</span> form_action <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; HTTP/1.0<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; wr.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Host: &quot;</span> <span style="color: #339933;">+</span> host <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; wr.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Length: &quot;</span> <span style="color: #339933;">+</span> data.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; wr.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Invio i dati tramite POST</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; wr.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; wr.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Leggo la risposta dall'host</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">BufferedReader</span> br <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>socket.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Solitamente, in caso di pagine di login, </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// il codice di risposta per un login valido è 302</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>br.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;HTTP/1.1 302 Found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; wr.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; br.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Prima di poter essere in grado di riempire il codice Java, bisogna analizzare il sito presso il quale si vuole effettuare il login, per estrapolare tutti i dati necessari. Questa operazione non richiede un particolare sforzo, ma un minimo di esperienza è sempre d&#8217;obbligo. Se avete bisogno di una mano, potete chiedere tranquillamente. Il metodo su illustrato è stato realizzato solo a scopo illustrativo e deve essere utilizzato solo in possesso delle credenziali di accesso, e non per scopi malevoli.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nerthase.com/blog/java-http-post-con-socket/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hacking: SQLInjection con SqlMap</title>
		<link>http://www.nerthase.com/blog/hacking-sqlinjection-con-sqlmap?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hacking-sqlinjection-con-sqlmap</link>
		<comments>http://www.nerthase.com/blog/hacking-sqlinjection-con-sqlmap#comments</comments>
		<pubDate>Tue, 30 Mar 2010 21:19:44 +0000</pubDate>
		<dc:creator>nerthase</dc:creator>
				<category><![CDATA[Guide]]></category>
		<category><![CDATA[Informatica & co]]></category>
		<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[Tutto il resto]]></category>
		<category><![CDATA[crack]]></category>
		<category><![CDATA[GET]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Guida]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[Manuale]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[POST]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Injection]]></category>
		<category><![CDATA[SQLmap]]></category>

		<guid isPermaLink="false">http://www.nerthase.com/blog/?p=499</guid>
		<description><![CDATA[Molte volte ci addentriamo nel mondo del Web, nella creazione di un proprio spazio personale che piano piano si evolve, ma non sempre adottiamo le giuste precauzioni ed i giusti accorgimenti per quanto riguarda la sicurezza. In questi casi, ci viene in aiuto sqlmap, un ottimo tool per l&#8217;analisi delle vulnerabilità presenti in molte applicazioni&#8230;]]></description>
			<content:encoded><![CDATA[<p>Molte volte ci addentriamo nel mondo del Web, nella creazione di un proprio spazio personale che piano piano si evolve, ma non sempre adottiamo le giuste precauzioni ed i giusti accorgimenti per quanto riguarda la sicurezza. In questi casi, ci viene in aiuto <strong><a title="SQL Map - Hacking SQL Injection Tool" href="http://sqlmap.sourceforge.net/" target="_blank">sqlmap</a><span style="font-weight: normal;">, un ottimo tool per l&#8217;analisi delle vulnerabilità presenti in molte applicazioni web che fanno uso di database SQL.</span></strong></p>
<p><img class="aligncenter size-full wp-image-497" title="SQL map" src="http://www.nerthase.com/blog/wp-content/uploads/2010/03/sqlmap.png" alt="" width="218" height="150" /></p>
<h4><span style="font-weight: normal;">Introduzione a SQLmap</span></h4>
<p><strong>SQLmap<span style="font-weight: normal;"> è in grado di automatizzare il processo di analisi di vulnerabilità o exploit ai quali sono soggetti tantissimi server web. SQLmap è scritto in </span>Python<span style="font-weight: normal;">, il che lo rende indipendente dal sistema operativo utilizzato. Per funzionare, richiede solo in </span>interprete Python in versione maggiore o uguale alla 2.5<span style="font-weight: normal;">. Ho già introdotto in precedenti articoli (<a title="Hacking Sql Injection" href="http://www.nerthase.com/blog/hacking-sql-injection" target="_blank">Hacking: SQL Injection</a>, <a title="Difesa e Protezione contro Sql Injection" href="http://www.nerthase.com/blog/difesa-contro-lsql-injection" target="_blank">Difesa contro l’SQL Injection</a>) il concetto di SQLInjection, tutti i pericoli che ne derivano e come difendersi progettando bene la propria applicazione web; ora vediamo come SQLmap riesce nell&#8217;analisi di SQLInjection in modo automatico e preciso.</span></strong> <strong>SQLmap<span style="font-weight: normal;"> implementa tre tecniche per l&#8217;analisi di vulnerabilità per l&#8217;SQL Injection:</span></strong> <strong><span style="font-weight: normal;"> </span></strong></p>
<div id="_mcePaste">
<ol>
<li><strong>Inferential blind SQL injection</strong>, also known as <strong>boolean based blind SQL injection</strong>: sqlmap appends to the affected parameter in the HTTP request, a syntatically valid SQL statement string containing a SELECT sub-statement, or any other SQL statement whose the user want to retrieve the output. For each HTTP response, by making a comparison based upon HTML page content hashes, or string matches, with the original request, the tool determines the output value of the statement character by character. The bisection algorithm implemented in sqlmap to perform this technique is able to fetch each output character with at maximum seven HTTP requests. This is sqlmap default SQL injection technique.</li>
<li><strong>UNION query (inband) SQL injection</strong>, also known as <strong>full UNION query SQL injection</strong>: sqlmap appends to the affected parameter in the HTTP request, a syntatically valid SQL statement string starting with a UNION ALL SELECT. This techique is useful if the web application page passes the output of the SELECT statement to a for cycle, or similar, so that each line of the query output is printed on the page content. sqlmap is also able to exploit <strong>partial (single entry) UNION query SQL injection</strong> vulnerabilities which occur when the output of the statement is not cycled in a for construct whereas only the first entry output is displayed. This technique is much faster if the target url is affected by because in a single HTTP response it returns the whole query output or a entry per each response within the page content. This SQL injection technique is an alternative to the first one.</li>
<li><strong>Batched (stacked) queries support</strong>, also known as <strong>multiple statements support</strong>: sqlmap tests if the web application supports stacked queries then, in case it does support, it appends to the affected parameter in the HTTP request, a semi-colon (;) followed by the SQL statement to be executed. This technique is useful to run SQL statements other than SELECT like, for instance, data definition or data manipulation statements possibly leading to file system read and write access and operating system command execution depending on the underlying back-end database management system and the session user privileges.</li>
</ol>
</div>
<p style="text-align: left;"><div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></p>
<h4><span style="font-weight: normal;">Uso di SQLmap</span></h4>
<p>SQLmap è un tool molto potente quanto sofisticato. Il suo uso non è molto semplice ed immediato, ma vediamo di imparare ad usarlo con una guida passo passo. Ricordo che è possibile trovare un <strong><a title="Manuale e guida per SqlMap - SQL Injection" href="http://sqlmap.sourceforge.net/doc/README.pdf" target="_blank">manuale molto completo sul sito ufficiale del progetto</a></strong>. Un primo sguardo all&#8217;uso del programma, può essere ottenuto con il comando  <code class="codecolorer text mac-classic"><span class="text">python sqlmap.py -h</span></code>  da digitare nel proprio <strong>Terminale</strong> in Mac OS X o in una qualsiasi console di comando di un sistema operativo Linux o Windows. Questo comando restituisce una lista molto completa e dettagliata di tutti le opzioni di uso del programma.</p>
<p>Un&#8217;opzione molto valida è quella di ottenere un output del programma, analizzando i vari livelli di output. I livelli di <strong>verbosity </strong>(opzione<strong> -v</strong>)<strong> </strong>disponibili sono:</p>
<p><code class="codecolorer text mac-classic"><span class="text">python sqlmap.py -u &quot;http://172.16.213.131/sqlmap/mysql/get_int.php?id=1&quot; -v 1</span></code></p>
<p><code class="codecolorer text mac-classic"><span class="text">python sqlmap.py -u &quot;http://172.16.213.131/sqlmap/mysql/get_int.php?id=1&quot; -v 2</span></code></p>
<p><code class="codecolorer text mac-classic"><span class="text">python sqlmap.py -u &quot;http://172.16.213.131/sqlmap/mysql/get_int.php?id=1&quot; -v 3</span></code></p>
<p><code class="codecolorer text mac-classic"><span class="text">python sqlmap.py -u &quot;http://172.16.213.131/sqlmap/mysql/get_int.php?id=1&quot; -v 4</span></code></p>
<p><code class="codecolorer text mac-classic"><span class="text">python sqlmap.py -u &quot;http://172.16.213.131/sqlmap/mysql/get_int.php?id=1&quot; -v 5</span></code></p>
<p>L&#8217;opzione <strong>-u</strong> specificata nel comando precedente è obbligatoria e serve per specificare l&#8217;indirizzo da analizzare. Alternativamente, può essere usata l&#8217;opzione <strong>&#8211;url</strong>.</p>
<p><code class="codecolorer text mac-classic"><span class="text">python sqlmap.py --url &quot;http://172.16.213.131/sqlmap/mysql/get_int.php?id=1&quot;</span></code></p>
<p>Un&#8217;altra modalità di analisi, che non includa direttamente l&#8217;indirizzo web del sito da analizzare, è quella di specificare un file contenente una <strong>richiesta HTTP</strong> valida, che quindi SQLmap effettuerà analizzando i risultati (opzione <strong>-r</strong>). In questo caso, deve esistere un file contenente la richiesta HTTP in questo modo:</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">POST /sqlmap/mysql/post_int.php<br />
<br />
HTTP/1.1 &nbsp;Host: 172.16.213.131<br />
<br />
User-Agent: Mozilla/4.0<br />
<br />
id=1</div></div>
<p>e quindi l&#8217;applicazione SQLmap dovrà essere eseguita nel modo seguente:</p>
<p><code class="codecolorer text mac-classic"><span class="text">python sqlmap.py -r request.txt</span></code></p>
<div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div>
<p>Una funzionalità ancora più interessante, consiste nell&#8217;analizzare un intero sito web, alla ricerca di tutte le possibili vulnerabilità. Questa feature è possibile grazie all&#8217;appoggio di Google, che è in grado di restituire risultati relativi ad un <strong>singolo dominio</strong> specificando ulteriori opzioni (opzione <strong>-g</strong> del programma SQLmap). Il comando diventa quindi:</p>
<p><code class="codecolorer text mac-classic"><span class="text">python sqlmap.py -g &quot;site:yourdomain.com ext:php&quot; -v 1</span></code></p>
<p>In questo caso SQLmap analizzerà i primi 100 risultati di Google relativi al dominio specificato, alla ricerca di risultati che contengano al loro interno richieste <strong>GET</strong>, in modo da poter verificare la presenza di possibili vulnerabilità.</p>
<p>SQLmap non è in grado di analizzare solo applicazioni web che fanno uso di richieste <strong>GET</strong>, ma anche quei servizi che per maggiore protezione, fanno uso di richieste <strong>POST</strong>. In questo caso entrano in gioco le opzioni <strong>&#8211;method </strong>e<strong> &#8211;data </strong>di SQLmap:</p>
<p><code class="codecolorer text mac-classic"><span class="text">python sqlmap.py -u &quot;http://test-site.it/test-page.php&quot; --method POST --data &quot;id=1&quot;</span></code></p>
<p>Queste sono solo alcune situazioni di utilizzo di SQLmap, è possibile trovarne altre sul <a title="Manuale e Guida per SQLmap - SQL Injection" href="http://sqlmap.sourceforge.net/doc/README.pdf" target="_blank">manuale ufficiale</a> presente sul sito web del programma. Di seguito uno dei <a title="Video Guida SQL map" href="http://sqlmap.sourceforge.net/demo.html" target="_blank">video ufficiali</a> per l&#8217;uso del programma disponibili anch&#8217;essi sul sito web del programma:</p>
<p style="text-align: center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/fGBQm9Nfn24&amp;hl=it_IT&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="480" height="385" src="http://www.youtube.com/v/fGBQm9Nfn24&amp;hl=it_IT&amp;fs=1&amp;" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nerthase.com/blog/hacking-sqlinjection-con-sqlmap/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Difesa contro l&#8217;SQL Injection</title>
		<link>http://www.nerthase.com/blog/difesa-contro-lsql-injection?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=difesa-contro-lsql-injection</link>
		<comments>http://www.nerthase.com/blog/difesa-contro-lsql-injection#comments</comments>
		<pubDate>Mon, 22 Mar 2010 21:48:33 +0000</pubDate>
		<dc:creator>nerthase</dc:creator>
				<category><![CDATA[Guide]]></category>
		<category><![CDATA[Informatica & co]]></category>
		<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[Hack]]></category>
		<category><![CDATA[Injection]]></category>
		<category><![CDATA[sicurezza]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.nerthase.com/blog/?p=416</guid>
		<description><![CDATA[Nel precedente articolo vi ho descritto la tecnica dell&#8217;SQL Injection, la sua pericolosità e le sue potenzialità. Ora, vediamo come è possibile adottare opportuni accorgimenti per difenderci contro questo attacco. La tecnica dell&#8217;SQL Injection nasce dal fatto che è possibile introdurre codice malevolo all&#8217;interno di normali campi di testo. Evitare ciò non è di solito&#8230;]]></description>
			<content:encoded><![CDATA[<p>Nel <a title="SQL Injection - Guida - Esempi" href="http://www.nerthase.com/blog/hacking-sql-injection" target="_blank">precedente articolo</a> vi ho descritto la tecnica dell&#8217;<a title="SQL Injection - Guida - Esempi" href="http://www.nerthase.com/blog/hacking-sql-injection" target="_blank">SQL Injection</a>, la sua pericolosità e le sue potenzialità. Ora, vediamo come è possibile adottare opportuni accorgimenti per difenderci contro questo attacco.</p>
<p>La tecnica dell&#8217;SQL Injection nasce dal fatto che è possibile introdurre codice malevolo all&#8217;interno di normali campi di testo. Evitare ciò non è di solito possibile, però è sicuramente possibile (quasi doveroso) effettuare un controllo sui dati che l&#8217;utente immette in questi campi. Vediamo di seguito un paio di tecniche che è possibile adottare per rendere innocui anche i tentativi maliziosi.</p>
<h4>Magic Quotes</h4>
<p>Nelle impostazioni di PHP, ce n&#8217;è una adatta al caso nostro. Si tratta del flag <strong>magic_quotes_gpc</strong> presente nel file di configurazione <strong>php.ini</strong>. Abilitando questo flag, si è garantiti che tutti i dati ricevuto tramite GET, POST e COOKIE saranno automaticamente controllati, ed eventualmente convertiti, prima di essere usati nei vostri script PHP. Vedete quindi, che con un semplice passaggio, si è assolutamente protetti contro ogni tipo di attacco SQL Injection. Di seguito la porzione del file <strong>php.ini</strong> relativa a questa funzione.</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">; Magic quotes<br />
;<br />
; Magic quotes for incoming GET/POST/Cookie data.<br />
magic_quotes_gpc = On<br />
; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc.<br />
magic_quotes_runtime = Off<br />
; Use Sybase-style magic quotes (escape ' with '' instead of \').<br />
magic_quotes_sybase = Off</div></td></tr></tbody></table></div>
<p>Tuttavia, molti servizi di hosting hanno di default questo flag ad OFF, con l&#8217;impossibilità di impostarlo su ON. Quando accade ciò, bisognerà fare manualmente, in fase di programmazione, il controllo di tutti i dati ottenuti dall&#8217;esterno, ed eventualmente la conversione dei caratteri speciali. Questo è di norma ciò che accade, e vediamo come è possibile realizzarlo.<br />
<div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></p>
<h4>Escape dei caratteri speciali</h4>
<p>Quando si compongono query SQL usando dati forniti dall&#8217;utente, è sempre opportuno convertire i caratteri speciali presenti in questi dati, in caratteri innocui per il database SQL. Ad esempio, la stringa <strong>l&#8217;attacco</strong> se inserita così come è in una query SQL, causerà un errore dovuto alla presenza del carattere <strong>&#8216;</strong> (apice singolo). Effettuando un escape dei caratteri speciali, la stringa sarà convertita in <strong>l\&#8217;attacco</strong>, e quindi il carattere <strong>&#8216;</strong> non sarà più pericoloso, perchè il motore SQL interpreta la sequenza <strong>\&#8217;</strong> come un unico carattere, che non sarà confuso con il carattere <strong>&#8216;</strong>.</p>
<p>In PHP questo è possibile attraverso la funzione<strong> mysql_real_escape_string($stringa_pericolosa)</strong>. Questa funzione effettua esattamente i controlli precedentemente illustrati e le relative sostituzioni, non solo per il carattere <strong>&#8216;</strong> (apice singolo) ma per tutti i caratteri speciali ritenuti pericolosi. L&#8217;utilizzo di questa funzione è altamente raccomandato su tutti i dati ricevuti da un utente esterno, proprio per evitare l&#8217;introduzione di codice malevolo all&#8217;interno delle proprie applicazioni.</p>
<p><a title="PHP Manual: mysql_real_escape_string" href="http://www.php.net/manual/en/function.mysql-real-escape-string.php" target="_blank">PHP Manual: mysql_real_escape_string</a></p>
<p>Con questo articolo spero di aver chiarito l&#8217;<a title="SQL Injection - Guida - Esempi" href="http://www.nerthase.com/blog/hacking-sql-injection" target="_blank">introduzione all&#8217;SQL Injection</a> fatta nel precedente articolo, e di avervi convinto a progettare bene fin dall&#8217;inizio il codice delle vostre applicazioni.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nerthase.com/blog/difesa-contro-lsql-injection/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Hacking: SQL Injection</title>
		<link>http://www.nerthase.com/blog/hacking-sql-injection?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hacking-sql-injection</link>
		<comments>http://www.nerthase.com/blog/hacking-sql-injection#comments</comments>
		<pubDate>Sun, 21 Mar 2010 21:36:01 +0000</pubDate>
		<dc:creator>nerthase</dc:creator>
				<category><![CDATA[Guide]]></category>
		<category><![CDATA[Informatica & co]]></category>
		<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[Hack]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Password]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sicurezza]]></category>
		<category><![CDATA[SQL Injection]]></category>

		<guid isPermaLink="false">http://www.nerthase.com/blog/?p=409</guid>
		<description><![CDATA[Tutti coloro che hanno lavorato almeno una volta con un database, soprattutto per quanto riguarda la gestione di utenti e relative password, sapranno che la tecnica più diffusa per appropriarsi di dati sensibili, è la tecnica chiamata Sql Injection. La SQL injection è una tecnica dell&#8217;hacking mirata a colpire le applicazioni web che si appoggiano&#8230;]]></description>
			<content:encoded><![CDATA[<p>Tutti coloro che hanno lavorato almeno una volta con un database, soprattutto per quanto riguarda la gestione di utenti e relative password, sapranno che la tecnica più diffusa per appropriarsi di dati sensibili, è la tecnica chiamata <strong>Sql Injection</strong>.</p>
<p><cite title="SQL Injection from Wikipedia">La </cite><strong><cite title="SQL Injection from Wikipedia">SQL injection</cite></strong><cite title="SQL Injection from Wikipedia"> è una tecnica dell&#8217;hacking mirata a colpire le applicazioni web che si appoggiano su un database di tipo SQL. Questo exploit sfrutta l&#8217;inefficienza dei controlli sui dati ricevuti in input ed inserisce codice maligno all&#8217;interno di una query SQL. Le conseguenze prodotte sono imprevedibili per il programmatore: l&#8217;Sql Injection permette al malintenzionato di autenticarsi con ampi privilegi in aree protette del sito (ovviamente, anche senza essere in possesso delle credenziali d&#8217;accesso) e di visualizzare e/o alterare dati sensibili.</cite><br />
<div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></p>
<p>Di seguito un esempio concreto per valutare le caratteristiche e la pericolosità di questa tecnica, con i seguenti componenti:</p>
<ul>
<li><strong>db_name</strong> &#8211; Questo è il database al quale faremo riferimento;</li>
<li><strong>users</strong> &#8211; Questa è la tabella all&#8217;interno del DB che conterrà i dati degli utenti, in particolare <strong>username</strong> e <strong>password</strong>;</li>
<li><strong>login.php</strong> &#8211; Pagina php contenente un semplice script per verificare se l&#8217;username e password forniti sono corretti (quindi sono all&#8217;interno del DB);</li>
<li><strong>form.html</strong> &#8211; Pagina html che chiede i dati (username e password) tramite browser e richiama lo script php.</li>
</ul>
<p>Vediamo dapprima il codice della pagina <strong>form.html</strong> che contiene un semplice form per la richiesta di <strong>username </strong>e <strong>password</strong>:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'login.php'</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'post'</span>&gt;</span><br />
&nbsp; &nbsp; Username: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text'</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'username'</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; Password: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'password'</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'password'</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'submit'</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Login'</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></div></td></tr></tbody></table></div>
<p>Di sopra ho trascritto solo la parte della pagina html contenente il form, che ha solo due campi, uno per l&#8217;<strong>username</strong> e uno per la <strong>password</strong>. L&#8217;azione del form, è impostata per richiamare la pagina <strong>login.php</strong> che recupererà i dati immessi tramite <em>$_POST['user']</em> e <em>$_POST['pwd']</em>. Dopo aver ricevuto questi dati, lo script PHP interagisce con il database per verificarne la validità. Ecco quindi il codice della pagina <strong>login.php</strong>:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// CONNESSIONE AL DATABASE</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$connessione</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;db_address&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;db_username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;db_password&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Errore!!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// SELEZIONE DEL DATABASE</span><br />
&nbsp; &nbsp; <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;db_name&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connessione</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Errore!!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// PREPARO LA QUERY</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM users WHERE username='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' AND password='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ESEGUO LA QUERY</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connessione</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// CONTO IL NUMERO DI RISULTATI TROVATI</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 1. I DATI SONO VALIDI</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 0. DATI NON VALIDI</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">mysql_affected_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;LOGIN ESEGUITO CON SUCCESSO&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;DATI NON CORRETTI&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>La tecnica di hackin <strong>Sql Injection</strong> consiste proprio nell&#8217;inviare allo script PHP dati maligni tramite il form in HTML. In questo caso, basta immettere per esempio come username <strong>nerthase</strong> e come password <strong>&#8216;OR &#8217;1&#8242;=&#8217;1</strong> per accedere con le credenziali dell&#8217;utente <em>nerthase</em> (ammesso che esso esista). La query per il database diventa quindi:</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT * FROM users WHERE username='nerthase' AND password='' OR '1'='1'</div></div>
<p>La clausola <strong>WHERE</strong> è sempre soddisfatta, in quanto la seconda parte di essa, che è in <strong>OR</strong>, è sempre verificata. L&#8217;attaccante quindi, è riuscito ad effettuare l&#8217;accesso con l&#8217;account <em>nerthase</em> anche senza conoscere la relativa password. Questa tecnica è molto diffusa tra la comunità Hacker, e soprattutto tra i dilettanti che si avvicinano per la prima volta a questo mondo. L&#8217;unica possibilità per evitare questo tipo di attacco, è un controllo sui dati ricevuti nello script PHP. Questo può essere fatto in diversi modi, a seconda delle necessità dell&#8217;applicazione:</p>
<ul>
<li>forzare il tipo dei dati ricevuti (se ci si aspetta un valore numerico, considerare validi solo input numerici;</li>
<li>effettuare l&#8217;escape dei dati ricevuti (ogni linguaggio, solitamente, mette a disposizione particolari strumenti per questo scopo);</li>
<li>criptare le credenziali di accesso prima di inserirle nella query SQL (evitare che le informazioni sensibili siano memorizzate nel DB in chiaro).</li>
</ul>
<p>Questi metodi possono essere anche combinati al fine di rafforzare la sicurezza dell&#8217;applicazione. Occorre, quindi, prestare particolare attenzione a tutte le varianti di un input, tenendo conto di ogni possibile (oppure improbabile) ipotesi. Potete approfondire questo argomento, per quanto riguarda la <a title="protezione contro l'SQL Injection" href="http://www.nerthase.com/blog/difesa-contro-lsql-injection" target="_blank">protezione contro l&#8217;SQL Injection</a> nel <a title="protezione contro l'SQL Injection" href="http://www.nerthase.com/blog/difesa-contro-lsql-injection" target="_blank">relativo articolo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nerthase.com/blog/hacking-sql-injection/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MySQL con Java grazie a JDBC</title>
		<link>http://www.nerthase.com/blog/mysql-con-java-grazie-a-jdbc?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mysql-con-java-grazie-a-jdbc</link>
		<comments>http://www.nerthase.com/blog/mysql-con-java-grazie-a-jdbc#comments</comments>
		<pubDate>Tue, 09 Mar 2010 20:55:35 +0000</pubDate>
		<dc:creator>nerthase</dc:creator>
				<category><![CDATA[Guide]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[connector]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.nerthase.com/blog/?p=342</guid>
		<description><![CDATA[Nei miei ultimi esperimenti con Java, mi è capitato di dover interagire con database MySQL. Per la parte che riguarda il database, ho sempre usato MAMP, un ottimo programma che gestisce in una semplice interfaccia grafica, Apache, MySQL e PHP. Per far comunicare Java ed il database MySQL c&#8217;è bisogno di un driver, in particolare&#8230;]]></description>
			<content:encoded><![CDATA[<p>Nei miei ultimi esperimenti con <a title="Java" href="http://www.nerthase.com/blog/tag/java" target="_blank">Java</a>, mi è capitato di dover interagire con database MySQL. Per la parte che riguarda il database, ho sempre usato <a title="Mac Apache MySQL Php" href="http://www.mamp.info/en/index.html" target="_blank">MAMP</a>, un ottimo programma che gestisce in una semplice interfaccia grafica, Apache, MySQL e PHP.</p>
<p>Per far comunicare Java ed il database MySQL c&#8217;è bisogno di un driver, in particolare ho usato <a title="Driver JDBC Connector MySQL" href="http://www.mysql.com/products/connector/" target="_blank">JDBC</a>. Un esempio di utilizzo del driver è il seguente:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Connection</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.DriverManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.ResultSet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Statement</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DataBaseTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> testDB <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Carico il Driver</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.mysql.jdbc.Driver&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Connetto al Database (URL DB, USER, PWD)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Connection</span> db <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jdbc:mysql://localhost:3306/test&quot;</span>, <span style="color: #0000ff;">&quot;root&quot;</span>, <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Creo uno Statement per l'invio della query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Statement</span> s <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">db</span>.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Invio la query e catturo i risultati</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">ResultSet</span> rs <span style="color: #339933;">=</span> s.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT nome FROM tabella WHERE nick = 'nerthase'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> o <span style="color: #339933;">=</span> rs.<span style="color: #006633;">first</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// In caso non siano stati trovati risultati, inserisco l'entry nel database</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>o<span style="color: #009900;">&#41;</span> s.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO tabella (nome, nick) VALUES ('mario', 'nerthase')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; s.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p><div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div><br />
Questo codice, implica che sia stato creato un database chiamato <em><strong>test</strong></em> con una tabella chiamata <strong><em>tabella</em></strong> costituita da almeno due campi chiamati <em><strong>nome</strong></em> e <em><strong>nick</strong></em>. Il particolare questo piccolo esempio di codice, verifica se nella tabella è presente una entry che verifica la condizione <em>nick = &#8216;nerthase&#8217;</em>, ed in caso questa non è presente, inserisce la entry completa corrispondente. Un esempio di tabella usata per il test potrebbe essere il seguente:</p>
<table style="width: 150px;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="text-align: center;"><strong>nome</strong></td>
<td style="text-align: center;"><strong>nick</strong></td>
</tr>
<tr>
<td style="text-align: center;">silvia</td>
<td style="text-align: center;">moonny</td>
</tr>
<tr>
<td style="text-align: center;">fabio</td>
<td style="text-align: center;">noeffex</td>
</tr>
<tr>
<td style="text-align: center;">mario</td>
<td style="text-align: center;">nerthase</td>
</tr>
</tbody>
</table>
<p>Questa è la mia prima esperienza con MySQL e soprattutto per l&#8217;integrazione con <a title="Java" href="http://www.nerthase.com/blog/tag/java" target="_blank">Java</a>. Questo metodo però funziona molto bene, senza alcun errore, almeno per il momento. Per consigli o chiarimenti non esitate a contattarmi.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nerthase.com/blog/mysql-con-java-grazie-a-jdbc/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Regex Tester &#8211; RegexPal</title>
		<link>http://www.nerthase.com/blog/regex-tester-regexpal?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=regex-tester-regexpal</link>
		<comments>http://www.nerthase.com/blog/regex-tester-regexpal#comments</comments>
		<pubDate>Thu, 28 Jan 2010 17:18:09 +0000</pubDate>
		<dc:creator>nerthase</dc:creator>
				<category><![CDATA[Guide]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[espressioni regolari]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[RegexPal]]></category>

		<guid isPermaLink="false">http://www.nerthase.com/blog/?p=128</guid>
		<description><![CDATA[Proviamo in tempo reale le nostre espressioni regolari con Regex Tester. Spesso nei miei esperimenti, ho bisogno di ricorrere alle espressioni regolari per catturare una particolare sequenza di caratteri, da una stringa di dimensioni più elevate. Per fare questo in Java ci sono le classi Pattern e Matcher, che richiedono come input la stringa originale,&#8230;]]></description>
			<content:encoded><![CDATA[<h3>Proviamo in tempo reale le nostre espressioni regolari con <a title="Regex Tester - RegexPal" href="http://regexpal.com/" target="_blank">Regex Tester</a>.</h3>
<p>Spesso nei miei esperimenti, ho bisogno di ricorrere alle espressioni regolari per catturare una particolare sequenza di caratteri, da una stringa di dimensioni più elevate.</p>
<p>Per fare questo in Java ci sono le classi Pattern e Matcher, che richiedono come input la stringa originale, e l&#8217;espressione regolare. L&#8217;uso di queste classi è semplice ed immediato, ma non sempre è altrettanto semplice ed intuitivo, capire perché un&#8217;espressione regolare non svolge correttamente il suo lavoro.<br />
<div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div><br />
Ho quindi scoperto <a title="Regex Tester - RegexPal" href="http://regexpal.com/" target="_blank">Regex Tester</a>, un sito web che permette di testare in tempo reale gli effetti di un&#8217;espressione regolare. Abbiamo a disposizione una guida rapida all&#8217;uso ed alla scrittura di espressioni regolari, e possiamo anche personalizzare il comportamento di alcuni caratteri speciali dell&#8217;espressione regolare.</p>
<p style="text-align: center;"><a class="lightbox" style="text-decoration: none;" title="Regex Tester" href="http://www.nerthase.com/blog/wp-content/uploads/2010/01/Regex-Tester.png"><img class="aligncenter size-full wp-image-131" title="Regex Tester" src="http://www.nerthase.com/blog/wp-content/uploads/2010/01/Regex-Tester.png" alt="" width="575" height="314" /></a></p>
<p>Fino a poco tempo fa non conoscevo nemmeno cosa fossero le espressioni regolari, ma vi assicuro che una volta che avete imparato ad utilizzarle, non ne potete fare più a meno.</p>
<p>Vediamo come <a title="Regex Tester - RegexPal" href="http://regexpal.com/" target="_blank">Regex Tester</a> può essere utilizzato per ovviare ad un problema molto comune, come può essere quello di estrarre da un testo, tutti gli indirizzi email che contiene. L&#8217;espressione regolare che uso in questo caso è:</p>
<p style="text-align: center;"><code class="codecolorer php mac-classic"><span class="php"><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>_\<span style="color: #339933;">.</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+@</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span></span></code></p>
<p><div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div><br />
Vediamo quindi <a title="Regex Tester - RegexPal" href="http://regexpal.com/" target="_blank">Regex Tester</a> come ci aiuta nel testare se l&#8217;espressione regolare restituisce quello che vogliamo. Per fare questo, prendo in considerazione una porzione di codice HTML rappresentante una tabella che contiene in una colonna degli indirizzi email.</p>
<p style="text-align: center;"><a class="lightbox" title="Regex per Email" href="http://www.nerthase.com/blog/wp-content/uploads/2010/01/regex-email.png"><img class="aligncenter size-full wp-image-130" title="Regex per Email" src="http://www.nerthase.com/blog/wp-content/uploads/2010/01/regex-email.png" alt="" width="607" height="355" /></a></p>
<p>Possiamo quindi verificare la correttezza dell&#8217;espressione regolare, e soprattutto il comportamento che ci aspettavamo.</p>
<p>Se avete dei dubbi sulla formulazione di una espressione regolare, di come interagire con esse in Java o Php non esitate a chiedere.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nerthase.com/blog/regex-tester-regexpal/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Google Data API</title>
		<link>http://www.nerthase.com/blog/google-data-api?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=google-data-api</link>
		<comments>http://www.nerthase.com/blog/google-data-api#comments</comments>
		<pubDate>Wed, 27 Jan 2010 08:46:48 +0000</pubDate>
		<dc:creator>nerthase</dc:creator>
				<category><![CDATA[Guide]]></category>
		<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[GData]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[YouTube]]></category>

		<guid isPermaLink="false">http://www.nerthase.com/blog/?p=111</guid>
		<description><![CDATA[Google Data Protocol è un protocollo che deriva da REST ed il suo scopo è quello di interfacciare applicazioni di terze parti con gli applicativi Google. Insieme ad alcuni amici dell&#8217;Università, stiamo sviluppando un progetto in cui è necessario interfacciarsi con YouTube. Le API di Google ci sono venute in soccorso per ogni problema o&#8230;]]></description>
			<content:encoded><![CDATA[<p><a title="Google Data Protocol" href="http://code.google.com/intl/it-IT/apis/gdata/" target="_blank">Google Data Protocol</a> è un protocollo che deriva da REST ed il suo scopo è quello di interfacciare applicazioni di terze parti con gli applicativi Google.</p>
<p>Insieme ad alcuni amici dell&#8217;Università, stiamo sviluppando un progetto in cui è necessario interfacciarsi con <a title="YouTube" href="http://www.youtube.com/" target="_blank">YouTube</a>. Le <a title="YouTube API" href="http://code.google.com/intl/it-IT/apis/youtube/getting_started.html" target="_blank">API di Google</a> ci sono venute in soccorso per ogni problema o dubbio incontrato.</p>
<p>Le API di Google sono sviluppate per diversi linguaggi di programmazione (HTML, XML, Java, Php, .Net e Pynthon) e supportano tutti gli applicativi di Google; potete trovare un elenco completo nell&#8217;apposita <a title="Google API Directory" href="http://code.google.com/intl/it-IT/apis/gdata/docs/directory.html" target="_blank">API Directory</a> di Google.<br />
<div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div><br />
Tutte le API sono ben fatte, funzionanti e soprattutto ben documentate. Per chi come me usa Eclipse, sviluppare un progetto usando le <a title="YouTube API" href="http://code.google.com/intl/it-IT/apis/youtube/getting_started.html" target="_blank">API di Google</a> sarà ancora più facile. Ci sono infatti guide su come configurare un progetto di Eclipse, su come integrare le API ed esempi di codice già pronti.</p>
<p>Attualmente io sto usando solo le API di YouTube, e gli esempi di codice messi a disposizione mi sono stati più che sufficienti. Davvero un ottimo lavoro da parte di Google.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nerthase.com/blog/google-data-api/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Redirect &amp; SEO</title>
		<link>http://www.nerthase.com/blog/redirect-seo?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=redirect-seo</link>
		<comments>http://www.nerthase.com/blog/redirect-seo#comments</comments>
		<pubDate>Wed, 20 Jan 2010 22:53:59 +0000</pubDate>
		<dc:creator>nerthase</dc:creator>
				<category><![CDATA[Guide]]></category>
		<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[redirect]]></category>
		<category><![CDATA[seo]]></category>

		<guid isPermaLink="false">http://www.nerthase.com/blog/?p=68</guid>
		<description><![CDATA[Rieccomi nuovamente alle prese con un articolo per voi&#8230; Questa volta parliamo di come i motori di ricerca gestiscono i reindirizzamenti delle pagine Web. In particolare vi parlo del redirect 301 che indica che una risorsa è stata spostata permanentemente. Il redirect 301 è usato nel caso in cui si voglia far puntare un vecchio&#8230;]]></description>
			<content:encoded><![CDATA[<p>Rieccomi nuovamente alle prese con un articolo per voi&#8230; Questa volta parliamo di come i motori di ricerca gestiscono i reindirizzamenti delle pagine Web. In particolare vi parlo del <strong>redirect 301</strong> che indica che una risorsa è stata spostata permanentemente. Il <strong>redirect 301</strong> è usato nel caso in cui si voglia far puntare un vecchio indirizzo ad un nuovo indirizzo, senza invalidare i link che facevo riferimento al vecchio indirizzo. Nel caso del mio blog, ho usato il redirect 301 per far puntare l&#8217;indirizzo <em>http://www.nerthase.com </em>all&#8217;indirizzo <em>http://www.nerthase.com/blog</em>.</p>
<p>Veniamo al dunque, ed illustriamo i vari metodi per implementare un <strong>redirect 301</strong>:<br />
<div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div></p>
<h4><strong>Redirect PHP</strong></h4>
<p style="padding-left: 30px;">Se si vuole far redigere l&#8217;indirizzo <em>www.miosito.it </em>all&#8217;indirizzo <em>www.mionuovosito.it</em> basterà posizionare all&#8217;interno della directory principale del primo sito un file <strong>index.php</strong> con all&#8217;interno queste semplici righe di codice:</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;?php<br />
Header( &quot;HTTP/1.1 301 Moved Permanently&quot; );<br />
Header( &quot;Location: http://www.mionuovosito.it&quot; );<br />
?&gt;</div></div>
<p style="padding-left: 30px;">In questo modo tutti i link che prima erano diretti a <em>www.miosito.it</em> verranno rediretti verso <em>www.mionuovosito.it</em>.</p>
<h4>Redirect ASP</h4>
<p>Il ragionamento è identico al caso precedente, ma si riferisce a pagine ASP:</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;%<br />
Response.Status = &quot;301 Moved Permanently&quot;<br />
Response.AddHeader &quot;Location&quot;, &quot;http://www.mionuovosito.it&quot;<br />
%&gt;</div></div>
<h4>Redirect HTML</h4>
<p style="padding-left: 30px;">Nel caso in cui si abbia a che fare con pagine HTML, o il server su cui ci si appoggia è talmente ridicolo che non supporta altri linguaggi (scherzo&#8230; <img src='http://www.nerthase.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ), si può ricorrere al redirect HTML, inserendo all&#8217;interno della sezione <strong>head</strong> il metatag che segue:</p>
<p style="padding-left: 30px; text-align: center;"><code class="codecolorer text mac-classic"><span class="text">&lt;META HTTP-EQUIV = Refresh CONTENT=&quot;0; URL = http://www.mionuovosito.it&quot;&gt;</span></code></p>
<p style="padding-left: 30px;">In questo caso si può impostare il tempo dopo il quale effettuare il redirect alla nuova pagina, cambiando il valore che qui appare come 0.</p>
<div class="clear-block"><div class="ad alignleft"><script type="text/javascript"><!--
google_ad_client = "ca-pub-3082594121433544";
/* 728x90, creato 30/07/09 */
google_ad_slot = "9645358027";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></div>
<h4>Redirect .htaccess</h4>
<p style="padding-left: 30px;">Se il nostro sito è ospitato da un server Apache, ed abbiamo la possibilità di editare il file <strong>.htaccess</strong>, è possibile modificare questo file per effettuare il redirect. Basterà aggiungere nel file <strong>.htaccess</strong> situato in <em>www.miosito.it </em>la riga:</p>
<p style="padding-left: 30px; text-align: center;"><code class="codecolorer text mac-classic"><span class="text">Redirect 301 / http://www.mionuovosito.com/</span></code></p>
<p style="padding-left: 30px;">In questo caso l&#8217;intero sito sarà redirezionato verso il nuovo sito. Nel caso si voglia redirezionare solo una pagina, o una sezione del sito, è possibile inserire:</p>
<p style="padding-left: 30px; text-align: center;"><code class="codecolorer text mac-classic"><span class="text">Redirect 301 /vecchiasezione/ http://www.mionuovosito.com/nuovasezione/</span></code></p>
<p>Il redirect preferito dai motori di ricerca è quello che utilizza il file <strong>.htaccess</strong>, che però impone alcune limitazioni sull&#8217;utilizzo del vecchio sito. Se si posiziona il file .htaccess nella radice del vecchio sito, non sarà più possibile accedervi tramite browser, nemmeno per tenerlo come <em>disco virtuale</em>. Per questo io ho utilizzato un <strong>redirect PHP</strong> dal sito <em>www.nerthase.net </em>verso il sito <em>www.nerthase.com</em>. In questo modo posso avere il sito <em>www.nerthase.net </em>come disco virtuale.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nerthase.com/blog/redirect-seo/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

