Guest User

bastion-config

a guest
Apr 20th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #!/bin/bash
  2. # The goal is to use a VPS (hostname: redbastion) to publicly offer a service (TCP 4444) but push that down over a
  3. # Wireguard interface wg0 to another host that actually listens on port 4444. The end goal is to DNAT ALL ports/protocols
  4. # but just using port 4444 now for PoC.
  5. # The config below works but with one major caveat. We must know the requester's IP in advance and put it in
  6. # Wireguard's AllowedIPs. Being in AllowedIPs causes that IP to be routed over wg0 so the response goes over tun0
  7. # rather than becoming a martian on eth0 (asymmetric route).
  8. #
  9. # The quickest fix is to add 0.0.0.0/0 to AllowedIPs on both sides but that would route ALL traffic through Wireguard
  10. # and I don't want my internal host routing all traffic through the VPS redbastion. I only want responses to TCP 4444
  11. # to go through wg0.
  12. #
  13. # A SNAT statement immediately fixes this but I would lose the true source IP and that is unacceptable in my use case.
  14. # I think the fix is to use tagged routing (http://cedric.dufour.name/blah/IT/TaggedRouting.html). I haven't implemented this yet
  15. # but am making notes so I'll know where to pick up.
  16.  
  17. # Definitions:
  18. # redbastion (AWS VPS). eth0: 1.1.1.1 wg0: 10.200.100.20.redbastion is intended to simply pass-thru traffic.
  19. # collector (on-prem server that will host the actual service). eth0: 2.2.2.2 wg0: 10.200.100.1
  20.  
  21.  
  22. PRIVATE_SUBNET="10.200.100.0/24"
  23. COLLECTOR_WIREGUARD_IFACE="10.200.100.1"
  24. WIREGUARD_PORT="60719"
  25.  
  26. # clear previous mess
  27. iptables -F
  28. iptables -t nat -F
  29. iptables -t mangle -F
  30.  
  31. # enable forwarding on interfaces
  32. sysctl -w net.ipv4.ip_forward=1
  33.  
  34. # allow loopback
  35. iptables -A INPUT -i lo -j ACCEPT
  36. iptables -A OUTPUT -o lo -j ACCEPT
  37. # TMP
  38. iptables -A OUTPUT -o wg0 -j ACCEPT
  39.  
  40. # for now, allow outbound connections
  41. iptables -A OUTPUT -o eth0 -j ACCEPT
  42.  
  43. # allow established connections
  44. iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  45. iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  46. #iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
  47.  
  48. # allow SSH administration from mgmt IPs and from collector wireguard internal ip 10.200.100.1
  49. iptables -A INPUT -p tcp -s x.x.x.x.x/26 --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
  50. # when i get locked out of redbastion allow ssh over the wg0 interface as a backup plan
  51. iptables -A INPUT -p tcp -s 10.200.100.1 --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
  52. # Allow Wireguard connection from collector eth0
  53. iptables -A INPUT -i eth0 -p udp -s x.x.x.x.x/32 --dport $WIREGUARD_PORT -j ACCEPT
  54.  
  55. # deal with forwards
  56. #iptables -A FORWARD -m conntrack --ctstate NEW -s $PRIVATE_SUBNET -m policy --pol none --dir in -j ACCEPT
  57. #iptables -t nat -A POSTROUTING -s $PRIVATE_SUBNET -m policy --pol none --dir out -j MASQUERADE
  58.  
  59.  
  60. iptables -A FORWARD -i eth0 -o wg0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  61. iptables -A FORWARD -i wg0 -o eth0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  62.  
  63. iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 4444 -j LOG --log-prefix "iptables DNAT hit: " --log-level 4
  64. iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 4444 -j DNAT --to-destination $COLLECTOR_WIREGUARD_IFACE:4444
  65. iptables -A FORWARD -i eth0 -o wg0 -p tcp -d 10.200.100.1 --dport 4444 -m state --state NEW,ESTABLISHED,RELATED -j LOG --log-prefix "iptables ALLOWING FORWARD 4444: " --log-level 4
  66. iptables -A FORWARD -i eth0 -o wg0 -p tcp -d 10.200.100.1 --dport 4444 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
  67.  
  68. ### IT WORKS WITH THIS STATEMENT BUT CAUSES THE SRC IP TO BE LOST
  69. # NEED 0.0.0.0 in wg AllowedIPs for this to work. it is HARD to troubleshoot!
  70. #iptables -t nat -A POSTROUTING ! -s 127.0.0.1 -j MASQUERADE
  71.  
  72.  
  73. iptables -A INPUT -i wg0 -j LOG --log-prefix "IPTables-Allowed input on wg0: " --log-level 4 -m limit --limit 1000/min
  74. iptables -A INPUT -i wg0 -j ACCEPT
  75.  
  76. # log dropped pkts
  77. iptables -A INPUT -j LOG --log-prefix "IPTables-DROP: " --log-level 4 -m limit --limit 1000/min
  78. iptables -A INPUT -j DROP
Advertisement
Add Comment
Please, Sign In to add comment