<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>Forum du club des développeurs et IT Pro - Blogs - ericd69</title>
		<link>https://www.developpez.net/forums/blogs/426277-ericd69/</link>
		<description>Developpez.com, le Club des Développeurs et IT Pro</description>
		<language>fr</language>
		<lastBuildDate>Fri, 10 Apr 2026 08:33:43 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>15</ttl>
		<image>
			<url>https://forum.developpez.be/images/misc/rss.jpg</url>
			<title>Forum du club des développeurs et IT Pro - Blogs - ericd69</title>
			<link>https://www.developpez.net/forums/blogs/426277-ericd69/</link>
		</image>
		<item>
			<title><![CDATA[Exemple d'automatisation d'un pare-feu via un service pour Linux Debian]]></title>
			<link>https://www.developpez.net/forums/blogs/426277-ericd69/b3098/exemple-d-automatisation-d-pare-feu-via-service-linux-debian/</link>
			<pubDate>Fri, 02 Jun 2017 20:49:20 GMT</pubDate>
			<description><![CDATA[Exemple d'automatisation d'un...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore"><div style="text-align: center;"><font size="7">Exemple d'automatisation d'un pare-feu<br />
via un service<br />
pour Linux Debian</font></div><hr /><br />
<br />
<u><b><font size="5">Résumé</font></b></u><br />
<br />
La mise en place d'un pare-feu est un point clé incontournable de la configuration d'un serveur en ligne qu'il soit virtuel ou réel. Ici je vais donc aborder la mise en place sous forme d'un service pour en faciliter l'utilisation. Je parlerais plus particulièrement du cas de serveurs chez OVH pour la partie monitoring externe.<br />
<br />
<br />
<u><b><font size="5">Prérequis</font></b></u><br />
<br />
Les utilitaires suivants sont nécessaires&nbsp;:<br />
<ul><li style=""><b><i><font color="#0000ff">ifconfig</font></i></b>, utilitaire pour la configuration réseau (matériel et logiciels)&nbsp;;</li><li style=""><b><i><font color="#0000ff">cat</font></i></b>, utilitaire de lecture de fichier texte&nbsp;;</li><li style=""><b><i><font color="#0000ff">grep</font></i></b>, utilitaire de recherche d'expressions dans des fichiers ou pipe&nbsp;;</li><li style=""><b><i><font color="#0000ff">awk</font></i></b>, utilitaire de recherche et modification de texte dans des fichiers ou pipe&nbsp;;</li><li style=""><b><i><font color="#0000ff">iptables</font></i></b>, utilitaire de filtrage réseau&nbsp;;</li><li style=""><b><i><font color="#0000ff">ssh</font></i></b>, utilitaire d'administration et de communication.</li></ul><br />
<br />
<u><b><font size="5">Notions utilisées</font></b></u><br />
<br />
Les notions suivantes seront préférables pour mieux comprendre ce qui est fait&nbsp;:<br />
<ul><li style="">comprendre diverses notions sur les réseaux (protocoles, NAT, IP, DNS, ports, etc.)&nbsp;;</li><li style="">comprendre la notion de pipelines et de redirection des sorties sous Linux&nbsp;;</li><li style="">comprendre la gestion de droit sous Linux&nbsp;;</li><li style="">comprendre les chaînes de caractères heredocs&nbsp;;</li><li style="">comprendre l'écriture de script exécutable dans une console sous Linux&nbsp;;</li><li style="">comprendre la notion de services sous Debian.</li></ul><br />
<br />
<u><b><font size="5">Nota</font></b></u><br />
<br />
Il y a moult façons de faire des recherches, extractions ou remplacement de texte dans les différentes distributions de Linux et même au sein d'une seule... Celles que je présente ci-dessous ne sont pas forcément les plus optimisées, mais vu que pour leur utilisation dans ce cadre, la vitesse est moins importante que la maintenabilité et la compréhension, j'ai préféré enchaîner des utilitaires que parfois tout faire avec un seul avec une expression plus pénible à trouver ou comprendre...<br />
<hr /><br />
<br />
<font color="#0000FF"><u><b><font size="5">1. Les services sous Debian</font></b></u></font><br />
<br />
<font color="#0000FF"><u><b><i><font size="4">1.1. Un service c'est quoi&nbsp;?</font></i></b></u></font><br />
Un service (ou démon) est un programme qui est lancé de manière automatique au démarrage du système et s'arrête en même temps que l'arrêt de celui-ci. Il sert à gérer des actions en tâche de fond et peut agir seul, comme <i>NTP</i>, le service de recalage de l'heure, ou attendre des actions à traiter comme le service <i>iptables</i> qui traitera toutes les interactions réseau.<br />
Un service est souvent composé de deux parties&nbsp;:<br />
<ul><li style="">un script qui va servir à interagir avec le service&nbsp;;</li><li style="">un exécutable persistant qui réalise les actions correspondant au service.</li></ul><br />
Le fait de placer un script dans <font color="#FFD700">/ect/init.d</font> ne l'active pas.<br />
Pour inscrire en tant que service un script <font color="#EE82EE"><b>nom_script</b></font> dans ce répertoire, il faut utiliser&nbsp;:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;">update-rc.d nom_script defaults</pre>
</div>Pour le désinscrire&nbsp;:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;">update-rc.d <span style="color: #339933;">-f</span> nom_script remove</pre>
</div>À partir de la version Jessie de Debian, le script d'interaction doit répondre aux <a href="https://wiki.debian.org/LSBInitScripts" target="_blank">principes de LSB (Linux Standard Base)</a> qui l'obligent à&nbsp;:<br />
<ul><li style="">permettre au moins les actions suivantes&nbsp;: <font color="#FF0000">start</font>, <font color="#FF0000">stop</font>, <font color="#FF0000">restart</font>, <font color="#FF0000">force-reload</font> et <font color="#FF0000">status</font>&nbsp;;</li><li style="">retourner un code de fin d'exécution cohérent (0 ras sinon un problème s'est produit)&nbsp;;</li><li style="">contenir un bloc d'information (un bloc de commentaires spécialement formaté) donnant obligatoirement les dépendances de démarrage et d'arrêt du service.</li></ul><br />
<font color="#FF0000">Un warning est émis à chaque appel à <i>update-rc.d</i> si ces trois conditions ne sont pas remplies.</font><br />
<br />
<font color="#0000FF"><u><b><i><font size="4">1.2. L'utilitaire iptables</font></i></b></u></font><br />
C'est donc l'utilitaire qui permet de contrôler les flux réseau, il est une interface en ligne de commande à <i>netfilter</i>. Il permet de générer des filtres sur les paquets en agissant selon leur protocole (TCP, UDP ou ICMP) , leur source ou destination en termes de port et/ou d'URL ou d'IP, le nombre de connexions ou des règles de traduction d'adresse (NAT) par exemple. À chaque invocation de <i>iptables</i> avec  une règle, celle-ci s'ajoute à la chaîne de règles qui seront testées dans l'ordre de leur ajout jusqu'à ce qu'une décision sur le flux soit prise (acceptation, rejet avec ou sans notification à l'émetteur). Des modules optionnels sont à installer voire à compiler selon les besoins. <i>iptables</i> tient son nom du fait que les règles sont organisées en tables. Une présentation succincte de <i>iptables</i> est faite <a href="https://wiki.debian.org/iptables" target="_blank">ici</a>.<br />
On pourra lister les règles en place avec&nbsp;:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;">iptables <span style="color: #339933;">-L</span></pre>
</div>On notera qu'il existe un paquet Debian pour rendre persistantes les règles&nbsp;: <a href="https://packages.debian.org/iptables-persistent" target="_blank">iptables-persistent</a>. Néanmoins il ne fait que sauvegarder ou restaurer les règles dans un fichier. C'est pour ça que je lui préfère le principe du programme que l'on transforme en service une fois la phase de test finie. En cas d'erreur, tant qu'on n’a pas testé les règles avec succès, un simple redémarrage du système suffit pour revenir à la configuration précédente.<br />
<br />
<br />
<font color="#0000FF"><u><b><font size="5">2. Un service de pare-feu</font></b></u></font><br />
<br />
<font color="#0000FF"><u><b><i><font size="4">2.1. Stratégie choisie</font></i></b></u></font><br />
Dans le cas du pare-feu, il n'y aura que le script d'interaction. L'idée va être de faire un script de génération, dans le répertoire <font color="#FFD700">/root</font>, pour le code du script d'interaction qui lui sera dans le répertoire <font color="#FFD700">/ect/init.d</font>.<br />
<br />
<font color="#0000FF"><u><b><i><font size="4">2.2. parefeu.sh</font></i></b></u></font><br />
<font color="#0000FF"><u><b><font size="3">2.2.1. But</font></b></u></font><br />
Le script sera placé dans le répertoire <font color="#FFD700">/root</font> et pour le rendre exécutable, on utilisera&nbsp;:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;"><span style="color: #0080ff;">chmod</span> <span style="color: #cc66cc;">700</span> <span style="color: black">/</span>root<span style="color: black">/</span>parefeu.sh</pre>
</div>Le but de ce script va être de&nbsp;:<br />
<ul><li style="">récupérer la valeur du port SSH&nbsp;;</li><li style="">récupérer les 24 bits supérieurs de l'IP du serveur de monitoring d'OVH&nbsp;;</li><li style="">créer le script du pare-feu.</li></ul><br />
<font color="#0000FF"><u><b><font size="3">2.2.2. Récupérer la valeur du port SSH</font></b></u></font><br />
On va utiliser <i>grep</i> sur <font color="#FFD700">/etc/ssh/</font><font color="#EE82EE"><b>sshd_config</b></font> pour récupérer la ligne du port utilisé par le service SSH et <i>awk</i> pour extraire le nombre qui est la 2<sup>e</sup> valeur de celle-ci.<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;"><span style="color: #339933;">ssh_port</span>=$<span style="color: black">&#40;</span><span style="color: #0080ff;">cat</span> <span style="color: black">/</span>etc<span style="color: black">/</span>ssh<span style="color: black">/</span>sshd_config <span style="color: black">|</span> <span style="color: #0080ff;">grep</span> Port <span style="color: black">|</span> <span style="color: #0080ff;">awk</span> <span style="color: #FF0000;">'{print $2;}'</span><span style="color: black">&#41;</span></pre>
</div>On peut se passer de <i>cat</i>:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;"><span style="color: #339933;">ssh_port</span>=$<span style="color: black">&#40;</span><span style="color: #0080ff;">grep</span> Port <span style="color: black">/</span>etc<span style="color: black">/</span>ssh<span style="color: black">/</span>sshd_config <span style="color: black">|</span> <span style="color: #0080ff;">awk</span> <span style="color: #FF0000;">'{print $2;}'</span><span style="color: black">&#41;</span></pre>
</div>Ou même tout faire avec <i>awk</i>:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;"><span style="color: #339933;">ssh_port</span>=$<span style="color: black">&#40;</span><span style="color: #0080ff;">awk</span> <span style="color: #FF0000;">'/Port/ {print $2;}'</span> <span style="color: black">/</span>etc<span style="color: black">/</span>ssh<span style="color: black">/</span>sshd_config<span style="color: black">&#41;</span></pre>
</div><font color="#0000FF"><u><b><font size="3">2.2.3. Récupérer les 24 bits supérieurs de l'IP du serveur de monitoring d'OVH</font></b></u></font><br />
L'utilitaire <i>ifconfig</i> permet de mettre en place ou de récupérer tout ou partie des configurations réseau. Nous avons besoin de l'IP externe du serveur, donc nous allons lire la partie concernant l'interface réseau <font color="#00FF00">eth0</font>. On fait un <i>grep</i> pour extraire la ligne qui contient l'IP et enfin on ne garde que l'IP grâce à un appel à <i>awk</i>. On obtient le code suivant&nbsp;:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;"><span style="color: #339933;">serveur_ip</span>=$<span style="color: black">&#40;</span><span style="color: #0080ff;">ifconfig</span> eth0 <span style="color: black">|</span> <span style="color: #0080ff;">grep</span> Bcast <span style="color: black">|</span> <span style="color: #0080ff;">awk</span> <span style="color: #FF0000;">'{sub(/[a-zA-Z]+\:/,&quot;&quot;,$2); print $2;}'</span><span style="color: black">&#41;</span></pre>
</div>On peut se passer de grep là aussi:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;"><span style="color: #339933;">serveur_ip</span>=$<span style="color: black">&#40;</span><span style="color: #0080ff;">ifconfig</span> eth0 <span style="color: black">|</span> <span style="color: #0080ff;">awk</span> <span style="color: #FF0000;">'/Bcast/ {sub(/[a-zA-Z]+\:/,&quot;&quot;,$2); print $2;}'</span><span style="color: black">&#41;</span></pre>
</div>Il peut être utile d'avoir l'IP complète pour certains cas ou réglages donc on extrait de celle-ci les codes des 3 premiers octets grâce à une commande <i>awk</i>:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;"><span style="color: #339933;">serveur_24</span>=$<span style="color: black">&#40;</span><span style="color: #0000ff;">echo</span> <span style="color: #339933;">$serveur_ip</span> <span style="color: black">|</span> <span style="color: #0080ff;">awk</span> <span style="color: #339933;">-F</span> <span style="color: #FF0000;">&quot;.&quot;</span> <span style="color: #FF0000;">'{print $1&quot;.&quot;$2&quot;.&quot;$3;}'</span><span style="color: black">&#41;</span></pre>
</div><font color="#0000FF"><u><b><font size="3">2.2.4. Désinscription du service du pare-feu si besoin</font></b></u></font><br />
Pour éviter d'avoir des warnings à propos de la modification du code du service, on teste la présence du fichier dans le répertoire <font color="#FFD700">/ect/init.d</font> et, si le fichier <font color="#FFD700">/ect/init.d/</font><font color="#EE82EE"><b>parefeu</b></font>, on désinscrit le service <font color="#EE82EE"><b>parefeu</b></font>. On utilise pour cela le code suivant&nbsp;:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:120px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td valign="top"><pre style="margin: 0"><span style="color: #0000ff;">if</span> <span style="color: black">&#91;</span> <span style="color: #339933;">-f</span> <span style="color: #FF0000;">&quot;/etc/init.d/<span style="color: #800000;">$parefeu</span>&quot;</span> <span style="color: black">&#93;</span>; <span style="color: #0000ff;">then</span>
    <span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Suppression du referencement du service du service&quot;</span>
    update-rc.d <span style="color: #339933;">-f</span> <span style="color: #339933;">$parefeu</span> remove
