Advertisement
mfillpot

firewall

Oct 1st, 2011
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 20.35 KB | None | 0 0
  1. #!/bin/bash
  2. # iptables firewall script by matthew fillpot
  3. #  Partially derived from AlienBOB's firewall generator
  4. #  http://connie.slackware.com/~alien/efg/index.php
  5.  
  6. # Local Settings
  7. #
  8.  
  9. # sysctl location.  If set, it will use sysctl to adjust the kernel parameters.
  10. # If this is set to the empty string (or is unset), the use of sysctl
  11. # is disabled.
  12.  
  13. SYSCTL="/sbin/sysctl -w"
  14.  
  15. # To echo the value directly to the /proc file instead
  16. # SYSCTL=""
  17.  
  18. # IPTables Location - adjust if needed
  19.  
  20. IPT="/usr/sbin/iptables"
  21. IPTS="/usr/sbin/iptables-save"
  22. IPTR="/usr/sbin/iptables-restore"
  23.  
  24. # Internet Interface
  25. INET_IFACE="wlan0"
  26.  
  27. # Localhost Interface
  28.  
  29. LO_IFACE="lo"
  30. LO_IP="127.0.0.1"
  31. LOC_SUB="192.168.1.1/24"
  32.  
  33. KMACS=(00:00:00:00:00:00 \
  34.   00:00:00:00:00:00)
  35.  
  36. # Color_options
  37. txtblk=$'\e[0;30m' # Black - Regular
  38. txtred=$'\e[0;31m' # Red
  39. txtgrn=$'\e[0;32m' # Green
  40. txtylw=$'\e[0;33m' # Yellow
  41. txtblu=$'\e[0;34m' # Blue
  42. txtpur=$'\e[0;35m' # Purple
  43. txtcyn=$'\e[0;36m' # Cyan
  44. txtwht=$'\e[0;37m' # White
  45. bldblk=$'\e[1;30m' # Black - Bold
  46. bldred=$'\e[1;31m' # Red
  47. bldgrn=$'\e[1;32m' # Green
  48. bldylw=$'\e[1;33m' # Yellow
  49. bldblu=$'\e[1;34m' # Blue
  50. bldpur=$'\e[1;35m' # Purple
  51. bldcyn=$'\e[1;36m' # Cyan
  52. bldwht=$'\e[1;37m' # White
  53. unkblk=$'\e[4;30m' # Black - Underline
  54. undred=$'\e[4;31m' # Red
  55. undgrn=$'\e[4;32m' # Green
  56. undylw=$'\e[4;33m' # Yellow
  57. undblu=$'\e[4;34m' # Blue
  58. undpur=$'\e[4;35m' # Purple
  59. undcyn=$'\e[4;36m' # Cyan
  60. undwht=$'\e[4;37m' # White
  61. bakblk=$'\e[40m'   # Black - Background
  62. bakred=$'\e[41m'   # Red
  63. bakgrn=$'\e[42m'   # Green
  64. bakylw=$'\e[43m'   # Yellow
  65. bakblu=$'\e[44m'   # Blue
  66. bakpur=$'\e[45m'   # Purple
  67. bakcyn=$'\e[46m'   # Cyan
  68. bakwht=$'\e[47m'   # White
  69. NORMAL=$'\e[0m'
  70.  
  71. # SET COLORS
  72. #RED=$'\e[31;01m'
  73. URGENT=${bakred}${bldwht}
  74. KERNCOLOR=${bldblu}
  75. PROCCOLOR=${bakblu}${bldwht}
  76. CAUTION=${bakylw}${bldwht}
  77.  
  78. # Save and Restore arguments handled here
  79. #if [ "$1" = "save" ]
  80. #then
  81. #   echo -n "Saving firewall to /etc/sysconfig/iptables ... "
  82. #   $IPTS > /etc/sysconfig/iptables
  83. #   echo "done"
  84. #   exit 0
  85. #elif [ "$1" = "restore" ]
  86. #then
  87. #   echo -n "Restoring firewall from /etc/sysconfig/iptables ... "
  88. #   $IPTR < /etc/sysconfig/iptables
  89. #   echo "done"
  90. #   exit 0
  91. #fi
  92.  
  93. #######################################################
  94.  
  95. fw_start() {
  96.  
  97. # Display a message stating the firewall is starting
  98.   echo "${PROCCOLOR}Firewall is being configured.....${NORMAL}"
  99.  
  100. # Flush the current rules
  101. flush_rules
  102.  
  103. # load the appropriate kernel modules
  104. load_modules
  105.  
  106. # load the necessary kernel bits
  107. load_kernel_bits
  108.  
  109. # Make the user Chains
  110. make_chains
  111.  
  112. # Allow all on localhost interface
  113. #$IPT -A INPUT -p ALL -i $LO_IFACE -j ACCEPT
  114.  
  115. # Accept Established Connections
  116. $IPT -A INPUT -p ALL  -m state --state ESTABLISHED,RELATED -j ACCEPT
  117.  
  118. # check for bad packets
  119. check_bad_packets
  120. # Drop bad packets
  121. $IPT -A INPUT -p ALL -j bad_packets
  122.  
  123. # check the icmp packets
  124. check_icmp
  125.  
  126. # Open ports for selected services
  127. check_services
  128.  
  129. ####    INPUT    ###############################################
  130.  
  131. # Add the rules for specific packet types
  132. udp_out
  133. udp_in
  134. tcp_out
  135. tcp_in
  136.  
  137. # Route the rest to the appropriate user chain
  138. $IPT -A INPUT -p TCP -j tcp_inbound
  139. $IPT -A INPUT -p UDP -j udp_inbound
  140. $IPT -A INPUT -p ICMP -j icmp_packets
  141.  
  142. # Drop without logging broadcasts that get this far.
  143. # Cuts down on log clutter.
  144. # Comment this line if testing new rules that impact
  145. # broadcast protocols.
  146. $IPT -A INPUT -m pkttype --pkt-type broadcast -j DROP
  147.  
  148. # Log packets that still don't match
  149. #$IPT -A INPUT -j LOG --log-prefix "fp=INPUT:99 a=DROP "
  150.  
  151.  
  152. ####    OUTPUT   ###############################################
  153. # Generally trust the firewall on output
  154.  
  155. $IPT -A OUTPUT -p TCP -j tcp_outbound
  156. $IPT -A OUTPUT -p UDP -j udp_outbound
  157.  
  158. # However, invalid icmp packets need to be dropped
  159. # to prevent a possible exploit.
  160. $IPT -A OUTPUT -m state -p icmp --state INVALID -j DROP
  161.  
  162. # To internet
  163. $IPT -A OUTPUT -p ALL -o $INET_IFACE -j ACCEPT
  164.  
  165. # Log packets that still don't match
  166. #$IPT -A OUTPUT -j LOG --log-prefix "fp=OUTPUT:99 a=DROP "
  167.  
  168.  
  169.  
  170. ###############################################################################
  171.  
  172. # block all other incoming traffic
  173. $IPT -A INPUT -j DROP
  174.  
  175. # Display a message stating the firewall has been started
  176.   echo "${PROCCOLOR}Firewall Started.....${NORMAL}"
  177.  
  178. }
  179.  
  180. fw_stop() {
  181.  
  182.   # Flush all rules
  183. flush_rules
  184.  
  185.   echo "${URGENT}Firewall Stopped.....${NORMAL}"
  186.  
  187. }
  188.  
  189. flush_rules() {
  190. #echo "Start flush"
  191.  
  192. # Flush all rules
  193.   $IPT -F
  194.   for CHN in `$IPT -L|grep references|cut -d " " -f 2`
  195.   do
  196.     $IPT -X $CHN
  197.   done
  198.  
  199. #echo "Flush Complete"
  200. }
  201.  
  202. #######################################################
  203.  
  204. load_modules() {
  205. # Load Modules
  206. #
  207.  
  208. # You should uncomment the line below and run it the first time just to
  209. # ensure all kernel module dependencies are OK.  There is no need to run
  210. # every time, however.
  211.  
  212. # /sbin/depmod -a
  213.  
  214. # Unless you have kernel module auto-loading disabled, you should not
  215. # need to manually load each of these modules.  Other than ip_tables,
  216. # ip_conntrack, and some of the optional modules, I've left these
  217. # commented by default.  Uncomment if you have any problems or if
  218. # you have disabled module autoload.  Note that some modules must
  219. # be loaded by another kernel module.
  220.  
  221. # core netfilter module
  222. load_module ip_tables
  223.  
  224. # the stateful connection tracking module
  225. #/sbin/modprobe ip_conntrack
  226. load_module nf_conntrack
  227. load_module nf_conntrack_ipv4
  228.  
  229. # filter table module
  230. # /sbin/modprobe iptable_filter
  231.  
  232. # mangle table module
  233. # /sbin/modprobe iptable_mangle
  234.  
  235. # nat table module
  236. # /sbin/modprobe iptable_nat
  237.  
  238. # LOG target module
  239. # /sbin/modprobe ipt_LOG
  240.  
  241. # This is used to limit the number of packets per sec/min/hrs
  242. load_module ipt_limit
  243.  
  244. # masquerade target module
  245. # /sbin/modprobe ipt_MASQUERADE
  246.  
  247. # filter using owner as part of the match
  248. # /sbin/modprobe ipt_owner
  249.  
  250. # REJECT target drops the packet and returns an ICMP response.
  251. # The response is configurable.  By default, connection refused.
  252. # /sbin/modprobe ipt_REJECT
  253.  
  254. # This target allows packets to be marked in the mangle table
  255. # /sbin/modprobe ipt_mark
  256.  
  257. # This target affects the TCP MSS
  258. # /sbin/modprobe ipt_tcpmss
  259.  
  260. # This match allows multiple ports instead of a single port or range
  261. # /sbin/modprobe multiport
  262.  
  263. # This match checks against the TCP flags
  264. #/sbin/modprobe ipt_state
  265. load_module ipt_state
  266.  
  267. # the module for full irc connection tracking
  268. #/sbin/modprobe ip_conntrack_irc
  269. load_module ip_conntrack_irc
  270.  
  271. # ftp base modules
  272. load_module nf_conntrack_ftp
  273. load_module ip_vs_ftp
  274. load_module nf_nat_ftp
  275.  
  276. #####################################################################
  277. # Kernel Parameter Configuration
  278. #
  279. # See http://ipsysctl-tutorial.frozentux.net/chunkyhtml/index.html
  280. # for a detailed tutorial on sysctl and the various settings
  281. # available.
  282.  
  283. # Required to enable IPv4 forwarding.
  284. # Redhat users can try setting FORWARD_IPV4 in /etc/sysconfig/network to true
  285. # Alternatively, it can be set in /etc/sysctl.conf
  286. #if [ "$SYSCTL" = "" ]
  287. #then
  288. #    echo "1" > /proc/sys/net/ipv4/ip_forward
  289. #else
  290. #    $SYSCTL net.ipv4.ip_forward="1"
  291. #fi
  292.  
  293. # This option allows a subnet to be firewalled with a single IP address.
  294. # It's used to build a DMZ.  Since that's not a focus of this firewall
  295. # script, it's not enabled by default, but is included for reference.
  296. # See: http://www.sjdjweis.com/linux/proxyarp/
  297. #if [ "$SYSCTL" = "" ]
  298. #then
  299. #    echo "1" > /proc/sys/net/ipv4/conf/all/proxy_arp
  300. #else
  301. #    $SYSCTL net.ipv4.conf.all.proxy_arp="1"
  302. #fi
  303.  
  304. # This option logs packets from impossible addresses.
  305. #if [ "$SYSCTL" = "" ]
  306. #then
  307. #    echo "1" > /proc/sys/net/ipv4/conf/all/log_martians
  308. #else
  309. #    $SYSCTL net.ipv4.conf.all.log_martians="1"
  310. #fi
  311.  
  312. }
  313.  
  314. load_module() {
  315.   if [ -z "$(cat /proc/modules|cut -d " " -f 1|grep $1)" ]; then
  316.     echo "${KERNCOLOR}Loading the $1 module......${NORMAL}"
  317.     /sbin/modprobe $1
  318.   fi
  319. }
  320.  
  321. load_kernel_bits() {
  322. # This enables dynamic address hacking.
  323. # This may help if you have a dynamic IP address \(e.g. slip, ppp, dhcp\).
  324. modlkb /proc/sys/net/ipv4/ip_dynaddr 1
  325.  
  326. # Drop ICMP echo-request messages sent to broadcast or multicast addresses
  327. # This kernel parameter instructs the kernel to ignore all ICMP
  328. # echo requests sent to the broadcast address.  This prevents
  329. # a number of smurfs and similar DoS nasty attacks.
  330. modlkb /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts 1
  331.  
  332. # Drop source routed packets
  333. # This option can be used to accept or refuse source routed
  334. # packets.  It is usually on by default, but is generally
  335. # considered a security risk.  This option turns it off.
  336. modlkb /proc/sys/net/ipv4/conf/all/accept_source_route 1
  337.  
  338. # Enable TCP SYN cookie protection from SYN floods
  339. modlkb /proc/sys/net/ipv4/tcp_syncookies 1
  340.  
  341. # Don't accept ICMP redirect messages
  342. # This option can disable ICMP redirects.  ICMP redirects
  343. # are generally considered a security risk and shouldn't be
  344. # needed by most systems.
  345. modlkb /proc/sys/net/ipv4/conf/all/accept_redirects 1
  346.  
  347. # However, we'll ensure the secure_redirects option is on instead.
  348. # This option accepts only from gateways in the default gateways list.
  349. modlkb /proc/sys/net/ipv4/conf/all/secure_redirects 1
  350.  
  351. # Don't send ICMP redirect messages
  352. modlkb /proc/sys/net/ipv4/conf/all/send_redirects 0
  353.  
  354. # Enable source address spoofing protection
  355. # This enables source validation by reversed path according to RFC1812.
  356. # In other words, did the response packet originate from the same interface
  357. # through which the source packet was sent?  It's recommended for single-homed
  358. # systems and routers on stub networks.  Since those are the configurations
  359. # this firewall is designed to support, I turn it on by default.
  360. # Turn it off if you use multiple NICs connected to the same network.
  361. # 1=on , 0=off
  362. modlkb /proc/sys/net/ipv4/conf/all/rp_filter 1
  363.  
  364. # Log packets with impossible source addresses
  365. #echo 1 > /proc/sys/net/ipv4/conf/all/log_martians
  366. }
  367.  
  368. modlkb() {
  369.   if [ ! "$(echo $2)" = "$1" ]; then
  370.     echo $2 > $1
  371.     #echo "$1 should be $2"
  372.     #cat $1
  373.   fi
  374. }
  375.  
  376. make_chains() {
  377. # User-Specified Chains
  378.  
  379. # Create a chain to filter INVALID packets
  380.   $IPT -N bad_packets
  381.  
  382. # Create another chain to filter bad tcp packets
  383. #  $IPT -N bad_tcp_packets
  384.  
  385. # Create separate chains for icmp, tcp (incoming and outgoing),
  386. # and incoming udp packets.
  387.   $IPT -N icmp_packets
  388.  
  389. # Used for UDP packets inbound from the Internet
  390.   $IPT -N udp_inbound
  391.  
  392. # Used to block outbound UDP services from internal network
  393. # Default to allow all
  394.   $IPT -N udp_outbound
  395.  
  396. # Used to allow inbound services if desired
  397. # Default fail except for established sessions
  398.   $IPT -N tcp_inbound
  399.  
  400. # Used to block outbound services from internal network
  401. # Default to allow all
  402.   $IPT -N tcp_outbound
  403. }
  404.  
  405. check_bad_packets() {
  406. # bad_packets chain
  407. #
  408.  
  409. # Drop INVALID packets immediately
  410. #$IPT -A bad_packets -p ALL -m state --state INVALID -j LOG \
  411. #    --log-prefix "fp=bad_packets:1 a=DROP "
  412.  
  413. $IPT -A bad_packets -p ALL -m state --state INVALID -j DROP
  414.  
  415. # Then check the tcp packets for additional problems
  416. #$IPT -A bad_packets -p tcp -j bad_tcp_packets
  417.  
  418. # All good, so return
  419. $IPT -A bad_packets -p ALL -j RETURN
  420.  
  421. # bad_tcp_packets chain
  422. #
  423. # All tcp packets will traverse this chain.
  424. # Every new connection attempt should begin with
  425. # a syn packet.  If it doesn't, it is likely a
  426. # port scan.  This drops packets in state
  427. # NEW that are not flagged as syn packets.
  428.  
  429.  
  430. #$IPT -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG \
  431. #    --log-prefix "fp=bad_tcp_packets:1 a=DROP "
  432. #$IPT -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP
  433.  
  434. #$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL NONE -j LOG \
  435. #    --log-prefix "fp=bad_tcp_packets:2 a=DROP "
  436. #$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL NONE -j DROP
  437.  
  438. #$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL ALL -j LOG \
  439. #    --log-prefix "fp=bad_tcp_packets:3 a=DROP "
  440. #$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL ALL -j DROP
  441.  
  442. #$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL FIN,URG,PSH -j LOG \
  443. #    --log-prefix "fp=bad_tcp_packets:4 a=DROP "
  444. #$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
  445. #
  446. #$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j LOG \
  447. #    --log-prefix "fp=bad_tcp_packets:5 a=DROP "
  448. #$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
  449. #
  450. #$IPT -A bad_tcp_packets -p tcp --tcp-flags SYN,RST SYN,RST -j LOG \
  451. #    --log-prefix "fp=bad_tcp_packets:6 a=DROP "
  452. #$IPT -A bad_tcp_packets -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
  453.  
  454. #$IPT -A bad_tcp_packets -p tcp --tcp-flags SYN,FIN SYN,FIN -j LOG \
  455. #    --log-prefix "fp=bad_tcp_packets:7 a=DROP "
  456. #$IPT -A bad_tcp_packets -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
  457.  
  458. }
  459.  
  460. check_icmp() {
  461. # icmp_packets chain
  462. #
  463. # This chain is for inbound (from the Internet) icmp packets only.
  464. # Type 8 (Echo Request) is not accepted by default
  465. # Enable it if you want remote hosts to be able to reach you.
  466. # 11 (Time Exceeded) is the only one accepted
  467. # that would not already be covered by the established
  468. # connection rule.  Applied to INPUT on the external interface.
  469. #
  470. # See: http://www.ee.siue.edu/~rwalden/networking/icmp.html
  471. # for more info on ICMP types.
  472. #
  473. # Note that the stateful settings allow replies to ICMP packets.
  474. # These rules allow new packets of the specified types.
  475.  
  476. # ICMP packets should fit in a Layer 2 frame, thus they should
  477. # never be fragmented.  Fragmented ICMP packets are a typical sign
  478. # of a denial of service attack.
  479. $IPT -A icmp_packets --fragment -p ICMP -j LOG \
  480.     --log-prefix "fp=icmp_packets:1 a=DROP "
  481. $IPT -A icmp_packets --fragment -p ICMP -j DROP
  482.  
  483. # Echo - uncomment to allow your system to be pinged.
  484. # Uncomment the LOG command if you also want to log PING attempts
  485. #
  486. # $IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j LOG \
  487. #    --log-prefix "fp=icmp_packets:2 a=ACCEPT "
  488. # $IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT
  489.  
  490. # By default, however, drop pings without logging. Blaster
  491. # and other worms have infected systems blasting pings.
  492. # Comment the line below if you want pings logged, but it
  493. # will likely fill your logs.
  494. $IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j DROP
  495.  
  496. # Time Exceeded
  497. $IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
  498.  
  499. # Not matched, so return so it will be logged
  500. $IPT -A icmp_packets -p ICMP -j DROP
  501.  
  502. ############ MY RULES ############################
  503. # Allow pings per minute to block ping DOS attacks
  504. #  $IPT -A INPUT -p icmp --icmp-type echo-request -m limit --limit 4/m -j ACCEPT
  505.  
  506. # Allow all echo replies including destination unreachable and time exceeded
  507. #  $IPT -A icmp_packets -p icmp --icmp-type echo-reply -j ACCEPT
  508. #  $IPT -A icmp_packets -p icmp --icmp-type destination-unreachable -j ACCEPT
  509. #  $IPT -A icmp_packets -p icmp --icmp-type time-exceeded -j ACCEPT
  510.  
  511. # Block all other icmp traffic
  512. #  $IPT -A icmp_packets -p icmp -j DROP
  513.  
  514.  
  515. }
  516.  
  517. udp_out() {
  518. # udp_outbound chain
  519. #
  520. # This chain is used with a private network to prevent forwarding for
  521. # UDP requests on specific protocols.  Applied to the FORWARD rule from
  522. # the internal network.  Ends with an ACCEPT
  523.  
  524.  
  525. # No match, so ACCEPT
  526. $IPT -A udp_outbound -p UDP -s 0/0 -j ACCEPT
  527.  
  528. }
  529.  
  530. udp_in() {
  531. # udp_inbound chain
  532. #
  533. # This chain describes the inbound UDP packets it will accept.
  534. # It's applied to INPUT on the external or Internet interface.
  535. # Note that the stateful settings allow replies.
  536. # These rules are for new requests.
  537. # It drops netbios packets (windows) immediately without logging.
  538.  
  539. # Drop netbios calls
  540. # Please note that these rules do not really change the way the firewall
  541. # treats netbios connections.  Connections from the localhost and
  542. # internal interface (if one exists) are accepted by default.
  543. # Responses from the Internet to requests initiated by or through
  544. # the firewall are also accepted by default.  To get here, the
  545. # packets would have to be part of a new request received by the
  546. # Internet interface.  You would have to manually add rules to
  547. # accept these.  I added these rules because some network connections,
  548. # such as those via cable modems, tend to be filled with noise from
  549. # unprotected Windows machines.  These rules drop those packets
  550. # quickly and without logging them.  This prevents them from traversing
  551. # the whole chain and keeps the log from getting cluttered with
  552. # chatter from Windows systems.
  553. $IPT -A udp_inbound -p UDP -s 0/0 --dport 137 -j DROP
  554. $IPT -A udp_inbound -p UDP -s 0/0 --dport 138 -j DROP
  555.  
  556. # Ident requests (Port 113) must have a REJECT rule rather than the
  557. # default DROP rule.  This is the minimum requirement to avoid
  558. # long delays while connecting.  Also see the tcp_inbound rule.
  559. $IPT -A udp_inbound -p UDP -s 0/0 --dport 113 -j REJECT
  560.  
  561. # A more sophisticated configuration could accept the ident requests.
  562. # $IPT -A udp_inbound -p UDP -s 0/0 --dport 113 -j ACCEPT
  563.  
  564. # Dynamic Address
  565. # If DHCP, the initial request is a broadcast. The response
  566. # doesn't exactly match the outbound packet.  This explicitly
  567. # allow the DHCP ports to alleviate this problem.
  568. # If you receive your dynamic address by a different means, you
  569. # can probably comment this line.
  570. $IPT -A udp_inbound -p UDP -s 0/0 --source-port 67 --dport 68 \
  571.      -j ACCEPT
  572.  
  573.  
  574. # Not matched, so return for logging
  575. $IPT -A udp_inbound -p UDP -j DROP
  576.  
  577. }
  578.  
  579. tcp_out() {
  580. # tcp_outbound chain
  581. #
  582. # This chain is used with a private network to prevent forwarding for
  583. # requests on specific protocols.  Applied to the FORWARD rule from
  584. # the internal network.  Ends with an ACCEPT
  585.  
  586. # No match, so ACCEPT
  587.   $IPT -A tcp_outbound -p TCP -s 0/0 -j ACCEPT
  588. }
  589.  
  590. tcp_in() {
  591. # tcp_inbound chain
  592. #
  593. # This chain is used to allow inbound connections to the
  594. # system/gateway.  Use with care.  It defaults to none.
  595. # It's applied on INPUT from the external or Internet interface.
  596.  
  597. # Ident requests (Port 113) must have a REJECT rule rather than the
  598. # default DROP rule.  This is the minimum requirement to avoid
  599. # long delays while connecting.  Also see the tcp_inbound rule.
  600.   $IPT -A tcp_inbound -p TCP -s 0/0 --dport 113 -j REJECT
  601.  
  602. # Not matched, so return so it will be logged
  603.   $IPT -A tcp_inbound -p TCP -j DROP
  604. }
  605.  
  606. #######################################################
  607.  
  608. check_services() {
  609.  
  610. # choose services to allow
  611.  
  612. Srv=1
  613.  
  614. # For items with values listed $1 = WEB or LOCAL
  615. #  Otherwise it assumes LOCAL of WEB based upon the type of service#
  616.  
  617. # Verified services
  618. #serv_allow sshd KNOWN 666
  619. #serv_allow ftp LOCAL "ftp" "ftp"
  620. #serv_allow CUPSD LOCAL 631 631
  621. #serv_allow nfsd KNOWN "2049 42118 47181" "2049 42118 47181"
  622. #serv_allow rpc KNOWN 111 111
  623. #serv_allow VNC WEB "5900 5800 5500" "5900 5800 5500"
  624. gtalk_allow
  625. }
  626.  
  627. gtalk_allow() {
  628. GPRT=$(lsof -i|grep GoogleTal|grep LISTEN|tr -s " "|cut -d " " -f 9|cut -d ":" -f 2|cut -d "-" -f 1|uniq)
  629. if [ "$GPRT" != "" ]; then
  630.   serv_allow "Gtalk:$GPRT" WEB $GPRT
  631. fi
  632. }
  633.  
  634. serv_allow() {
  635.     #symtax - serv allow {service} {domain} {tcp ports} {udp ports}
  636.  
  637.     if [[ -x /etc/rc.d/rc.$1 || ! -f /etc/rc.d/rc.$i ]]; then
  638.  
  639.     # get tcp ports
  640.     declare -a PRTS
  641.     PORTS=($3)
  642.     for PRT in "${PORTS[@]}"
  643.     do
  644.         case "$2" in
  645.         "WEB")
  646.             $IPT -A tcp_inbound -p TCP -s 0/0 --dport $PRT -j ACCEPT
  647.             ;;
  648.         "KNOWN")
  649.             for MAC in "${KMACS[@]}"
  650.             do
  651.                 $IPT -A tcp_inbound -p TCP -s 0/0 --dport $PRT -m mac --mac-source $MAC -j ACCEPT
  652.             done
  653.             ;;
  654.         *)
  655.             $IPT -A tcp_inbound -p TCP -s $LOC_SUB --dport $PRT -j ACCEPT
  656.             ;;
  657.         esac
  658.     done
  659.  
  660.     # get the udp ports
  661.     PORTS=($4)
  662.     for PRT in "${PORTS[@]}"
  663.     do
  664.         case "$2" in
  665.         "WEB")
  666.             $IPT -A udp_inbound -p UDP -s 0/0 --dport $PRT -j ACCEPT
  667.             ;;
  668.         "KNOWN")
  669.             for MAC in "${KMACS[@]}"
  670.             do
  671.                 $IPT -A udp_inbound -p UDP -s 0/0 --dport $PRT -m mac --mac-source $MAC -j ACCEPT
  672.             done
  673.             ;;
  674.         *)
  675.             $IPT -A udp_inbound -p UDP -s $LOC_SUB --dport $PRT -j ACCEPT
  676.             ;;
  677.         esac
  678.     done
  679.    
  680.     # Write a visible notification
  681.     case "$2" in
  682.     "WEB")
  683.         echo "${URGENT}Allowing $1 for all hosts......${NORMAL}"
  684.         ;;
  685.     "KNOWN")
  686.         echo "${CAUTION}Allowing $1 for known hosts......${NORMAL}"
  687.         ;;
  688.     *)
  689.         echo "${CAUTION}Allowing $1 for local hosts......${NORMAL}"
  690.         ;;
  691.     esac   
  692.  
  693.     fi
  694. }
  695.  
  696. #######################################################
  697.  
  698. case "$1" in
  699.   start)
  700.     fw_start
  701.     ;;
  702.   stop)
  703.     fw_stop
  704.     ;;
  705.   restart)
  706.     fw_stop
  707.     fw_start
  708.     ;;
  709.   *)
  710.     echo $"Usage: $0 {start|stop|restart}"
  711.     ;;
  712. esac
  713.  
  714.  
  715. # TO-DO
  716.  
  717.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement