crabs_firewall-3.0.1 ©2005-2011 - Christophe CAZAJUS (crabs-mettre_le_signe_at-crabs-world.com)
1 # 2 # ============================================================================= 3 # crabs_firewall-3.0.1 : Firewall-Masquerade avec interface http 4 # Copyright (C) 2005-2011 : Christophe CAZAJUS (crabs-mettre_le_signe_at-crabs-world.com) 5 # 6 # Ce source fait partie d'un projet logiciel libre. Vous pouvez le distribuer 7 # et/ou le modifier en respectant les termes de la GNU General Public License 8 # version 2 ou (suite a votre propre choix) une version ulterieure. 9 # 10 # Ce programme est distribue dans l'espoir qu'il puisse etre utile, mais 11 # sans aucune garantie, meme si il est associe a un produit qui vous en 12 # propose une. Conformez-vous a la GNU General Public License pour avoir 13 # plus de precisions. 14 # 15 # L'auteur ne peut etre tenu responsable de l'utilisation faite des 16 # composantes associees a ce projet (en partie ou dans leur totalite). 17 # 18 # Une copie du fichier de la GNU GPL est fournie dans le repertoire DOC 19 # de ce projet sous le nom gnu_gpl.txt 20 # 21 # ============================================================================= 22 # 23 function get_hosts( nom, input ) 24 { 25 if ( input == "O" ) 26 { 27 for( i=0; i<nb_network; i++ ) 28 { 29 if ( nom == eth_i[i] ) return "-i " eth_i[i] ; 30 if ( nom == eth_n[i] ) 31 { 32 if ( eth_a[i] == "0.0.0.0/0" ) return "-i " eth_i[i] ; 33 return "-s " eth_a[i] ; 34 } 35 } 36 } 37 else 38 { 39 for( i=0; i<nb_network; i++ ) 40 { 41 if ( nom == eth_i[i] ) return "-o " eth_i[i] ; 42 if ( nom == eth_n[i] ) 43 { 44 if ( eth_a[i] == "0.0.0.0/0" ) return "-o " eth_i[i] ; 45 return "-d " eth_a[i] ; 46 } 47 } 48 } 49 if ( nom == "local" ) return "local" ; 50 if ( hosts[nom] == "" ) return "??" ; 51 if ( input == "O" ) 52 return "-s " hosts[nom] ; 53 else 54 return "-d " hosts[nom] ; 55 } 56 57 function get_host_only( nom ) 58 { 59 if ( hosts[nom] == "" ) return "??" ; 60 return hosts[nom] ; 61 } 62 63 function do_nat() 64 { 65 if ( nat_done == 1 ) return ; 66 nat_done = 1 ; 67 printf( "iptables -t nat -F\niptables -t nat -X\n" ) ; 68 printf( "iptables -t nat -P PREROUTING ACCEPT\n" ) ; 69 printf( "iptables -t nat -P POSTROUTING ACCEPT\n" ) ; 70 printf( "iptables -t nat -P OUTPUT ACCEPT\n" ) ; 71 } 72 73 function do_input( src, srv, proto ) 74 { 75 printf( "iptables -A INPUT %s -p %s --dport %s %s -j ACCEPT\n", 76 src, proto, srv, state ) ; 77 } 78 79 function do_output( dst, srv, proto ) 80 { 81 printf( "iptables -A OUTPUT %s -p %s --dport %s %s -j ACCEPT\n", 82 dst, proto, srv, state ) ; 83 } 84 85 function do_forward( src, dst, srv, proto ) 86 { 87 printf( "iptables -A FORWARD %s %s -p %s --dport %s %s -j ACCEPT\n", 88 src, dst, proto, srv, state ) ; 89 } 90 91 BEGIN { 92 lo_done = 0 ; nat_done=0 ; state = "-m state --state NEW" ; 93 while ( getline < "/etc/hosts" > 0 ) 94 { 95 if ( $1 ~ /^#/ ) continue ; 96 for( i=2; i<=NF; i++ ) 97 { 98 if ( $i ~ /^#/ ) break ; 99 hosts[$i] = $1 ; 100 } 101 } 102 ip_forward = "1" ; 103 fw_policy = "DROP" ; 104 lo_policy = "DROP" ; 105 eth_policy = "DROP" ; 106 printf( "iptables -F\niptables -X\n" ) ; 107 nb_network=0 ; 108 } 109 110 NF==0 { print $0 ; next ; } 111 $1 ~ /^#/ { print $0 ; next } 112 113 { printf( "### [%s]\n", $0); } 114 115 $1 == "policy" && $2 == "local" { 116 lo_policy = $3 ; 117 next ; 118 } 119 120 $1 == "policy" && $2 == "forward" { 121 printf( "iptables -P FORWARD %s\n", $3 ) ; 122 fw_policy = $3 ; 123 next ; 124 } 125 126 $1 == "policy" && $2 == "eth" { 127 printf( "iptables -P INPUT %s\n", $3 ) ; 128 printf( "iptables -P OUTPUT %s\n", $3 ) ; 129 eth_policy = $3 ; 130 next ; 131 } 132 133 $1 == "module" { 134 printf( "modprobe %s\n", $2 ) ; 135 next ; 136 } 137 138 $1 == "forward" { 139 ip_forward = $2 140 next ; 141 } 142 143 $1=="network" { 144 eth_i[nb_network] = $2 ; eth_n[nb_network] = $3 ; eth_a[nb_network] = $4 145 nb_network++ ; 146 next ; 147 } 148 149 $1 == "masquerade" { 150 # masquerade interface 151 do_nat() ; 152 printf( "iptables -t nat -A POSTROUTING -o %s -j MASQUERADE\n", $2 ) ; 153 next ; 154 } 155 156 $1 == "redirect" { 157 # redirect interface port proto destination_redirige port_redirige 158 do_nat() ; 159 printf( "iptables -t nat -A PREROUTING -i %s -p %s --dport %s",$2,$4,$3 ) ; 160 printf( " -j DNAT --to %s:%s\n", get_host_only($5), $6 ) ; 161 next ; 162 } 163 164 $1 == "trusted" { 165 # trusted src dst : tout traffic peut passer 166 src = get_hosts( $2, "O" ) ; 167 dst = get_hosts( $3, "N" ) ; 168 if ( ( src == "local" ) && ( dst == "local" ) ) 169 { 170 printf( "iptables -A INPUT -i lo -j ACCEPT\n" ) ; 171 printf( "iptables -A OUTPUT -o lo -j ACCEPT\n" ) ; 172 } 173 else if ( src == "local" ) 174 printf("iptables -A OUTPUT %s -j ACCEPT\n", dst ) ; 175 else if ( dst == "local" ) 176 printf("iptables -A INPUT %s -j ACCEPT\n", src ) ; 177 else 178 printf("iptables -A FORWARD %s %s -j ACCEPT\n", src, dst ) ; 179 next ; 180 } 181 182 $1 == "route_redirect" { 183 # relais de la trame / reponse ICMP par defaut de route 184 printf( "iptables -A FORWARD -i %s -o %s -p icmp -j ACCEPT\n" , $2 , $2 ) ; 185 printf( "iptables -A FORWARD -i %s -o %s -p tcp --tcp-flags SYN,FIN,ACK SYN -j ACCEPT\n" , $2 , $2 ) ; 186 printf( "iptables -A OUTPUT -o %s -p icmp --icmp-type 5 -j ACCEPT\n" , $2 ) ; 187 next ; 188 } 189 { 190 port = $3 ; proto = $4 ; 191 src = get_hosts( $1, "O" ) ; 192 dst = get_hosts( $2, "N" ) ; 193 if ( src == "local" ) do_output( dst, port, proto ) ; 194 else if ( dst == "local" ) do_input( src, port, proto ) ; 195 else do_forward( src, dst, port, proto ) ; 196 } 197 198 END { 199 state = "-m state --state ESTABLISHED,RELATED" ; 200 if ( lo_policy != "ACCEPT" ) 201 { 202 printf( "iptables -A INPUT %s -j ACCEPT\n", state ) ; 203 printf( "iptables -A OUTPUT %s -j ACCEPT\n", state ) ; 204 } 205 if ( fw_policy != "ACCEPT" ) 206 printf( "iptables -A FORWARD %s -j ACCEPT\n", state ) ; 207 208 do_nat() ; 209 210 printf( "iptables -A INPUT -i lo -j %s\n", lo_policy ) ; 211 printf( "iptables -A OUTPUT -o lo -j %s\n", lo_policy ) ; 212 printf( "iptables -A FORWARD -i lo -j %s\n", fw_policy ) ; 213 214 printf( "iptables -A INPUT -i eth0 -j %s\n", eth_policy ) ; 215 printf( "iptables -A OUTPUT -o eth0 -j %s\n", eth_policy ) ; 216 printf( "iptables -A FORWARD -i eth0 -j %s\n", fw_policy ) ; 217 218 printf( "iptables -A INPUT -i eth1 -j %s\n", eth_policy ) ; 219 printf( "iptables -A OUTPUT -o eth1 -j %s\n", eth_policy ) ; 220 printf( "iptables -A FORWARD -i eth1 -j %s\n", fw_policy ) ; 221 222 printf( "iptables -A INPUT -j %s\n", eth_policy ) ; 223 printf( "iptables -A OUTPUT -j %s\n", eth_policy ) ; 224 printf( "iptables -A FORWARD -j %s\n", fw_policy ) ; 225 printf( "echo %s > /proc/sys/net/ipv4/ip_forward\n", ip_forward ) ; 226 }
Date de génération : 22/09/2011 21:48