<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Aptos;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:12.0pt;
font-family:"Aptos",sans-serif;
mso-ligatures:standardcontextual;
mso-fareast-language:EN-US;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Aptos",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-AU link="#467886" vlink="#96607D" style='word-wrap:break-word'><div class=WordSection1><p class=MsoNormal><span lang=EN-US>Hi all,<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>Just having some fun with iptables. I am trying to route traffic from AWS(10.0.0.1) to my local server (10.0.0.2) using wireguard. <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>I have setup a connection on vpn0 on both servers, but I am struggling a little with iptables.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>While I am experimenting I setup the following iptables rules using this script on the AWS VPN server. <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>I plan to include some of these inside the wireguard configuration once I have it all working.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>root@vpn:~# cat routeTraffic.sh<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>#!/bin/bash<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -F<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -X<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -t nat -F<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -t nat -X<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -t mangle -F<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -t mangle -X<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -P INPUT DROP<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -P OUTPUT DROP<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -P FORWARD ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A INPUT -i ens5 -p tcp -m tcp --dport 22 -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A INPUT -i vpn0 -p tcp -m tcp --dport 22 -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A INPUT -i ens5 -p tcp -m tcp --dport 25 -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A INPUT -i ens5 -p tcp -m tcp --dport 53 -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A INPUT -i ens5 -p udp -m udp --dport 53 -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A INPUT -i ens5 -p tcp -m tcp --dport 80 -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A INPUT -i ens5 -p tcp -m tcp --dport 443 -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A INPUT -i ens5 -p udp -m udp --dport 55107 -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A INPUT -i vpn0 -p udp -m udp --dport 55107 -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A INPUT -i ens5 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A OUTPUT -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US># 1. Redirect the incoming packet to the tunnel destination<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -t nat -A PREROUTING -p tcp --dport 53 -j DNAT --to-destination 10.0.0.2:53<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -t nat -A PREROUTING -p udp --dport 53 -j DNAT --to-destination 10.0.0.2:53<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 10.0.0.2:80<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 10.0.0.2:443<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US># 2. Allow the traffic through via the FORWARD chain<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A FORWARD -p tcp -d 10.0.0.2 --dport 53 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A FORWARD -p udp -d 10.0.0.2 --dport 53 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A FORWARD -p tcp -d 10.0.0.2 --dport 80 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A FORWARD -p tcp -d 10.0.0.2 --dport 443 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -A FORWARD -j LOG<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US># 3. Apply Masquerading to ensure return traffic goes back through the server<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>iptables -t nat -A POSTROUTING -o vpn0 -j MASQUERADE<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US> Initially I had “iptables -P FORWARD DROP” with the hope of adding a white-list of allowed rules. However, this didn’t work. I then added the LOG statement which produces the following:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>Feb 11 10:46:19 vpn.host.com kernel: IN=vpn0 OUT=ens5 MAC= SRC=10.0.0.2 DST=176.X.X.X LEN=60 TOS=0x00 PREC=0x00 TTL=63 ID=0 DF PROTO=TCP SPT=80 DPT=55784 WINDOW=64296 RES=0x00 ACK SYN URGP=0<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>Feb 11 10:46:19 vpn.host.com kernel: IN=vpn0 OUT=ens5 MAC= SRC=10.0.0.2 DST=176.X.X.X LEN=52 TOS=0x00 PREC=0x00 TTL=63 ID=42901 DF PROTO=TCP SPT=80 DPT=55784 WINDOW=502 RES=0x00 ACK URGP=0<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>Feb 11 10:46:19 vpn.host.com kernel: IN=vpn0 OUT=ens5 MAC= SRC=10.0.0.2 DST=176.X.X.X LEN=2788 TOS=0x00 PREC=0x00 TTL=63 ID=42902 DF PROTO=TCP SPT=80 DPT=55784 WINDOW=502 RES=0x00 ACK PSH URGP=0<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>Feb 11 10:46:19 vpn.host.com kernel: IN=vpn0 OUT=ens5 MAC= SRC=10.0.0.2 DST=176.X.X.X LEN=2788 TOS=0x00 PREC=0x00 TTL=63 ID=42904 DF PROTO=TCP SPT=80 DPT=55784 WINDOW=502 RES=0x00 ACK PSH URGP=0<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>Feb 11 10:46:38 vpn.host.com kernel: IN=vpn0 OUT=ens5 MAC= SRC=10.0.0.2 DST=176.X.Y.Y LEN=52 TOS=0x00 PREC=0x00 TTL=63 ID=0 DF PROTO=TCP SPT=443 DPT=59476 WINDOW=64860 RES=0x00 ACK SYN URGP=0<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>Any suggestions on what FORWARD rule I need to add for this to work with FORWARD DROP ? It looks like these are the response packets which come from VPN0 and are going back to the internet on ENS5. I though the “NEW,ESTABLISHED,RELATED” statement would handle these as they are part of the response in return an accepted request.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>Any thought or suggestions would be most helpful.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>Kind regards<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>Alex<o:p></o:p></span></p></div></body></html>