Guest User

Untitled

a guest
Jul 22nd, 2018
1,463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #!/bin/bash -xu
  2.  
  3. #Define the policies to accept in order to avoid being cut out running this remotely via SSH
  4. sudo iptables -P INPUT ACCEPT
  5. sudo iptables -P FORWARD ACCEPT
  6. sudo iptables -P OUTPUT ACCEPT
  7.  
  8. #To begin with, flush the tables
  9. sudo iptables -t mangle -F
  10. sudo iptables -t nat -F
  11. sudo iptables -t filter -F
  12. sudo iptables -X
  13.  
  14.  
  15. # Allow Access Point NAT
  16. sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  17.  
  18.  
  19. # Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
  20. sudo iptables -t filter -A INPUT -i lo -j ACCEPT
  21. sudo iptables -t filter -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT
  22.  
  23. # Accepts all established inbound connections
  24. sudo iptables -t filter -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  25.  
  26. #Drop all multicast traffic
  27. sudo iptables -t filter -A INPUT -d 224.0.0.0/4 -j DROP
  28. sudo iptables -t filter -A OUTPUT -d 224.0.0.0/4 -j DROP
  29.  
  30. # Allows all outbound traffic
  31. sudo iptables -t filter -A OUTPUT -j ACCEPT
  32.  
  33. # Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
  34. sudo iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT
  35. sudo iptables -t filter -A INPUT -p tcp --dport 443 -j ACCEPT
  36.  
  37. # Allows SSH connections
  38. sudo iptables -t filter -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
  39. sudo iptables -t filter -A INPUT -p tcp -m state --state NEW -m recent --dport 22 --set --name ssh --rsource
  40. sudo iptables -t filter -A INPUT -p tcp -m state --state NEW -m recent --dport 22 ! --rcheck --seconds 60 --hitcount 4 --name ssh --rsource -j ACCEPT
  41.  
  42. # Allow ping and other icmp traffic
  43. sudo iptables -t filter -A INPUT -p icmp -j ACCEPT
  44.  
  45. # Allow forwarded the following forwards
  46. sudo iptables -t filter -A FORWARD -o wlan0 -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
  47. sudo iptables -t filter -A FORWARD -o eth0 -i wlan0 -j ACCEPT
  48.  
  49. # log iptables denied calls
  50. sudo iptables -t filter -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
  51.  
  52. # Reject all other inbound
  53. sudo iptables -t filter -A INPUT -j DROP
  54. sudo iptables -t filter -A FORWARD -j DROP
  55.  
  56. #Reset the policies
  57. sudo iptables -P INPUT DROP
  58. sudo iptables -P FORWARD DROP
  59. sudo iptables -P OUTPUT DROP
  60.  
  61. sudo dmesg -c
  62. clear
Add Comment
Please, Sign In to add comment