<?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>Amicalement Web - Astuces et Bons plans dans le développement web &#187; apache</title>
	<atom:link href="http://www.amicalement-web.net/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.amicalement-web.net</link>
	<description>Astuces et bons plans d&#039;un web developpeur</description>
	<lastBuildDate>Thu, 03 Jun 2010 10:06:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Benchmark Apache: doctrine hydrate object vs array</title>
		<link>http://www.amicalement-web.net/benchmark-apache-doctrine-hydrate-object-vs-array/2009/10/08/</link>
		<comments>http://www.amicalement-web.net/benchmark-apache-doctrine-hydrate-object-vs-array/2009/10/08/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 09:00:59 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[hydrate]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=806</guid>
		<description><![CDATA[ Il faut se rendre à l&#8217;évidence, défendre une idée sans argument, c&#8217;est bien souvent très compliqué. Et quand il a fallu que j&#8217;arrive à quantifier le réel apport d&#8217;une hydratation en tableau plutôt qu&#8217;en object, c&#8217;était un peu difficile. Rappelez-vous, j&#8217;en avais parlé dans les secrets de doctrine.
Je me suis donc dis que j&#8217;allais [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/clock.png" alt="clock" title="clock" width="614" height="100" class="alignnone size-full wp-image-829" /> Il faut se rendre à l&#8217;évidence, défendre une idée sans argument, c&#8217;est bien souvent très compliqué. Et quand il a fallu que j&#8217;arrive à quantifier le réel apport d&#8217;une hydratation en tableau plutôt qu&#8217;en object, c&#8217;était un peu difficile. Rappelez-vous, j&#8217;en avais parlé <a  href="/doctrine-quelques-petits-secrets/2009/08/26/">dans les secrets de doctrine</a>.</p>
<p>Je me suis donc dis que j&#8217;allais tout simplement le benchmarker grosso modo, afin d&#8217;avoir une idée du gain qu&#8217;on peut avoir à passer par des tableaux. Car en fait, on sait très bien que le 2e est plus rapide, mais la question, c&#8217;est de combien.<br />
<span id="more-806"></span><br />
J&#8217;ai donc pris un modèle assez classique. Un couple de Departement/Region de France.</p>
<p>Dans mon fichier Table de mes départements, je rajoute la méthode suivante pour faire ma jointure sur régions</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">-&gt;</span><span style="color: #004000;">leftJoin</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d.Regions r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Hydratation en objet</h3>
<p>Parfait, maintenant, je crée une action qui récupère seulement tous les items de ma table, soit environ <strong>100 départements</strong>, en relation avec une région chacun.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Departements'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ca reste un cas tout a fait commun. J&#8217;ai donc lancé un <code>ab</code>, un programme livré avec apache qui permet de lancer l&#8217;exécution d&#8217;une page web, n fois avec n requêtes concurrentielles.  j&#8217;ai décidé de le lancer <strong>100 fois avec 10 requêtes à la fois</strong>. Et voici le résultat:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Document Path:          <span style="color: #000000; font-weight: bold;">/</span>hydrate<span style="color: #000000; font-weight: bold;">/</span>index
Document Length:        <span style="color: #000000;">2400</span> bytes
&nbsp;
Concurrency Level:      <span style="color: #000000;">10</span>
Time taken <span style="color: #000000; font-weight: bold;">for</span> tests:  <span style="color: #000000;">16.862</span> seconds
Complete requests:      <span style="color: #000000;">100</span>
Failed requests:        <span style="color: #000000;">0</span>
Write errors:           <span style="color: #000000;">0</span>
Total transferred:      <span style="color: #000000;">269400</span> bytes
HTML transferred:       <span style="color: #000000;">240000</span> bytes
Requests per second:    <span style="color: #000000;">5.93</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #666666; font-style: italic;">#/sec] (mean)</span>
Time per request:       <span style="color: #000000;">1686.205</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ms<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>mean<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Time per request:       <span style="color: #000000;">168.621</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ms<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>mean, across all concurrent requests<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Transfer rate:          <span style="color: #000000;">15.60</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>Kbytes<span style="color: #000000; font-weight: bold;">/</span>sec<span style="color: #7a0874; font-weight: bold;">&#93;</span> received
&nbsp;
Connection Times <span style="color: #7a0874; font-weight: bold;">&#40;</span>ms<span style="color: #7a0874; font-weight: bold;">&#41;</span>
              min  mean<span style="color: #7a0874; font-weight: bold;">&#91;</span>+<span style="color: #000000; font-weight: bold;">/</span>-sd<span style="color: #7a0874; font-weight: bold;">&#93;</span> median   max
Connect:        <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>   <span style="color: #000000;">0.5</span>      <span style="color: #000000;">0</span>       <span style="color: #000000;">4</span>
Processing:  <span style="color: #000000;">1172</span> <span style="color: #000000;">1665</span> <span style="color: #000000;">167.3</span>   <span style="color: #000000;">1662</span>    <span style="color: #000000;">2337</span>
Waiting:     <span style="color: #000000;">1172</span> <span style="color: #000000;">1663</span> <span style="color: #000000;">166.5</span>   <span style="color: #000000;">1661</span>    <span style="color: #000000;">2337</span>
Total:       <span style="color: #000000;">1172</span> <span style="color: #000000;">1665</span> <span style="color: #000000;">167.4</span>   <span style="color: #000000;">1662</span>    <span style="color: #000000;">2338</span>
&nbsp;
Percentage of the requests served within a certain <span style="color: #000000; font-weight: bold;">time</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>ms<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000;">50</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1662</span>
  <span style="color: #000000;">66</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1709</span>
  <span style="color: #000000;">75</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1750</span>
  <span style="color: #000000;">80</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1783</span>
  <span style="color: #000000;">90</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1860</span>
  <span style="color: #000000;">95</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1928</span>
  <span style="color: #000000;">98</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">2102</span>
  <span style="color: #000000;">99</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">2338</span>
 <span style="color: #000000;">100</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">2338</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>longest request<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<h3>Hydratation en tableau</h3>
<p>2e action, le même code, le même résultat HTML souhaité, mais on passe dans le execute le fameux paramètre. Notre résultat ne sera donc plus une Doctrine_Collection, mais un tableau associatif avec en clé, les noms des champs.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex2<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Departements'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">HYDRATE_ARRAY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Et voici le résultat</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Document Path:          <span style="color: #000000; font-weight: bold;">/</span>hydrate<span style="color: #000000; font-weight: bold;">/</span>index2
Document Length:        <span style="color: #000000;">2400</span> bytes
&nbsp;
Concurrency Level:      <span style="color: #000000;">10</span>
Time taken <span style="color: #000000; font-weight: bold;">for</span> tests:   <span style="color: #000000;">10.086</span> seconds
Complete requests:      <span style="color: #000000;">100</span>
Failed requests:        <span style="color: #000000;">0</span>
Write errors:           <span style="color: #000000;">0</span>
Total transferred:      <span style="color: #000000;">269400</span> bytes
HTML transferred:       <span style="color: #000000;">240000</span> bytes
Requests per second:    <span style="color: #000000;">9.92</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #666666; font-style: italic;">#/sec] (mean)</span>
Time per request:       <span style="color: #000000;">1008.569</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ms<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>mean<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Time per request:       <span style="color: #000000;">100.857</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ms<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>mean, across all concurrent requests<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Transfer rate:          <span style="color: #000000;">26.09</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>Kbytes<span style="color: #000000; font-weight: bold;">/</span>sec<span style="color: #7a0874; font-weight: bold;">&#93;</span> received
&nbsp;
Connection Times <span style="color: #7a0874; font-weight: bold;">&#40;</span>ms<span style="color: #7a0874; font-weight: bold;">&#41;</span>
              min  mean<span style="color: #7a0874; font-weight: bold;">&#91;</span>+<span style="color: #000000; font-weight: bold;">/</span>-sd<span style="color: #7a0874; font-weight: bold;">&#93;</span> median   max
Connect:        <span style="color: #000000;">0</span>    <span style="color: #000000;">1</span>   <span style="color: #000000;">1.4</span>      <span style="color: #000000;">0</span>       <span style="color: #000000;">6</span>
Processing:   <span style="color: #000000;">598</span>  <span style="color: #000000;">992</span> <span style="color: #000000;">112.9</span>   <span style="color: #000000;">1003</span>    <span style="color: #000000;">1239</span>
Waiting:      <span style="color: #000000;">598</span>  <span style="color: #000000;">991</span> <span style="color: #000000;">113.0</span>   <span style="color: #000000;">1002</span>    <span style="color: #000000;">1239</span>
Total:        <span style="color: #000000;">599</span>  <span style="color: #000000;">993</span> <span style="color: #000000;">113.0</span>   <span style="color: #000000;">1003</span>    <span style="color: #000000;">1239</span>
&nbsp;
Percentage of the requests served within a certain <span style="color: #000000; font-weight: bold;">time</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>ms<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000;">50</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1003</span>
  <span style="color: #000000;">66</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1051</span>
  <span style="color: #000000;">75</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1081</span>
  <span style="color: #000000;">80</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1085</span>
  <span style="color: #000000;">90</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1104</span>
  <span style="color: #000000;">95</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1128</span>
  <span style="color: #000000;">98</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1149</span>
  <span style="color: #000000;">99</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1239</span>
 <span style="color: #000000;">100</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1239</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>longest request<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<h3>Bilan</h3>
<p>Alors évidemment, il faut prendre en compte la différence de temps de réponse qu&#8217;on peut avoir entre 2 lancement de <code>ab</code>. Mais l&#8217;objectif est surtout de montrer que le gain est non négligeable et vraiment réel.</p>
<p>On constate que le HTML reçu est bien le même. On a donc le même rendu!<br />
Et côté performance, le 1er cas nous donne une requête en <strong>1,7sec</strong> quand le 2e nous donne <strong>1s</strong> soit près de<strong> 60% de mieux</strong>.</p>
<p>Conclusion, quand on souhaite réaliser seulement de l&#8217;affichage de données, il faut hydrater en tableau! Même si dans le cas présent, on parle de 0,7sec, il faut voir que c&#8217;est un projet vierge avec rien d&#8217;autre autour. Le gain est donc bien réel et pas seulement gadget.</p>
<p><small>Crédit photo: http://www.flickr.com/photos/don3rdse/3208161023/</small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/benchmark-apache-doctrine-hydrate-object-vs-array/2009/10/08/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Développement Web: les bons plans de la semaine #15</title>
		<link>http://www.amicalement-web.net/developpement-web-les-bons-plans-de-la-semaine-15/2009/09/17/</link>
		<comments>http://www.amicalement-web.net/developpement-web-les-bons-plans-de-la-semaine-15/2009/09/17/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 12:00:27 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Bons plans]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=727</guid>
		<description><![CDATA[ Une grosse semaine de boulot plus tard et quelques jours de vacances à jouer à rock band beatles me reposer un peu, me voilà de retour pour la liste de liens qui sera finalement une liste pour les 10 derniers jours et qui décalera la prochaine à lundi prochain (pas celui qui arrive quoi)!
Pas [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/echec.png" alt="echec" title="echec" width="614" height="100" class="alignnone size-full wp-image-732" /> Une grosse semaine de boulot plus tard et quelques jours de vacances à <strike>jouer à rock band beatles</strike> me reposer un peu, me voilà de retour pour la liste de liens qui sera finalement une liste pour les 10 derniers jours et qui décalera la prochaine à lundi prochain (pas celui qui arrive quoi)!<br />
Pas d&#8217;inquiétude, j&#8217;ai d&#8217;autres surprises pour vous ce lundi et la semaine prochaine.<br />
<span id="more-727"></span><br />
Mais on verra ça plus tard, pour l&#8217;instant voici de quoi cliquer:</p>
<ul>
<li><a  href="http://css-tricks.com/number-increment-buttons/">Ajouter un +/- à un input en jquery</a><br />
C&#8217;est une fonctionnalité que j&#8217;ai longtemps cherché, en vain. Voilà qui est maintenant réparé
</li>
<li><a  href="http://blogtoolbox.fr/comment-integrer-google-custom-search-engine-a-son-blog/">Integrer google search à son blog</a><br />
Une des fonctionnalités sur laquelle je bosse pour aw. Voici un bon début avec ce tutorial</li>
<li><a  href="http://net.tutsplus.com/tutorials/javascript-ajax/10-super-helpful-traversing-functions-in-jquery/">Tutorial de manipulation du DOM en jquery</a><br />
Un tutorial un peu avancé qui permet de pointer du doigt la puissance de manipulation du DOM avec jQuery</li>
<li><a  href="http://chrisiufer.com/loupe/sample.html">Plugin jQuery pour zoom d&#8217;image</a><br />
Encore un type de plugin assez répandu, mais qui est bien fichu à mon goût</li>
<li><a  href="http://www.filamentgroup.com/lab/styling_buttons_and_toolbars_with_the_jquery_ui_css_framework/">Habillez vos design avec jquery UI</a><br />
Voici un tutorial pour apprendre comment utiliser jqueryui pour habillez uniformément et sans effort un design web</li>
<li><a  href="http://vanadiumjs.com/">Un nouveau validateur de formulaire</a><br />
Son seul défaut, c&#8217;est qu&#8217;il utilise des class css. Je suis pas très fan du principe. Mais il reste bien pensé et très complet</li>
<li><a  href="http://www.jankoatwarpspeed.com/post/2009/09/16/Animate-validation-feedback-using-jQuery.aspx">Une utilisation des animations jquery</a><br />
Parce qu&#8217;avoir de bons outils, ça ne suffit pas, il faut aussi avoir de bonnes idées. En voici une qui utilise les animations jquery pour transmettre un message de retour à l&#8217;utilisateur.
</li>
<li><a  href="http://net.tutsplus.com/tutorials/php/getting-clean-with-php/">Apprendre à utiliser les filtres en PHP</a><br />
Arrivée avec PHP 5.2, les filtres permettent de &#8230; filter vos données. Très puissants mais encore un peu obscurs, voici un tutorial qui vous donnera les bases de ces fonctions.</li>
<li><a  href="http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/">Mod_rewrite avec Apache</a><br />
Oui, on en a bcp parlé il y a quelques années avec l&#8217;arrivée de la réécriture d&#8217;urls. Mais voici un tutorial bien fourni sur toutes les possibilités et toutes les options de ce mod beaucoup plus complet qu&#8217;on peut l&#8217;imaginer</li>
<li><a  href="http://woork.blogspot.com/2009/09/rediscovering-html-tables.html">Rédécouvrez les tableaux HTML</a><br />
Element HTML diabolique pour certains depuis l&#8217;ascension du CSS, il ne faut pas oublier que les tableaux ont une utilité et sont surtout très fournis en options et personnalisations. Voici de quoi vous rafraichir la mémoire.</li>
<li><a  href="http://www.catswhocode.com/blog/10-code-snippets-to-interact-with-twitter">10 snippets pour intéragir avec twitter</a><br />
Un petit listing de 10 possibilités d&#8217;interagir avec twitter via son API. Preuve que maintenant un service web à succès se doit d&#8217;avoir une API à la hauteur de l&#8217;imagination de ses utilisateurs.
</li>
</ul>
<p>C&#8217;est tout pour ces 10 jours, la faute à ma non présence sur twitter ces derniers jours. Mais je vais corriger ça ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/developpement-web-les-bons-plans-de-la-semaine-15/2009/09/17/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Veille sécuritaire avec flux RSS (part 1)</title>
		<link>http://www.amicalement-web.net/veille-securitaire-avec-flux-rss-part-1/2009/08/28/</link>
		<comments>http://www.amicalement-web.net/veille-securitaire-avec-flux-rss-part-1/2009/08/28/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 07:57:00 +0000</pubDate>
		<dc:creator>Benoit</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[sécurité]]></category>
		<category><![CDATA[veille]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=598</guid>
		<description><![CDATA[
Il existe sur Internet, de nombreux sites de veille sécuritaire permettant de vous maintenir au courant des dernières vulnérabilités découvertes. Si vous utilisez une application open-source (forum, blog, cms &#8230;), des visites régulières sur ces sites ou abonnement aux flux RSS, vous permettent de maintenir à jour votre application et minimiser les risques de piratages.
Mais, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/veille.secu2.png" alt="Veille sécuritaire" width="614" height="100" class="alignnone size-full wp-image-640" /></p>
<p>Il existe sur Internet, de nombreux sites de veille sécuritaire permettant de vous maintenir au courant des dernières vulnérabilités découvertes. Si vous utilisez une application open-source (forum, blog, cms &#8230;), des visites régulières sur ces sites ou abonnement aux flux RSS, vous permettent de maintenir à jour votre application et minimiser les risques de piratages.</p>
<p>Mais, que vous utilisiez une application web open-source (ou non) à jour, cela n&#8217;empêche pas les tentatives d&#8217;attaques, c&#8217;est pourquoi je vous propose ici un petit script python pour effectuer vous même votre veille sécuritaire.</p>
<p>Cet article est la 1ère partie d&#8217;une série de 3 :</p>
<ul>
<li><b>Part 1</b>: Configuration des logs apache, création de la BDD puis présentation et configuration du script de traitement.</li>
<li><b>Part 2</b>: Script de génération du flux RSS avec présentation détaillées des informations dans le flux.</li>
<li><b>Part 3</b>: Amélioration du script avec des expressions régulières plus poussés, rajout d&#8217;options &#8230;</li>
</ul>
<p><span id="more-598"></span>Il est intéressant de noter que la Partie 1 est la plus importante, les 2 autres parties pouvant être réalisées sans attendre la suite de l&#8217;article si vous savez programmer.</p>
<h3>Logs apache, BDD &amp; script de traitement</h3>
<p>
<h4>Configuration des logs apache</h4>
<p>Ce script de veille se base sur les fichiers de logs apache, il faut donc vous assurer dans un premier temps que les directives de logs sont bien renseignées au niveau de votre configuration.<br />
L&#8217;article ne portant pas sur la configuration d&#8217;apache, je ne vais pas m&#8217;étendre dessus, voici donc les lignes nécessaires à la génération des logs.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">LogFormat</span> %V %{canonical}p %h %{X-Forwarded-For}i %{PHPSESSID}C %t \<span style="color: #7f007f;">&quot;%r<span style="color: #000099; font-weight: bold;">\&quot;</span> %gt;s %b <span style="color: #000099; font-weight: bold;">\&quot;</span>%{Referer}i<span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #000099; font-weight: bold;">\&quot;</span>%{User-Agent}i<span style="color: #000099; font-weight: bold;">\&quot;</span>
ErrorLog /var/log/apache/my_error_log</span></pre></div></div>

<p>Ceci est un exemple de format de log, vous pouvez le modifier sans problème :</p>
<ul>
<li><b>%V</b>: Nom du serveur</li>
<li><b>%{canonical}p</b>: port</li>
<li><b>%h</b>: IP du visiteur</li>
<li><b>%{X-Forwareded-for}i</b>: IP du proxy</li>
<li><b>%{PHPSESSID}C</b>: ID de session (pratique pour retracer le chemin d&#8217;un pirate potentiel)</li>
<li><b>%t</b>: Date</li>
<li><b>%r</b>: Requête</li>
<li><b>%&gt;s</b>: Status de la requête</li>
<li><b>%b</b>: Taille de la requête</li>
<li><b>%{Referer}i</b>: Referer (lien précédent)</li>
<li><b>%{User-Agent}i</b>: Informations sur le navigateur du visiteur</li>
</ul>
<p>Pour plus d&#8217;infos, je vous invite à consulter <a  href="http://httpd.apache.org/docs/2.0/logs.html">la documentation officielle concernant les directives logs d&#8217;apache</a>.<br />
Une fois votre fichier modifié, n&#8217;oubliez pas de le recharger. Un simple reload doit normalement suffire.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 reload</pre></div></div>

<h4>Création de la BDD MySQL</h4>
<p>Les informations traitées par le script seront enregistrées en BDD pour pouvoir garder une trace, voici le code SQL pour la création des tables concernées.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">--</span>
<span style="color: #808080; font-style: italic;">-- Création de la table des attaques potentielles recensées</span>
<span style="color: #808080; font-style: italic;">--</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`veille_vuln`</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #ff0000;">`id_vuln`</span> bigint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	<span style="color: #ff0000;">`ip`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #ff0000;">`url`</span> text<span style="color: #66cc66;">,</span>
	<span style="color: #ff0000;">`log`</span> text<span style="color: #66cc66;">,</span>
	<span style="color: #ff0000;">`temps`</span> datetime<span style="color: #66cc66;">,</span>
	<span style="color: #ff0000;">`id_type`</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id_vuln`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`ip`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`url`</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`log`</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`temps`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`id_type`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET utf8;
&nbsp;
<span style="color: #808080; font-style: italic;">--</span>
<span style="color: #808080; font-style: italic;">-- Création de la table des types d'attaques surveillées</span>
<span style="color: #808080; font-style: italic;">--</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`veille_vuln_type`</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #ff0000;">`id_type`</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span><span style="color: #66cc66;">,</span>
	<span style="color: #ff0000;">`nom`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">45</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #ff0000;">`link`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id_type`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET utf8;
&nbsp;
<span style="color: #808080; font-style: italic;">--</span>
<span style="color: #808080; font-style: italic;">-- Insertion des types de vulnérabilités surveillées</span>
<span style="color: #808080; font-style: italic;">--</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`veille_vuln_type`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id_type`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`nom`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`link`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> 
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'XSS'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'http://fr.wikipedia.org/wiki/Cross_site_scripting'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'2'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Unicode'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'3'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Include'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'4'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'SQL Injection'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'http://fr.wikipedia.org/wiki/Injection_SQL'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'5'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Mot sensible'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<h4>Script veille-secu</h4>
<p>Une fois les logs apache configurés et la <acronym title="Base De Données">BDD</acronym> créée, nous allons  nous attaquer au script de traitement.<br />
En début de script, appel des différentes librairies nécessaires.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Importation des librairies necessaires</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>, <span style="color: #dc143c;">cgi</span>, <span style="color: #dc143c;">time</span>, MySQLdb, <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">getopt</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">getopt</span>,GetoptError</pre></div></div>

<p>Ensuite, la partie à configurer pour :</p>
<ul>
<li>Effectuer la connexion à la <acronym title="Base De Données">BDD</acronym></li>
<li>Indiquer le fichier <strong>pointer</strong></li>
<li>Gérer vos expressions régulières.</li>
</ul>
<p>Le fichier <strong>pointer</strong> va vous permettre de conserver la dernière ligne de logs traitée par le script afin de ne pas repasser sur tout le fichier et utiliser des ressources mémoires inutilement.<br />
Juste après ces infos, deux bloc d&#8217;expressions régulières qui permettront de traiter la ligne de log pour la découper puis de détecter une éventuelle tentative d&#8217;attaque.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#---------------------------------#</span>
<span style="color: #808080; font-style: italic;">#	PARTIE A CONFIGURER</span>
<span style="color: #808080; font-style: italic;">#---------------------------------#</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Declaration des variables generales (chemin absolu)</span>
file_pointer = <span style="color: #483d8b;">'/path/to/your/pointer'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Declaration des variables de connexion a la BDD MySQL</span>
bdd_host	= <span style="color: #483d8b;">'127.0.0.1'</span>
bdd_user	= <span style="color: #483d8b;">'mon_user'</span>
bdd_pass	= <span style="color: #483d8b;">'mon_pass'</span>
bdd_db		= <span style="color: #483d8b;">'ma_bdd'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Declaration des regexp de traitement des logs</span>
pattern_ip 	= <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'[0-9]{1,3}<span style="color: #000099; font-weight: bold;">\.</span>[0-9]{1,3}<span style="color: #000099; font-weight: bold;">\.</span>[0-9]{1,3}<span style="color: #000099; font-weight: bold;">\.</span>[0-9]{1,3}'</span><span style="color: black;">&#41;</span>
pattern_date 	= <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'[0-9]{2}<span style="color: #000099; font-weight: bold;">\/</span>...<span style="color: #000099; font-weight: bold;">\/</span>[0-9]{4}:[0-9]{2}:[0-9]{2}:[0-9]{2}'</span><span style="color: black;">&#41;</span>
pattern_url 	= <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'(GET|POST|PUT|DELETE|HEAD|PROPFIND|LOCK|OPTIONS)<span style="color: #000099; font-weight: bold;">\ </span>[^ ]*<span style="color: #000099; font-weight: bold;">\ </span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Declaration des regexp de detection d'attaques potentielles</span>
p_faille_xss 	=  <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'((<span style="color: #000099; font-weight: bold;">\%</span>3C)|&lt;)[^<span style="color: #000099; font-weight: bold;">\n</span>]+((<span style="color: #000099; font-weight: bold;">\%</span>3E)|&gt;)'</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span><span style="color: black;">&#41;</span>
p_faille_unicode = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\.</span>exe|<span style="color: #000099; font-weight: bold;">\/</span>syntaxe<span style="color: #000099; font-weight: bold;">\/</span>winnt<span style="color: #000099; font-weight: bold;">\/</span>system32<span style="color: #000099; font-weight: bold;">\/</span>'</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span><span style="color: black;">&#41;</span>
p_faille_inc 	= <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'(http|ftp):<span style="color: #000099; font-weight: bold;">\/</span><span style="color: #000099; font-weight: bold;">\/</span>|<span style="color: #000099; font-weight: bold;">\/</span>etc<span style="color: #000099; font-weight: bold;">\/</span>(passwd|shadow)|<span style="color: #000099; font-weight: bold;">\/</span>(sbin|bin)<span style="color: #000099; font-weight: bold;">\/</span>|<span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\/</span>'</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span><span style="color: black;">&#41;</span>
p_faille_sql 	= <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'(<span style="color: #000099; font-weight: bold;">\ </span>|%20)(union|or)(<span style="color: #000099; font-weight: bold;">\ </span>|%20)'</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span><span style="color: black;">&#41;</span>
p_faille_word 	= <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'(root|htaccess|passwd|<span style="color: #000099; font-weight: bold;">\.</span>log|<span style="color: #000099; font-weight: bold;">\.</span>conf)'</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span><span style="color: black;">&#41;</span></pre></div></div>

<p>La suite du fichier concerne le traitement des options et des arguments qu&#8217;il est possible de passer au script, ici une seule option <em>&laquo;&nbsp;&#8211;help&nbsp;&raquo;</em> et un seul argument <em>&laquo;&nbsp;le fichier&nbsp;&raquo;</em>. Nous verrons dans la partie 3 des cas beaucoup plus poussés pour faire de ce script un mini <acronym title="Intrusion Detection System">IDS</acronym> et limiter au maximum les faux-positifs.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#-----------------------------------------------------#</span>
<span style="color: #808080; font-style: italic;">#	NE PAS MODIFIER CETTE PARTIE</span>
<span style="color: #808080; font-style: italic;">#-----------------------------------------------------#</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Declaration des options et arguments</span>
optionmap = <span style="color: black;">&#91;</span>
	<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;-h&quot;</span>, <span style="color: #483d8b;">&quot;--help&quot;</span>, <span style="color: #483d8b;">&quot;Affiche l'aide&quot;</span><span style="color: black;">&#93;</span>,
	<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;-f&quot;</span>, <span style="color: #483d8b;">&quot;--logfile&quot;</span>, <span style="color: #483d8b;">&quot;Specifie le fichier de log a traiter (chemin absolu)&quot;</span><span style="color: black;">&#93;</span>,
<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Traitement des options et arguments</span>
args = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
params = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">try</span>:
	args, params = <span style="color: #dc143c;">getopt</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>, <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>o<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> o <span style="color: #ff7700;font-weight:bold;">in</span> optionmap<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, \
		<span style="color: black;">&#91;</span>x<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x,y: x+y, <span style="color: black;">&#91;</span>z<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> z <span style="color: #ff7700;font-weight:bold;">in</span> optionmap<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
	args = <span style="color: black;">&#91;</span>a <span style="color: #ff7700;font-weight:bold;">for</span> a,b <span style="color: #ff7700;font-weight:bold;">in</span> args<span style="color: black;">&#93;</span>
&nbsp;
	log = <span style="color: #ff4500;">0</span>
	mode = <span style="color: #008000;">None</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> option <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;--help&quot;</span>, <span style="color: #483d8b;">&quot;-h&quot;</span><span style="color: black;">&#93;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> option <span style="color: #ff7700;font-weight:bold;">in</span> args:
			args.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>option<span style="color: black;">&#41;</span>
			mode = <span style="color: #483d8b;">&quot;help&quot;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> option <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;--logfile&quot;</span>, <span style="color: #483d8b;">&quot;-f&quot;</span><span style="color: black;">&#93;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> option <span style="color: #ff7700;font-weight:bold;">in</span> args:
			args.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>option<span style="color: black;">&#41;</span>
			log = params.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Traitement des errreurs</span>
<span style="color: #ff7700;font-weight:bold;">except</span> GetoptError, e:
	<span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;option invalide: &quot;</span><span style="color: black;">&#41;</span>
	<span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>e<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
	mode = <span style="color: #483d8b;">&quot;help&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Affichage de l'aide si le mode &quot;help&quot; est defini ou si le fichier de log est equivalent a 0</span>
<span style="color: #ff7700;font-weight:bold;">if</span> mode == <span style="color: #483d8b;">&quot;help&quot;</span> <span style="color: #ff7700;font-weight:bold;">or</span> log == <span style="color: #ff4500;">0</span>:
	<span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Syntax: veille-secu <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> m <span style="color: #ff7700;font-weight:bold;">in</span> optionmap:
		<span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>m<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> + m<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot;   <span style="color: #000099; font-weight: bold;">\t</span>: &quot;</span> + m<span style="color: black;">&#91;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> o <span style="color: #ff7700;font-weight:bold;">in</span> m<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>:
			<span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> + o + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
	<span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Connexion à la <acronym title="Base De Données">BDD</acronym> avec les informations renseignées en début de fichier.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Connexion a la BDD</span>
cnx = MySQLdb.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>
	host	= bdd_host,
	<span style="color: #dc143c;">user</span>	= bdd_user,
	passwd	= bdd_pass,
	db		= bdd_db
<span style="color: black;">&#41;</span>
c = cnx.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Comme expliqué précédemment, nous allons ici vérifier l&#8217;existence et la valeur du <strong>pointer</strong> pour ne pas reprendre le fichier complet puis ouvrir le fichier de log au niveau du <strong>pointer</strong>.<br />
Nous verrons en fin d&#8217;article la possibilité de remettre ce pointer à 0 si vous avez un logrotate activé sur votre serveur.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Vérification du pointer pour ne pas reprendre le fichier a 0</span>
pointer = <span style="color: #ff4500;">0</span>
verif = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>file_pointer,<span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> num <span style="color: #ff7700;font-weight:bold;">in</span> verif.<span style="color: black;">xreadlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	pointer = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span>
verif.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Ouverture du fichier de log et vérification du pointer</span>
logapache = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>log,<span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> pointer <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">0</span>:
	logapache.<span style="color: black;">seek</span><span style="color: black;">&#40;</span>pointer, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Traitement du fichier ligne par ligne. A chaque ligne de log, le script va analyser l&#8217;url et déterminer, en fonction des regexp définies plus haut, si la requête est une tentative d&#8217;attaque ou non.<br />
Dans cet exemple du script, les regexp de détection sont assez basiques, dans la partie 3 nous élaborerons des techniques plus poussées.</p>
<p>Si une attaque potentielle est détectée, alors elle sera enregistrée en <acronym title="Base De Données">BDD</acronym> pour générer par la suite le flux RSS ou tout simplement consulter les différents enregistrements à partir d&#8217;une interface web comme nous le verrons dans la partie 2 de l&#8217;article.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Traitement du fichier ligne par ligne</span>
k=<span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> logapache.<span style="color: black;">xreadlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #808080; font-style: italic;"># Initialisation des Variables</span>
	data_ip 		= <span style="color: #483d8b;">'NULL'</span>
	data_date		= <span style="color: #483d8b;">'NULL'</span>
	data_url		= <span style="color: #483d8b;">'NULL'</span>
	<span style="color: #808080; font-style: italic;"># IP</span>
	field_ip = pattern_ip.<span style="color: black;">search</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> field_ip:
		data_ip = field_ip.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #808080; font-style: italic;"># DATE</span>
	field_date = pattern_date.<span style="color: black;">search</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> field_date:
		tmp_date = <span style="color: #dc143c;">time</span>.<span style="color: black;">strptime</span><span style="color: black;">&#40;</span>field_date.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;%d/%b/%Y:%H:%M:%S&quot;</span><span style="color: black;">&#41;</span>
		data_date = <span style="color: #dc143c;">time</span>.<span style="color: black;">strftime</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%Y-%m-%d %H:%M:%S&quot;</span>, tmp_date<span style="color: black;">&#41;</span>
	<span style="color: #808080; font-style: italic;"># URL</span>
	field_url = pattern_url.<span style="color: black;">search</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> field_url:
		data_method,data_url = <span style="color: black;">&#40;</span>field_url.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: black;">&#91;</span><span style="color: #483d8b;">'0'</span>,<span style="color: #483d8b;">'0'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>:<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
		<span style="color: #808080; font-style: italic;"># ATTAQUES POTENTIELLES</span>
		field_faille_xss 		= p_faille_xss.<span style="color: black;">search</span><span style="color: black;">&#40;</span>data_url<span style="color: black;">&#41;</span>
		field_faille_unicode 	= p_faille_unicode.<span style="color: black;">search</span><span style="color: black;">&#40;</span>data_url<span style="color: black;">&#41;</span>
		field_faille_inc 		= p_faille_inc.<span style="color: black;">search</span><span style="color: black;">&#40;</span>data_url<span style="color: black;">&#41;</span>
		field_faille_sql 		= p_faille_sql.<span style="color: black;">search</span><span style="color: black;">&#40;</span>data_url<span style="color: black;">&#41;</span>
		field_faille_word 		= p_faille_word.<span style="color: black;">search</span><span style="color: black;">&#40;</span>data_url<span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> field_faille_xss <span style="color: #ff7700;font-weight:bold;">or</span> field_faille_unicode <span style="color: #ff7700;font-weight:bold;">or</span> field_faille_inc <span style="color: #ff7700;font-weight:bold;">or</span> field_faille_sql <span style="color: #ff7700;font-weight:bold;">or</span> field_faille_word:
			<span style="color: #ff7700;font-weight:bold;">if</span> field_faille_xss:
				faille_type = <span style="color: #483d8b;">'1'</span>
			<span style="color: #ff7700;font-weight:bold;">elif</span> field_faille_unicode:
				faille_type = <span style="color: #483d8b;">'2'</span>
			<span style="color: #ff7700;font-weight:bold;">elif</span> field_faille_inc:
				faille_type = <span style="color: #483d8b;">'3'</span>
			<span style="color: #ff7700;font-weight:bold;">elif</span> field_faille_sql:
				faille_type = <span style="color: #483d8b;">'4'</span>
			<span style="color: #ff7700;font-weight:bold;">elif</span> field_faille_word:
				faille_type = <span style="color: #483d8b;">'5'</span>
			c.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;INSERT IGNORE veille_vuln (ip, url, log, temps, id_type) VALUES (%s, %s, %s, %s, %s)&quot;</span>, <span style="color: black;">&#40;</span>data_ip, data_url, line, data_date, faille_type<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
			k = k + <span style="color: #ff4500;">1</span></pre></div></div>

<p>Une fois le fichier traité, nous allons successivement :</p>
<ul>
<li>Fermer la connexion MySQL</li>
<li>Récupérer la position du pointer</li>
<li>Fermer le fichier de log</li>
</ul>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Fermeture de la connexion MySQL</span>
cnx.<span style="color: black;">commit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
c.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Recuperation position actuelle du pointer puis fermeture du fichier</span>
eof = logapache.<span style="color: black;">tell</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
logapache.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Sauvegarde du pointer en cours</span>
new_verif = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>file_pointer,<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
new_verif.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>eof<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
new_verif.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Si vous n&#8217;avez qu&#8217;un seul fichier de log que vous videz de temps en temps, il suffit de mettre en tâche cron le script <strong>veille-secu</strong>, mais si vous utilisez un logrotate, il faudra rajouter une étape.</p>
<p>Ci-dessous, on fait tourner le script toutes les 5min.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">*/</span><span style="color: #000000;">5</span>	<span style="color: #000000; font-weight: bold;">*</span>	<span style="color: #000000; font-weight: bold;">*</span>	<span style="color: #000000; font-weight: bold;">*</span>	<span style="color: #000000; font-weight: bold;">*</span>	<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>veille-secu <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>your<span style="color: #000000; font-weight: bold;">/</span>apache_log</pre></div></div>

<p>Dans le cas d&#8217;une utilisation de logrotate, dans votre fichier de conf, on va ajouter au niveau de <strong>postrotate</strong> une commande permettant de remettre le <strong>pointer</strong> à 0 à chaque rotation de log.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">postrotate
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>apache2.pid <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
		<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null
		<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>your<span style="color: #000000; font-weight: bold;">/</span>pointer
	<span style="color: #000000; font-weight: bold;">fi</span>
endscript</pre></div></div>

<p>Voilà, j&#8217;espère que ce premier article vous plaira sinon Tim risque de pas être content et il ne voudra plus venir me voir ^^</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/veille-securitaire-avec-flux-rss-part-1/2009/08/28/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
