顯示具有 iptables 標籤的文章。 顯示所有文章
顯示具有 iptables 標籤的文章。 顯示所有文章

2015年12月2日 星期三

iptables

iptables 需要將ppp設為MASQUERADE偽裝IP才有辦法連到internet,但是這樣做會造成inner ip顯示全部變為gateway ip。修正方法為改用SNAT加指定EXTIP,但是就變成要有固定IP才可以使用。

出現錯誤訊息

Options error: server and client cannot be used together

代表conf檔案內有用到不是server指令的指令

iptables restart in ubuntu

sudo service ufw stop
sudo service ufw start

刪除單一chain下的規則
iptables -t nat -D POSTROUTING <number>

enable forwarding in ubuntu
echo 1 >/proc/sys/net/ipv4/ip_forward

如果要兩個網路能夠成功的達成路由﹐對方網路也必須有相應的路由設定指向本地網路才行

pfsense 內 openvpn檔案存放位置/var/etc/openvpn

traceroute
mac: traceroute
dos: tracert
linux: tracepath

terminate openvpn
sudo killall openvpn
參考資料
http://acman.bluenest.net/wordpress/archives/118
http://askubuntu.com/questions/298419/how-to-disconnect-from-openvpn
iptables
http://serverfault.com/questions/431593/iptables-forwarding-between-two-interface
http://askubuntu.com/questions/161551/how-to-start-stop-iptables
http://stackoverflow.com/questions/8239047/iptables-how-to-delete-postrouting-rule
http://s2.naes.tn.edu.tw/~kv/iptables.htm
https://gigenchang.wordpress.com/2014/04/19/10%E5%88%86%E9%90%98%E5%AD%B8%E6%9C%83iptables/
http://www.pcnet.idv.tw/pcnet/network/network_ip_routing.htm
pfsense存放位置
https://forum.pfsense.org/index.php?topic=13123.0
openvpn with two interface in and out
https://forums.openvpn.net/topic11033.html
https://forums.openvpn.net/topic14072.html
http://ubuntuforums.org/showthread.php?t=1606136
route
http://yinung2.blogspot.tw/2013/05/route_17.html
bridge vs routing
https://community.openvpn.net/openvpn/wiki/BridgingAndRouting
https://openvpn.net/index.php/open-source/documentation/miscellaneous/76-ethernet-bridging.html
chain using openvpn
http://serverfault.com/questions/512160/vpn-chaining-using-openvpn
dns server on openvpn
http://superuser.com/questions/637579/setting-dns-servers-using-openvpn-client-config-file
show route on mac
http://stackoverflow.com/questions/6782658/how-to-get-default-gateway-in-mac-osx
自己

2015年10月5日 星期一

openvpn server, pfsense ipsec 設定

iptables 需要將ppp設為MASQUERADE偽裝IP才有辦法連到internet

出現錯誤訊息

Options error: server and client cannot be used together

代表conf檔案內有用到不是server指令的指令

iptables restart in ubuntu

如使用ufw
sudo service ufw stop
sudo service ufw start

刪除單一chain下的規則
iptables -t nat -D POSTROUTING <number>

enable forwarding in ubuntu
echo 1 >/proc/sys/net/ipv4/ip_forward

如果要兩個網路能夠成功的達成路由﹐對方網路也必須有相應的路由設定指向本地網路才行

pfsense 內 openvpn檔案存放位置/var/etc/openvpn

traceroute
mac: traceroute
dos: tracert
linux: tracepath

terminate openvpn
sudo killall openvpn

port-share
可設一個port(通常為443)如果沒有TLS handshake則導到網頁,有則連接上openvpn server
方法是在Advanced configuration > Advanced 加上以下指令
port-share x.x.x.x 443
x.x.x.x為apache或web server的位址

pfsense 的ipsec設定可以參考下面的參考資料
因為後來看到似乎ipsec比較穩定,所以有用pfsense架設了一個ipsec VPN,到中國後才知道GFW(網路長城)的厲害啊...
總之現在就是openvpn PPTP ipsec輪著用,通常至少有一個能work

要push dns 到client時使用push "dhcp-option DNS <ip>"
如果是client端conf則使用dhcp-option DNS <ip>
可同時複數使用如增加google dns push "dhcp-option DNS 8.8.8.8"
這樣就可以同時使用local dns server與internet dns server

順帶一提,有問題的vpn是hola vpn。

2019/4/17更新
後來將VPN network與local network分開,導致兩邊無法互連問題,經過研究發現是因為設了ipv4 local network,這會導致直接優先在local route尋找而不是透過gateway轉遞。另外也須加上 push "route <local subnet> <local subnet netmask>"

參考資料
http://acman.bluenest.net/wordpress/archives/118
apache ssl (https) setting
https://www.bestvpn.com/blog/5919/how-to-hide-openvpn-traffic-an-introduction/
http://neidi.homeip.net/blog/cblog/index.php?id=414
http://kirby86a.pixnet.net/blog/post/95136568-ubuntu-12.04%E5%95%9F%E7%94%A8apache%E7%9A%84-ssl
openvpn port share
https://doc.pfsense.org/index.php/Sharing_a_Port_with_OpenVPN_and_a_Web_Server
pfsense ipsec
https://doc.pfsense.org/index.php/IPsec_for_road_warriors_in_PfSense_2.0.1_with_PSK_in_stead_of_xauth
https://www.shrew.net/download/vpn
https://doc.pfsense.org/index.php/IPsec_Road_Warrior/Mobile_Client_How-To
iptables
http://serverfault.com/questions/431593/iptables-forwarding-between-two-interface
http://askubuntu.com/questions/161551/how-to-start-stop-iptables
http://stackoverflow.com/questions/8239047/iptables-how-to-delete-postrouting-rule
http://s2.naes.tn.edu.tw/~kv/iptables.htm
https://gigenchang.wordpress.com/2014/04/19/10%E5%88%86%E9%90%98%E5%AD%B8%E6%9C%83iptables/
http://www.pcnet.idv.tw/pcnet/network/network_ip_routing.htm
pfsense存放位置
https://forum.pfsense.org/index.php?topic=13123.0
openvpn with two interface in and out
https://forums.openvpn.net/topic11033.html
https://forums.openvpn.net/topic14072.html
http://ubuntuforums.org/showthread.php?t=1606136
route
http://yinung2.blogspot.tw/2013/05/route_17.html
bridge vs routing
https://community.openvpn.net/openvpn/wiki/BridgingAndRouting
https://openvpn.net/index.php/open-source/documentation/miscellaneous/76-ethernet-bridging.html
chain using openvpn
http://serverfault.com/questions/512160/vpn-chaining-using-openvpn
dns server on openvpn
http://superuser.com/questions/637579/setting-dns-servers-using-openvpn-client-config-file
show route on mac
http://stackoverflow.com/questions/6782658/how-to-get-default-gateway-in-mac-osx
different subnet
https://blog.bobbyallen.me/2016/02/07/enabling-openvpn-clients-to-access-to-the-lan/

Forward PPTP server packet from Pi

這個問題花了我不少時間去處理,原因居然是因為Rasbian使用的3.18 kernel將PPTP packet視為invalid packet。不過還好後來有找到解決方法,分享給各位。

ping: sendmsg: Operation not permitted
出現以上訊息時代表iptables沒設定好,可重設iptables
# Reset/Flush iptables
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
# Flush end
但須注意此時已經policy設為ACCEPT,為debug使用,記得結束後要將iptables -P 設回需要的policy

PPTP forward參考設定如下

#!/bin/sh
### BEGIN INIT INFO
# Provides:         firewall.sh
# Required-Start:   $all
# Required-Stop:
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
### END INIT INFO

INTIF="你的對內介面,如brlan"

INTNET="對內介面的網段"

INTIP="PPTP server的IP"

EXTIF="對外介面,如為撥接通常為ppp0"

#Loading required stateful/NAT kernel modules...

/sbin/depmod -a
/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_conntrack_irc
/sbin/modprobe iptable_nat
/sbin/modprobe ip_nat_ftp
/sbin/modprobe ip_nat_irc

#below has to be loaded for pptp
/sbin/modprobe nf_nat_pptp 
/sbin/modprobe nf_conntrack_pptp 
/sbin/modprobe nf_conntrack_proto_gre 
#就是上面這三個kernel需要load,否則再怎麼設都是枉然

echo "1" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/ip_dynaddr

UNIVERSE="0.0.0.0/0"

iptables -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT

iptables -A FORWARD -i $EXTIF -o $INTIF -d $INTIP -j ACCEPT

# Enable SNAT (MASQUERADE) functionality on $EXTIF
iptables -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE

#DMZ setting
iptables -t nat -A PREROUTING -i $EXTIF -p tcp -m multiport --dport 1:65535 -j DNAT --to $INTIP
iptables -t nat -A PREROUTING -i $EXTIF -p udp -m multiport --dport 1:65535 -j DNAT --to $INTIP

# PPTP PREROUTING GRE packets
iptables -t nat -A PREROUTING -i $EXTIF -p 47 -j DNAT --to $INTIP
iptables -A FORWARD -i $EXTIF -p 47 -j ACCEPT

如果不是使用DMZ的話可將DMZ setting改成--dport 1723只轉port 1723到PPTP server就好
另外如果是使用pfsense之類的firewall,INTIP為pfsense的WAN端




將script註冊為service
sudo update-rc.d <script undre /etc/init.d/> defaults
將service移除
sudo update-rc.d -f foobar remove
設定啟動相依性(如在某service啟動後啟動)
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs

2015/10/5更新
遇到一個問題是source ip被snat蓋掉。因為以前使用ddwrt時沒有遇到這個問題,因此研究了一下,發現是需要設-m state --state NEW在iptables裡對應PREROUTING的port。才能夠觸發output。否則單純用MASQUERADE是可以但是就變成snat會將source ip蓋掉變成內部ip,不是我要的。
因此上面#DMZ setting要多增加兩行
iptables -t nat -A INPUT -i $EXTIF -p udp -m state --state NEW -m multiport --dport 1:65535 -j ACCEPT
iptables -t nat -A INPUT -i $EXTIF -p tcp -m state --state NEW -m multiport --dport 1:65535 -j ACCEPT

參考資料
register service
https://mkaz.github.io/2013/07/03/run-script-at-start-on-debian/
setting linux firewall
http://www.aboutdebian.com/firewall.htm
http://www.linuxjournal.com/article/3866
PPTP forward
http://ubuntuforums.org/showthread.php?t=801207
http://www.linuxquestions.org/questions/linux-networking-3/port-forward-gre-and-pptp-using-iptables-210334/
http://serverfault.com/questions/466030/pptp-iptables-routing-issue
https://wiki.archlinux.org/index.php/PPTP_server#iptables_firewall_configuration
https://lists.debian.org/debian-firewall/2004/04/msg00103.html
http://wiki.linuxmce.org/index.php/PPTP_server
tcpdump PPTP packet
http://serverfault.com/questions/342604/how-to-sniff-request-packet-on-vpn-server
DMZ, nat preserve source ip
https://www.debian-administration.org/article/73/Port_forwarding_for_iptables_DMZ