<span style="color: #0000ff;">else</span>
    <span style="color: #0080ff;">touch</span> <span style="color: black">/</span>etc<span style="color: black">/</span>init.d<span style="color: black">/</span><span style="color: #339933;">$parefeu</span>
    <span style="color: #0080ff;">chmod</span> <span style="color: #cc66cc;">755</span> <span style="color: black">/</span>etc<span style="color: black">/</span>init.d<span style="color: black">/</span><span style="color: #339933;">$parefeu</span>
<span style="color: #0000ff;">fi</span></pre></td></tr></table></pre>
</div><font color="#0000FF"><u><b><font size="3">2.2.5. Créer le script du pare-feu</font></b></u></font><br />
Pour ce faire, on utilise <i>cat</i> à qui on passe un here doc dont le délimiteur est la chaîne de caractères EOF, et on redirige sa sortie vers un fichier en faisant un remplacement complet du contenu. Comme le délimiteur n'a pas de quotes, simples ou doubles, autour de lui, tout ce qui suit un $ est interprété comme une variable et replacé par sa valeur. On obtient le code suivant:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:72px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br />3<br /></div></td><td valign="top"><pre style="margin: 0"><span style="color: #0080ff;">cat</span> <span style="color: black">&lt;&lt;</span>- EOF <span style="color: black">&gt;</span> <span style="color: black">/</span>etc<span style="color: black">/</span>init.d<span style="color: black">/</span><span style="color: #339933;">$parefeu</span>
<span style="color: #808080;">#code ici</span>
EOF</pre></td></tr></table></pre>
</div><font color="#0000FF"><u><b><font size="3">2.2.6. Inscription du service du pare-feu</font></b></u></font><br />
C'est l'action finale du script, elle est réalisée par&nbsp;:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;">update-rc.d <span style="color: #339933;">$parefeu</span> defaults</pre>
</div><font color="#0000FF"><u><b><font size="3">2.2.7. Code complet du script</font></b></u></font><br />
Le code complet du script <font color="#FFD700">/root/</font><font color="#EE82EE"><b>parefeu.sh</b></font> est:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:204px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="40"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">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 />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br /></div></td><td valign="top"><pre style="margin: 0"><span style="color: #808080;">#!/bin/sh</span>
reset
<span style="color: #808080;"># Emplacement du service</span>
<span style="color: #339933;">parefeu</span>=<span style="color: #FF0000;">'parefeu'</span>
<span style="color: #808080;"># Configuration du service</span>
<span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;\033[7m INSTALLATION DU PAREFEU \033[0m&quot;</span>
<span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Recuperation de l'IP du serveur sur eth0&quot;</span>
<span style="color: #339933;">serveur_ip</span>=$<span style="color: black">&#40;</span><span style="color: #0080ff;">ifconfig</span> eth0 <span style="color: black">|</span> <span style="color: #0080ff;">awk</span> <span style="color: #FF0000;">'/Bcast/ {sub(/[a-zA-Z]+\:/,&quot;&quot;,$2); print $2;}'</span><span style="color: black">&#41;</span>
<span style="color: #0000ff;">echo</span> <span style="color: #339933;">$serveur_ip</span>
<span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Recuperation de la portion d'IP du serveur pour le SLA&quot;</span>
<span style="color: #339933;">serveur_24</span>=$<span style="color: black">&#40;</span><span style="color: #0000ff;">echo</span> <span style="color: #339933;">$serveur_ip</span> <span style="color: black">|</span> <span style="color: #0080ff;">awk</span> <span style="color: #339933;">-F</span> <span style="color: #FF0000;">&quot;.&quot;</span> <span style="color: #FF0000;">'{print $1&quot;.&quot;$2&quot;.&quot;$3;}'</span><span style="color: black">&#41;</span>
<span style="color: #0000ff;">echo</span> <span style="color: #339933;">$serveur_24</span>
<span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Recuperation du port SSH&quot;</span>
<span style="color: #339933;">ssh_port</span>=$<span style="color: black">&#40;</span><span style="color: #0080ff;">awk</span> <span style="color: #FF0000;">'/Port/ {print $2;}'</span> <span style="color: black">/</span>etc<span style="color: black">/</span>ssh<span style="color: black">/</span>sshd_config<span style="color: black">&#41;</span>
<span style="color: #0000ff;">echo</span> <span style="color: #339933;">$ssh_port</span>
<span style="color: #0000ff;">if</span> <span style="color: black">&#91;</span> <span style="color: #339933;">-f</span> <span style="color: #FF0000;">&quot;/etc/init.d/<span style="color: #800000;">$parefeu</span>&quot;</span> <span style="color: black">&#93;</span>; <span style="color: #0000ff;">then</span>
    <span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Suppression du referencement du service du service&quot;</span>
    update-rc.d <span style="color: #339933;">-f</span> <span style="color: #339933;">$parefeu</span> remove
<span style="color: #0000ff;">else</span>
    <span style="color: #0080ff;">touch</span> <span style="color: black">/</span>etc<span style="color: black">/</span>init.d<span style="color: black">/</span><span style="color: #339933;">$parefeu</span>
    <span style="color: #0080ff;">chmod</span> <span style="color: #cc66cc;">755</span><span style="color: black">/</span>etc<span style="color: black">/</span>init.d<span style="color: black">/</span><span style="color: #339933;">$parefeu</span>
<span style="color: #0000ff;">fi</span>
<span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Creation du code du service&quot;</span>
<span style="color: #0080ff;">cat</span> <span style="color: black">&lt;&lt;</span>- EOF <span style="color: black">&gt;</span> <span style="color: black">/</span>etc<span style="color: black">/</span>init.d<span style="color: black">/</span><span style="color: #339933;">$parefeu</span>
<span style="color: #808080;">#!/bin/sh</span>
<span style="color: #808080;">### BEGIN INIT INFO</span>
<span style="color: #808080;"># Provides:          parefeu</span>
<span style="color: #808080;"># Required-Start:    $syslog $network</span>
<span style="color: #808080;"># Required-Stop:     $syslog $network</span>
<span style="color: #808080;"># Default-Start:     2 3 4 5</span>
<span style="color: #808080;"># Default-Stop:      0 1 6</span>
<span style="color: #808080;"># Short-Description: Start firewall daemon at boot time</span>
<span style="color: #808080;"># Description:       Custom Firewall scrip.</span>
<span style="color: #808080;">### END INIT INFO</span>
&nbsp;
firewall_start<span style="color: black">&#40;</span><span style="color: black">&#41;</span>
<span style="color: black">&#123;</span>
    <span style="color: #808080;"># ---</span>
    <span style="color: #808080;"># Vider les tables actuelles</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-F</span>
    <span style="color: #808080;"># Vider les r&egrave;gles personnelles</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-X</span>
    <span style="color: #808080;"># Interdire toute connexion entrante et sortante</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-P</span> INPUT DROP
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-P</span> FORWARD DROP
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-P</span> OUTPUT DROP
    <span style="color: #808080;"># ---</span>
    <span style="color: #808080;"># Ne pas casser les connexions etablies</span>
    iptables <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> RELATED,ESTABLISHED <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> RELATED,ESTABLISHED <span style="color: #339933;">-j</span> ACCEPT
    <span style="color: #808080;"># Autoriser loopback</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> lo <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> lo <span style="color: #339933;">-j</span> ACCEPT
    <span style="color: #808080;"># Differentes securites</span>
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">1</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>tcp_syncookies
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">0</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>ip_forward
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">1</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>icmp_echo_ignore_broadcasts
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">1</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>conf<span style="color: black">/</span>all<span style="color: black">/</span>log_martians
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">1</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>icmp_ignore_bogus_error_responses
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">1</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>conf<span style="color: black">/</span>all<span style="color: black">/</span>rp_filter
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">0</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>conf<span style="color: black">/</span>all<span style="color: black">/</span>send_redirects
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">0</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>conf<span style="color: black">/</span>all<span style="color: black">/</span>accept_source_route
    <span style="color: #808080;"># ---</span>
    <span style="color: #808080;"># R&egrave;gle de rejet et log des tentatives de connexion en trop sur SSH</span>
    iptables <span style="color: #339933;">-N</span> SSH_DROP
    iptables <span style="color: #339933;">-A</span> SSH_DROP <span style="color: #339933;">-j</span> LOG <span style="color: #339933;">--log-prefix</span> <span style="color: #FF0000;">'[fw drop -&gt; Attack ssh] : '</span>
    iptables <span style="color: #339933;">-A</span> SSH_DROP <span style="color: #339933;">-j</span> DROP
    <span style="color: #808080;"># ---</span>
    <span style="color: #808080;"># Autoriser le fonctionnement du DNS en UDP</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> udp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">53</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> udp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">53</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    <span style="color: #808080;"># ---</span>
    <span style="color: #808080;"># Autoriser ICMP pour les serveur SLA OVH</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> icmp <span style="color: #339933;">-s</span> 92.222.184.0<span style="color: black">/</span><span style="color: #cc66cc;">24</span>,92.222.185.0<span style="color: black">/</span><span style="color: #cc66cc;">24</span>,92.222.186.0<span style="color: black">/</span><span style="color: #cc66cc;">24</span>,167.114.37.0<span style="color: black">/</span><span style="color: #cc66cc;">24</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> icmp <span style="color: #339933;">-s</span> 37.187.231.251<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,151.80.118.100<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,151.80.231.244<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,151.80.231.245<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,151.80.231.246<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,151.80.231.247<span style="color: black">/</span><span style="color: #cc66cc;">32</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> icmp <span style="color: #339933;">-s</span> 213.186.33.62<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,213.186.45.4<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,213.251.184.9<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,37.59.0.235<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,8.33.137.2<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,213.186.33.13<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,213.186.50.98<span style="color: black">/</span><span style="color: #cc66cc;">32</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> icmp <span style="color: #339933;">-s</span> <span style="color: #808080;">${serveur_24}</span>.250<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,<span style="color: #808080;">${serveur_24}</span>.251<span style="color: black">/</span><span style="color: #cc66cc;">32</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    <span style="color: #808080;"># Autoriser le monitring OVH</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> udp <span style="color: #339933;">-dport</span> <span style="color: #cc66cc;">6100</span>:<span style="color: #cc66cc;">6200</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    <span style="color: #808080;"># ---</span>
    <span style="color: #808080;"># Autoriser le ssh avec anti force brute</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #808080;">${ssh_port}</span> <span style="color: #339933;">-m</span> recent <span style="color: #339933;">--set</span> <span style="color: #339933;">--name</span> SSH_BAD
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #808080;">${ssh_port}</span> <span style="color: #339933;">-m</span> recent <span style="color: #339933;">--update</span> <span style="color: #339933;">--seconds</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">--hitcount</span> <span style="color: #cc66cc;">4</span> <span style="color: #339933;">--name</span> SSH_BAD <span style="color: #339933;">-j</span> SSH_DROP
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #808080;">${ssh_port}</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #808080;">${ssh_port}</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    <span style="color: #808080;"># ---</span>
    <span style="color: #808080;"># Autoriser DNS, HTTP, HTTPS en TCP</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">-m</span> multiport <span style="color: #339933;">--dports</span> <span style="color: #cc66cc;">53</span>,<span style="color: #cc66cc;">80</span>,<span style="color: #cc66cc;">443</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">-m</span> multiport <span style="color: #339933;">--dports</span> <span style="color: #cc66cc;">53</span>,<span style="color: #cc66cc;">80</span>,<span style="color: #cc66cc;">443</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    <span style="color: #808080;"># Autoriser le FTP sortant en TCP</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">20</span>:<span style="color: #cc66cc;">21</span> <span style="color: #339933;">-j</span> ACCEPT
    modprobe ip_conntrack
    modprobe ip_conntrack_ftp
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--sport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">20</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> ESTABLISHED <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--sport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">21</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW,ESTABLISHED <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--sport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> ESTABLISHED,RELATED <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--sport</span> <span style="color: #cc66cc;">20</span> <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> ESTABLISHED,RELATED <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">-m</span> multiport <span style="color: #339933;">--sports</span> <span style="color: #cc66cc;">21</span>,<span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> ESTABLISHED <span style="color: #339933;">-j</span> ACCEPT
    <span style="color: #808080;"># ---</span>
    <span style="color: #808080;"># Autoriser le recalage d'horloge syst&egrave;me</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-p</span> udp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">123</span> <span style="color: #339933;">-j</span> ACCEPT
<span style="color: black">&#125;</span>
&nbsp;
firewall_stop<span style="color: black">&#40;</span><span style="color: black">&#41;</span>
<span style="color: black">&#123;</span>
    iptables <span style="color: #339933;">-F</span>
    iptables <span style="color: #339933;">-X</span>
    iptables <span style="color: #339933;">-P</span> INPUT ACCEPT
    iptables <span style="color: #339933;">-P</span> FORWARD ACCEPT
    iptables <span style="color: #339933;">-P</span> OUTPUT ACCEPT
<span style="color: #808080;">#</span>
<span style="color: #808080;">#    iptables -t nat -F</span>
<span style="color: #808080;">#    iptables -t nat -X</span>
<span style="color: #808080;">#    iptables -t nat -P PREROUTING ACCEPT</span>
<span style="color: #808080;">#    iptables -t nat -P POSTROUTING ACCEPT</span>
<span style="color: #808080;">#    iptables -t nat -P OUTPUT ACCEPT</span>
<span style="color: #808080;">#</span>
<span style="color: #808080;">#    iptables -t mangle -F</span>
<span style="color: #808080;">#    iptables -t mangle -X</span>
<span style="color: #808080;">#    iptables -t mangle -P PREROUTING ACCEPT</span>
<span style="color: #808080;">#    iptables -t mangle -P OUTPUT ACCEPT</span>
<span style="color: black">&#125;</span>
&nbsp;
<span style="color: #808080;"># Corps du programme</span>
<span style="color: #0000ff;">case</span> <span style="color: #339933;">$1</span> <span style="color: #0000ff;">in</span>
    <span style="color: #FF0000;">'force-reload'</span><span style="color: black">&#41;</span>
        <span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Demarrage du parefeu&quot;</span>
        firewall_start
        <span style="color: #0000ff;">exit</span> <span style="color: #cc66cc;">0</span>
    <span style="color: black">;;</span>
    <span style="color: #FF0000;">'regles'</span> <span style="color: black">&#41;</span>
        <span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Regles actuelles du parefeu&quot;</span>
        iptables <span style="color: #339933;">-L</span> <span style="color: #339933;">-v</span> <span style="color: #339933;">-n</span>
        <span style="color: #0000ff;">exit</span> <span style="color: #cc66cc;">0</span>
    <span style="color: black">;;</span>
    <span style="color: #FF0000;">'restart'</span><span style="color: black">&#41;</span>
        <span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Redemarrage du parefeu&quot;</span>
        firewall_start
        <span style="color: #0000ff;">exit</span> <span style="color: #cc66cc;">0</span>
    <span style="color: black">;;</span>
    <span style="color: #FF0000;">'start'</span><span style="color: black">&#41;</span>
        <span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Demarrage du parefeu&quot;</span>
        firewall_start
        <span style="color: #0000ff;">exit</span> <span style="color: #cc66cc;">0</span>
    <span style="color: black">;;</span>
    <span style="color: #FF0000;">'status'</span> <span style="color: black">&#41;</span>
        <span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Etat du chrgement du parefeu&quot;</span>
        iptables <span style="color: #339933;">-L</span> <span style="color: #339933;">-v</span> <span style="color: #339933;">-n</span>
        <span style="color: #0000ff;">exit</span> <span style="color: #cc66cc;">0</span>
    <span style="color: black">;;</span>
    <span style="color: #FF0000;">'stop'</span><span style="color: black">&#41;</span>
        <span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Arret du parefeu&quot;</span>
        firewall_stop
        <span style="color: #0000ff;">exit</span> <span style="color: #cc66cc;">0</span>
    <span style="color: black">;;</span>
    <span style="color: black">*</span><span style="color: black">&#41;</span>
        <span style="color: #0000ff;">echo</span> <span style="color: #FF0000;">&quot;usage: parefeu {regles|start|stop|restart|status}&quot;</span>
        <span style="color: #0000ff;">exit</span> <span style="color: #cc66cc;">1</span>
    <span style="color: black">;;</span>
<span style="color: #0000ff;">esac</span>
EOF
<span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Activation du service&quot;</span>
update-rc.d <span style="color: #339933;">$parefeu</span> defaults
<span style="color: #0000ff;">exit</span> <span style="color: #cc66cc;">0</span></pre></td></tr></table></pre>
</div><br />
<font color="#0000FF"><u><b><i><font size="4">2.3. Comment est écrit le service pare-feu</font></i></b></u></font><br />
<font color="#0000FF"><u><b><font size="3">2.3.1. Utiliser des fonctions en bash</font></b></u></font><br />
Pour faciliter la maintenance et permettre la réutilisation, on va placer le code des fonctionnalités du service dans des fonctions. Une fonction s'écrit de la sorte:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:84px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br />3<br />4<br /></div></td><td valign="top"><pre style="margin: 0">nom_fonction<span style="color: black">&#40;</span><span style="color: black">&#41;</span>
<span style="color: black">&#123;</span>
<span style="color: #808080;">#code du corps de la fonction</span>
<span style="color: black">&#125;</span></pre></td></tr></table></pre>
</div>L'invocation d'une fonction est vue comme un appel à un utilitaire ou un script, dans la fonction on utilise donc $1 pour désigner le premier paramètre, $2 le second, etc. On peut utiliser exit pour fixer un code d'erreur ou non lors de la sortie de celle-ci. Si on prend l'exemple d'une fonction de décompression générique:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:204px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="33"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">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 valign="top"><pre style="margin: 0">decompression <span style="color: black">&#40;</span><span style="color: black">&#41;</span>
<span style="color: black">&#123;</span>
    <span style="color: #0000ff;">if</span> <span style="color: black">&#91;</span> <span style="color: #339933;">-f</span> <span style="color: #339933;">$1</span> <span style="color: black">&#93;</span>; <span style="color: #0000ff;">then</span>
        <span style="color: #0000ff;">case</span> <span style="color: #339933;">$1</span> <span style="color: #0000ff;">in</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.7z<span style="color: black">&#41;</span> 7z x <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.lzma<span style="color: black">&#41;</span> unlzma <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.rar<span style="color: black">&#41;</span> unrar x <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.tar<span style="color: black">&#41;</span> <span style="color: #0080ff;">tar</span> xvf <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.tar.bz2<span style="color: black">&#41;</span> <span style="color: #0080ff;">tar</span> xvjf <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.bz2<span style="color: black">&#41;</span> <span style="color: #0080ff;">bunzip2</span> <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.tar.gz<span style="color: black">&#41;</span> <span style="color: #0080ff;">tar</span> xvzf <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.gz<span style="color: black">&#41;</span> <span style="color: #0080ff;">gunzip</span> <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.tar.xz<span style="color: black">&#41;</span> <span style="color: #0080ff;">tar</span> Jxvf <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.xz<span style="color: black">&#41;</span> xz <span style="color: #339933;">-d</span> <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.tbz2<span style="color: black">&#41;</span> <span style="color: #0080ff;">tar</span> xvjf <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.tgz<span style="color: black">&#41;</span> <span style="color: #0080ff;">tar</span> xvzf <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.zip<span style="color: black">&#41;</span> <span style="color: #0080ff;">unzip</span> <span style="color: #339933;">$1</span> <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span>.Z<span style="color: black">&#41;</span> uncompress <span style="color: black">;;</span>
            <span style="color: black">&#40;</span><span style="color: black">*</span><span style="color: black">&#41;</span> <span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;'<span style="color: #800000;">$1</span>' est impossible &agrave; d&eacute;compresser...&quot;</span> <span style="color: black">;;</span>
        <span style="color: #0000ff;">esac</span>
    <span style="color: #0000ff;">else</span>
        <span style="color: #0000ff;">echo</span> <span style="color: #339933;">-e</span> <span style="color: #FF0000;">&quot;Erreur: '<span style="color: #800000;">$1</span>' n'est pas un fichier valide!&quot;</span>
        <span style="color: #0000ff;">exit</span> <span style="color: #cc66cc;">1</span>
    <span style="color: #0000ff;">fi</span>
<span style="color: black">&#125;</span></pre></td></tr></table></pre>
</div>Dans le contexte où elle est définie, si on prend l'exemple de la fonction de décompression, on peut l'invoquer comme suit:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;">decompression fichier</pre>
</div><font color="#0000FF"><u><b><font size="3">2.3.2. Les fonctions du script du service</font></b></u></font><br />
On va donc utiliser les fonctions suivantes dans le script:<br />
<ul><li style="">firewall_start() qui sert à mettre en place les règles choisies.</li><li style="">firewall_stop() qui va supprimer tous les filtrages.</li></ul><br />
<font color="#0000FF"><u><b><font size="3">2.3.3. Le corps du script du service</font></b></u></font><br />
Le premier paramètre est l'action que le service doit réaliser, un case va lui être appliqué pour déterminer sa valeur:<br />
<ul><li style=""><font color="#FF0000">start</font>, obligatoire, appelle firewall_start().</li><li style=""><font color="#FF0000">stop</font>, obligatoire, appelle firewall_stop().</li><li style=""><font color="#FF0000">restart</font>, obligatoire, appelle firewall_start().</li><li style=""><font color="#FF0000">force-reload</font>, obligatoire, appelle firewall_start().</li><li style=""><font color="#FF0000">status</font>, obligatoire, donne l'activité du service (indépendamment de ce qu'on met dedans) et son historique de chargement.</li><li style=""><font color="#A0A0A0">règles</font>, particulières à ce service, liste les règles actives et la répartition de chacune d'elles de ce qu'elles ont traité comme octets et paquets.</li></ul><br />
<br />
<font color="#0000FF"><u><b><font size="5">3. Les règles du pare-feu utilisées dans cet exemple</font></b></u></font><br />
<br />
<font color="#0000FF"><u><b><i><font size="4">3.1. Initialisation</font></i></b></u></font><br />
<font color="#0000FF"><u><b><font size="3">3.1.1. Non crypté</font></b></u></font><br />
On commence par supprimer toutes les règles:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:84px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br />3<br />4<br /></div></td><td valign="top"><pre style="margin: 0">    <span style="color: #808080;"># Vider les tables actuelles</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-F</span>
    <span style="color: #808080;"># Vider les r&egrave;gles personnelles</span>
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-X</span></pre></td></tr></table></pre>
</div><font color="#0000FF"><u><b><font size="3">3.1.2. Règles par défaut</font></b></u></font><br />
On bloque tout en entrée et sortie:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:72px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br />3<br /></div></td><td valign="top"><pre style="margin: 0">    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-P</span> INPUT DROP
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-P</span> FORWARD DROP
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-P</span> OUTPUT DROP</pre></td></tr></table></pre>
</div><font color="#0000FF"><u><b><font size="3">3.1.3. Non crypté</font></b></u></font><br />
On maintient toutes les connexions sont déja établies ou les nouvelles qui découle de celles-ci:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:60px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br /></div></td><td valign="top"><pre style="margin: 0">    iptables <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> RELATED,ESTABLISHED <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> RELATED,ESTABLISHED <span style="color: #339933;">-j</span> ACCEPT</pre></td></tr></table></pre>
</div>On a donc toutes les règles suivantes qui ne concerneront que le droit ou non d'ouvrir un flux, on aura donc dans celles-ci:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;"><span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW</pre>
</div><font color="#0000FF"><u><b><font size="3">3.1.4. On ouvre la boucle locale</font></b></u></font><br />
On ouvre tout en entrée et sortie sur la boucle locale:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:60px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br /></div></td><td valign="top"><pre style="margin: 0">    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> lo <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> lo <span style="color: #339933;">-j</span> ACCEPT</pre></td></tr></table></pre>
</div><font color="#0000FF"><u><b><font size="3">3.1.5. Quelques petits réglages de sécurité</font></b></u></font><br />
On va mettre en place de réglages pour augmenter la sécurité, mais comme ils sont dépendant du noyau, certains peuvent ne pas fonctionner:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:132px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td valign="top"><pre style="margin: 0">    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">1</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>tcp_syncookies
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">0</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>ip_forward
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">1</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>icmp_echo_ignore_broadcasts
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">1</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>conf<span style="color: black">/</span>all<span style="color: black">/</span>log_martians
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">1</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>icmp_ignore_bogus_error_responses
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">1</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>conf<span style="color: black">/</span>all<span style="color: black">/</span>rp_filter
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">0</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>conf<span style="color: black">/</span>all<span style="color: black">/</span>send_redirects
    <span style="color: #0000ff;">echo</span> <span style="color: #cc66cc;">0</span> <span style="color: black">&gt;</span> <span style="color: black">/</span>proc<span style="color: black">/</span>sys<span style="color: black">/</span>net<span style="color: black">/</span>ipv4<span style="color: black">/</span>conf<span style="color: black">/</span>all<span style="color: black">/</span>accept_source_route</pre></td></tr></table></pre>
</div><br />
<font color="#0000FF"><u><b><i><font size="4">3.2. Le service ssh</font></i></b></u></font><br />
On va commencer par créer une chaîne de règles, nommée SSH_DROP, pour mettre dans le log les tentatives de connexion avant de les jeter sans notification aux émetteurs des paquets:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:84px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br />3<br />4<br /></div></td><td valign="top"><pre style="margin: 0">    <span style="color: #808080;"># R&egrave;gle de rejet et log des tentatives de connexion en trop sur SSH</span>
    iptables <span style="color: #339933;">-N</span> SSH_DROP
    iptables <span style="color: #339933;">-A</span> SSH_DROP <span style="color: #339933;">-j</span> LOG <span style="color: #339933;">--log-prefix</span> <span style="color: #FF0000;">'[fw drop -&gt; Attack ssh] : '</span>
    iptables <span style="color: #339933;">-A</span> SSH_DROP <span style="color: #339933;">-j</span> DROP</pre></td></tr></table></pre>
</div>On ouvre en entrée et sortie le port qu'on a mis dans la variable [COLOR=&quot;#00FF00&quot;]$ssh_port[,/COLOR], mais on impose une limite de 4 connexions par minute pour limiter les tentatives d'attaques par force brute.<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:84px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br />3<br />4<br /></div></td><td valign="top"><pre style="margin: 0">    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #808080;">${ssh_port}</span> <span style="color: #339933;">-m</span> recent <span style="color: #339933;">--set</span> <span style="color: #339933;">--name</span> SSH_BAD
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #808080;">${ssh_port}</span> <span style="color: #339933;">-m</span> recent <span style="color: #339933;">--update</span> <span style="color: #339933;">--seconds</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">--hitcount</span> <span style="color: #cc66cc;">4</span> <span style="color: #339933;">--name</span> SSH_BAD <span style="color: #339933;">-j</span> SSH_DROP
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #808080;">${ssh_port}</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #808080;">${ssh_port}</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT</pre></td></tr></table></pre>
</div><br />
<font color="#0000FF"><u><b><i><font size="4">3.3. Les principaux services</font></i></b></u></font><br />
<font color="#0000FF"><u><b><font size="3">3.3.1. En TCP</font></b></u></font><br />
On fait une déclaration multiport pour les services TCP courants (DNS, HTTP, HTTPS):<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:60px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br /></div></td><td valign="top"><pre style="margin: 0">    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">-m</span> multiport <span style="color: #339933;">--dports</span> <span style="color: #cc66cc;">53</span>,<span style="color: #cc66cc;">80</span>,<span style="color: #cc66cc;">443</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">-m</span> multiport <span style="color: #339933;">--dports</span> <span style="color: #cc66cc;">53</span>,<span style="color: #cc66cc;">80</span>,<span style="color: #cc66cc;">443</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT</pre></td></tr></table></pre>
</div>On notera que le procédé de création de certificats SSL Let's encrypt, maintenant bien supporté, ainsi que les utilitaires <i>apt-get</i>, <i>aptitude</i> et <i>wget</i> utilisent les ports 80 et 443 en entrée et sortie.<br />
<font color="#0000FF"><u><b><font size="3">3.3.2. En UDP</font></b></u></font><br />
On ouvre pour le DNS pour son fonctionnement en UDP:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:60px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br /></div></td><td valign="top"><pre style="margin: 0">    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> udp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">53</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> udp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">53</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT</pre></td></tr></table></pre>
</div>On ouvre le port du service de mise à jour de l'horloge du serveur:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;">    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-p</span> udp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">123</span> <span style="color: #339933;">-j</span> ACCEPT</pre>
</div><font color="#0000FF"><u><b><font size="3">3.3.3.  Les autres services TCP et UDP</font></b></u></font><br />
Ma volonté a été de faire un serveur minimaliste donc les services tels que les serveurs de mails ou FTP ne seront pas abordés ici et seront à rajouter au script si on veut les prendre en compte.<br />
<br />
<font color="#0000FF"><u><b><i><font size="4">3.4. Le FTP sortant en passif</font></i></b></u></font><br />
L'utilitaire <i>wget</i> utilise parfois le protocole FTP. Si en FTP actif, ouvrir les ports 20 et 21 suffit, en mode passif les choses sont plus compliquées, ce qui explique un échec de téléchargement alors que la connexion à l'hôte se passe bien. Cela est dû à une connexion supplémentaire qui se négocie via l'un des ports 20 ou 21. Du coup, les états ESTABLISHED et RELATED ne permettent pas de gérer cette nouvelle connexion. Pour se faire, deux modules optionnels de <i>IPTABLES</i>, ip_conntrack et ip_conntrack_ftp, doivent être activés grâce à modprobe:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:60px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br /></div></td><td valign="top"><pre style="margin: 0">    modprobe ip_conntrack
    modprobe ip_conntrack_ftp</pre></td></tr></table></pre>
</div>Il reste à ouvrir les ports disponibles à partir de 1024 pour la connexion supplémentaire en cas de connexion sur les ports 20 et 21:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:96px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br />3<br />4<br />5<br /></div></td><td valign="top"><pre style="margin: 0">    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--sport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">20</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> ESTABLISHED <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--sport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">21</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW,ESTABLISHED <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--sport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> ESTABLISHED,RELATED <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--sport</span> <span style="color: #cc66cc;">20</span> <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> ESTABLISHED,RELATED <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">-m</span> multiport <span style="color: #339933;">--sports</span> <span style="color: #cc66cc;">21</span>,<span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">1024</span>:<span style="color: #cc66cc;">65535</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> ESTABLISHED <span style="color: #339933;">-j</span> ACCEPT</pre></td></tr></table></pre>
</div><br />
<font color="#0000FF"><u><b><i><font size="4">3.5. Le monitoring de l'hébergeur</font></i></b></u></font><br />
<font color="#0000FF"><u><b><font size="3">3.5.1. En TCP</font></b></u></font><br />
Dans le cas d'OVH, on a tout un tas de serveurs qui servent pour voir la présence sur le réseau des serveurs dédiés et VPS. Ils utilisent le protocole ICPM, on le leur permet donc avec leurs IP comme source:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:84px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br />3<br />4<br /></div></td><td valign="top"><pre style="margin: 0">    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> icmp <span style="color: #339933;">-s</span> 92.222.184.0<span style="color: black">/</span><span style="color: #cc66cc;">24</span>,92.222.185.0<span style="color: black">/</span><span style="color: #cc66cc;">24</span>,92.222.186.0<span style="color: black">/</span><span style="color: #cc66cc;">24</span>,167.114.37.0<span style="color: black">/</span><span style="color: #cc66cc;">24</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> icmp <span style="color: #339933;">-s</span> 37.187.231.251<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,151.80.118.100<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,151.80.231.244<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,151.80.231.245<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,151.80.231.246<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,151.80.231.247<span style="color: black">/</span><span style="color: #cc66cc;">32</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> icmp <span style="color: #339933;">-s</span> 213.186.33.62<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,213.186.45.4<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,213.251.184.9<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,37.59.0.235<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,8.33.137.2<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,213.186.33.13<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,213.186.50.98<span style="color: black">/</span><span style="color: #cc66cc;">32</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT
    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-i</span> eth0 <span style="color: #339933;">-p</span> icmp <span style="color: #339933;">-s</span> <span style="color: #808080;">${serveur_24}</span>.250<span style="color: black">/</span><span style="color: #cc66cc;">32</span>,<span style="color: #808080;">${serveur_24}</span>.251<span style="color: black">/</span><span style="color: #cc66cc;">32</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT</pre></td></tr></table></pre>
</div><font color="#0000FF"><u><b><font size="3">3.5.2. Le service RTM</font></b></u></font><br />
C'est un script qui envoie à intervalle régulier préprogrammé tout ou partie d'un ensemble d'informations sur l'état du matériel, pour OVH:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:36px;">    iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-o</span> eth0 <span style="color: #339933;">-p</span> udp <span style="color: #339933;">-dport</span> <span style="color: #cc66cc;">6100</span>:<span style="color: #cc66cc;">6200</span> <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> NEW <span style="color: #339933;">-j</span> ACCEPT</pre>
</div><br />
<font color="#0000FF"><u><b><font size="5">4. Conclusion</font></b></u></font><br />
<br />
On a ainsi un petit exemple de script facilement personnalisable pour la situation de chacun en rajoutant les services nécessaires. On pourra se baser sur le sujet du site bien connu de alsacreation sur le sujet: <a href="https://www.alsacreations.com/tuto/lire/622-Securite-firewall-iptables.html" target="_blank">ici</a>. On y trouvera des réglages supplémentaires notamment pour OVH<br />
Pour un serveur de mail, on utilisera certaines de ces actions:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:204px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="33"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">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 /></div></td><td valign="top"><pre style="margin: 0"><span style="color: #808080;"># Mail SMTP:25</span>
iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">25</span> <span style="color: #339933;">-j</span> ACCEPT
iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">25</span> <span style="color: #339933;">-j</span> ACCEPT
&nbsp;
<span style="color: #808080;"># Mail POP3:110</span>
iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">110</span> <span style="color: #339933;">-j</span> ACCEPT
iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">110</span> <span style="color: #339933;">-j</span> ACCEPT
&nbsp;
<span style="color: #808080;"># Mail IMAP:143</span>
iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">143</span> <span style="color: #339933;">-j</span> ACCEPT
iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">143</span> <span style="color: #339933;">-j</span> ACCEPT
&nbsp;
<span style="color: #808080;"># Mail POP3S:995</span>
iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">995</span> <span style="color: #339933;">-j</span> ACCEPT
iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">995</span> <span style="color: #339933;">-j</span> ACCEPT</pre></td></tr></table></pre>
</div>Pour un serveur FTP dynamique:<br />
<div class="bbcode_container">
	<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
	<td style="border: 0; padding: 0; text-align: left">Code bash :</td>
	<td style="border: 0; padding: 0; text-align: right"><a href="#" onclick="return ano_selectionnerCode(this);">Sélectionner tout</a> -
	<a href="#" onclick="return ano_etendreCode(this);">Visualiser dans une fenêtre à part</a></td></tr></table>
	<pre class="bbcode_code" style="height:120px;"><table cellspacing="0" cellpadding="0"><tr><td valign="top" width="26"><div style="border: 1px dashed gray; padding-left: 5px; padding-right: 5px; margin-right: 5px; text-align: right; font-family: monospace">1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td valign="top"><pre style="margin: 0"><span style="color: #808080;"># FTP Out</span>
iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> OUTPUT <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">20</span>:<span style="color: #cc66cc;">21</span> <span style="color: #339933;">-j</span> ACCEPT
&nbsp;
<span style="color: #808080;"># FTP In</span>
modprobe ip_conntrack_ftp <span style="color: #808080;"># ligne facultative avec les serveurs OVH</span>
iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-p</span> tcp <span style="color: #339933;">--dport</span> <span style="color: #cc66cc;">20</span>:<span style="color: #cc66cc;">21</span> <span style="color: #339933;">-j</span> ACCEPT
iptables <span style="color: #339933;">-t</span> filter <span style="color: #339933;">-A</span> INPUT <span style="color: #339933;">-m</span> state <span style="color: #339933;">--state</span> ESTABLISHED,RELATED <span style="color: #339933;">-j</span> ACCEPT</pre></td></tr></table></pre>
</div>Pour un serveur FTP passif, une plage supplémentaire de ports à partir de 1024 sera nécessaire.</blockquote>

]]></content:encoded>
			<dc:creator>ericd69</dc:creator>
			<guid isPermaLink="true">https://www.developpez.net/forums/blogs/426277-ericd69/b3098/exemple-d-automatisation-d-pare-feu-via-service-linux-debian/</guid>
		</item>
	</channel>
</rss>
