Advertisement
Guest User

Untitled

a guest
Nov 6th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.78 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. PATH=/usr/sbin:/sbin:/bin:/usr/bin
  4. DEFAULT="DROP"
  5. LAN_PALACIO="192.168.0.0/24"
  6. HOST_FW="192.168.0.254"
  7. ANYRED="0.0.0.0/0"
  8. LOO_RED="127.0.0.0/8"
  9. INT_IF="eth0"
  10. EXT_IF="eth1"
  11. EXT_IP="192.168.1.254"
  12.  
  13. MAN_IF="eth2"
  14. MAN_IP="192.168.5.254"
  15. MAN_LAN="192.168.5.0/24"
  16.  
  17. # flush de reglas
  18. echo -n "Flush de reglas de Firewall ..."
  19.  
  20. iptables -F
  21. iptables -X
  22. iptables -Z
  23. iptables -t nat -F
  24. iptables -t mangle -F
  25.  
  26. # por defecto todo a DROP
  27. echo -n "Aplicando Politica por predeterminada ..."
  28.  
  29. iptables -P INPUT $DEFAULT
  30. iptables -P OUTPUT ACCEPT
  31.  
  32. iptables -P FORWARD $DEFAULT
  33. ###iptables -P FORWARD ACCEPT
  34.  
  35. iptables -t nat -P PREROUTING ACCEPT
  36. iptables -t nat -P POSTROUTING ACCEPT
  37.  
  38. echo -n "Aplicando reglas del Firewall ..."
  39.  
  40. # creamos cadenas personalizadas
  41. echo -n "Creando cadenas personalizadas"
  42.  
  43. ###iptables -N SYN-FLOOD # limitar la cantidad de conexiones
  44. ###iptables -A SYN-FLOOD -m limit --limit 12/s --limit-burst 24 -j RETURN
  45. ###iptables -A SYN-FLOOD -j DROP
  46.  
  47. # definimos que logeuamos
  48. echo -n "Definimos el nivel de LOG"
  49.  
  50. ### Logueamos todo en /var/log/syslog
  51. #iptables -A INPUT -j LOG
  52. #iptables -A OUTPUT -j LOG
  53. #iptables -A FORWARD -j LOG
  54. #iptables -t nat -A PREROUTING -j LOG
  55. #iptables -t nat -A POSTROUTING -j LOG
  56.  
  57. # activamos la opcion de sync cookies, nos protejemos del ataque DOS de bomba SYNC
  58. echo "1" > /proc/sys/net/ipv4/tcp_syncookies
  59.  
  60. # opcion contra mensajes de error ICMP, evitamos ataques DOS de bomba ICMP
  61. echo "1" > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
  62.  
  63. # no respondemos al broadcast
  64. echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  65.  
  66. # Drop source routed packets
  67. echo "0" > /proc/sys/net/ipv4/conf/all/accept_source_route
  68.  
  69. # Evitamos el spoofing
  70. for interface in /proc/sys/net/ipv4/conf/*/rp_filter; do
  71. echo "1" > ${interface}
  72. done
  73.  
  74. # Los ICMPs redirigidos que pueden alterar la tabla de rutas.
  75. for interface in /proc/sys/net/ipv4/conf/*/accept_redirects; do
  76. echo "0" > ${interface}
  77. done
  78.  
  79. # No guardamos registros de los marcianos.
  80. echo "1" > /proc/sys/net/ipv4/conf/all/log_martians
  81.  
  82. # Denegamos el Broadcast de NetBIOS
  83. iptables -A FORWARD -p tcp --sport 137:139 -o $EXT_IF -j DROP
  84. iptables -A FORWARD -p udp --sport 137:139 -o $EXT_IF -j DROP
  85. iptables -A OUTPUT -p tcp --sport 137:139 -o $EXT_IF -j DROP
  86. iptables -A OUTPUT -p udp --sport 137:139 -o $EXT_IF -j DROP
  87.  
  88. # Evitamos IP Spoofing via eth1 (dat1)
  89. # Cuando el Firewall dependa del Micronet SP891 solo se permitira el tafico desde la ip privada del Micronet
  90. iptables -A INPUT -i $EXT_IF -s 172.16.0.0/12 -j DROP
  91. iptables -A INPUT -i $EXT_IF -s 192.168.0.0/24 -j DROP
  92.  
  93. # Chequeamos ataque DOS
  94. ###iptables -t nat -A PREROUTING -i $EXT_IF -d $EXT_IP -p tcp --syn -j SYN-FLOOD
  95.  
  96. # Denegamos TeamViewer
  97. iptables -A FORWARD -p tcp --dport 5938 -o $EXT_IF -j DROP
  98.  
  99. # Activamos el Masquerade (NAT) para la red local
  100. echo 1 > /proc/sys/net/ipv4/ip_forward
  101. iptables -t nat -A POSTROUTING -s $LAN_PALACIO -o $EXT_IF -j MASQUERADE
  102.  
  103. ############### VER ESTA REGLA ###############################
  104. ############### por ahora hay que dejarla porque en la casado usan pop3
  105. # Activamos el Masquerade (NAT) para la red MAN
  106. iptables -t nat -A POSTROUTING -s $MAN_LAN -o $EXT_IF -j MASQUERADE
  107.  
  108. ### Permitimos accesos desde lo a la maquina local
  109. iptables -A INPUT -i lo -j ACCEPT
  110. iptables -A OUTPUT -o lo -j ACCEPT
  111. iptables -A INPUT -i lo -s $LOO_RED -d $LOO_RED -j ACCEPT
  112. iptables -A OUTPUT -o lo -s $LOO_RED -d $LOO_RED -j ACCEPT
  113.  
  114. ### Permitimos el PostEnrutado de paquetes enviados localmente
  115. iptables -t nat -A POSTROUTING -o lo -s $LOO_RED -j ACCEPT
  116.  
  117. ### Denegamos el acceso al 8080 para evitar que salteen el DANSGUARDIAN
  118. iptables -A INPUT -p tcp --dport 8080 -j DROP
  119.  
  120. #######################################
  121. ##### Seccion Ingreso de Paquetes #####
  122. #######################################
  123.  
  124. # Aceptamos conexiones nuevas desde la red interna
  125. iptables -A INPUT -i $INT_IF -m state --state NEW -j ACCEPT
  126.  
  127. # Aceptamos conexiones establecidas o relacionadas desde el exterior
  128. iptables -A INPUT -i $EXT_IF -m state --state ESTABLISHED,RELATED -j ACCEPT
  129.  
  130. # Logueamos las conexiones INVALIDas
  131. iptables -A INPUT -p tcp -m state --state INVALID -j LOG --log-prefix "[FW: Paquete Invalido]"
  132.  
  133. # Aceptamos conexiones a los puertos 80 y 3128 desde la red interna
  134. iptables -A INPUT -i $INT_IF -s $LAN_PALACIO -p tcp --dport 80 -j ACCEPT
  135. iptables -A INPUT -i $INT_IF -s $LAN_PALACIO -p tcp --dport 3128 -j ACCEPT
  136.  
  137.  
  138. # Aceptamos conexiones a los puertos 80 y 3128 desde la red MAN
  139. iptables -A INPUT -i $MAN_IF -p tcp --dport 80 -j ACCEPT
  140. iptables -A INPUT -i $MAN_IF -p tcp --dport 3128 -j ACCEPT
  141. ## tengo que ver si habilito 110 y 25 pero necesito el 53
  142.  
  143. ### 20141020 ########################################################
  144. ### habilitamos el acceso ssh y tcp para el sistema de sergio
  145. ### no usamos la cadena INPUT porque los paquetes pasan hasta la LAN
  146. ### en el caso del 80 y del 3128 si se usa INPUT porque salen directo hacia internet
  147.  
  148.  
  149. ### ssh para oficina de inspeccion
  150. ###iptables -A INPUT -i $MAN_IF -p tcp --dport 2222 -j ACCEPT
  151.  
  152. ### tcp para oficina de personal externa
  153. #iptables -A INPUT -i $MAN_IF -p tcp --dport 2510 -j ACCEPT
  154. #iptables -A INPUT -i $MAN_IF -p tcp --dport 2520 -j ACCEPT
  155. #iptables -A INPUT -i $MAN_IF -p tcp --dport 2530 -j ACCEPT
  156. ######################################################################
  157.  
  158.  
  159. ### Aceptamos conexiones DNS
  160. ###iptables -A INPUT -s $ANYRED -i $EXT_IF -p udp -m udp --sport 53 --dport 1024:65535 -j ACCEPT
  161. iptables -A INPUT -m state --state ESTABLISHED,RELATED -s $ANYRED -i $EXT_IF -p udp -m udp --sport 53 --dport 1024:65535 -j ACCEPT
  162. iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -d $ANYRED -o $EXT_IF -p udp -m udp --dport 53 --sport 1024:65535 -j ACCEPT
  163.  
  164. echo -n "Permitimos Ping Pong INPUT,OUTPUT"
  165.  
  166. ### Permitimos paquetes ICMP (ping, traceroute) con limites para evitar DOS
  167. ### Aceptamos ping y pong
  168. iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
  169. iptables -A OUTPUT -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
  170. iptables -A INPUT -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
  171. iptables -A OUTPUT -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
  172.  
  173. ### Aceptamos redirecciones
  174. iptables -A INPUT -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
  175. iptables -A OUTPUT -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
  176.  
  177. ### Aceptamos tiempo excedido
  178. iptables -A INPUT -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
  179. iptables -A OUTPUT -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
  180.  
  181. ### Aceptamos destino inalcanzable
  182. iptables -A INPUT -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
  183. iptables -A OUTPUT -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
  184.  
  185. echo -n "Habilitamos los puertos para eth3 3128, 110 y 425"
  186.  
  187. # permitimos el forward solo hacia inet
  188. iptables -A FORWARD -i eth2 -o eth1 -j ACCEPT
  189.  
  190.  
  191. echo -n "Proxy Transparente..."
  192.  
  193. ### Proxy Transparente
  194. iptables -t nat -A PREROUTING -i eth0 -s $LAN_PALACIO -p tcp --dport 80 -j REDIRECT --to-port 3128
  195. iptables -t nat -A PREROUTING -i eth2 -s $MAN_LAN -p tcp --dport 80 -j REDIRECT --to-port 3128
  196.  
  197. ### Aceptamos conexiones DNS
  198. ###iptables -A INPUT -s $ANYRED -i $EXT_IF -p udp -m udp --sport 53 --dport 1024:65535 -j ACCEPT
  199. iptables -A INPUT -m state --state ESTABLISHED,RELATED -s $ANYRED -i $EXT_IF -p udp -m udp --sport 53 --dport 1024:65535 -j ACCEPT
  200. iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -d $ANYRED -o $EXT_IF -p udp -m udp --dport 53 --sport 1024:65535 -j ACCEPT
  201.  
  202. echo -n "Permitimos Ping Pong INPUT,OUTPUT"
  203.  
  204. ### Permitimos paquetes ICMP (ping, traceroute) con limites para evitar DOS
  205. ### Aceptamos ping y pong
  206. iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
  207. iptables -A OUTPUT -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
  208. iptables -A INPUT -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
  209. iptables -A OUTPUT -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
  210.  
  211. ### Aceptamos redirecciones
  212. iptables -A INPUT -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
  213. iptables -A OUTPUT -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
  214.  
  215. ### Aceptamos tiempo excedido
  216. iptables -A INPUT -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
  217. iptables -A OUTPUT -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
  218.  
  219. ### Aceptamos destino inalcanzable
  220. iptables -A INPUT -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
  221. iptables -A OUTPUT -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
  222.  
  223. echo -n "Habilitamos los puertos para eth3 3128, 110 y 425"
  224.  
  225. # permitimos el forward solo hacia inet
  226. iptables -A FORWARD -i eth2 -o eth1 -j ACCEPT
  227.  
  228.  
  229. echo -n "Proxy Transparente..."
  230.  
  231. ### Proxy Transparente
  232. iptables -t nat -A PREROUTING -i eth0 -s $LAN_PALACIO -p tcp --dport 80 -j REDIRECT --to-port 3128
  233. iptables -t nat -A PREROUTING -i eth2 -s $MAN_LAN -p tcp --dport 80 -j REDIRECT --to-port 3128
  234.  
  235.  
  236.  
  237. iptables -A INPUT -i $MAN_IF -p tcp --dport 2222 -j ACCEPT
  238. iptables -A FORWARD -s $MAN_LAN -p tcp --dport 2222 -j ACCEPT
  239. iptables -A OUTPUT -d $ANYRED -o $EXT_IF -p tcp --dport 2222 --sport 1024:65535 -j ACCEPT
  240.  
  241.  
  242.  
  243. ### Proxy Transparente POP3
  244. iptables -A FORWARD -s $MAN_LAN -p tcp --dport 425 -j ACCEPT
  245.  
  246. ### Allow access from Sistemas on ssh port
  247.  
  248. # Logueamos las conexiones SSH
  249. iptables -A INPUT -p tcp -s $SISTEMAS -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
  250. iptables -A INPUT -p tcp -s $VMREMOTA -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
  251. ###iptables -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
  252.  
  253. # Permitimos las conexiones SSH desde $SISTEMAS
  254. iptables -A INPUT -i $INT_IF -p tcp -s $SISTEMAS -m tcp --dport 22 -j ACCEPT
  255. iptables -A INPUT -i $INT_IF -p tcp -s $VMREMOTA -m tcp --dport 22 -j ACCEPT
  256.  
  257. echo -n "Reenvios..."
  258.  
  259. ### Aceptamos algunos reenvios
  260. # Permitimos paquetes ICMP (ping, traceroute)
  261. #+ con limites para evitar ataques DoS
  262. # Aceptamos ping y pong
  263. iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
  264. iptables -A FORWARD -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
  265. # Aceptamos redirecciones
  266. iptables -A FORWARD -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
  267. # Aceptamos tiempo excedido
  268. iptables -A FORWARD -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
  269. # Aceptamos destino inalcanzabe
  270. iptables -A FORWARD -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
  271. # Aceptamos todas en INT_IF
  272. iptables -t nat -A PREROUTING -i $INT_IF -p icmp --icmp-type any -m limit --limit 2/s -j ACCEPT
  273. iptables -t nat -A POSTROUTING -o $INT_IF -p icmp --icmp-type any -m limit --limit 2/s -j ACCEPT
  274.  
  275. echo -n "Conexiones salientes..."
  276.  
  277. ### Aceptamos conexiones salientes
  278. # Permitimos cualquier salida tcp desde la propia maquina
  279. iptables -A OUTPUT -o $EXT_IF -p tcp -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
  280. # y conexiones entrantes relacionadas
  281. iptables -A INPUT -i $EXT_IF -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
  282.  
  283. # Permitimos el reenvio de paquetes enviados desde la LAN
  284. iptables -A FORWARD -i $INT_IF -j ACCEPT
  285. # ... y conexiones salientes relacionadas
  286. iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  287.  
  288. # Permitimos el NAT de paquetes enviados desde la LAN
  289. iptables -t nat -A PREROUTING -i $INT_IF -j ACCEPT
  290. # ... y conexiones salientes relacionadas
  291. iptables -t nat -A PREROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
  292.  
  293. # Permitimos el NAT de paquetes enviados desde Inet hacia la ip publica
  294. iptables -t nat -A PREROUTING -i $EXT_IF -d $EXT_IP -j ACCEPT
  295. # ... y conexiones salientes relacionadas
  296. iptables -t nat -A PREROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
  297.  
  298. echo -n "NAT desde ippublica a Inet..."
  299.  
  300. # Permitimos el NAT de paquetes enviados desde la ip publica hacia Inet
  301. iptables -t nat -A POSTROUTING -o $EXT_IF -s $EXT_IP -j ACCEPT
  302. # ... y conexiones salientes relacionadas
  303. iptables -t nat -A POSTROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
  304.  
  305. # Permitimos el PostEnrutado de paquets enviados localmente
  306. iptables -t nat -A POSTROUTING -o $INT_IF -s $LAN_PALACIO -j ACCEPT
  307.  
  308. # Permitimos salidas por ftp para las descargar de los repositorios de Debian
  309. #iptables -A
  310.  
  311.  
  312. ### Proxy Transparente POP3
  313. iptables -A FORWARD -s $MAN_LAN -p tcp --dport 425 -j ACCEPT
  314.  
  315. ### Allow access from Sistemas on ssh port
  316.  
  317. # Logueamos las conexiones SSH
  318. iptables -A INPUT -p tcp -s $SISTEMAS -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
  319. iptables -A INPUT -p tcp -s $VMREMOTA -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
  320. ###iptables -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
  321.  
  322. # Permitimos las conexiones SSH desde $SISTEMAS
  323. iptables -A INPUT -i $INT_IF -p tcp -s $SISTEMAS -m tcp --dport 22 -j ACCEPT
  324. iptables -A INPUT -i $INT_IF -p tcp -s $VMREMOTA -m tcp --dport 22 -j ACCEPT
  325.  
  326. echo -n "Reenvios..."
  327.  
  328. ### Aceptamos algunos reenvios
  329. # Permitimos paquetes ICMP (ping, traceroute)
  330. #+ con limites para evitar ataques DoS
  331. # Aceptamos ping y pong
  332. iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
  333. iptables -A FORWARD -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
  334. # Aceptamos redirecciones
  335. iptables -A FORWARD -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
  336. # Aceptamos tiempo excedido
  337. iptables -A FORWARD -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
  338. # Aceptamos destino inalcanzabe
  339. iptables -A FORWARD -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
  340. # Aceptamos todas en INT_IF
  341. iptables -t nat -A PREROUTING -i $INT_IF -p icmp --icmp-type any -m limit --limit 2/s -j ACCEPT
  342. iptables -t nat -A POSTROUTING -o $INT_IF -p icmp --icmp-type any -m limit --limit 2/s -j ACCEPT
  343.  
  344. echo -n "Conexiones salientes..."
  345.  
  346. ### Aceptamos conexiones salientes
  347. # Permitimos cualquier salida tcp desde la propia maquina
  348. iptables -A OUTPUT -o $EXT_IF -p tcp -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
  349. # y conexiones entrantes relacionadas
  350. iptables -A INPUT -i $EXT_IF -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
  351.  
  352. # Permitimos el reenvio de paquetes enviados desde la LAN
  353. iptables -A FORWARD -i $INT_IF -j ACCEPT
  354. # ... y conexiones salientes relacionadas
  355. iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  356.  
  357. # Permitimos el NAT de paquetes enviados desde la LAN
  358. iptables -t nat -A PREROUTING -i $INT_IF -j ACCEPT
  359. # ... y conexiones salientes relacionadas
  360. iptables -t nat -A PREROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
  361.  
  362. # Permitimos el NAT de paquetes enviados desde Inet hacia la ip publica
  363. iptables -t nat -A PREROUTING -i $EXT_IF -d $EXT_IP -j ACCEPT
  364. # ... y conexiones salientes relacionadas
  365. iptables -t nat -A PREROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
  366.  
  367. echo -n "NAT desde ippublica a Inet..."
  368.  
  369. # Permitimos el NAT de paquetes enviados desde la ip publica hacia Inet
  370. iptables -t nat -A POSTROUTING -o $EXT_IF -s $EXT_IP -j ACCEPT
  371. # ... y conexiones salientes relacionadas
  372. iptables -t nat -A POSTROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
  373.  
  374. # Permitimos el PostEnrutado de paquets enviados localmente
  375. iptables -t nat -A POSTROUTING -o $INT_IF -s $LAN_PALACIO -j ACCEPT
  376.  
  377. # Permitimos salidas por ftp para las descargar de los repositorios de Debian
  378. #iptables -A OUTPUT -o eth0 -p tcp --dport 21 -j ACCEPT
  379. #iptables -A OUTPUT -o eth0 -p tcp --dport 20 -j ACCEPT
  380.  
  381. iptables -A INPUT -i eth1 -j DROP
  382. iptables -A INPUT -i eth3 -j DROP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement