mirror of
https://github.com/bol-van/zapret.git
synced 2025-05-24 22:32:58 +03:00
history purge
This commit is contained in:
346
docs/bsd.eng.txt
Normal file
346
docs/bsd.eng.txt
Normal file
@@ -0,0 +1,346 @@
|
||||
Supported versions
|
||||
------------------
|
||||
|
||||
FreeBSD 11.x+ , OpenBSD 6.x+, partially MacOS Sierra+
|
||||
|
||||
Older versions may work or not. pfSense is not supported.
|
||||
|
||||
BSD features
|
||||
------------
|
||||
|
||||
BSD does not have NFQUEUE. Similar mechanism - divert sockets.
|
||||
In BSD compiling the source from nfq directory result in dvtws binary instead of nfqws.
|
||||
dvtws shares most of the code with nfqws and offers almost identical parameters.
|
||||
|
||||
FreeBSD has 2 firewalls : IPFilter (ipfw) and Packet Filter (PF). OpenBSD has only PF.
|
||||
|
||||
To compile sources in FreeBSD use 'make', in OpenBSD - use 'make bsd', in MacOS - use 'make mac'.
|
||||
Compile all programs : make -C /opt/zapret
|
||||
Compile all programs with PF support : make -C /opt/zapret CFLAGS=-DUSE_PF
|
||||
In FreeBSD enable PF only if you use it. Its undesirable if you don't.
|
||||
PF is enabled automatically in OpenBSD and MacOS.
|
||||
|
||||
Divert sockets are internal type sockets in the BSD kernel. They have no relation to network addresses
|
||||
or network packet exchange. They are identified by a port number 1..65535. Its like queue number in NFQUEUE.
|
||||
Traffic can be diverted to a divert socket using firewall rule.
|
||||
If nobody listens on the specified divert port packets are dropped. Its similar to NFQUEUE without --queue-bypass.
|
||||
|
||||
ipset/*.sh scripts work with ipfw lookup tables if ipfw is present.
|
||||
ipfw table is analog to linux ipset. Unlike ipsets ipfw tables share v4 an v6 addresses and subnets.
|
||||
If ipfw is absent scripts check LISTS_RELOAD config variable.
|
||||
If its present then scripts execute a command from LISTS_RELOAD.
|
||||
If LISTS_RELOAD=- scripts do not load tables even if ipfw exists.
|
||||
|
||||
PF can load ip tables from a file. To use this feature with ipset/*.sh scripts disable gzip file creation
|
||||
using "GZIP_LISTS=0" directive in the /opt/zapret/config file.
|
||||
|
||||
BSD kernel doesn't implement splice syscall. tpws uses regular recv/send operations with data copying to user space.
|
||||
Its slower but not critical.
|
||||
tpws uses nonblocking sockets with linux specific epoll feature.
|
||||
In BSD systems epoll is emulated by epoll-shim library on top of kqueue.
|
||||
|
||||
dvtws uses some programming HACKs, assumptions and knowledge of discovered bugs and limitations.
|
||||
BSD systems have many limitations, version specific features and bugs in low level networking, especially for ipv6.
|
||||
Many years have passed but BSD code still has 15-20 year artificial limiters in the code.
|
||||
dvtws uses additinal divert socket(s) for layer 3 packet injection if raw sockets do not allow it.
|
||||
It works for the moment but who knows. Such a usage is not very documented.
|
||||
|
||||
mdig and ip2net are fully compatible with BSD.
|
||||
|
||||
FreeBSD
|
||||
-------
|
||||
|
||||
Divert sockets require special kernel module 'ipdivert'.
|
||||
Write the following to config files :
|
||||
/boot/loader.conf (create if absent) :
|
||||
-----------
|
||||
ipdivert_load="YES"
|
||||
net.inet.ip.fw.default_to_accept=1
|
||||
-----------
|
||||
/etc/rc.conf :
|
||||
-----------
|
||||
firewall_enable="YES"
|
||||
firewall_script="/etc/rc.firewall.my"
|
||||
-----------
|
||||
/etc/rc.firewall.my :
|
||||
-----------
|
||||
ipfw -q -f flush
|
||||
-----------
|
||||
Later you will add ipfw commands to /etc/rc.firewall.my to be reapplied after reboot.
|
||||
You can also run zapret daemons from there. Start them with "--daemon" options, for example :
|
||||
-----------
|
||||
pkill ^dvtws$
|
||||
/opt/zapret/nfq/dvtws --port=989 --daemon --dpi-desync=split2
|
||||
-----------
|
||||
To restart firewall and daemons run : /etc/rc.d/ipfw restart
|
||||
|
||||
|
||||
Assume LAN='em1', WAN="em0".
|
||||
|
||||
tpws transparent mode quick start.
|
||||
|
||||
For all traffic:
|
||||
ipfw delete 100
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from me to any 80,443 proto ip4 xmit em0 not uid daemon
|
||||
ipfw add 100 fwd ::1,988 tcp from me to any 80,443 proto ip6 xmit em0 not uid daemon
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from any to any 80,443 proto ip4 recv em1
|
||||
ipfw add 100 fwd ::1,988 tcp from any to any 80,443 proto ip6 recv em1
|
||||
/opt/zapret/tpws/tpws --port=988 --user=daemon --bind-addr=::1 --bind-addr=127.0.0.1
|
||||
|
||||
Process only table zapret with the exception of table nozapret :
|
||||
ipfw delete 100
|
||||
ipfw add 100 allow tcp from me to table\(nozapret\) 80,443
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from me to table\(zapret\) 80,443 proto ip4 xmit em0 not uid daemon
|
||||
ipfw add 100 fwd ::1,988 tcp from me to table\(zapret\) 80,443 proto ip6 xmit em0 not uid daemon
|
||||
ipfw add 100 allow tcp from any to table\(nozapret\) 80,443 recv em1
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from any to any 80,443 proto ip4 recv em1
|
||||
ipfw add 100 fwd ::1,988 tcp from any to any 80,443 proto ip6 recv em1
|
||||
/opt/zapret/tpws/tpws --port=988 --user=daemon --bind-addr=::1 --bind-addr=127.0.0.1
|
||||
|
||||
Tables zapret, nozapret, ipban are created by ipset/*.sh scripts the same way as in Linux.
|
||||
Its a good idea to update tables periodically :
|
||||
crontab -e
|
||||
write the line : 0 12 */2 * * /opt/zapret/ipset/get_config.sh
|
||||
|
||||
When using ipfw tpws does not require special permissions for transparent mode.
|
||||
However without root its not possible to bind to ports <1024 and change UID/GID. Without changing UID tpws
|
||||
will run into recursive loop, and that's why its necessary to write ipfw rules with the right UID.
|
||||
Redirecting to ports >=1024 is dangerous. If tpws is not running any unprivileged process can
|
||||
listen to that port and intercept traffic.
|
||||
|
||||
|
||||
dvtws quick start.
|
||||
|
||||
For all traffic:
|
||||
ipfw delete 100
|
||||
ipfw add 100 divert 989 tcp from any to any 80,443 out not diverted not sockarg xmit em0
|
||||
/opt/zapret/nfq/dvtws --port=989 --dpi-desync=split2
|
||||
|
||||
Process only table zapret with the exception of table nozapret :
|
||||
ipfw delete 100
|
||||
ipfw add 100 allow tcp from me to table\(nozapret\) 80,443
|
||||
ipfw add 100 divert 989 tcp from any to table\(zapret\) 80,443 out not diverted not sockarg xmit em0
|
||||
/opt/zapret/nfq/dvtws --port=989 --dpi-desync=split2
|
||||
|
||||
Reinjection loop avoidance.
|
||||
FreeBSD artificially ignores sockarg for ipv6 in the kernel.
|
||||
This limitation is coming from the ipv6 early age. Code is still in "testing" state. 10-20 years. Everybody forgot about it.
|
||||
dvtws sends ipv6 forged frames using another divert socket (HACK). they can be filtered out using 'diverted'.
|
||||
ipv4 frames are filtered using 'sockarg'.
|
||||
|
||||
PF in FreeBSD:
|
||||
The setup is similar to OpenBSD, but there are important nuances.
|
||||
1) Don't forget to build special PF-enabled version of tpws : make CFLAGS=-DUSE_PF
|
||||
2) It's not possible to redirect to ::1. Need to redirect to the link-local address of the incoming interface.
|
||||
Look for fe80:... address in ifconfig and use it for redirection target.
|
||||
3) pf.conf syntax is a bit different from OpenBSD.
|
||||
4) How to set maximum table size : sysctl net.pf.request_maxcount=2000000
|
||||
5) The word 'divert-packet' is absent in the pfctl binary, divert-packet rules are not working.
|
||||
'divert-to' is not the same thing. Looks like its not possible to use dvtws with PF in FreeBSD.
|
||||
/etc/pf.conf
|
||||
-----------
|
||||
rdr pass on em1 inet6 proto tcp to port {80,443} -> fe80::31c:29ff:dee2:1c4d port 988
|
||||
rdr pass on em1 inet proto tcp to port {80,443} -> 127.0.0.1 port 988
|
||||
-----------
|
||||
/opt/zapret/tpws/tpws --port=988 --bind-addr=127.0.0.1 --bind-iface6=em1 --bind-linklocal=force
|
||||
|
||||
Its not clear how to do rdr-to outgoing traffic. I could not make route-to scheme work.
|
||||
|
||||
|
||||
OpenBSD
|
||||
-------
|
||||
|
||||
In OpenBSD default tpws bind is ipv6 only. to bind to ipv4 specify --bind-addr=0.0.0.0
|
||||
Use --bind-addr=0.0.0.0 --bind-addr=:: to achieve the same default bind as in others OSes.
|
||||
|
||||
tpws for forwarded traffic only :
|
||||
|
||||
/etc/pf.conf
|
||||
------------
|
||||
pass in quick on em1 inet proto tcp to port {80,443} rdr-to 127.0.0.1 port 988
|
||||
pass in quick on em1 inet6 proto tcp to port {80,443} rdr-to ::1 port 988
|
||||
------------
|
||||
pfctl -f /etc/pf.conf
|
||||
tpws --port=988 --user=daemon --bind-addr=::1 --bind-addr=127.0.0.1
|
||||
|
||||
Its not clear how to do rdr-to outgoing traffic. I could not make route-to scheme work.
|
||||
rdr-to support is done using /dev/pf, that's why transparent mode requires root.
|
||||
|
||||
dvtws for all traffic:
|
||||
|
||||
/etc/pf.conf
|
||||
------------
|
||||
pass out quick on em0 proto tcp to port {80,443} divert-packet port 989
|
||||
------------
|
||||
pfctl -f /etc/pf.conf
|
||||
./dvtws --port=989 --dpi-desync=split2
|
||||
|
||||
dwtws only for table zapret with the exception of table nozapret :
|
||||
|
||||
/etc/pf.conf
|
||||
------------
|
||||
table <zapret> file "/opt/zapret/ipset/zapret-ip.txt"
|
||||
table <zapret-user> file "/opt/zapret/ipset/zapret-ip-user.txt"
|
||||
table <nozapret> file "/opt/zapret/ipset/zapret-ip-exclude.txt"
|
||||
pass out quick on em0 inet proto tcp to <nozapret> port {80,443}
|
||||
pass out quick on em0 inet proto tcp to <zapret> port {80,443} divert-packet port 989
|
||||
pass out quick on em0 inet proto tcp to <zapret-user> port {80,443} divert-packet port 989
|
||||
table <zapret6> file "/opt/zapret/ipset/zapret-ip6.txt"
|
||||
table <zapret6-user> file "/opt/zapret/ipset/zapret-ip-user6.txt"
|
||||
table <nozapret6> file "/opt/zapret/ipset/zapret-ip-exclude6.txt"
|
||||
pass out quick on em0 inet6 proto tcp to <nozapret6> port {80,443}
|
||||
pass out quick on em0 inet6 proto tcp to <zapret6> port {80,443} divert-packet port 989
|
||||
pass out quick on em0 inet6 proto tcp to <zapret6-user> port {80,443} divert-packet port 989
|
||||
------------
|
||||
pfctl -f /etc/pf.conf
|
||||
./dvtws --port=989 --dpi-desync=split2
|
||||
|
||||
|
||||
dvtws in OpenBSD sends all fakes through a divert socket because raw sockets have critical artificial limitations.
|
||||
Looks like pf automatically prevent reinsertion of diverted frames. Loop problem does not exist.
|
||||
|
||||
Sadly PF auto applies return rule to divert-packet.
|
||||
Not only outgoing packets go through dvtws but also incoming.
|
||||
This adds great unneeded overhead that will be the most noticable on http/https downloads.
|
||||
I could not figure out how to disable this feature.
|
||||
Thats why you are encouraged to use table filters with your personal blocked site lists.
|
||||
|
||||
OpenBSD forcibly recomputes tcp checksum after divert. Thats why most likely
|
||||
dpi-desync-fooling=badsum will not work. dvtws will warn if you specify this parameter.
|
||||
|
||||
ipset scripts do not reload PF by default. To enable reload specify command in /opt/zapret/config :
|
||||
LISTS_RELOAD="pfctl -f /etc/pf.conf"
|
||||
Newer pfctl versions can reload tables only : pfctl -Tl -f /etc/pf.conf
|
||||
But OpenBSD 6.8 pfctl is old enough and does not support that. Newer FreeBSD do.
|
||||
Don't forget to disable gzip compression :
|
||||
GZIP_LISTS=0
|
||||
If some list files do not exist and have references in pf.conf it leads to error.
|
||||
You need to exclude those tables from pf.conf and referencing them rules.
|
||||
After configuration is done you can put ipset script :
|
||||
crontab -e
|
||||
write the line : 0 12 */2 * * /opt/zapret/ipset/get_config.sh
|
||||
|
||||
|
||||
MacOS
|
||||
-----
|
||||
|
||||
Initially, the kernel of this OS was based on BSD. That's why it is still BSD but a lot was modified by Apple.
|
||||
As usual a mass commercial project priorities differ from their free counterparts.
|
||||
Apple guys do what they want.
|
||||
What everyone have updated long ago they keep old like a mammoth. But who cares ?
|
||||
|
||||
MacOS used to have ipfw but it was removed later and replaced by PF.
|
||||
It looks like divert sockets are internally replaced with raw. Its possible to request a divert socket
|
||||
but it behaves exactly as raw socket with all its BSD inherited + apple specific bugs and feature.
|
||||
The fact is that divert-packet in /etc/pf.conf does not work. pfctl binary does not contain the word 'divert'.
|
||||
dvtws does compile but is useless.
|
||||
|
||||
After some efforts tpws works. Apple has removed some important stuff from their newer SDKs (DIOCNATLOOK) making
|
||||
them undocumented and unsupported. With important definitions copied from an older SDK it was possible to make
|
||||
transparent mode working again. But this is not guaranteed to work in the future versions.
|
||||
Another MacOS unique feature is root requirement while polling /dev/pf.
|
||||
By default tpws drops root. Its necessary to specify --user=root to stay with root.
|
||||
In other aspects PF behaves very similar to FreeBSD and shares the same pf.conf syntax.
|
||||
|
||||
In MacOS redirection works both for passthrough and outgoing traffic. Outgoing redirection requires route-to rule.
|
||||
Because tpws is forced to run as root to avoid loop its necessary to exempt root from the redirection.
|
||||
That's why DPI bypass will not work for local requests from root.
|
||||
|
||||
If you do ipv6 routing you have to get rid of "secured" ipv6 address assignment.
|
||||
"secured" addresses are designed to be permanent and not related to the MAC address.
|
||||
And they really are. Except for link-locals.
|
||||
If you just reboot the system link-locals will not change. But next day they will change. Not necessary to wait so long.
|
||||
Just change the system time to tomorrow and reboot. Link-locals will change. (at least they change in vmware guest)
|
||||
Looks like its a kernel bug. Link locals should not change. Its useless and can be harmful. Cant use LL as a gateway.
|
||||
The easiest solution is to disable "secured" addresses.
|
||||
Outgoing connections prefer randomly generated temporary addressesas like in other systems.
|
||||
Put the string "net.inet6.send.opmode=0" to /etc/sysctl.conf. If not present - create it.
|
||||
Then reboot the system.
|
||||
If you dont like this solution you can assign an additional static ipv6 address from fd00::/8 range with /128 prefix
|
||||
to your LAN interface and use it as the gateway address.
|
||||
|
||||
tpws transparent mode only for outgoing connections. en0 - WAN.
|
||||
|
||||
/etc/pf.conf
|
||||
------------
|
||||
rdr pass on lo0 inet proto tcp from !127.0.0.0/8 to any port {80,443} -> 127.0.0.1 port 988
|
||||
rdr pass on lo0 inet6 proto tcp from !::1 to any port {80,443} -> fe80::1 port 988
|
||||
pass out route-to (lo0 127.0.0.1) inet proto tcp from any to any port {80,443} user { >root }
|
||||
pass out route-to (lo0 fe80::1) inet6 proto tcp from any to any port {80,443} user { >root }
|
||||
------------
|
||||
pfctl -ef /etc/pf.conf
|
||||
/opt/zapret/tpws/tpws --user=root --port=988 --bind-addr=127.0.0.1 --bind-iface6=en0 --bind-linklocal=force
|
||||
|
||||
|
||||
tpws transparent mode for both passthrough and outgoing connections. en0 - WAN, en1 - WAN.
|
||||
|
||||
ifconfig en1 | grep fe80
|
||||
inet6 fe80::bbbb:bbbb:bbbb:bbbb%en1 prefixlen 64 scopeid 0x8
|
||||
/etc/pf.conf
|
||||
------------
|
||||
rdr pass on en1 inet proto tcp from any to any port {80,443} -> 127.0.0.1 port 988
|
||||
rdr pass on en1 inet6 proto tcp from any to any port {80,443} -> fe80::bbbb:bbbb:bbbb:bbbb port 988
|
||||
rdr pass on lo0 inet proto tcp from !127.0.0.0/8 to any port {80,443} -> 127.0.0.1 port 988
|
||||
rdr pass on lo0 inet6 proto tcp from !::1 to any port {80,443} -> fe80::1 port 988
|
||||
pass out route-to (lo0 127.0.0.1) inet proto tcp from any to any port {80,443} user { >root }
|
||||
pass out route-to (lo0 fe80::1) inet6 proto tcp from any to any port {80,443} user { >root }
|
||||
------------
|
||||
pfctl -ef /etc/pf.conf
|
||||
/opt/zapret/tpws/tpws --user=root --port=988 --bind-addr=127.0.0.1 --bind-iface6=en0 --bind-linklocal=force --bind-iface6=en1 --bind-linklocal=force
|
||||
|
||||
|
||||
Build from source : make -C /opt/zapret mac
|
||||
|
||||
ipset/*.sh scripts work.
|
||||
|
||||
|
||||
MacOS easy install
|
||||
------------------
|
||||
|
||||
install_easy.sh supports MacOS
|
||||
|
||||
Shipped precompiled binaries are built for 64-bit MacOS with -mmacosx-version-min=10.8 option.
|
||||
They should run on all supported MacOS versions.
|
||||
If no - its easy to build your own. Running 'make' automatically installs developer tools.
|
||||
|
||||
!! Internet sharing is not supported !!
|
||||
Routing is supported but only manually configured through PF.
|
||||
If you enable internet sharing tpws stops functioning. When you disable internet sharing you may lose web site access.
|
||||
To fix : pfctl -f /etc/pf.conf
|
||||
If you need internet sharing use tpws socks mode.
|
||||
|
||||
launchd is used for autostart (/Library/LaunchDaemons/zapret.plist)
|
||||
Control script : /opt/zapret/init.d/macos/zapret
|
||||
The following commands fork with both tpws and firewall (if INIT_APPLY_FW=1 in config)
|
||||
/opt/zapret/init.d/macos/zapret start
|
||||
/opt/zapret/init.d/macos/zapret stop
|
||||
/opt/zapret/init.d/macos/zapret restart
|
||||
Work with tpws only :
|
||||
/opt/zapret/init.d/macos/zapret start-daemons
|
||||
/opt/zapret/init.d/macos/zapret stop-daemons
|
||||
/opt/zapret/init.d/macos/zapret restart-daemons
|
||||
Work with PF only :
|
||||
/opt/zapret/init.d/macos/zapret start-fw
|
||||
/opt/zapret/init.d/macos/zapret stop-fw
|
||||
/opt/zapret/init.d/macos/zapret restart-fw
|
||||
Reloading PF tables :
|
||||
/opt/zapret/init.d/macos/zapret reload-fw-tables
|
||||
|
||||
Installer configures LISTS_RELOAD in the config so ipset/*.sh scripts automatically reload PF tables.
|
||||
Installer creates cron job for ipset/get_config.sh, as in OpenWRT.
|
||||
|
||||
start-fw script automatically patches /etc/pf.conf inserting there "zapret" anchors.
|
||||
Auto patching requires pf.conf with apple anchors preserved.
|
||||
If your pf.conf is highly customized and patching fails you will see the warning. Do not ignore it.
|
||||
In that case you need to manually insert "zapret" anchors to your pf.conf (keeping the right rule type ordering) :
|
||||
rdr-anchor "zapret"
|
||||
anchor "zapret"
|
||||
unistall_easy.sh unpatches pf.conf
|
||||
|
||||
start-fw creates 3 anchor files in /etc/pf.anchors : zapret,zapret-v4,zapret-v6.
|
||||
Last 2 are referenced by anchor "zapret".
|
||||
Tables nozapret,nozapret6 belong to anchor "zapret".
|
||||
Tables zapret,zapret-user belong to anchor "zapret-v4".
|
||||
Tables zapret6,zapret6-user belong to anchor "zapret-v6".
|
||||
If an ip version is disabled then corresponding anchor is empty and is not referenced from the anchor "zapret".
|
||||
Tables are only created for existing list files in the ipset directory.
|
375
docs/bsd.txt
Normal file
375
docs/bsd.txt
Normal file
@@ -0,0 +1,375 @@
|
||||
Поддерживаемые версии
|
||||
---------------------
|
||||
|
||||
FreeBSD 11.x+ , OpenBSD 6.x+, частично MacOS Sierra+
|
||||
|
||||
На более старых может собираться, может не собираться, может работать или не работать.
|
||||
На FreeBSD 10 собирается и работает dvtws. С tpws есть проблемы из-за слишком старой версии компилятора clang.
|
||||
Вероятно, будет работать, если обновить компилятор.
|
||||
На pfSense если и можно завести, то это не просто. Собранные на FreeBSD с той же версией ядра бинарики не работают.
|
||||
Статические бинарики тоже. Модуль ipdivert отсутствует.
|
||||
|
||||
|
||||
Особенности BSD систем
|
||||
----------------------
|
||||
|
||||
В BSD нет nfqueue. Похожий механизм - divert sockets.
|
||||
Из каталога "nfq" под BSD собирается dvtws вместо nfqws.
|
||||
Он разделяет с nfqws большую часть кода и почти совпадает по параметрам командной строки.
|
||||
|
||||
FreeBSD содержит 2 фаервола : IPFilter (ipfw) и Packet Filter (PF). OpenBSD содержит только PF.
|
||||
|
||||
Под FreeBSD tpws и dvtws собираются через "make", под OpenBSD - "make bsd", под MacOS - "make mac".
|
||||
FreeBSD make распознает BSDmakefile , OpenBSD и MacOS - нет. Поэтому там используется отдельный target в Makefile.
|
||||
Сборка всех исходников : make -C /opt/zapret
|
||||
Сборка всех исходников с поддержкой PF : make -C /opt/zapret CFLAGS=-DUSE_PF
|
||||
В FreeBSD поддержку PF нужно включать только, если вы его используете. Иначе это нежелательно !
|
||||
В OpenBSD и MacOS PF при сборке включается автоматически.
|
||||
|
||||
divert сокет - внутренний тип сокета ядра BSD. Он не привязывается ни к какому сетевому адресу, не участвует
|
||||
в обмене данными через сеть и идентифицируется по номеру порта 1..65535. Аналогия с номером очереди NFQUEUE.
|
||||
На divert сокеты заворачивается трафик посредством правил ipfw или PF.
|
||||
Если в фаерволе есть правило divert, но на divert порту никто не слушает, то пакеты дропаются.
|
||||
Это поведение аналогично правилам NFQUEUE без параметра --queue-bypass.
|
||||
На FreeBSD divert сокеты могут быть только ipv4, хотя на них принимаются и ipv4, и ipv6 фреймы.
|
||||
На OpenBSD divert сокеты создаются отдельно для ipv4 и ipv6 и работают только с одной версией ip каждый.
|
||||
На MacOS похоже, что divert сокеты из ядра вырезаны. См подробнее раздел про MacOS.
|
||||
Отсылка в divert сокет работает аналогично отсылке через raw socket на linux. Передается полностью IP фрейм, начиная
|
||||
с ip загловка . Эти особенности учитываются в dvtws.
|
||||
|
||||
Скрипты ipset/*.sh при наличии ipfw работают с ipfw lookup tables.
|
||||
Это прямой аналог ipset. lookup tables не разделены на v4 и v6. Они могут содержать v4 и v6 адреса и подсети одновременно.
|
||||
Если ipfw отсутствует, то действие зависит от переменной LISTS_RELOAD в config.
|
||||
Если она задана, то выполняется команда из LISTS_RELOAD. В противном случае не делается ничего.
|
||||
Если LISTS_RELOAD=-, то заполнение таблиц отключается даже при наличии ipfw.
|
||||
|
||||
PF может загружать ip таблицы из файла. Чтобы использовать эту возможность следует отключить сжатие gzip для листов
|
||||
через параметр файла config "GZIP_LISTS=0".
|
||||
|
||||
BSD не содержит системного вызова splice. tpws работает через переброску данных в user mode в оба конца.
|
||||
Это медленнее, но не критически.
|
||||
Управление асинхронными сокетами в tpws основано на linux-specific механизме epoll.
|
||||
В BSD для его эмуляции используется epoll-shim - прослойка для эмуляции epoll на базе kqueue.
|
||||
|
||||
Некоторые функции dvtws пришлось реализовывать через хаки.
|
||||
В BSD много ограничений, особенностей и багов при работе с низкоуровневой сетью, в особенности в области ipv6.
|
||||
Казалось бы столько лет прошло, а в коде все еще сидят ограничители 15-20 летней давности.
|
||||
Прямая отсылка ipv6 фреймов с измененным source address и вовсе невозможна через raw sockets.
|
||||
OpenBSD не дает отсылать через raw sockets tcp фреймы.
|
||||
Там, где функции нельзя было реализовать напрямую, либо их реализация привела бы к залезанию в низкоуровневые дебри,
|
||||
используются те же divert сокеты. Оказывается через них можно скармливать ядру любые пакеты, обходя ограничения
|
||||
raw sockets. Не знаю насколько это легально, но пока это работает. Однако, имейте в виду. Что-то может сломаться.
|
||||
|
||||
mdig и ip2net полностью работоспособны в BSD. В них нет ничего системо-зависимого.
|
||||
|
||||
FreeBSD
|
||||
-------
|
||||
|
||||
divert сокеты требуют специального модуля ядра ipdivert.
|
||||
Поместите следующие строки в /boot/loader.conf (создать, если отсутствует) :
|
||||
-----------
|
||||
ipdivert_load="YES"
|
||||
net.inet.ip.fw.default_to_accept=1
|
||||
-----------
|
||||
В /etc/rc.conf :
|
||||
-----------
|
||||
firewall_enable="YES"
|
||||
firewall_script="/etc/rc.firewall.my"
|
||||
-----------
|
||||
/etc/rc.firewall.my :
|
||||
-----------
|
||||
ipfw -q -f flush
|
||||
-----------
|
||||
В /etc/rc.firewall.my можно дописывать правила ipfw, чтобы они восстанавливались после перезагрузки.
|
||||
Оттуда же можно запускать и демоны zapret, добавив в параметры "--daemon". Например так :
|
||||
-----------
|
||||
pkill ^dvtws$
|
||||
/opt/zapret/nfq/dvtws --port=989 --daemon --dpi-desync=split2
|
||||
-----------
|
||||
Для перезапуска фаервола и демонов достаточно будет сделать : /etc/rc.d/ipfw restart
|
||||
|
||||
|
||||
Краткая инструкция по запуску tpws в прозрачном режиме.
|
||||
Предполагается, что интерфейс LAN называется em1, WAN - em0.
|
||||
|
||||
Для всего трафика :
|
||||
ipfw delete 100
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from me to any 80,443 proto ip4 xmit em0 not uid daemon
|
||||
ipfw add 100 fwd ::1,988 tcp from me to any 80,443 proto ip6 xmit em0 not uid daemon
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from any to any 80,443 proto ip4 recv em1
|
||||
ipfw add 100 fwd ::1,988 tcp from any to any 80,443 proto ip6 recv em1
|
||||
/opt/zapret/tpws/tpws --port=988 --user=daemon --bind-addr=::1 --bind-addr=127.0.0.1
|
||||
|
||||
Для трафика только на таблицу zapret, за исключением таблицы nozapret :
|
||||
ipfw delete 100
|
||||
ipfw add 100 allow tcp from me to table\(nozapret\) 80,443
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from me to table\(zapret\) 80,443 proto ip4 xmit em0 not uid daemon
|
||||
ipfw add 100 fwd ::1,988 tcp from me to table\(zapret\) 80,443 proto ip6 xmit em0 not uid daemon
|
||||
ipfw add 100 allow tcp from any to table\(nozapret\) 80,443 recv em1
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from any to any 80,443 proto ip4 recv em1
|
||||
ipfw add 100 fwd ::1,988 tcp from any to any 80,443 proto ip6 recv em1
|
||||
/opt/zapret/tpws/tpws --port=988 --user=daemon --bind-addr=::1 --bind-addr=127.0.0.1
|
||||
|
||||
Таблицы zapret, nozapret, ipban создаются скриптами из ipset по аналогии с Linux.
|
||||
Обновление скриптов можно забить в cron под root :
|
||||
crontab -e
|
||||
Создать строчку "0 12 */2 * * /opt/zapret/ipset/get_config.sh"
|
||||
|
||||
При использовании ipfw tpws не требует повышенных привилегий для реализации прозрачного режима.
|
||||
Однако, без рута невозможен бинд на порты <1024 и смена UID/GID. Без смены UID будет рекурсия,
|
||||
поэтому правила ipfw нужно создавать с учетом UID, под которым работает tpws.
|
||||
Переадресация на порты >=1024 может создать угрозу перехвата трафика непривилегированным
|
||||
процессом, если вдруг tpws не запущен.
|
||||
|
||||
|
||||
Краткая инструкция по запуску dvtws.
|
||||
|
||||
Для всего трафика :
|
||||
ipfw delete 100
|
||||
ipfw add 100 divert 989 tcp from any to any 80,443 out not diverted not sockarg xmit em0
|
||||
/opt/zapret/nfq/dvtws --port=989 ---dpi-desync=split2
|
||||
|
||||
Для трафика только на таблицу zapret, за исключением таблицы nozapret :
|
||||
ipfw delete 100
|
||||
ipfw add 100 allow tcp from me to table\(nozapret\) 80,443
|
||||
ipfw add 100 divert 989 tcp from any to table\(zapret\) 80,443 out not diverted not sockarg xmit em0
|
||||
/opt/zapret/nfq/dvtws --port=989 --dpi-desync=split2
|
||||
|
||||
Недопущение зацикливания - повторного вхождения фейк пакетов на обработку.
|
||||
FreeBSD игнорирует sockarg в ipv6.
|
||||
Это искусственное ограничение в коде ядра, которое тянется уже лет 10-20.
|
||||
Кто-то в свое время посчитал код сырым, и до сих пор никто не удосужился поправить.
|
||||
dvtws в FreeBSD отсылает ipv4 фреймы через raw socket. Такие пакеты не 'diverted'. Они отсекаются по 'sockarg'.
|
||||
Для отсылки ipv6 фейков используется divert socket, потому что ipv6 raw сокеты в BSD не дают самому
|
||||
формировать IP заголовок и подменять source address. Фейки в ipv6 'diverted'. Они отсекаются по 'diverted'.
|
||||
В linux nfqws для недопущения зацикливания используется fwmark.
|
||||
|
||||
|
||||
PF в FreeBSD:
|
||||
Настройка аналогична OpenBSD, но есть важные нюансы.
|
||||
1) Не забыть собрать специальную версию под PF : make CFLAGS=-DUSE_PF
|
||||
2) Нельзя сделать ipv6 rdr на ::1. Нужно делать на link-local адрес входящего интерфейса.
|
||||
Смотрите через ifconfig адрес fe80:... и добавляете в правило
|
||||
3) Синтаксис pf.conf немного отличается. Более новая версия PF.
|
||||
4) Лимит на количество элементов таблиц задается так : sysctl net.pf.request_maxcount=2000000
|
||||
5) Слово 'divert-packet' отсутствует в бинарике pfctl, правила divert-packet выдают ошибку.
|
||||
'divert-to' - это не то. Не похоже, что в FreeBSD можно завести dvtws через PF.
|
||||
/etc/pf.conf
|
||||
-----------
|
||||
rdr pass on em1 inet6 proto tcp to port {80,443} -> fe80::31c:29ff:dee2:1c4d port 988
|
||||
rdr pass on em1 inet proto tcp to port {80,443} -> 127.0.0.1 port 988
|
||||
-----------
|
||||
/opt/zapret/tpws/tpws --port=988 --bind-addr=127.0.0.1 --bind-iface6=em1 --bind-linklocal=force
|
||||
|
||||
В PF непонятно как делать rdr-to с той же системы, где работает proxy. Вариант с route-to у меня не заработал.
|
||||
|
||||
|
||||
OpenBSD
|
||||
-------
|
||||
|
||||
В tpws бинд по умолчанию только на ipv6. для бинда на ipv4 указать "--bind-addr=0.0.0.0"
|
||||
Используйте --bind-addr=0.0.0.0 --bind-addr=:: для достижения того же результата, как в других ОС по умолчанию.
|
||||
(лучше все же так не делать, а сажать на определенные внутренние адреса или интерфейсы)
|
||||
|
||||
tpws для проходящего трафика :
|
||||
|
||||
/etc/pf.conf
|
||||
------------
|
||||
pass in quick on em1 inet proto tcp to port {80,443} rdr-to 127.0.0.1 port 988
|
||||
pass in quick on em1 inet6 proto tcp to port {80,443} rdr-to ::1 port 988
|
||||
------------
|
||||
pfctl -f /etc/pf.conf
|
||||
tpws --port=988 --user=daemon --bind-addr=::1 --bind-addr=127.0.0.1
|
||||
|
||||
В PF непонятно как делать rdr-to с той же системы, где работает proxy. Вариант с route-to у меня не заработал.
|
||||
Поддержка rdr-to реализована через /dev/pf, поэтому прозрачный режим требует root.
|
||||
|
||||
dvtws для всего трафика :
|
||||
|
||||
/etc/pf.conf
|
||||
------------
|
||||
pass out quick on em0 proto tcp to port {80,443} divert-packet port 989
|
||||
------------
|
||||
pfctl -f /etc/pf.conf
|
||||
./dvtws --port=989 --dpi-desync=split2
|
||||
|
||||
dvtws для трафика только на таблицу zapret, за исключением таблицы nozapret :
|
||||
|
||||
/etc/pf.conf
|
||||
------------
|
||||
set limit table-entries 2000000
|
||||
table <zapret> file "/opt/zapret/ipset/zapret-ip.txt"
|
||||
table <zapret-user> file "/opt/zapret/ipset/zapret-ip-user.txt"
|
||||
table <nozapret> file "/opt/zapret/ipset/zapret-ip-exclude.txt"
|
||||
pass out quick on em0 inet proto tcp to <nozapret> port {80,443}
|
||||
pass out quick on em0 inet proto tcp to <zapret> port {80,443} divert-packet port 989
|
||||
pass out quick on em0 inet proto tcp to <zapret-user> port {80,443} divert-packet port 989
|
||||
table <zapret6> file "/opt/zapret/ipset/zapret-ip6.txt"
|
||||
table <zapret6-user> file "/opt/zapret/ipset/zapret-ip-user6.txt"
|
||||
table <nozapret6> file "/opt/zapret/ipset/zapret-ip-exclude6.txt"
|
||||
pass out quick on em0 inet6 proto tcp to <nozapret6> port {80,443}
|
||||
pass out quick on em0 inet6 proto tcp to <zapret6> port {80,443} divert-packet port 989
|
||||
pass out quick on em0 inet6 proto tcp to <zapret6-user> port {80,443} divert-packet port 989
|
||||
------------
|
||||
pfctl -f /etc/pf.conf
|
||||
./dvtws --port=989 --dpi-desync=split2
|
||||
|
||||
|
||||
В OpenBSD dvtws все фейки отсылает через divert socket, поскольку эта возможность через raw sockets заблокирована.
|
||||
Видимо pf автоматически предотвращает повторный заворот diverted фреймов, поэтому проблемы зацикливания нет.
|
||||
|
||||
К сожалению, в PF присутствует "удобная" функция, которая автоматически применяет к правилу divert-packet
|
||||
обратный трафик. Через divert пойдет все соединение, а не только исходящие пакеты.
|
||||
Это добавит огромный ненужный overhead по процессингу входящих пакетов в dvtws, который будет наиболее заметен
|
||||
на скачивании по http/https. Мне не удалось понять как этого избежать.
|
||||
Поэтому использование фильтр-таблиц крайне рекомендовано !
|
||||
|
||||
OpenBSD принудительно пересчитывает tcp checksum после divert, поэтому скорее всего
|
||||
dpi-desync-fooling=badsum у вас не заработает. При использовании этого параметра
|
||||
dvtws предупредит о возможной проблеме.
|
||||
|
||||
Скрипты из ipset не перезагружают таблицы в PF по умолчанию.
|
||||
Чтобы они это делали, добавьте параметр в /opt/zapret/config :
|
||||
LISTS_RELOAD="pfctl -f /etc/pf.conf"
|
||||
Более новые версии pfctl понимают команду перезагрузить только таблицы : pfctl -Tl -f /etc/pf.conf
|
||||
Но это не относится к OpenBSD 6.8. В новых FreeBSD есть.
|
||||
Не забудьте выключить сжатие gzip :
|
||||
GZIP_LISTS=0
|
||||
Если в вашей конфигурации какого-то файла листа нет, то его необходимо исключить из правил PF.
|
||||
Если вдруг листа нет, и он задан в pf.conf, будет ошибка перезагрузки фаервола.
|
||||
После настройки обновление листов можно поместить в cron :
|
||||
crontab -e
|
||||
дописать строчку : 0 12 */2 * * /opt/zapret/ipset/get_config.sh
|
||||
|
||||
Если будете пользоваться скриптом ipset/get_combined.sh, установите GNU grep : pkg_add ggrep.
|
||||
Родной древний как мамонт, безумно медленный с опцией -f.
|
||||
|
||||
|
||||
MacOS
|
||||
-----
|
||||
|
||||
Иначально ядро этой ОС "darwin" основывалось на BSD, потому в ней много похожего на другие версии BSD.
|
||||
Однако, как и в других массовых коммерческих проектах, приоритеты смещаются в сторону от оригинала.
|
||||
Яблочники что хотят, то и творят. Меняют, убирают, оставляют какие-то безумно старые версии API и утилит.
|
||||
То, что уже давно везде обновили, может быть еще древним как мамонт в самой последней версии MacOS.
|
||||
Но кого это волнует ?
|
||||
|
||||
Раньше был ipfw, потом его убрали, заменили на PF.
|
||||
Есть сомнения, что divert сокеты в ядре остались. Попытка создать divert socket не выдает ошибок,
|
||||
но полученный сокет ведет себя точно так же, как raw, со всеми его унаследованными косяками + еще яблочно специфическими.
|
||||
В PF divert-packet не работает. Простой grep бинарика pfctl показывает, что там нет слова "divert",
|
||||
а в других версиях BSD оно есть. dvtws собирается, но совершенно бесполезен.
|
||||
|
||||
tpws удалось адаптировать, он работоспособен. Получение адреса назначения для прозрачного прокси в PF (DIOCNATLOOK)
|
||||
убрали из заголовков в новых SDK, сделав фактически недокументированным.
|
||||
В tpws перенесены некоторые определения из более старых версий яблочных SDK. С ними удалось завести прозрачный режим.
|
||||
Однако, что будет в следующих версиях угадать сложно. Гарантий нет.
|
||||
Еще одной особенностью PF в MacOS является проверка на рута в момент обращения к /dev/pf, чего нет в остальных BSD.
|
||||
tpws по умолчанию сбрасывает рутовые привилегии. Необходимо явно указать параметр --user=root.
|
||||
В остальном PF себя ведет похоже на FreeBSD. Синтаксис pf.conf тот же.
|
||||
|
||||
На MacOS работает редирект как с проходящего трафика, так и с локальной системы через route-to.
|
||||
Поскольку tpws вынужден работать под root, для исключения рекурсии приходится пускать исходящий от root трафик напрямую.
|
||||
Отсюда имеем недостаток : обход DPI для рута работать не будет.
|
||||
|
||||
Если вы пользуетесь MaсOS в качестве ipv6 роутера, то нужно будет решить вопрос с регулярно изменяемым link-local адресом.
|
||||
С некоторых версий MacOS использует по умолчанию постоянные "secured" ipv6 адреса вместо генерируемых на базе MAC адреса.
|
||||
Все замечательно, но есть одна проблема. Постоянными остаются только global scope адреса.
|
||||
Link locals периодически меняются. Смена завязана на системное время. Перезагрузки адрес не меняют,
|
||||
Но если перевести время на день вперед и перезагрузиться - link local станет другим. (по крайней мере в vmware это так)
|
||||
Информации по вопросу крайне мало, но тянет на баг. Не должен меняться link local. Скрывать link local не имеет смысла,
|
||||
а динамический link local нельзя использовать в качестве адреса шлюза.
|
||||
Проще всего отказаться от "secured" адресов.
|
||||
Поместите строчку "net.inet6.send.opmode=0" в /etc/sysctl.conf. Затем перезагрузите систему.
|
||||
Все равно для исходящих соединений будут использоваться temporary адреса, как и в других системах.
|
||||
Или вам идея не по вкусу, можно прописать дополнительный статический ipv6 из диапазона fd00::/8 -
|
||||
выберите любой с длиной префикса 128. Это можно сделать в системных настройках, создав дополнительный адаптер на базе
|
||||
того же сетевого интерфейса, отключить в нем ipv4 и вписать статический ipv6. Он добавится к автоматически настраеваемым.
|
||||
|
||||
Настройка tpws на macos в прозрачном режиме только для исходящих запросов, где en0 - WAN :
|
||||
|
||||
/etc/pf.conf
|
||||
------------
|
||||
rdr pass on lo0 inet proto tcp from !127.0.0.0/8 to any port {80,443} -> 127.0.0.1 port 988
|
||||
rdr pass on lo0 inet6 proto tcp from !::1 to any port {80,443} -> fe80::1 port 988
|
||||
pass out route-to (lo0 127.0.0.1) inet proto tcp from any to any port {80,443} user { >root }
|
||||
pass out route-to (lo0 fe80::1) inet6 proto tcp from any to any port {80,443} user { >root }
|
||||
------------
|
||||
pfctl -ef /etc/pf.conf
|
||||
/opt/zapret/tpws/tpws --user=root --port=988 --bind-addr=127.0.0.1 --bind-iface6=en0 --bind-linklocal=force
|
||||
|
||||
|
||||
Настройка tpws на macos роутере в прозрачном режиме, где en0 - WAN, en1 - LAN.
|
||||
|
||||
ifconfig en1 | grep fe80
|
||||
inet6 fe80::bbbb:bbbb:bbbb:bbbb%en1 prefixlen 64 scopeid 0x8
|
||||
/etc/pf.conf
|
||||
------------
|
||||
rdr pass on en1 inet proto tcp from any to any port {80,443} -> 127.0.0.1 port 988
|
||||
rdr pass on en1 inet6 proto tcp from any to any port {80,443} -> fe80::bbbb:bbbb:bbbb:bbbb port 988
|
||||
rdr pass on lo0 inet proto tcp from !127.0.0.0/8 to any port {80,443} -> 127.0.0.1 port 988
|
||||
rdr pass on lo0 inet6 proto tcp from !::1 to any port {80,443} -> fe80::1 port 988
|
||||
pass out route-to (lo0 127.0.0.1) inet proto tcp from any to any port {80,443} user { >root }
|
||||
pass out route-to (lo0 fe80::1) inet6 proto tcp from any to any port {80,443} user { >root }
|
||||
------------
|
||||
pfctl -ef /etc/pf.conf
|
||||
/opt/zapret/tpws/tpws --user=root --port=988 --bind-addr=127.0.0.1 --bind-iface6=en0 --bind-linklocal=force --bind-iface6=en1 --bind-linklocal=force
|
||||
|
||||
|
||||
Сборка : make -C /opt/zapret mac
|
||||
|
||||
Скрипты получения листов ipset/*.sh работают.
|
||||
Если будете пользоваться ipset/get_combined.sh, нужно установить gnu grep через brew.
|
||||
Имеющийся очень старый и безумно медленный с оцией -f.
|
||||
|
||||
|
||||
MacOS простая установка
|
||||
-----------------------
|
||||
|
||||
В MacOS поддерживается install_easy.sh
|
||||
|
||||
В комплекте идут бинарики, собраные под 64-bit с опцией -mmacosx-version-min=10.8.
|
||||
Они должны работать на всех поддерживаемых версиях macos.
|
||||
Если вдруг не работают - можно собрать свои. Developer tools ставятся автоматом при запуске make.
|
||||
|
||||
!! Internet sharing средствами системы НЕ ПОДДЕРЖИВАЕТСЯ !!
|
||||
Поддерживается только роутер, настроенный своими силами через PF.
|
||||
Если вы вдруг включили шаринг, а потом выключили, то доступ к сайтам может пропасть совсем.
|
||||
Лечение : pfctl -f /etc/pf.conf
|
||||
Если вам нужен шаринг интернета, лучше отказаться от прозрачного режима и использовать socks.
|
||||
|
||||
Для автостарта используется launchd (/Library/LaunchDaemons/zapret.plist)
|
||||
Управляющий скрипт : /opt/zapret/init.d/macos/zapret
|
||||
Следующие команды работают с tpws и фаерволом одновременно (если INIT_APPLY_FW=1 в config)
|
||||
/opt/zapret/init.d/macos/zapret start
|
||||
/opt/zapret/init.d/macos/zapret stop
|
||||
/opt/zapret/init.d/macos/zapret restart
|
||||
Работа только с tpws :
|
||||
/opt/zapret/init.d/macos/zapret start-daemons
|
||||
/opt/zapret/init.d/macos/zapret stop-daemons
|
||||
/opt/zapret/init.d/macos/zapret restart-daemons
|
||||
Работа только с PF :
|
||||
/opt/zapret/init.d/macos/zapret start-fw
|
||||
/opt/zapret/init.d/macos/zapret stop-fw
|
||||
/opt/zapret/init.d/macos/zapret restart-fw
|
||||
Перезагрузка всех IP таблиц из файлов :
|
||||
/opt/zapret/init.d/macos/zapret reload-fw-tables
|
||||
|
||||
Инсталятор настраивает LISTS_RELOAD в config, так что скрипты ipset/*.sh автоматически перезагружают IP таблицы в PF.
|
||||
Автоматически создается cron job на ipset/get_config.sh, по аналогии с openwrt.
|
||||
|
||||
При start-fw скрипт автоматически модицифирует /etc/pf.conf, вставляя туда anchors "zapret".
|
||||
Модификация расчитана на pf.conf, в котором сохранены дефолтные anchors от apple.
|
||||
Если у вас измененный pf.conf и модификация не удалась, об этом будет предупреждение. Не игнорируйте его.
|
||||
В этом случае вам нужно вставить в свой pf.conf (в соответствии с порядком типов правил) :
|
||||
rdr-anchor "zapret"
|
||||
anchor "zapret"
|
||||
При деинсталяции через uninstall_easy.sh модификации pf.conf убираются.
|
||||
|
||||
start-fw создает 3 файла anchors в /etc/pf.anchors : zapret,zapret-v4,zapret-v6.
|
||||
Последние 2 подключаются из anchor "zapret".
|
||||
Таблицы nozapret,nozapret6 принадлежат anchor "zapret".
|
||||
Таблицы zapret,zapret-user - в anchor "zapret-v4".
|
||||
Таблицы zapret6,zapret6-user - в anchor "zapret-v6".
|
||||
Если какая-то версия протокола отключена - соответствующий anchor пустой и не упоминается в anchor "zapret".
|
||||
Таблицы и правила создаются только на те листы, которые фактически есть в директории ipset.
|
89
docs/bsdfw.txt
Normal file
89
docs/bsdfw.txt
Normal file
@@ -0,0 +1,89 @@
|
||||
WAN=em0 LAN=em1
|
||||
|
||||
FreeBSD IPFW :
|
||||
|
||||
ipfw delete 100
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from me to any 80,443 proto ip4 xmit em0 not uid daemon
|
||||
ipfw add 100 fwd ::1,988 tcp from me to any 80,443 proto ip6 xmit em0 not uid daemon
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from any to any 80,443 proto ip4 recv em1
|
||||
ipfw add 100 fwd ::1,988 tcp from any to any 80,443 proto ip6 recv em1
|
||||
|
||||
ipfw delete 100
|
||||
ipfw add 100 allow tcp from me to table\(nozapret\) 80,443
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from me to table\(zapret\) 80,443 proto ip4 xmit em0 not uid daemon
|
||||
ipfw add 100 fwd ::1,988 tcp from me to table\(zapret\) 80,443 proto ip6 xmit em0 not uid daemon
|
||||
ipfw add 100 allow tcp from any to table\(nozapret\) 80,443 recv em1
|
||||
ipfw add 100 fwd 127.0.0.1,988 tcp from any to any 80,443 proto ip4 recv em1
|
||||
ipfw add 100 fwd ::1,988 tcp from any to any 80,443 proto ip6 recv em1
|
||||
|
||||
/opt/zapret/tpws/tpws --port=988 --user=daemon --bind-addr=::1 --bind-addr=127.0.0.1
|
||||
|
||||
|
||||
; Loop avoidance.
|
||||
; FreeBSD artificially ignores sockarg for ipv6 in the kernel.
|
||||
; This limitation is coming from the ipv6 early age. Code is still in "testing" state. 10-20 years. Everybody forgot about it.
|
||||
; dvtws sends ipv6 forged frames using another divert socket (HACK). they can be filtered out using 'diverted'.
|
||||
|
||||
|
||||
ipfw delete 100
|
||||
ipfw add 100 divert 989 tcp from any to any 80,443 out not diverted not sockarg xmit em0
|
||||
|
||||
ipfw delete 100
|
||||
ipfw add 100 allow tcp from me to table\(nozapret\) 80,443
|
||||
ipfw add 100 divert 989 tcp from any to table\(zapret\) 80,443 out not diverted not sockarg xmit em0
|
||||
|
||||
/opt/zapret/nfq/dvtws --port=989 --debug --dpi-desync=split
|
||||
|
||||
|
||||
sample ipfw NAT setup :
|
||||
|
||||
WAN=em0
|
||||
LAN=em1
|
||||
ipfw -q flush
|
||||
ipfw -q nat 1 config if $WAN unreg_only reset
|
||||
ipfw -q add 10 allow ip from any to any via $LAN
|
||||
ipfw -q add 20 allow ip from any to any via lo0
|
||||
ipfw -q add 300 nat 1 ip4 from any to any in recv $WAN
|
||||
ipfw -q add 301 check-state
|
||||
ipfw -q add 350 skipto 390 tcp from any to any out xmit $WAN setup keep-state
|
||||
ipfw -q add 350 skipto 390 udp from any to any out xmit $WAN keep-state
|
||||
ipfw -q add 360 allow all from any to me in recv $WAN
|
||||
ipfw -q add 390 nat 1 ip4 from any to any out xmit $WAN
|
||||
ipfw -q add 10000 allow ip from any to any
|
||||
|
||||
Forwarding :
|
||||
sysctl net.inet.ip.forwarding=1
|
||||
sysctl net.inet6.ip6.forwarding=1
|
||||
|
||||
|
||||
OpenBSD PF :
|
||||
|
||||
; dont know how to rdr-to from local system. doesn't seem to work. only works for routed traffic.
|
||||
|
||||
/etc/pf.conf
|
||||
pass in quick on em1 inet proto tcp to port {80,443} rdr-to 127.0.0.1 port 988
|
||||
pass in quick on em1 inet6 proto tcp to port {80,443} rdr-to ::1 port 988
|
||||
pfctl -f /etc/pf.conf
|
||||
/opt/zapret/tpws/tpws --port=988 --user=daemon --bind-addr=::1 --bind-addr=127.0.0.1
|
||||
|
||||
; dvtws works both for routed and local
|
||||
|
||||
pass out quick on em0 proto tcp to port {80,443} divert-packet port 989
|
||||
pfctl -f /etc/pf.conf
|
||||
./dvtws --port=989 --dpi-desync=split2
|
||||
|
||||
; dvtws with table limitations : to zapret,zapret6 but not to nozapret,nozapret6
|
||||
; reload tables : pfctl -f /etc/pf.conf
|
||||
set limit table-entries 2000000
|
||||
table <zapret> file "/opt/zapret/ipset/zapret-ip.txt"
|
||||
table <zapret-user> file "/opt/zapret/ipset/zapret-ip-user.txt"
|
||||
table <nozapret> file "/opt/zapret/ipset/zapret-ip-exclude.txt"
|
||||
pass out quick on em0 inet proto tcp to <nozapret> port {80,443}
|
||||
pass out quick on em0 inet proto tcp to <zapret> port {80,443} divert-packet port 989
|
||||
pass out quick on em0 inet proto tcp to <zapret-user> port {80,443} divert-packet port 989
|
||||
table <zapret6> file "/opt/zapret/ipset/zapret-ip6.txt"
|
||||
table <zapret6-user> file "/opt/zapret/ipset/zapret-ip-user6.txt"
|
||||
table <nozapret6> file "/opt/zapret/ipset/zapret-ip-exclude6.txt"
|
||||
pass out quick on em0 inet6 proto tcp to <nozapret6> port {80,443}
|
||||
pass out quick on em0 inet6 proto tcp to <zapret6> port {80,443} divert-packet port 989
|
||||
pass out quick on em0 inet6 proto tcp to <zapret6-user> port {80,443} divert-packet port 989
|
191
docs/changes.txt
Normal file
191
docs/changes.txt
Normal file
@@ -0,0 +1,191 @@
|
||||
v1
|
||||
|
||||
Initial release
|
||||
|
||||
v2
|
||||
|
||||
nfqws : command line options change. now using standard getopt.
|
||||
nfqws : added options for window size changing and "Host:" case change
|
||||
ISP support : tested on mns.ru and beeline (corbina)
|
||||
init scripts : rewritten init scripts for simple choise of ISP
|
||||
create_ipset : now using 'ipset restore', it works much faster
|
||||
readme : updated. now using UTF-8 charset.
|
||||
|
||||
v3
|
||||
|
||||
tpws : added transparent proxy (supports TPROXY and DNAT).
|
||||
can help when ISP tracks whole HTTP session, not only the beginning
|
||||
ipset : added zapret-hosts-user.txt which contain user defined host names to be resolved
|
||||
and added to zapret ip list
|
||||
ISP support : dom.ru support via TPROXY/DNAT
|
||||
ISP support : successfully tested sknt.ru on 'domru' configuration
|
||||
other configs will probably also work, but cannot test
|
||||
compile : openwrt compile howto
|
||||
|
||||
v4
|
||||
|
||||
tpws : added ability to insert extra space after http method : "GET /" => "GET /"
|
||||
ISP support : TKT support
|
||||
|
||||
v5
|
||||
|
||||
nfqws : ipv6 support in nfqws
|
||||
|
||||
v6
|
||||
|
||||
ipset : added "get_antizapret.sh"
|
||||
|
||||
v7
|
||||
|
||||
tpws : added ability to insert "." after Host: name
|
||||
|
||||
v8
|
||||
|
||||
openwrt init : removed hotplug.d/firewall because of race conditions. now only use /etc/firewall.user
|
||||
|
||||
v9
|
||||
|
||||
ipban : added ipban ipset. place domains banned by ip to zapret-hosts-user-ipban.txt
|
||||
these IPs must be soxified for both http and https
|
||||
ISP support : tiera support
|
||||
ISP support : added DNS filtering to ubuntu and debian scripts
|
||||
|
||||
v10
|
||||
|
||||
tpws : added split-pos option. split every message at specified position
|
||||
|
||||
v11
|
||||
|
||||
ipset : scripts optimizations
|
||||
|
||||
v12
|
||||
|
||||
nfqws : fix wrong tcp checksum calculation if packet length is odd and platform is big-endian
|
||||
|
||||
v13
|
||||
|
||||
added binaries
|
||||
|
||||
v14
|
||||
|
||||
change get_antizapret script to work with https://github.com/zapret-info/z-i/raw/master/dump.csv
|
||||
filter out 192.168.*, 127.*, 10.* from blocked ips
|
||||
|
||||
v15
|
||||
|
||||
added --hostspell option to nfqws and tpws
|
||||
ISP support : beeline now catches "host" but other spellings still work
|
||||
openwrt/LEDE : changed init script to work with procd
|
||||
tpws, nfqws : minor cosmetic fixes
|
||||
|
||||
v16
|
||||
|
||||
tpws: split-http-req=method : split inside method name, not after
|
||||
ISP support : mns.ru changed split pos to 3 (got redirect page with HEAD req : curl -I ej.ru)
|
||||
|
||||
v17
|
||||
|
||||
ISP support : athome moved from nfqws to tpws because of instability and http request hangs
|
||||
tpws : added options unixeol,methodeol,hosttab
|
||||
|
||||
v18
|
||||
|
||||
tpws,nfqws : added hostnospace option
|
||||
|
||||
v19
|
||||
|
||||
tpws : added hostlist option
|
||||
|
||||
v20
|
||||
|
||||
added ip2net. ip2net groups ips from iplist into subnets and reduces ipset size twice
|
||||
|
||||
v21
|
||||
|
||||
added mdig. get_reestr.sh is *real* again
|
||||
|
||||
v22
|
||||
|
||||
total review of init script logic
|
||||
dropped support of older debian 7 and ubuntu 12/14 systems
|
||||
install_bin.sh : auto binaries preparation
|
||||
docs: readme review. some new topics added, others deleted
|
||||
docs: VPN setup with policy based routing using wireguard
|
||||
docs: wireguard modding guide
|
||||
|
||||
v23
|
||||
|
||||
major init system rewrite
|
||||
openwrt : separate firewall include /etc/firewall.zapret
|
||||
install_easy.sh : easy setup on openwrt, debian, ubuntu, centos, fedora, opensuse
|
||||
|
||||
v24
|
||||
|
||||
separate config from init scripts
|
||||
gzip support in ipset/*.sh and tpws
|
||||
|
||||
v25
|
||||
|
||||
init : move to native systemd units
|
||||
use links to units, init scripts and firewall includes, no more copying
|
||||
|
||||
v26
|
||||
|
||||
ipv6 support
|
||||
tpws : advanced bind options
|
||||
|
||||
v27
|
||||
|
||||
tpws : major connection code rewrite. originally it was derived from not top quality example , with many bugs and potential problems.
|
||||
next generation connection code uses nonblocking sockets. now its in EXPERIMENTAL state.
|
||||
|
||||
v28
|
||||
|
||||
tpws : added socks5 support
|
||||
ipset : major RKN getlist rewrite. added antifilter.network support
|
||||
|
||||
v29
|
||||
|
||||
nfqws : DPI desync attack
|
||||
ip exclude system
|
||||
|
||||
v30
|
||||
|
||||
nfqws : DPI desync attack modes : fake,rst
|
||||
|
||||
v31
|
||||
|
||||
nfqws : DPI desync attack modes : disorder,disorder2,split,split2.
|
||||
nfqws : DPI desync fooling mode : badseq. multiple modes supported
|
||||
|
||||
v32
|
||||
|
||||
tpws : multiple binds
|
||||
init scripts : run only one instance of tpws in any case
|
||||
|
||||
v33
|
||||
|
||||
openwrt : flow offloading support
|
||||
config : MODE refactoring
|
||||
|
||||
v34
|
||||
|
||||
nfqws : dpi-desync 2 mode combos
|
||||
nfqws : dpi-desync without parameter no more supported. previously it meant "fake"
|
||||
nfqws : custom fake http request and tls client hello
|
||||
|
||||
v35
|
||||
|
||||
limited FreeBSD and OpenBSD support
|
||||
|
||||
v36
|
||||
|
||||
full FreeBSD and OpenBSD support
|
||||
|
||||
v37
|
||||
|
||||
limited MacOS support
|
||||
|
||||
v38
|
||||
|
||||
MacOS easy install
|
42
docs/compile/build_howto_openwrt.txt
Normal file
42
docs/compile/build_howto_openwrt.txt
Normal file
@@ -0,0 +1,42 @@
|
||||
How to compile native programs for use in openwrt
|
||||
-------------------------------------------------
|
||||
|
||||
1) <fetch correct version of openwrt>
|
||||
|
||||
cd ~
|
||||
|
||||
<chaos calmer>
|
||||
git clone git://git.openwrt.org/15.05/openwrt.git
|
||||
<barrier breaker>
|
||||
git clone git://git.openwrt.org/14.07/openwrt.git
|
||||
<trunk>
|
||||
git clone git://git.openwrt.org/openwrt.git
|
||||
|
||||
cd openwrt
|
||||
|
||||
2) ./scripts/feeds update -a
|
||||
./scripts/feeds install -a
|
||||
|
||||
3) #add zapret packages to build root
|
||||
#copy package descriptions
|
||||
copy compile/openwrt/* to ~/openwrt
|
||||
#copy source code of tpws
|
||||
copy tpws to ~/openwrt/package/zapret/tpws
|
||||
#copy source code of nfq
|
||||
copy nfq to ~/openwrt/package/zapret/nfq
|
||||
#copy source code of ip2net
|
||||
copy ip2net to ~/openwrt/package/zapret/ip2net
|
||||
|
||||
4) make menuconfig
|
||||
#select your target architecture
|
||||
#select packages Network/Zapret/* as "M"
|
||||
|
||||
5) make toolchain/compile
|
||||
|
||||
6) make package/tpws/compile
|
||||
make package/nfqws/compile
|
||||
make package/ip2net/compile
|
||||
make package/mdig/compile
|
||||
|
||||
7) find bin -name tpws*.ipk
|
||||
#take your tpws*.ipk , nfqws*.ipk , ip2net*.ipk, mdig*.ipk from there
|
32
docs/compile/openwrt/package/zapret/ip2net/Makefile
Normal file
32
docs/compile/openwrt/package/zapret/ip2net/Makefile
Normal file
@@ -0,0 +1,32 @@
|
||||
#
|
||||
|
||||
include $(TOPDIR)/rules.mk
|
||||
|
||||
PKG_NAME:=ip2net
|
||||
PKG_RELEASE:=1
|
||||
|
||||
include $(INCLUDE_DIR)/package.mk
|
||||
|
||||
define Package/ip2net
|
||||
SECTION:=net
|
||||
CATEGORY:=Network
|
||||
TITLE:=ip2net
|
||||
SUBMENU:=Zapret
|
||||
endef
|
||||
|
||||
define Build/Prepare
|
||||
mkdir -p $(PKG_BUILD_DIR)
|
||||
$(CP) ./ip2net/* $(PKG_BUILD_DIR)/
|
||||
endef
|
||||
|
||||
define Build/Compile
|
||||
$(MAKE) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS)
|
||||
endef
|
||||
|
||||
define Package/ip2net/install
|
||||
$(INSTALL_DIR) $(1)/opt/zapret/ip2net
|
||||
$(INSTALL_BIN) $(PKG_BUILD_DIR)/ip2net $(1)/opt/zapret/ip2net
|
||||
endef
|
||||
|
||||
$(eval $(call BuildPackage,ip2net))
|
||||
|
1
docs/compile/openwrt/package/zapret/ip2net/readme.txt
Normal file
1
docs/compile/openwrt/package/zapret/ip2net/readme.txt
Normal file
@@ -0,0 +1 @@
|
||||
Copy "ip2net" folder here !
|
32
docs/compile/openwrt/package/zapret/mdig/Makefile
Normal file
32
docs/compile/openwrt/package/zapret/mdig/Makefile
Normal file
@@ -0,0 +1,32 @@
|
||||
#
|
||||
|
||||
include $(TOPDIR)/rules.mk
|
||||
|
||||
PKG_NAME:=mdig
|
||||
PKG_RELEASE:=1
|
||||
|
||||
include $(INCLUDE_DIR)/package.mk
|
||||
|
||||
define Package/mdig
|
||||
SECTION:=net
|
||||
CATEGORY:=Network
|
||||
TITLE:=mdig
|
||||
SUBMENU:=Zapret
|
||||
endef
|
||||
|
||||
define Build/Prepare
|
||||
mkdir -p $(PKG_BUILD_DIR)
|
||||
$(CP) ./mdig/* $(PKG_BUILD_DIR)/
|
||||
endef
|
||||
|
||||
define Build/Compile
|
||||
$(MAKE) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS)
|
||||
endef
|
||||
|
||||
define Package/mdig/install
|
||||
$(INSTALL_DIR) $(1)/opt/zapret/mdig
|
||||
$(INSTALL_BIN) $(PKG_BUILD_DIR)/mdig $(1)/opt/zapret/mdig
|
||||
endef
|
||||
|
||||
$(eval $(call BuildPackage,mdig))
|
||||
|
1
docs/compile/openwrt/package/zapret/mdig/readme.txt
Normal file
1
docs/compile/openwrt/package/zapret/mdig/readme.txt
Normal file
@@ -0,0 +1 @@
|
||||
Copy "mdig" folder here !
|
34
docs/compile/openwrt/package/zapret/nfqws/Makefile
Normal file
34
docs/compile/openwrt/package/zapret/nfqws/Makefile
Normal file
@@ -0,0 +1,34 @@
|
||||
#
|
||||
|
||||
include $(TOPDIR)/rules.mk
|
||||
|
||||
PKG_NAME:=nfqws
|
||||
PKG_RELEASE:=1
|
||||
|
||||
include $(INCLUDE_DIR)/package.mk
|
||||
|
||||
define Package/nfqws
|
||||
SECTION:=net
|
||||
CATEGORY:=Network
|
||||
TITLE:=nfqws
|
||||
SUBMENU:=Zapret
|
||||
DEPENDS:=+libnetfilter-queue +libcap +zlib
|
||||
endef
|
||||
|
||||
define Build/Prepare
|
||||
mkdir -p $(PKG_BUILD_DIR)
|
||||
$(CP) ./nfq/* $(PKG_BUILD_DIR)/
|
||||
endef
|
||||
|
||||
define Build/Compile
|
||||
$(MAKE) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS)
|
||||
endef
|
||||
|
||||
define Package/nfqws/install
|
||||
$(INSTALL_DIR) $(1)/opt/zapret/nfq
|
||||
$(INSTALL_BIN) $(PKG_BUILD_DIR)/nfqws $(1)/opt/zapret/nfq
|
||||
endef
|
||||
|
||||
$(eval $(call BuildPackage,nfqws))
|
||||
|
||||
|
1
docs/compile/openwrt/package/zapret/nfqws/readme.txt
Normal file
1
docs/compile/openwrt/package/zapret/nfqws/readme.txt
Normal file
@@ -0,0 +1 @@
|
||||
Copy "nfq" folder here !
|
33
docs/compile/openwrt/package/zapret/tpws/Makefile
Normal file
33
docs/compile/openwrt/package/zapret/tpws/Makefile
Normal file
@@ -0,0 +1,33 @@
|
||||
#
|
||||
|
||||
include $(TOPDIR)/rules.mk
|
||||
|
||||
PKG_NAME:=tpws
|
||||
PKG_RELEASE:=1
|
||||
|
||||
include $(INCLUDE_DIR)/package.mk
|
||||
|
||||
define Package/tpws
|
||||
SECTION:=net
|
||||
CATEGORY:=Network
|
||||
TITLE:=tpws
|
||||
SUBMENU:=Zapret
|
||||
DEPENDS:=+zlib +libcap
|
||||
endef
|
||||
|
||||
define Build/Prepare
|
||||
mkdir -p $(PKG_BUILD_DIR)
|
||||
$(CP) ./tpws/* $(PKG_BUILD_DIR)/
|
||||
endef
|
||||
|
||||
define Build/Compile
|
||||
$(MAKE) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS)
|
||||
endef
|
||||
|
||||
define Package/tpws/install
|
||||
$(INSTALL_DIR) $(1)/opt/zapret/tpws
|
||||
$(INSTALL_BIN) $(PKG_BUILD_DIR)/tpws $(1)/opt/zapret/tpws
|
||||
endef
|
||||
|
||||
$(eval $(call BuildPackage,tpws))
|
||||
|
1
docs/compile/openwrt/package/zapret/tpws/readme.txt
Normal file
1
docs/compile/openwrt/package/zapret/tpws/readme.txt
Normal file
@@ -0,0 +1 @@
|
||||
Copy "tpws" folder here !
|
159
docs/https.txt
Normal file
159
docs/https.txt
Normal file
@@ -0,0 +1,159 @@
|
||||
Расскажу как я решал вопрос с блокировкой https на роутере.
|
||||
На тех провайдерах, что мне доступны, все, кроме одного либо банили https по IP (вообще нет конекта), либо захватывали TLS сессию и она намертво зависала - пакеты больше не приходили. На домру удалось выяснить, что DPI цепляется к SNI (Server Name Indication) в TLS, но сплит TLS запроса не помог. Я пришел к выводу, что https самым разумным будет прозрачно заворачивать в socks.
|
||||
Tor поддерживает "из коробки" режим transparent proxy. Это можно использовать в теории, но практически - только на роутерах с 128 мб памяти и выше. Таких роутеров не так много. В основном объем памяти 32 или 64 мб. И тор еще и тормозной.
|
||||
Другой вариант напрашивается, если у вас есть доступ к какой-нибудь unix системе с SSH, где сайты не блокируются. Например, у вас есть VPS вне России. Именно так и поступил.
|
||||
Понятийно требуются следующие шаги :
|
||||
1) Выделять IP, на которые надо проксировать трафик. У нас уже имеется ipset "zapret", технология создания которого отработана.
|
||||
2) Сделать так, чтобы все время при загрузке системы на некотором порту возникал socks.
|
||||
3) Установить transparent соксификатор. Redsocks прекрасно подошел на эту роль.
|
||||
4) Завернуть через iptables трафик с порта назначения 443 и на ip адреса из ipset 'zapret' на соксификатор
|
||||
Буду рассматривать систему на базе openwrt, где уже установлена система обхода dpi "zapret".
|
||||
По крайней мере нужно иметь заполненный ipset 'zapret', устанавливать tpws или nfqws не обязательно.
|
||||
Более того, если они на вашей системе не срабатывают, то можно соксифицировать не только https, но и http.
|
||||
|
||||
* Сделать так, чтобы все время при загрузке системы на некотором порту возникал socks
|
||||
|
||||
Т.к. дефолтный dropbear клиент не поддерживает создание socks, то для начала придется заменить dropbear ssh client на openssh : пакеты openssh-client и openssh-client-utils.
|
||||
Устанавливать их нужно с опцией opkg --force-overwrite, поскольку они перепишут ssh клиент от dropbear.
|
||||
После установки пакетов расслабим неоправданно жестокие права : chmod 755 /etc/ssh.
|
||||
Следует создать пользователя, под которым будем крутить ssh client. Допустим, это будет 'proxy'.
|
||||
Сначала установить пакет shadow-useradd.
|
||||
------------------
|
||||
useradd -d /home/proxy proxy
|
||||
mkdir -p /home/proxy
|
||||
chown proxy:proxy /home/proxy
|
||||
------------------
|
||||
Openssh ловит разные глюки, если у него нет доступа к /dev/tty.
|
||||
Добавим в /etc/rc.local строчку : "chmod 666 /dev/tty"
|
||||
Сгенерируем для него ключ RSA для доступа к ssh серверу.
|
||||
------------------
|
||||
su proxy
|
||||
cd
|
||||
mkdir -m 700 .ssh
|
||||
cd .ssh
|
||||
ssh-keygen
|
||||
ls
|
||||
exit
|
||||
------------------
|
||||
Должны получиться файлы id_rsa и id_rsa.pub.
|
||||
Строчку из id_rsa.pub следует добавить на ssh сервер в файл $HOME/.ssh/authorized_keys.
|
||||
Более подробно о доступе к ssh через авторизацию по ключам : https://beget.com/ru/articles/ssh_by_key
|
||||
Предположим, ваш ssh сервер - vps.mydomain.com, пользователь называется 'proxy'.
|
||||
Проверить подключение можно так : ssh -N -D 1098 -l proxy vps.mydomain.com.
|
||||
Сделайте это под пользователем "proxy", поскольку при первом подключении ssh спросит о правильности hostkey.
|
||||
Соединение может отвалиться в любой момент, поэтому нужно зациклить запуск ssh.
|
||||
Для этого лучший вариант - использовать procd - упрощенная замена systemd на openwrt версий BB и выше.
|
||||
--- /etc/init.d/socks_vps ---
|
||||
#!/bin/sh /etc/rc.common
|
||||
START=50
|
||||
STOP=50
|
||||
USE_PROCD=1
|
||||
USERNAME=proxy
|
||||
COMMAND="ssh -N -D 1098 -l proxy vps.mydomain.com"
|
||||
start_service() {
|
||||
procd_open_instance
|
||||
procd_set_param user $USERNAME
|
||||
procd_set_param respawn 10 10 0
|
||||
procd_set_param command $COMMAND
|
||||
procd_close_instance
|
||||
}
|
||||
-----------------------------
|
||||
Этому файлу нужно дать права : chmod +x /etc/init.d/socks_vps
|
||||
Запуск : /etc/init.d/socks_vps start
|
||||
Останов : /etc/init.d/socks_vps stop
|
||||
Включить автозагрузку : /etc/init.d/socks_vps enable
|
||||
Проверка : curl -4 --socks5 127.0.0.1:1098 https://rutracker.org
|
||||
|
||||
* Организовать прозрачную соксификацию
|
||||
|
||||
Установить пакет redsocks.
|
||||
Конфиг :
|
||||
-- /etc/redsocks.conf : ---
|
||||
base {
|
||||
log_debug = off;
|
||||
log_info = on;
|
||||
log = "syslog:local7";
|
||||
daemon = on;
|
||||
user = nobody;
|
||||
group = nogroup;
|
||||
redirector = iptables;
|
||||
}
|
||||
redsocks {
|
||||
local_ip = 127.0.0.1;
|
||||
local_port = 1099;
|
||||
ip = 127.0.0.1;
|
||||
port = 1098;
|
||||
type = socks5;
|
||||
}
|
||||
---------------------------
|
||||
После чего перезапускаем : /etc/init.d/redsocks restart
|
||||
Смотрим появился ли листенер : netstat -tnlp | grep 1099
|
||||
Автостарт redsocks при таком конфиге не работает, потому что на момент запуска сеть не инициализирована, и у нас даже нет 127.0.0.1.
|
||||
Вместо штатного автостарта будем вешаться на события поднятия интерфейса. Разберем это позже.
|
||||
Пока что отключим автостарт : /etc/init.d/redsocks disable
|
||||
|
||||
* Завертывание соединений через iptables
|
||||
|
||||
Будем завертывать любые tcp соединения на ip из ipset "ipban" и https на ip из ipset "zapret".
|
||||
|
||||
--- /etc/firewall.user -----
|
||||
SOXIFIER_PORT=1099
|
||||
|
||||
. /opt/zapret/init.d/openwrt/functions
|
||||
|
||||
create_ipset no-update
|
||||
|
||||
network_find_wan_all wan_iface
|
||||
for ext_iface in $wan_iface; do
|
||||
network_get_device ext_device $ext_iface
|
||||
ipt OUTPUT -t nat -o $ext_device -p tcp --dport 443 -m set --match-set zapret dst -j REDIRECT --to-port $SOXIFIER_PORT
|
||||
ipt OUTPUT -t nat -o $ext_device -p tcp -m set --match-set ipban dst -j REDIRECT --to-port $SOXIFIER_PORT
|
||||
done
|
||||
|
||||
network_get_device DEVICE lan
|
||||
sysctl -w net.ipv4.conf.$DEVICE.route_localnet=1
|
||||
ipt prerouting_lan_rule -t nat -p tcp --dport 443 -m set --match-set zapret dst -j DNAT --to 127.0.0.1:$SOXIFIER_PORT
|
||||
ipt prerouting_lan_rule -t nat -p tcp -m set --match-set ipban dst -j DNAT --to 127.0.0.1:$SOXIFIER_PORT
|
||||
----------------------------
|
||||
|
||||
Внести параметр "reload" в указанное место :
|
||||
--- /etc/config/firewall ---
|
||||
config include
|
||||
option path '/etc/firewall.user'
|
||||
option reload '1'
|
||||
----------------------------
|
||||
|
||||
Перезапуск : /etc/init.d/firewall restart
|
||||
Все, теперь можно проверять :
|
||||
/etc/init.d/redsocks stop
|
||||
curl -4 https://rutracker.org
|
||||
# должно обломаться с надписью "Connection refused". если не обламывается - значит ip адрес rutracker.org не в ipset,
|
||||
# либо не сработали правила фаервола. например, из-за не установленных модулей ipt
|
||||
/etc/init.d/redsocks start
|
||||
curl -4 https://rutracker.org
|
||||
# должно выдать страницу
|
||||
|
||||
* Автозапуск redsocks
|
||||
|
||||
Я сделал для себя небольшой скриптик, вешающийся на события поднятия и опускания интерфейсов.
|
||||
|
||||
--- /etc/hotplug.d/iface/99-exec-on-updown ---
|
||||
#!/bin/sh
|
||||
if [ "$ACTION" = ifup ]; then
|
||||
cmd=$(uci get network.$INTERFACE.exec_on_up)
|
||||
[ -n "$cmd" ] && $cmd
|
||||
fi
|
||||
if [ "$ACTION" = ifdown ]; then
|
||||
cmd=$(uci get network.$INTERFACE.exec_on_down)
|
||||
[ -n "$cmd" ] && $cmd
|
||||
fi
|
||||
----------------------------------------------
|
||||
|
||||
Теперь можно в описания интерфейсов внести в соответствующий раздел :
|
||||
--- /etc/config/nework ---
|
||||
config interface 'wan'
|
||||
........
|
||||
option exec_on_up '/etc/init.d/redsocks start'
|
||||
--------------------------
|
||||
reboot. Заходим снова, смотрим, что есть redsocks, есть ssh, опять проверяем curl -4 https://rutracker.org.
|
||||
Пробуем зайти на https://rutracker.org с компа внутри локалки.
|
62
docs/iptables.txt
Normal file
62
docs/iptables.txt
Normal file
@@ -0,0 +1,62 @@
|
||||
For window size changing :
|
||||
|
||||
iptables -t mangle -I PREROUTING -p tcp --sport 80 --tcp-flags SYN,ACK SYN,ACK -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
iptables -t mangle -I PREROUTING -p tcp --sport 80 --tcp-flags SYN,ACK SYN,ACK -m set --match-set zapret src -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
|
||||
For outgoing data manipulation ("Host:" case changing) :
|
||||
|
||||
iptables -t mangle -I POSTROUTING -p tcp --dport 80 -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
iptables -t mangle -I POSTROUTING -p tcp --dport 80 -m set --match-set zapret dst -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
iptables -t mangle -I POSTROUTING -p tcp --dport 80 -m set --match-set zapret dst -m connbytes --connbytes-dir=original --connbytes-mode=packets --connbytes 1:5 -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
|
||||
For dpi desync attack :
|
||||
|
||||
iptables -t mangle -I POSTROUTING -p tcp -m multiport --dports 80,443 -m connbytes --connbytes-dir=original --connbytes-mode=packets --connbytes 2:4 -m mark ! --mark 0x40000000/0x40000000 -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
|
||||
|
||||
For TPROXY :
|
||||
|
||||
sysctl -w net.ipv4.ip_forward=1
|
||||
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
|
||||
|
||||
ip -f inet rule add fwmark 1 lookup 100
|
||||
ip -f inet route add local default dev lo table 100
|
||||
# prevent loop
|
||||
iptables -t filter -I INPUT -p tcp --dport 988 -j REJECT
|
||||
iptables -t mangle -A PREROUTING -i eth1 -p tcp --dport 80 -j MARK --set-mark 1
|
||||
iptables -t mangle -A PREROUTING -i eth1 -p tcp --dport 80 -j TPROXY --tproxy-mark 0x1/0x1 --on-port 988
|
||||
|
||||
iptables -t mangle -A PREROUTING -i eth1 -p tcp --dport 80 -m set --match-set zapret dst -j MARK --set-mark 1
|
||||
iptables -t mangle -A PREROUTING -i eth1 -p tcp --dport 80 -m mark --mark 0x1/0x1 -j TPROXY --tproxy-mark 0x1/0x1 --on-port 988
|
||||
|
||||
For DNAT :
|
||||
|
||||
# run tpws as user "tpws". its required to avoid loops.
|
||||
sysctl -w net.ipv4.conf.eth1.route_localnet=1
|
||||
iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to 127.0.0.127:988
|
||||
iptables -t nat -I OUTPUT -p tcp --dport 80 -m owner ! --uid-owner tpws -j DNAT --to 127.0.0.127:988
|
||||
|
||||
|
||||
Reset all iptable rules :
|
||||
|
||||
iptables -F
|
||||
iptables -X
|
||||
iptables -t nat -F
|
||||
iptables -t nat -X
|
||||
iptables -t mangle -F
|
||||
iptables -t mangle -X
|
||||
iptables -t raw -F
|
||||
iptables -t raw -X
|
||||
|
||||
Reset iptable policies :
|
||||
|
||||
iptables -P INPUT ACCEPT
|
||||
iptables -P FORWARD ACCEPT
|
||||
iptables -P OUTPUT ACCEPT
|
||||
iptables -t mangle -P POSTROUTING ACCEPT
|
||||
iptables -t mangle -P PREROUTING ACCEPT
|
||||
iptables -t mangle -P INPUT ACCEPT
|
||||
iptables -t mangle -P FORWARD ACCEPT
|
||||
iptables -t mangle -P OUTPUT ACCEPT
|
||||
iptables -t raw -P PREROUTING ACCEPT
|
||||
iptables -t raw -P OUTPUT ACCEPT
|
643
docs/readme.eng.txt
Normal file
643
docs/readme.eng.txt
Normal file
@@ -0,0 +1,643 @@
|
||||
What is it for
|
||||
--------------
|
||||
|
||||
Bypass the blocking of http/https web sites on DPI without the use of third-party servers.
|
||||
|
||||
The project is mainly aimed at the Russian audience to fight russian regulator named "Roskomnadzor".
|
||||
Some features of the project are russian reality specific (such as getting list of sites
|
||||
blocked by Roskomnadzor), but most others are common.
|
||||
|
||||
(EXPERIMENTAL) FreeBSD and OpenBSD are also supported.
|
||||
(EXPERIMENTAL, PARTIAL) MacOS limited support.
|
||||
see docs/bsd.eng.txt
|
||||
|
||||
How it works
|
||||
------------
|
||||
|
||||
In the simplest case you are dealing with passive DPI. Passive DPI can read passthrough traffic,
|
||||
inject its own packets, but cannot drop packets.
|
||||
If the request is prohibited the passive DPI will inject its own RST packet and optionally http redirect packet.
|
||||
If fake packets from DPI are only sent to client, you can use iptables commands to drop them if you can write
|
||||
correct filter rules. This requires manual in-deep traffic analysis and tuning for specific ISP.
|
||||
This is how we bypass the consequences of a ban trigger.
|
||||
|
||||
If the passive DPI sends an RST packet also to the server, there is nothing you can do about it.
|
||||
Your task is to prevent ban trigger from firing up. Iptables alone will not work.
|
||||
This project is aimed at preventing the ban rather than eliminating its consequences.
|
||||
|
||||
To do that send what DPI does not expect and what breaks its algorithm of recognizing requests and blocking them.
|
||||
|
||||
Some DPIs cannot recognize the http request if it is divided into TCP segments.
|
||||
For example, a request of the form "GET / HTTP / 1.1 \ r \ nHost: kinozal.tv ......"
|
||||
we send in 2 parts: first go "GET", then "/ HTTP / 1.1 \ r \ nHost: kinozal.tv .....".
|
||||
Other DPIs stumble when the "Host:" header is written in another case: for example, "host:".
|
||||
Sometimes work adding extra space after the method: "GET /" => "GET /"
|
||||
or adding a dot at the end of the host name: "Host: kinozal.tv."
|
||||
|
||||
There is also more advanced magic for bypassing DPI at the packet level.
|
||||
|
||||
|
||||
How to put this into practice in the linux system
|
||||
-------------------------------------------------
|
||||
|
||||
In short, the options can be classified according to the following scheme:
|
||||
|
||||
1) Passive DPI not sending RST to the server. ISP tuned iptables commands can help.
|
||||
This option is out of the scope of the project. If you do not allow ban trigger to fire, then you won’t have to
|
||||
deal with its consequences.
|
||||
2) Modification of the TCP connection at the stream level. Implemented through a proxy or transparent proxy.
|
||||
3) Modification of TCP connection at the packet level. Implemented through the NFQUEUE handler and raw sockets.
|
||||
|
||||
For options 2 and 3, tpws and nfqws programs are implemented, respectively.
|
||||
You need to run them with the necessary parameters and redirect certain traffic with iptables.
|
||||
|
||||
To redirect a TCP connection to a transparent proxy, the following commands are used:
|
||||
|
||||
forwarded fraffic :
|
||||
iptables -t nat -I PREROUTING -i <internal_interface> -p tcp --dport 80 -j DNAT --to 127.0.0.127:988
|
||||
outgoing traffic :
|
||||
iptables -t nat -I OUTPUT -o <external_interface> -p tcp --dport 80 -m owner ! --uid-owner tpws -j DNAT --to 127.0.0.127:988
|
||||
|
||||
DNAT on localhost works in the OUTPUT chain, but does not work in the PREROUTING chain without enabling the route_localnet parameter:
|
||||
|
||||
sysctl -w net.ipv4.conf.<internal_interface>.route_localnet=1
|
||||
|
||||
You can use "-j REDIRECT --to-port 988" instead of DNAT, but in this case the transparent proxy process
|
||||
should listen on the ip address of the incoming interface or on all addresses. Listen all - not good
|
||||
in terms of security. Listening one (local) is possible, but automated scripts will have to recognize it,
|
||||
then dynamically enter it into the command. In any case, additional efforts are required.
|
||||
Using route_localnet can also introduce some security risks. You make available from internal_interface everything
|
||||
bound to 127.0.0.0/8. Services are usually bound to 127.0.0.1. Its possible to deny input to 127.0.0.1 from all interfaces except lo
|
||||
or bind tpws to any other IP from 127.0.0.0/8 range, for example to 127.0.0.127, and allow incomings only to that IP :
|
||||
|
||||
iptables -A INPUT ! -i lo -d 127.0.0.127 -j ACCEPT
|
||||
iptables -A INPUT ! -i lo -d 127.0.0.0/8 -j DROP
|
||||
|
||||
Owner filter is necessary to prevent recursive redirection of connections from tpws itself.
|
||||
tpws must be started under OS user "tpws".
|
||||
|
||||
|
||||
NFQUEUE redirection of the outgoing traffic and forwarded traffic going towards the external interface,
|
||||
can be done with the following commands:
|
||||
|
||||
iptables -t mangle -I POSTROUTING -o <external_interface> -p tcp --dport 80 -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
|
||||
In order not to touch the traffic to unblocked addresses, you can take a list of blocked hosts, resolve it
|
||||
into IP addresses and put them to ipset 'zapret', then add a filter to the command:
|
||||
|
||||
iptables -t mangle -I POSTROUTING -o <external_interface> -p tcp --dport 80 -m set --match-set zapret dst -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
|
||||
Some DPIs catch only the first http request, ignoring subsequent requests in a keep-alive session.
|
||||
Then we can reduce CPU load, refusing to process unnecessary packets.
|
||||
|
||||
iptables -t mangle -I POSTROUTING -o <внешний_интерфейс> -p tcp --dport 80 -m connbytes --connbytes-dir=original --connbytes-mode=packets --connbytes 2:4 -m mark ! --mark 0x40000000/0x40000000 -m set --match-set zapret dst -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
|
||||
Mark filter does not allow nfqws-generated packets to enter the queue again.
|
||||
Its necessary to use this filter when also using "connbytes 2:4". Without it packet ordering can be changed breaking the whole idea.
|
||||
|
||||
|
||||
ip6tables
|
||||
---------
|
||||
|
||||
ip6tables work almost exactly the same way as ipv4, but there are a number of important nuances.
|
||||
In DNAT, you should take the address --to in square brackets. For example :
|
||||
|
||||
ip6tables -t nat -I OUTPUT -o <external_interface> -p tcp --dport 80 -m owner ! --uid-owner tpws -j DNAT --to [::1]:988
|
||||
|
||||
The route_localnet parameter does not exist for ipv6.
|
||||
DNAT to localhost (:: 1) is possible only in the OUTPUT chain.
|
||||
In the PREROUTING DNAT chain, it is possible to any global address or to the link local address of the same interface
|
||||
the packet came from.
|
||||
NFQUEUE works without changes.
|
||||
|
||||
When it will not work
|
||||
----------------------
|
||||
|
||||
* If DNS server returns false responses. ISP can return false IP addresses or not return anything
|
||||
when blocked domains are queried. If this is the case change DNS to public ones, such as 8.8.8.8 or 1.1.1.1.
|
||||
Sometimes ISP hijacks queries to any DNS server. Dnscrypt or dns-over-tls help.
|
||||
* If blocking is done by IP.
|
||||
* If a connection passes through a filter capable of reconstructing a TCP connection, and which
|
||||
follows all standards. For example, we are routed to squid. Connection goes through the full OS tcpip stack,
|
||||
fragmentation disappears immediately as a means of circumvention. Squid is correct, it will find everything
|
||||
as it should, it is useless to deceive him.
|
||||
BUT. Only small providers can afford using squid, since it is very resource intensive.
|
||||
Large companies usually use DPI, which is designed for much greater bandwidth.
|
||||
|
||||
nfqws
|
||||
-----
|
||||
|
||||
This program is a packet modifier and a NFQUEUE queue handler.
|
||||
For BSD systems there is dvtws. Its built from the same source and has almost the same parameters (see bsd.eng.txt).
|
||||
nfqws takes the following parameters:
|
||||
|
||||
--debug=0|1 ; 1=print debug info
|
||||
--qnum=<nfqueue_number>
|
||||
--wsize=<window_size> ; set window size. 0 = do not modify (obsolete !)
|
||||
--hostcase ; change Host: => host:
|
||||
--hostspell=HoSt ; exact spelling of the "Host" header. must be 4 chars. default is "host"
|
||||
--hostnospace ; remove space after Host: and add it to User-Agent: to preserve packet size
|
||||
--domcase ; mix domain case after Host: like this : TeSt.cOm
|
||||
--daemon ; daemonize
|
||||
--pidfile=<filename> ; write pid to file
|
||||
--user=<username> ; drop root privs
|
||||
--uid=uid[:gid] ; drop root privs
|
||||
--dpi-desync[=<mode>][,<mode2>] ; try to desync dpi state. modes : fake rst rstack disorder disorder2 split split2
|
||||
--dpi-desync-fwmark=<int|0xHEX> ; override fwmark for desync packet. default = 0x40000000
|
||||
--dpi-desync-ttl=<int> ; set ttl for desync packet
|
||||
--dpi-desync-fooling=none|md5sig|ts|badseq|badsum ; can take multiple comma separated values
|
||||
--dpi-desync-retrans=0|1 ; (fake,rst,rstack only) 0(default)=reinject original data packet after fake 1=drop original data packet to force its retransmission
|
||||
--dpi-desync-repeats=<N> ; send every desync packet N times
|
||||
--dpi-desync-skip-nosni=0|1 ; 1(default)=do not apply desync to requests without hostname in the SNI
|
||||
--dpi-desync-split-pos=<1..1500> ; (for split* and disorder* only) split TCP packet at specified position
|
||||
--dpi-desync-any-protocol=0|1 ; 0(default)=desync only http and tls 1=desync any nonempty data packet
|
||||
--dpi-desync-fake-http=<filename> ; file containing fake http request. replacement for built-in
|
||||
--dpi-desync-fake-tls=<filename> ; file containing fake TLS ClientHello (for https). replacement for built-in
|
||||
--hostlist=<filename> ; apply fooling only to the listed hosts (one host per line, subdomains auto apply)
|
||||
|
||||
The manipulation parameters can be combined in any way.
|
||||
|
||||
WARNING. --wsize parameter is now not used anymore in scripts. TCP split can be achieved using DPI desync attack.
|
||||
|
||||
DPI DESYNC ATTACK
|
||||
After completion of the tcp 3-way handshake, the first data packet from the client goes.
|
||||
It usually has "GET / ..." or TLS ClientHello. We drop this packet, replacing with something else.
|
||||
It can be a fake version with another harmless but valid http or https request (fake), tcp reset packet (rst,rstack),
|
||||
split into 2 segments original packet with fake segment in the middle (disorder).
|
||||
In articles these attack have names "TCB desynchronization" and "TCB teardown".
|
||||
Fake packet must reach DPI, but do not reach the destination server.
|
||||
The following means are available: set a low TTL, send a packet with bad checksum,
|
||||
add tcp option "MD5 signature". All of them have their own disadvantages :
|
||||
|
||||
* md5sig does not work on all servers
|
||||
* badsum doesn't work if your device is behind NAT which does not pass invalid packets.
|
||||
Linux NAT by default does not pass them without special setting "sysctl -w net.netfilter.nf_conntrack_checksum=0"
|
||||
Openwrt sets it from the box, other routers in most cases dont, and its not always possible to change it.
|
||||
If nfqws is on the router, its not neccessary to switch of "net.netfilter.nf_conntrack_checksum".
|
||||
Fake packet doesn't go through FORWARD chain, it goes through OUTPUT. But if your router is behind another NAT, for example ISP NAT,
|
||||
and that NAT does not pass invalid packets, you cant do anything.
|
||||
* badseq packets will be dropped by server, but DPI also can ignore them
|
||||
* TTL looks like the best option, but it requires special tuning for earch ISP. If DPI is further than local ISP websites
|
||||
you can cut access to them. Manual IP exclude list is required. Its possible to use md5sig with ttl.
|
||||
This way you cant hurt anything, but good chances it will help to open local ISP websites.
|
||||
If automatic solution cannot be found then use zapret-hosts-user-exclude.txt.
|
||||
|
||||
--dpi-desync-fooling takes multiple comma separated values.
|
||||
|
||||
For fake,rst,rstack modes original packet can be sent after the fake one or just dropped.
|
||||
If its dropped OS will perform first retransmission after 0.2 sec, then the delay increases exponentially.
|
||||
Delay can help to make sure fake and original packets are properly ordered and processed on DPI.
|
||||
When dpi-desync-retrans=1 its mandatory to use connbytes in iptables rule. Otherwise loop happens.
|
||||
|
||||
Disorder mode splits original packet and sends packets in the following order :
|
||||
1. 2nd segment
|
||||
2. fake 1st segment, data filled with zeroes
|
||||
3. 1st segment
|
||||
4. fake 1st segment, data filled with zeroes (2nd copy)
|
||||
Original packet is always dropped. --dpi-desync-split-pos sets split position (default 3).
|
||||
If position is higher than packet length, pos=1 is used.
|
||||
This sequence is designed to make reconstruction of critical message as difficult as possible.
|
||||
Fake segments may not be required to bypass some DPIs, but can potentially help if more sophisticated reconstruction
|
||||
algorithms are used.
|
||||
Mode 'disorder2' disables sending of fake segments.
|
||||
|
||||
Split mode is very similar to disorder but without segment reordering :
|
||||
1. fake 1st segment, data filled with zeroes
|
||||
2. 1st segment
|
||||
3. fake 1st segment, data filled with zeroes (2nd copy)
|
||||
4. 2nd segment
|
||||
Mode 'split2' disables sending of fake segments. It can be used as a faster alternative to --wsize.
|
||||
|
||||
In disorder2 and split2 modes no fake packets are sent, so ttl and fooling options are not required.
|
||||
|
||||
There are DPIs that analyze responses from the server, particularly the certificate from the ServerHello
|
||||
that contain domain name(s). The ClientHello delivery confirmation is an ACK packet from the server
|
||||
with ACK sequence number corresponding to the length of the ClientHello+1.
|
||||
In the disorder variant, a selective acknowledgement (SACK) usually arrives first, then a full ACK.
|
||||
If, instead of ACK or SACK, there is an RST packet with minimal delay, DPI cuts you off at the request stage.
|
||||
If the RST is after a full ACK after a delay of about ping to the server, then probably DPI acts
|
||||
on the server response. The DPI may be satisfied with good ClientHello and stop monitoring the TCP session
|
||||
without checking ServerHello. Then you were lucky. 'fake' option could work.
|
||||
If it does not stop monitoring and persistently checks the ServerHello, also performing reconstruction of TCP segments,
|
||||
doing something about it is hardly possible without the help of the server.
|
||||
The best solution is to enable TLS 1.3 support on the server. TLS 1.3 sends the server certificate in encrypted form.
|
||||
This is recommendation to all admins of blocked sites. Enable TLS 1.3. You will give more opportunities to overcome DPI.
|
||||
|
||||
Hosts are extracted from plain http request Host: header and SNI of ClientHelllo TLS message.
|
||||
Subdomains are applied automatically. gzip lists are supported.
|
||||
|
||||
iptables for performing the attack on the first packet :
|
||||
|
||||
iptables -t mangle -I POSTROUTING -o <external_interface> -p tcp -m multiport --dports 80,443 -m connbytes --connbytes-dir=original --connbytes-mode=packets --connbytes 2:4 -m mark ! --mark 0x40000000/0x40000000 -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
|
||||
This is good if DPI does not track all requests in http keep-alive session.
|
||||
If it does, then pass all outgoing packets for http and only first data packet for https :
|
||||
|
||||
iptables -t mangle -I POSTROUTING -o <external_interface> -p tcp --dport 443 -m connbytes --connbytes-dir=original --connbytes-mode=packets --connbytes 2:4 -m mark ! --mark 0x40000000/0x40000000 -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
iptables -t mangle -I POSTROUTING -o <external_interface> -p tcp --dport 80 -m mark ! --mark 0x40000000/0x40000000 -j NFQUEUE --queue-num 200 --queue-bypass
|
||||
|
||||
mark is needed to keep away generated packets from NFQUEUE. nfqws sets fwmark when it sends generated packets.
|
||||
nfqws can internally filter marked packets. but when connbytes filter is used without mark filter
|
||||
packet ordering can be changed breaking the whole idea of desync attack.
|
||||
|
||||
DESYNC COMBOS
|
||||
dpi-desync parameter can take 2 comma separated arguments.
|
||||
1st phase mode can be fake,rst,rstack, 2nd phase mode - disorder,disorder2,split,split2.
|
||||
Can be useful for ISPs with more than one DPI.
|
||||
|
||||
VIRTUAL MACHINES
|
||||
Most of nfqws packet magic does not work from VMs powered by virtualbox and vmware when network is NATed.
|
||||
Hypervisor forcibly changes ttl and does not forward fake packets.
|
||||
Set up bridge networking.
|
||||
|
||||
|
||||
tpws
|
||||
-----
|
||||
|
||||
tpws is transparent proxy.
|
||||
|
||||
--debug=0|1|2 ; 0(default)=silent 1=verbose 2=debug
|
||||
--bind-addr=<v4_addr>|<v6_addr>; for v6 link locals append %interface_name : fe80::1%br-lan
|
||||
--bind-iface4=<interface_name> ; bind to the first ipv4 addr of interface
|
||||
--bind-iface6=<interface_name> ; bind to the first ipv6 addr of interface
|
||||
--bind-linklocal=prefer|force ; prefer or force ipv6 link local
|
||||
--bind-wait-ifup=<sec> ; wait for interface to appear and up
|
||||
--bind-wait-ip=<sec> ; after ifup wait for ip address to appear up to N seconds
|
||||
--bind-wait-ip-linklocal=<sec> ; accept only link locals first N seconds then any
|
||||
--bind-wait-only ; wait for bind conditions satisfaction then exit. return code 0 if success.
|
||||
--port=<port> ; port number to listen on
|
||||
--socks ; implement socks4/5 proxy instead of transparent proxy
|
||||
--local-rcvbuf=<bytes> ; SO_RCVBUF for local legs
|
||||
--local-sndbuf=<bytes> ; SO_SNDBUF for local legs
|
||||
--remote-rcvbuf=<bytes> ; SO_RCVBUF for remote legs
|
||||
--remote-sndbuf=<bytes> ; SO_SNDBUF for remote legs
|
||||
--skip-nodelay ; do not set TCP_NODELAY for outgoing connections. incompatible with split.
|
||||
--no-resolve ; disable socks5 remote dns
|
||||
--maxconn=<max_connections> ; max number of local legs
|
||||
--maxfiles=<max_open_files> ; max file descriptors (setrlimit). min requirement is (X*connections+16), where X=6 in tcp proxy mode, X=4 in tampering mode.
|
||||
; its worth to make a reserve with 1.5 multiplier. by default maxfiles is (X*connections)*1.5+16
|
||||
--max-orphan-time=<sec> ; if local leg sends something and closes and remote leg is still connecting then cancel connection attempt after N seconds
|
||||
|
||||
--hostlist=<filename> ; only act on host in the list (one host per line, subdomains auto apply, gzip lists supported)
|
||||
--split-http-req=method|host ; split http request at specified logical position.
|
||||
--split-pos=<numeric_offset> ; split at specified pos. split-http-req takes precedence over split-pos for http reqs.
|
||||
--split-any-protocol ; split not only http and https
|
||||
--hostcase ; change Host: => host:
|
||||
--hostspell ; exact spelling of "Host" header. must be 4 chars. default is "host"
|
||||
--hostdot ; add "." after Host: name
|
||||
--hosttab ; add tab after Host: name
|
||||
--hostnospace ; remove space after Host:
|
||||
--hostpad=<bytes> ; add dummy padding headers before Host:
|
||||
--domcase ; mix domain case after Host: like this : TeSt.cOm
|
||||
--methodspace ; add extra space after method
|
||||
--methodeol ; add end-of-line before method
|
||||
--unixeol ; replace 0D0A to 0A
|
||||
--daemon ; daemonize
|
||||
--pidfile=<filename> ; write pid to file
|
||||
--user=<username> ; drop root privs
|
||||
--uid=uid[:gid] ; drop root privs
|
||||
|
||||
The manipulation parameters can be combined in any way.
|
||||
|
||||
split-http-req takes precedence over split-pos for http reqs.
|
||||
split-pos works by default only on http and TLS ClientHello. use --split-any-protocol to act on any packet
|
||||
|
||||
tpws can bind to multiple interfaces and IP addresses (up to 32).
|
||||
Port number is always the same.
|
||||
Parameters --bind-iface* и --bind-addr create new bind.
|
||||
Other parameters --bind-* are related to the last bind.
|
||||
To bind to all ipv4 specify --bind-addr "0.0.0.0", all ipv6 - "::". --bind-addr="" - mean bind to all ipv4 and ipv6.
|
||||
If no binds are specified default bind to all ipv4 and ipv6 addresses is created.
|
||||
The --bind-wait* parameters can help in situations where you need to get IP from the interface, but it is not there yet, it is not raised
|
||||
or not configured.
|
||||
In different systems, ifup events are caught in different ways and do not guarantee that the interface has already received an IP address of a certain type.
|
||||
In the general case, there is no single mechanism to hang oneself on an event of the type "link local address appeared on the X interface."
|
||||
|
||||
in socks proxy mode no additional system privileges are required
|
||||
connection to local IPs of the system where tpws runs are prohibited
|
||||
tpws supports remote dns resolving (curl : --socks5-hostname firefox : socks_remote_dns=true) , but does it in blocking mode.
|
||||
tpws uses async sockets for all activity but resolving can break this model.
|
||||
if tpws serves many clients it can cause trouble. also DoS attack is possible against tpws.
|
||||
if remote resolving causes trouble configure clients to use local name resolution and use
|
||||
--no-resolve option on tpws side.
|
||||
|
||||
Ways to get a list of blocked IP
|
||||
--------------------------------
|
||||
|
||||
1) Enter the blocked domains to ipset/zapret-hosts-user.txt and run ipset/get_user.sh
|
||||
At the output, you get ipset/zapret-ip-user.txt with IP addresses.
|
||||
|
||||
2) ipset/get_reestr_*.sh. Russian specific
|
||||
|
||||
3) ipset/get_antifilter_*.sh. Russian specific
|
||||
|
||||
4) ipset/get_config.sh. This script calls what is written into the GETLIST variable from the config file.
|
||||
If the variable is not defined, then only lists for ipsets nozapret/nozapret6 are resolved.
|
||||
|
||||
So, if you're not russian, the only way for you is to manually add blocked domains.
|
||||
Or write your own ipset/get_iran_blocklist.sh , if you know where to download this one.
|
||||
|
||||
On routers, it is not recommended to call these scripts more than once in 2 days to minimize flash memory writes.
|
||||
|
||||
ipset/create_ipset.sh executes forced ipset update.
|
||||
With "no-update" parameter create_ipset.sh creates ipset but populate it only if it was actually created.
|
||||
It's useful when multiple subsequent calls are possible to avoid wasting of cpu time redoing the same job.
|
||||
Ipset loading is resource consuming. Its a good idea to call create_ipset without "no-update" parameter
|
||||
only once a several days. Use it with "no-update" option in other cases.
|
||||
|
||||
ipset scripts automatically call ip2net utility.
|
||||
ip2net helps to reduce ip list size by combining IPs to subnets. Also it cuts invalid IPs from the list.
|
||||
Stored lists are already processed by ip2net. They are error free and ready for loading.
|
||||
|
||||
create_ipset.sh supports loading ip lists from gzip files. First it looks for the filename with the ".gz" extension,
|
||||
such as "zapret-ip.txt.gz", if not found it falls back to the original name "zapret-ip.txt".
|
||||
So your own get_iran_blockslist.sh can use "zz" function to produce gz. Study how other russian get_XXX.sh work.
|
||||
Gzipping helps saving a lot of precious flash space on embedded systems.
|
||||
User lists are not gzipped because they are not expected to be very large.
|
||||
|
||||
You can add a list of domains to ipset/zapret-hosts-user-ipban.txt. Their ip addresses will be placed
|
||||
in a separate ipset "ipban". It can be used to route connections to transparent proxy "redsocks" or VPN.
|
||||
|
||||
IPV6: if ipv6 is enabled, then additional txt's are created with the same name, but with a "6" at the end before the extension.
|
||||
zapret-ip.txt => zapret-ip6.txt
|
||||
The ipsets zapret6 and ipban6 are created.
|
||||
|
||||
IP EXCLUSION SYSTEM. All scripts resolve zapret-hosts-user-exclude.txt file, creating zapret-ip-exclude.txt and zapret-ip-exclude6.txt.
|
||||
They are the source for ipsets nozapret/nozapret6. All rules created by init scripts are created with these ipsets in mind.
|
||||
The IPs placed in them are not involved in the process.
|
||||
zapret-hosts-user-exclude.txt can contain domains, ipv4 and ipv6 addresses or subnets.
|
||||
|
||||
FreeBSD. ipset/*.sh scripts also work in FreeBSD. Instead of ipset they create ipfw lookup tables with the same names as in Linux.
|
||||
ipfw tables can store both ipv4 and ipv6 addresses and subnets. There's no 4 and 6 separation.
|
||||
|
||||
LISTS_RELOAD config parameter defines a custom lists reloading command.
|
||||
Its useful on BSD systems with PF.
|
||||
LISTS_RELOAD=- disables reloading ip list backend.
|
||||
|
||||
|
||||
Domain name filtering
|
||||
---------------------
|
||||
|
||||
An alternative to ipset is to use tpws or nfqws with a list of domains. Only one list is supported.
|
||||
|
||||
Enter the blocked domains to ipset/zapret-hosts-users.txt. Remove ipset/zapret-hosts.txt.gz.
|
||||
Then the init script will run tpws with the zapret-hosts-users.txt list.
|
||||
|
||||
Other option ( Roskomnadzor list - get_hostlist.sh ) is russian specific.
|
||||
You can write your own replacement for get_hostlist.sh.
|
||||
|
||||
When filtering by domain name, daemons should run without filtering by ipset.
|
||||
When using large regulator lists estimate the amount of RAM on the router !
|
||||
|
||||
|
||||
Choosing parameters
|
||||
-------------------
|
||||
|
||||
The file /opt/zapret/config is used by various components of the system and contains basic settings.
|
||||
It needs to be viewed and edited if necessary.
|
||||
|
||||
|
||||
Main mode :
|
||||
tpws - use tpws
|
||||
tpws - use nfqws
|
||||
filter - only fill ipset or load hostlist
|
||||
custom - use custom script for running daemons and establishing firewall rules
|
||||
|
||||
MODE=tpws
|
||||
|
||||
Enable http fooling :
|
||||
|
||||
MODE_HTTP=1
|
||||
|
||||
Apply fooling to keep alive http sessions. Only applicable to nfqws. Tpws always fool keepalives.
|
||||
Not enabling this can save CPU time.
|
||||
|
||||
MODE_HTTP_KEEPALIVE=0
|
||||
|
||||
Enable https fooling :
|
||||
|
||||
MODE_HTTPS=1
|
||||
|
||||
Host filtering mode :
|
||||
none - apply fooling to all hosts
|
||||
ipset - limit fooling to hosts from ipset zapret/zapret6
|
||||
hostlist - limit fooling to hosts from hostlist
|
||||
|
||||
MODE_FILTER=none
|
||||
|
||||
Its possible to change manipulation options used by tpws :
|
||||
|
||||
TPWS_OPT="--hostspell=HOST --split-http-req=method --split-pos=3"
|
||||
|
||||
nfqws options for DPI desync attack:
|
||||
|
||||
DESYNC_MARK=0x40000000
|
||||
NFQWS_OPT_DESYNC="--dpi-desync=fake --dpi-desync-ttl=0 --dpi-desync-fooling=badsum --dpi-desync-fwmark=$DESYNC_MARK"
|
||||
|
||||
flow offloading control (openwrt only)
|
||||
donttouch : disable system flow offloading setting if selected mode is incompatible with it, dont touch it otherwise and dont configure selective flow offloading
|
||||
none : always disable system flow offloading setting and dont configure selective flow offloading
|
||||
software : always disable system flow offloading setting and configure selective software flow offloading
|
||||
hardware : always disable system flow offloading setting and configure selective hardware flow offloading
|
||||
|
||||
FLOWOFFLOAD=donttouch
|
||||
|
||||
The GETLIST parameter tells the install_easy.sh installer which script to call
|
||||
to update the list of blocked ip or hosts.
|
||||
Its called via get_config.sh from scheduled tasks (crontab or systemd timer).
|
||||
Put here the name of the script that you will use to update the lists.
|
||||
If not, then the parameter should be commented out.
|
||||
|
||||
You can individually disable ipv4 or ipv6. If the parameter is commented out or not equal to "1",
|
||||
use of the protocol is permitted.
|
||||
#DISABLE_IPV4=1
|
||||
DISABLE_IPV6=1
|
||||
|
||||
The number of threads for mdig multithreaded DNS resolver (1..100).
|
||||
The more of them, the faster, but will your DNS server be offended by hammering ?
|
||||
MDIG_THREADS=30
|
||||
|
||||
temp directory. Used by ipset/*.sh scripts for large lists processing.
|
||||
/tmp by default. Can be reassigned if /tmp is tmpfs and RAM is low.
|
||||
TMPDIR=/opt/zapret/tmp
|
||||
|
||||
ipset options :
|
||||
|
||||
IPSET_OPT="hashsize 262144 maxelem 2097152"
|
||||
|
||||
Kernel automatically increases hashsize if ipset is too large for the current hashsize.
|
||||
This procedure requires internal reallocation and may require additional memory.
|
||||
On low RAM systems it can cause errors.
|
||||
Do not use too high hashsize. This way you waste your RAM. And dont use too low hashsize to avoid reallocs.
|
||||
|
||||
ip2net options. separate for ipv4 and ipv6.
|
||||
IP2NET_OPT4="--prefix-length=22-30 --v4-threshold=3/4"
|
||||
IP2NET_OPT6="--prefix-length=56-64 --v6-threshold=5"
|
||||
|
||||
Enable gzip compression for large lists. Used by ipset/*.sh scripts.
|
||||
GZIP_LISTS=1
|
||||
|
||||
Command to reload ip/host lists after update.
|
||||
Comment or leave empty for auto backend selection : ipset or ipfw if present.
|
||||
On BSD systems with PF no auto reloading happens. You must provide your own command.
|
||||
Newer FreeBSD versions support table only reloading : pfctl -Tl -f /etc/pf.conf
|
||||
Set to "-" to disable reload.
|
||||
LISTS_RELOAD="pfctl -f /etc/pf.conf"
|
||||
|
||||
The following settings are not relevant for openwrt :
|
||||
|
||||
If your system works as a router, then you need to enter the names of the internal and external interfaces:
|
||||
IFACE_LAN = eth0
|
||||
IFACE_WAN = eth1
|
||||
IMPORTANT: configuring routing, masquerade, etc. not a zapret task.
|
||||
Only modes that intercept transit traffic are enabled.
|
||||
|
||||
The INIT_APPLY_FW=1 parameter enables the init script to independently apply iptables rules.
|
||||
With other values or if the parameter is commented out, the rules will not be applied.
|
||||
This is useful if you have a firewall management system, in the settings of which you should tie the rules.
|
||||
|
||||
|
||||
Screwing to the firewall control system or your launch system
|
||||
-------------------------------------------------------------
|
||||
|
||||
If you use some kind of firewall management system, then it may conflict with an existing startup script.
|
||||
When re-applying the rules, it could break the iptables settings from the zapret.
|
||||
In this case, the rules for iptables should be screwed to your firewall separately from running tpws or nfqws.
|
||||
|
||||
The following calls allow you to apply or remove iptables rules separately:
|
||||
|
||||
/opt/zapret/init.d/sysv/zapret start-fw
|
||||
/opt/zapret/init.d/sysv/zapret stop-fw
|
||||
|
||||
And you can start or stop the demons separately from the firewall:
|
||||
|
||||
/opt/zapret/init.d/sysv/zapret start-daemons
|
||||
/opt/zapret/init.d/sysv/zapret stop-daemons
|
||||
|
||||
|
||||
Simple install to desktop linux system
|
||||
--------------------------------------
|
||||
|
||||
Simple install works on most modern linux distributions with systemd, OpenWRT and MacOS.
|
||||
Run install_easy.sh and answer its questions.
|
||||
|
||||
Simple install to openwrt
|
||||
-------------------------
|
||||
|
||||
install_easy.sh works on openwrt but there're additional challenges.
|
||||
They are mainly about possibly low flash free space.
|
||||
Simple install will not work if it has no space to install itself and required packages from the repo.
|
||||
|
||||
Another challenge would be to bring zapret to the router. You can download zip from github and use it.
|
||||
Do not repack zip contents in Windows, because this way you break chmod and links.
|
||||
Install openssh-sftp-server and unzip to openwrt and use sftp to transfer the file.
|
||||
|
||||
The best way to start is to put zapret dir to /tmp and run /tmp/zapret/install_easy.sh from there.
|
||||
After installation remove /tmp/zapret to free RAM.
|
||||
|
||||
The absolute minimum for openwrt is 64/8 system, 64/16 is comfortable, 128/extroot is recommended.
|
||||
|
||||
|
||||
Android
|
||||
-------
|
||||
|
||||
Its not possible to use nfqws and tpws in transparent proxy mode without root privileges.
|
||||
Without root tpws can run in --socks mode.
|
||||
|
||||
I have no NFQUEUE presence statistics in stock android kernels, but its present on my MTK device.
|
||||
If NFQUEUE is present nfqws works.
|
||||
|
||||
There's no ipset support unless you run custom kernel. In common case task of bringing up ipset
|
||||
on android is ranging from "not easy" to "almost impossible", unless you find working kernel
|
||||
image for your device.
|
||||
|
||||
Android does not use /etc/passwd, tpws --user won't work. There's replacement.
|
||||
Use numeric uids in --uid option.
|
||||
Its recommended to use gid 3003 (AID_INET), otherwise tpws will not have inet access.
|
||||
Example : --uid 1:3003
|
||||
In iptables use : "! --uid-owner 1" instead of "! --uid-owner tpws".
|
||||
|
||||
Write your own shell script with iptables and tpws, run it using your root manager.
|
||||
Autorun scripts are here :
|
||||
magisk : /data/adb/service.d
|
||||
supersu : /system/su.d
|
||||
|
||||
I haven't checked whether android can kill iptable rules at its own will during wifi connection/disconnection,
|
||||
mobile data on/off, ...
|
||||
|
||||
How to run tpws on root-less android.
|
||||
You can't write to /system, /data, can't run from sd card.
|
||||
Selinux prevents running executables in /data/local/tmp from apps.
|
||||
Use adb and adb shell.
|
||||
mkdir /data/local/tmp/zapret
|
||||
adb push tpws /data/local/tmp/zapret
|
||||
chmod 755 /data/local/tmp/zapret /data/local/tmp/zapret/tpws
|
||||
chcon u:object_r:system_file:s0 /data/local/tmp/zapret/tpws
|
||||
Now its possible to run /data/local/tmp/zapret/tpws from any app such as tasker.
|
||||
|
||||
|
||||
FreeBSD, OpenBSD, MacOS
|
||||
-----------------------
|
||||
|
||||
see docs/bsd.eng.txt
|
||||
|
||||
|
||||
Windows (WSL)
|
||||
-------------
|
||||
|
||||
Using WSL (Windows subsystem for Linux) it's possible to run tpws in socks mode under rather new builds of
|
||||
windows 10 and windows server.
|
||||
Its not required to install any linux distributions as suggested in most articles.
|
||||
tpws is static binary. It doesn't need a distribution.
|
||||
|
||||
Install WSL : dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all
|
||||
Copy binaries/x86_64/tpws_wsl.tgz to the target system.
|
||||
Run : wsl --import tpws "%USERPROFILE%\tpws" tpws_wsl.tgz
|
||||
Run tpws : wsl --exec /tpws --uid=1 --no-resolve --socks --bind-addr=127.0.0.1 --port=1080 <fooling_options>
|
||||
Configure socks as 127.0.0.1:1080 in a browser or another program.
|
||||
|
||||
Cleanup : wsl --unregister tpws
|
||||
|
||||
Tested in windows 10 build 19041 (20.04).
|
||||
|
||||
NOTICE. There is native windows solution GoodByeDPI. It works on packet level like nfqws.
|
||||
|
||||
|
||||
Other devices
|
||||
-------------
|
||||
|
||||
Author's goal does not include easy supporting as much devices as possibles.
|
||||
Please do not ask for easy supporting firmwares. It requires a lot of work and owning lots of devices. Its counterproductive.
|
||||
As a devices owner its easier for you and should not be too hard if firmware is open.
|
||||
Most closed stock firmwares are not designed for custom usage and sometimes actively prevent it.
|
||||
In the latter case you have to hack into it and reverse engineer. Its not easy.
|
||||
Binaries are universal. They can run on almost all firmwares.
|
||||
You will need :
|
||||
* root shell access. true sh shell, not microtik-like console
|
||||
* startup hook
|
||||
* r/w partition to store binaries and startup script with executable permission (+x)
|
||||
* tpws can be run almost anywhere but nfqws require kernel support for NFQUEUE. Its missing in most firmwares.
|
||||
* too old 2.6 kernels are unsupported and can cause errors
|
||||
If binaries crash with segfault (rare but happens on some kernels) try to unpack upx like this : upx -d tpws.
|
||||
First manually debug your scenario. Run iptables + daemon and check if its what you want.
|
||||
Write your own script with iptables magic and run required daemon from there. Put it to startup.
|
||||
Dont ask me how to do it. Its different for all firmwares and requires studying.
|
||||
Find manual or reverse engineer yourself.
|
||||
Check for race conditions. Firmware can clear or modify iptables after your startup script.
|
||||
If this is the case then run another script in background and add some delay there.
|
||||
|
||||
|
||||
Https blocking bypass
|
||||
----------------------
|
||||
|
||||
SOMETIMES (but not often) a tls handshake split trick works.
|
||||
Try MODE=..._https
|
||||
May be you're lucky.
|
||||
|
||||
MORE OFTEN DPI desync attack work, but it may require some manual tuning.
|
||||
|
||||
OTHERWISE you have to redirect traffic through a third-party host.
|
||||
It is proposed to use transparent redirect through socks5 using iptables + redsocks, or iptables + iproute + vpn.
|
||||
Redsocks variant is described in https.txt.
|
||||
iproute + wireguard - in wireguard_iproute_openwrt.txt.
|
||||
(they are russian)
|
1314
docs/readme.txt
Normal file
1314
docs/readme.txt
Normal file
File diff suppressed because it is too large
Load Diff
133
docs/wireguard/010-wg-mod.patch
Normal file
133
docs/wireguard/010-wg-mod.patch
Normal file
@@ -0,0 +1,133 @@
|
||||
Index: WireGuard-0.0.20190123/src/cookie.c
|
||||
===================================================================
|
||||
--- WireGuard-0.0.20190123.orig/src/cookie.c
|
||||
+++ WireGuard-0.0.20190123/src/cookie.c
|
||||
@@ -193,6 +193,8 @@ void wg_cookie_message_create(struct mes
|
||||
xchacha20poly1305_encrypt(dst->encrypted_cookie, cookie, COOKIE_LEN,
|
||||
macs->mac1, COOKIE_LEN, dst->nonce,
|
||||
checker->cookie_encryption_key);
|
||||
+ // MOD : randomize trash
|
||||
+ dst->header.trash = gen_trash();
|
||||
}
|
||||
|
||||
void wg_cookie_message_consume(struct message_handshake_cookie *src,
|
||||
Index: WireGuard-0.0.20190123/src/messages.h
|
||||
===================================================================
|
||||
--- WireGuard-0.0.20190123.orig/src/messages.h
|
||||
+++ WireGuard-0.0.20190123/src/messages.h
|
||||
@@ -53,23 +53,41 @@ enum limits {
|
||||
MAX_QUEUED_PACKETS = 1024 /* TODO: replace this with DQL */
|
||||
};
|
||||
|
||||
+/*
|
||||
enum message_type {
|
||||
- MESSAGE_INVALID = 0,
|
||||
- MESSAGE_HANDSHAKE_INITIATION = 1,
|
||||
- MESSAGE_HANDSHAKE_RESPONSE = 2,
|
||||
- MESSAGE_HANDSHAKE_COOKIE = 3,
|
||||
- MESSAGE_DATA = 4
|
||||
+ MESSAGE_INVALID = 0,
|
||||
+ MESSAGE_HANDSHAKE_INITIATION = 1,
|
||||
+ MESSAGE_HANDSHAKE_RESPONSE = 2,
|
||||
+ MESSAGE_HANDSHAKE_COOKIE = 3,
|
||||
+ MESSAGE_DATA = 4
|
||||
};
|
||||
+*/
|
||||
+
|
||||
+// MOD : message type
|
||||
+enum message_type {
|
||||
+ MESSAGE_INVALID = 0xE319CCD0,
|
||||
+ MESSAGE_HANDSHAKE_INITIATION = 0x48ADE198,
|
||||
+ MESSAGE_HANDSHAKE_RESPONSE = 0xFCA6A8F3,
|
||||
+ MESSAGE_HANDSHAKE_COOKIE = 0x64A3BB18,
|
||||
+ MESSAGE_DATA = 0x391820AA
|
||||
+};
|
||||
+
|
||||
+// MOD : generate fast trash without true RNG
|
||||
+__le32 gen_trash(void);
|
||||
|
||||
struct message_header {
|
||||
- /* The actual layout of this that we want is:
|
||||
- * u8 type
|
||||
- * u8 reserved_zero[3]
|
||||
- *
|
||||
- * But it turns out that by encoding this as little endian,
|
||||
- * we achieve the same thing, and it makes checking faster.
|
||||
- */
|
||||
- __le32 type;
|
||||
+ /* The actual layout of this that we want is:
|
||||
+ * u8 type
|
||||
+ * u8 reserved_zero[3]
|
||||
+ *
|
||||
+ * But it turns out that by encoding this as little endian,
|
||||
+ * we achieve the same thing, and it makes checking faster.
|
||||
+ */
|
||||
+
|
||||
+ // MOD : trash field to change message size and add 4 byte offset to all fields
|
||||
+ __le32 trash;
|
||||
+
|
||||
+ __le32 type;
|
||||
};
|
||||
|
||||
struct message_macs {
|
||||
Index: WireGuard-0.0.20190123/src/noise.c
|
||||
===================================================================
|
||||
--- WireGuard-0.0.20190123.orig/src/noise.c
|
||||
+++ WireGuard-0.0.20190123/src/noise.c
|
||||
@@ -17,6 +17,24 @@
|
||||
#include <linux/highmem.h>
|
||||
#include <crypto/algapi.h>
|
||||
|
||||
+
|
||||
+// MOD : trash generator
|
||||
+__le32 gtrash = 0;
|
||||
+__le32 gen_trash(void)
|
||||
+{
|
||||
+ if (gtrash)
|
||||
+ gtrash = gtrash*1103515243 + 12345;
|
||||
+ else
|
||||
+ // first value is true random
|
||||
+ get_random_bytes_wait(>rash, sizeof(gtrash));
|
||||
+ return gtrash;
|
||||
+}
|
||||
+
|
||||
/* This implements Noise_IKpsk2:
|
||||
*
|
||||
* <- s
|
||||
@@ -515,6 +533,10 @@ wg_noise_handshake_create_initiation(str
|
||||
&handshake->entry);
|
||||
|
||||
handshake->state = HANDSHAKE_CREATED_INITIATION;
|
||||
+
|
||||
+ // MOD : randomize trash
|
||||
+ dst->header.trash = gen_trash();
|
||||
+
|
||||
ret = true;
|
||||
|
||||
out:
|
||||
@@ -655,6 +677,10 @@ bool wg_noise_handshake_create_response(
|
||||
&handshake->entry);
|
||||
|
||||
handshake->state = HANDSHAKE_CREATED_RESPONSE;
|
||||
+
|
||||
+ // MOD : randomize trash
|
||||
+ dst->header.trash = gen_trash();
|
||||
+
|
||||
ret = true;
|
||||
|
||||
out:
|
||||
Index: WireGuard-0.0.20190123/src/send.c
|
||||
===================================================================
|
||||
--- WireGuard-0.0.20190123.orig/src/send.c
|
||||
+++ WireGuard-0.0.20190123/src/send.c
|
||||
@@ -200,6 +200,10 @@ static bool encrypt_packet(struct sk_buf
|
||||
header->header.type = cpu_to_le32(MESSAGE_DATA);
|
||||
header->key_idx = keypair->remote_index;
|
||||
header->counter = cpu_to_le64(PACKET_CB(skb)->nonce);
|
||||
+
|
||||
+ // MOD : randomize trash
|
||||
+ header->header.trash = gen_trash();
|
||||
+
|
||||
pskb_put(skb, trailer, trailer_len);
|
||||
|
||||
/* Now we can encrypt the scattergather segments */
|
244
docs/wireguard/wireguard-mod.txt
Normal file
244
docs/wireguard/wireguard-mod.txt
Normal file
@@ -0,0 +1,244 @@
|
||||
Посвящено возможной блокировке в РФ VPN протоколов через DPI.
|
||||
Предпосылками являются последние законодательные акты и во всю сочащиеся "секретные" записки.
|
||||
В РФ разрабатываются и готовятся к применению более продвинутые решения по блокировке трафика.
|
||||
Вполне вероятно будут резать стандартные VPN протоколы. Нам надо быть к этому готовыми.
|
||||
|
||||
Один из возможных и перспективных путей решения данного вопроса - кустомная модификация
|
||||
исходников VPN с целью незначительного изменения протокола, ломающего стандартные модули обнаружения в DPI.
|
||||
Это относительно сложно, доступно только для гиков.
|
||||
Никто не будет разрабатывать специальные модули обнаружения в DPI, если только кто-то не сделает простое и
|
||||
удобное решение для всех, и его станут широко применять. Но это маловероятно, и даже если и так,
|
||||
то всегда можно модифицировать протокол чуток по другому. Делать моды для DPI несравненно дольше
|
||||
и дороже, чем клепать на коленке изменения протокола для wireguard.
|
||||
|
||||
|
||||
ЗАМЕЧЕНИЕ : альтернативой модификации конечного софта для VPN является использование "навесных"
|
||||
обфускаторов. см : https://github.com/bol-van/ipobfs
|
||||
|
||||
|
||||
Рассмотрю что нам надо пропатчить в wireguard. Модифицированный wireguard проверен на виртуалках
|
||||
с десктопным linux, он работает, сообщения в wireshark действительно не вписываются в стандартный
|
||||
протокол и не опознаются.
|
||||
|
||||
Wireguard протокол очень простой. Все сообщения описаны в messages.h
|
||||
Поставим себе целью сделать 2 простые модификации :
|
||||
1) Добавим в начало всех сообщений немного мусора, чтобы изменить размер сообщений и смещения полей
|
||||
2) Изменим коды типов сообщений
|
||||
Этого может быть вполне достаточно для обмана DPI
|
||||
|
||||
--messages.h--------------------------
|
||||
/*
|
||||
enum message_type {
|
||||
MESSAGE_INVALID = 0,
|
||||
MESSAGE_HANDSHAKE_INITIATION = 1,
|
||||
MESSAGE_HANDSHAKE_RESPONSE = 2,
|
||||
MESSAGE_HANDSHAKE_COOKIE = 3,
|
||||
MESSAGE_DATA = 4
|
||||
};
|
||||
*/
|
||||
|
||||
// MOD : message type
|
||||
enum message_type {
|
||||
MESSAGE_INVALID = 0xE319CCD0,
|
||||
MESSAGE_HANDSHAKE_INITIATION = 0x48ADE198,
|
||||
MESSAGE_HANDSHAKE_RESPONSE = 0xFCA6A8F3,
|
||||
MESSAGE_HANDSHAKE_COOKIE = 0x64A3BB18,
|
||||
MESSAGE_DATA = 0x391820AA
|
||||
};
|
||||
|
||||
// MOD : generate fast trash without true RNG
|
||||
__le32 gen_trash(void);
|
||||
|
||||
struct message_header {
|
||||
/* The actual layout of this that we want is:
|
||||
* u8 type
|
||||
* u8 reserved_zero[3]
|
||||
*
|
||||
* But it turns out that by encoding this as little endian,
|
||||
* we achieve the same thing, and it makes checking faster.
|
||||
*/
|
||||
|
||||
// MOD : trash field to change message size and add 4 byte offset to all fields
|
||||
__le32 trash;
|
||||
|
||||
__le32 type;
|
||||
};
|
||||
--------------------------------------
|
||||
|
||||
Напишем функцию для генерации trash. Функция должна быть быстрая, важно не замедлить скорость.
|
||||
Мы не расчитываем, что нас будут специально ловить, иначе бы пришлось делать полноценный обфускатор.
|
||||
Задача лишь сломать стандартный модуль обнаружения протокола wireguard. Потому истинная рандомность
|
||||
trash не важна.
|
||||
Но все же немного "трэша" не повредит. Гонки между тредами так же пофигистичны. Это же трэш.
|
||||
|
||||
--noise.c-----------------------------
|
||||
// MOD : trash generator
|
||||
__le32 gtrash = 0;
|
||||
__le32 gen_trash(void)
|
||||
{
|
||||
if (gtrash)
|
||||
gtrash = gtrash*1103515243 + 12345;
|
||||
else
|
||||
// first value is true random
|
||||
get_random_bytes_wait(>rash, sizeof(gtrash));
|
||||
return gtrash;
|
||||
}
|
||||
--------------------------------------
|
||||
|
||||
Теперь осталось найти все места, где создаются сообщения и внести туда заполнение поля trash.
|
||||
Сообщений всего 4. Их можно найти по присваиванию полю type одного из значений enum message_type.
|
||||
|
||||
2 места в noise.c в функциях wg_noise_handshake_create_initiation и wg_noise_handshake_create_response,
|
||||
1 место в cookie.c в функции wg_cookie_message_create
|
||||
Дописываем в конец инициализации структуры сообщения :
|
||||
|
||||
--------------------------------------
|
||||
// MOD : randomize trash
|
||||
dst->header.trash = gen_trash();
|
||||
--------------------------------------
|
||||
|
||||
и 1 место в send.c в функции encrypt_packet
|
||||
|
||||
--------------------------------------
|
||||
// MOD : randomize trash
|
||||
header->header.trash = gen_trash();
|
||||
--------------------------------------
|
||||
|
||||
|
||||
Вот и весь патчинг. Полный patch (версия wireguard 0.0.20190123) лежит в 010-wg-mod.patch.
|
||||
Патчинг кода - самое простое. Для десктопного linux дальше все просто.
|
||||
Пересобираем через make, устанавливаем через make install, перегружаем
|
||||
модуль wireguard, перезапускаем интерфейсы, и все готово.
|
||||
|
||||
Настоящий геморой начнется когда вы это попытаетесь засунуть на роутер под openwrt.
|
||||
Одна из больших проблем linux - отсутствие совместимости драйверов на уровне бинариков.
|
||||
Поэтому собирать необходимо в точности под вашу версию ядра и в точности под его .config.
|
||||
Вам придется либо полностью самостоятельно собирать всю прошивку, либо найти SDK в точности
|
||||
от вашей версии прошивки для вашей архитектуры и собрать модуль с помощью этого SDK.
|
||||
Последний вариант более легкий.
|
||||
Для сборки вам понадобится система на linux x86_64. Ее можно установить в виртуалке.
|
||||
Теоретически можно пользоваться WSL из win10, но на практике там очень медленное I/O,
|
||||
по крайней мере на старых версиях win10. Безумно медленное. Будете собирать вечность.
|
||||
Может в новых win10 что-то и улучшили, но я бы сразу расчитывал на полноценный linux.
|
||||
|
||||
Находим здесь вашу версию : https://downloads.openwrt.org/
|
||||
Скачиваем файл openwrt-sdk-*.tar.xz или lede-sdk-*.tar.xz
|
||||
Например : https://downloads.openwrt.org/releases/18.06.2/targets/ar71xx/generic/openwrt-sdk-18.06.2-ar71xx-generic_gcc-7.3.0_musl.Linux-x86_64.tar.xz
|
||||
Если ваша версия непонятна или стара, то проще будет найти последнюю прошивку и перешить роутер.
|
||||
Распаковываем SDK. Следующими командами можно собрать оригинальный вариант wireguard :
|
||||
|
||||
# scripts/feeds update -a
|
||||
# scripts/feeds install -a
|
||||
# make defconfig
|
||||
# make -j 4 package/wireguard/compile
|
||||
|
||||
Сборка будет довольно долгой. Ведь придется подтащить ядро, собрать его, собрать зависимости.
|
||||
"-j 4" означает использовать 4 потока. Впишите вместо 4 количество доступных cpu cores.
|
||||
|
||||
Получим следующие файлы :
|
||||
|
||||
openwrt-sdk-18.06.2-ar71xx-generic_gcc-7.3.0_musl.Linux-x86_64/bin/targets/ar71xx/generic/packages/kmod-wireguard_4.9.152+0.0.20190123-1_mips_24kc.ipk
|
||||
openwrt-sdk-18.06.2-ar71xx-generic_gcc-7.3.0_musl.Linux-x86_64/bin/packages/mips_24kc/base/wireguard-tools_0.0.20190123-1_mips_24kc.ipk
|
||||
|
||||
Но это будет оригинальный wireguard. Нам нужен патченый.
|
||||
Установим quilt и mc для нормального редактора вместо vim :
|
||||
|
||||
# sudo apt-get update
|
||||
# sudo apt-get install quilt mc
|
||||
|
||||
# make package/wireguard/clean
|
||||
# make package/wireguard/prepare V=s QUILT=1
|
||||
|
||||
|
||||
Сорцы приготовлены для сборки в :
|
||||
openwrt-sdk-18.06.2-ar71xx-generic_gcc-7.3.0_musl.Linux-x86_64/build_dir/target-mips_24kc_musl/linux-ar71xx_generic/WireGuard-0.0.20190123/src
|
||||
|
||||
# cd build_dir/target-mips_24kc_musl/linux-ar71xx_generic/WireGuard-0.0.20190123/src
|
||||
# quilt push -a
|
||||
# quilt new 010-wg-mod.patch
|
||||
# export EDITOR=mcedit
|
||||
|
||||
Далее будет открываться редактор mcedit, в который нужно вносить изменения в каждый файл :
|
||||
|
||||
# quilt edit messages.h
|
||||
# quilt edit cookie.c
|
||||
# quilt edit noise.c
|
||||
# quilt edit send.c
|
||||
# quilt diff
|
||||
# quilt refresh
|
||||
|
||||
Получили файл патча в :
|
||||
openwrt-sdk-18.06.2-ar71xx-generic_gcc-7.3.0_musl.Linux-x86_64/build_dir/target-mips_24kc_musl/linux-ar71xx_generic/WireGuard-0.0.20190123/patches/010-wg-mod.patch
|
||||
|
||||
Выходим в корень SDK.
|
||||
|
||||
# make package/wireguard/compile V=99
|
||||
|
||||
Если не было ошибок, то получили измененные ipk.
|
||||
Патч можно зафиксировать в описании пакета :
|
||||
|
||||
# make package/wireguard/update
|
||||
|
||||
Получим :
|
||||
openwrt-sdk-18.06.2-ar71xx-generic_gcc-7.3.0_musl.Linux-x86_64/feeds/base/package/network/services/wireguard/patches/010-wg-mod.patch
|
||||
При последующей очистке и пересборке он будет автоматом применяться.
|
||||
|
||||
|
||||
АЛЬТЕРНАТИВА : можно не возиться с quilt.
|
||||
сделайте
|
||||
# make package/wireguard/clean
|
||||
# make package/wireguard/prepare
|
||||
и напрямую модифицируйте или копируйте файлы в
|
||||
openwrt-sdk-18.06.2-ar71xx-generic_gcc-7.3.0_musl.Linux-x86_64/build_dir/target-mips_24kc_musl/linux-ar71xx_generic/WireGuard-0.0.20190123/src
|
||||
затем
|
||||
# make package/wireguard/compile
|
||||
|
||||
Если нужно поменять версию wireguard, то идите в
|
||||
openwrt-sdk-18.06.2-ar71xx-generic_gcc-7.3.0_musl.Linux-x86_64/feeds/base/package/network/services/wireguard/Makefile
|
||||
поменяйте там версию в PKG_VERSION на последнюю из : https://git.zx2c4.com/WireGuard
|
||||
скачайте tar.xz с этой версией , вычислите его sha256sum, впишите в PKG_HASH
|
||||
|
||||
1 раз где-нибудь пропатчите файлы последней версии wireguard в текстовом редакторе, скопируйте в build_dir,
|
||||
сделайте версию для openwrt. эти же файлы скопируйте на ваш сервер с десктопным linux, сделайте там make / make install
|
||||
|
||||
Но имейте в виду, что build_dir - локация для временных файлов.
|
||||
make clean оттуда все снесет, включая ваши модификации. Модифицированные файлы лучше сохранить отдельно,
|
||||
чтобы потом было легко скопировать обратно.
|
||||
|
||||
Полученные ipk копируем на роутер в /tmp, устанавливаем через
|
||||
# cd /tmp
|
||||
# rm -r /tmp/opkg-lists
|
||||
# opkg install *.ipk
|
||||
Если требует зависимостей, то
|
||||
# opkg update
|
||||
# opkg install .... <зависимости>
|
||||
# rm -r /tmp/opkg-lists
|
||||
# opkg install *.ipk
|
||||
|
||||
В /tmp/opkg-lists opkg хранит кэш списка пакетов. Если попытаться установить файл ipk, и такой же пакет
|
||||
найдется в репозитории, opkg будет устанавливать из репозитория. А нам это не надо.
|
||||
|
||||
# rmmod wireguard
|
||||
# kmodloader
|
||||
# dmesg | tail
|
||||
должны увидеть что-то вроде :
|
||||
[8985.415490] wireguard: WireGuard 0.0.20190123 loaded. See www.wireguard.com for information.
|
||||
[8985.424178] wireguard: Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
||||
значит модуль загрузился
|
||||
|
||||
Могут понадобиться ключи opkg --force-reinstall, --force-depends.
|
||||
--force-depends поможет при несоответствии hash версии ядра. То есть версия x.x.x та же самая, но hash конфигурации разный.
|
||||
При несоответствии x.x.x вы что-то делаете не так, работать это не будет.
|
||||
Например : 4.14.56-1-b1186491495127cc6ff81d29c00a91fc, 4.14.56-1-3f8a21a63974cfb7ee67e41f2d4b805d
|
||||
Это свидетельствует о несоответствии .config ядра при сборке прошивки и в SDK.
|
||||
Если несоответствие легкое, то может все прокатить, но при более серьезной разнице в .config модуль может не загрузиться
|
||||
или вызвать стабильные или хаотические падения ядра и перезагрузки (включая вариант беcконечной перезагрузки - bootloop).
|
||||
Так что перед --force-depends убедитесь, что знаете как лечится такая ситуация, и не стоит это делать при отсутствии физического
|
||||
доступа к девайсу.
|
||||
|
||||
Когда поднимите линк, и вдруг ничего не будет работать, то посмотрите в wireshark udp пакеты
|
||||
на порт endpoint. Они не должны начинаться с 0,1,2,3,4. В первых 4 байтах должен быть рандом,
|
||||
в следующих 4 байтах - значения из измененного enum message_type. Если пакет все еще начинается с 0..4,
|
||||
значит модуль wireguard оригинальный, что-то не собралось, не скопировалось, не перезапустилось.
|
||||
В противном случае должен подняться линк, пинги ходить. Значит вы победили, поздравляю.
|
||||
Регулятору будет намного сложнее поймать ваш VPN.
|
519
docs/wireguard/wireguard_iproute_openwrt.txt
Normal file
519
docs/wireguard/wireguard_iproute_openwrt.txt
Normal file
@@ -0,0 +1,519 @@
|
||||
Есть возможность поднять свой VPN сервер ? Не хотим использовать redsocks ?
|
||||
Хотим завертывать на VPN только часть трафика ?
|
||||
Например, из ipset zapret только порт tcp:443, из ipban - весь трафик, не только tcp ?
|
||||
Да, с VPN такое возможно.
|
||||
Опишу понятийно как настраивается policy based routing в openwrt на примере wireguard.
|
||||
Вместо wireguard можно использовать openvpn или любой другой. Но wireguard прекрасен сразу несколькими вещами.
|
||||
Главная из которых - в разы большая скорость, даже немного превышающая ipsec.
|
||||
Ведь openvpn основан на tun, а tun - всегда в разы медленнее решения в kernel mode,
|
||||
и если для PC оно может быть не так актуально, для soho роутеров - более чем.
|
||||
Wireguard может дать 50 mbps там, где openvpn еле тащит 10.
|
||||
Но есть и дополнительное требование. Wireguard работает в ядре, значит ядро должно
|
||||
быть под вашим контролем. vps на базе openvz не подойдет ! Нужен xen, kvm,
|
||||
любой другой вариант, где загружается ваше собственное ядро, а не используется
|
||||
общее, разделяемое на множество vps. В openvz вам никто не даст лезть в ядро.
|
||||
|
||||
Если вдруг окажется, что основные VPN протоколы блокируется DPI, включая wireguard,
|
||||
то стоит смотреть в сторону либо обфускации трафика до состояния нераспознаваемого
|
||||
мусора, либо маскировки под TLS (лучше на порт 443). Скорость, конечно, вы потеряете, но это
|
||||
та самая ситуация, которая описывается словами "медленно или никак".
|
||||
Маскированные под TLS протоколы DPI может распознать двумя действиями :
|
||||
пассивно через анализ статистических характеристик пакетов (время, размер, периодичность, ..)
|
||||
или активно через подключение к вашему серверу от себя и попытку поговорить с сервером по
|
||||
известным протоколам (называется active probing). Если вы подключаетесь к серверу
|
||||
с фиксированных IP, то активный пробинг можно надежно заблокировать через ограничение
|
||||
диапазонов IP адресов, с которых можно подключаться к серверу. В ином случае можно использовать
|
||||
технику "port knocking".
|
||||
Перспективным направлением так же считаю легкую собственную модификацию исходников
|
||||
существующих VPN с целью незначительного изменения протокола, которая ломает стандартные
|
||||
модули обнаружения в DPI. В wireguard можно добавить в начало пакета handshake лишнее поле,
|
||||
заполненное случайным мусором. Разумеется, в таком случае требуется держать измененную версию
|
||||
как на сервере, так и на клиенте. Если затея срабатывает, то вы получаете максимальную
|
||||
скорость, при этом полностью нагибая регулятора.
|
||||
Полезная инфа по теме : https://habr.com/ru/post/415977/
|
||||
|
||||
Понятийно необходимо выполнить следующие шаги :
|
||||
1) Поднять vpn сервер.
|
||||
2) Настроить vpn клиент. Результат этого шага - получение поднятого интерфейса vpn.
|
||||
Будь то wireguard, openvpn или любой другой тип vpn.
|
||||
3) Создать такую схему маршрутизации, при которой пакеты, помечаемые особым mark,
|
||||
попадают на vpn, а остальные идут обычным способом.
|
||||
4) Создать правила, выставляющие mark для всего трафика, который необходимо рулить на vpn.
|
||||
Критерии могут быть любые, ограниченные лишь возможностями iptables и вашим воображением.
|
||||
|
||||
Будем считать наш vpn сервер находится на ip 91.15.68.202.
|
||||
Вешать его будем на udp порт 12345. На этот же порт будем вешать и клиентов.
|
||||
Сервер работает под debian 9. Клиент работает под openwrt.
|
||||
Для vpn отведем подсеть 192.168.254.0/24.
|
||||
|
||||
--- Поднятие сервера ---
|
||||
|
||||
На сервере должны быть установлены заголовки ядра (linux-headers-...) и компилятор gcc.
|
||||
Качаем последний tar.xz с wireguard отсюда : https://git.zx2c4.com/WireGuard/
|
||||
|
||||
# tar xf WireGuard*.tar.xz
|
||||
# cd WireGuard-*/src
|
||||
# make
|
||||
# strip --strip-debug wireguard.ko
|
||||
# sudo make install
|
||||
|
||||
wireguard основан на понятии криптороутинга. Каждый пир (сервер - тоже пир)
|
||||
имеет пару открытый/закрытый ключ. Закрытый ключ остается у пира,
|
||||
открытый прописывается у его партнера. Каждый пир авторизует другого
|
||||
по знанию приватного ключа, соответствующего прописанному у него публичному ключу.
|
||||
Протокол построен таким образом, что на все неправильные udp пакеты не следует ответа.
|
||||
Не знаешь приватный ключ ? Не смог послать правильный запрос ? Долбись сколько влезет,
|
||||
я тебе ничего не отвечу. Это защищает от активного пробинга со стороны DPI и просто
|
||||
экономит ресурсы.
|
||||
Значит первым делом нужно создать 2 пары ключей : для сервера и для клиента.
|
||||
wg genkey генерит приватный ключ, wg pubkey получает из него публичный ключ.
|
||||
|
||||
# wg genkey
|
||||
oAUkmhoREtFQ5D5yZmeHEgYaSWCcLYlKe2jBP7EAGV0=
|
||||
# echo oAUkmhoREtFQ5D5yZmeHEgYaSWCcLYlKe2jBP7EAGV0= | wg pubkey
|
||||
bCdDaPYSTBZVO1HTmKD+Tztuf3PbOWGDWfz7Lb1E6C4=
|
||||
# wg genkey
|
||||
OKXX0TSlyjJmGt3/yHlHxi0AqjJ0vh+Msne3qEHk0VM=
|
||||
# echo OKXX0TSlyjJmGt3/yHlHxi0AqjJ0vh+Msne3qEHk0VM= | wg pubkey
|
||||
EELdA2XzjcKxtriOCPBXMOgxlkgpbRdIyjtc3aIpkxg=
|
||||
|
||||
Пишем конфиг
|
||||
--/etc/wireguard/wgvps.conf-------------------
|
||||
[Interface]
|
||||
PrivateKey = OKXX0TSlyjJmGt3/yHlHxi0AqjJ0vh+Msne3qEHk0VM=
|
||||
ListenPort = 12345
|
||||
|
||||
[Peer]
|
||||
#Endpoint =
|
||||
PublicKey = bCdDaPYSTBZVO1HTmKD+Tztuf3PbOWGDWfz7Lb1E6C4=
|
||||
AllowedIPs = 192.168.254.3
|
||||
PersistentKeepalive=20
|
||||
----------------------------------------------
|
||||
|
||||
Wireguard - минималистичный vpn. В нем нет никаких средств для автоконфигурации ip.
|
||||
Все придется прописывать руками.
|
||||
В wgvps.conf должны быть перечислены все пиры с их публичными ключами,
|
||||
а так же прописаны допустимые для них ip адреса.
|
||||
Назначим нашему клиенту 192.168.254.3. Сервер будет иметь ip 192.168.254.1.
|
||||
Endpoint должен быть прописан хотя бы на одном пире.
|
||||
Если endpoint настроен для пира, то wireguard будет периодически пытаться к нему подключиться.
|
||||
В схеме клиент/сервер у сервера можно не прописывать endpoint-ы пиров, что позволит
|
||||
менять ip и быть за nat. Endpoint пира настраивается динамически после успешной фазы
|
||||
проверки ключа.
|
||||
|
||||
Включаем маршрутизцию :
|
||||
# echo net.ipv4.ip_forward = 1 >>/etc/sysctl.conf
|
||||
# sysctl -p
|
||||
|
||||
Интерфейс конфигурится стандартно для дебианоподобных систем :
|
||||
|
||||
--/etc/network/interfaces.d/wgvps-------------
|
||||
auto wgvps
|
||||
iface wgvps inet static
|
||||
address 192.168.254.1
|
||||
netmask 255.255.255.0
|
||||
pre-up ip link add $IFACE type wireguard
|
||||
pre-up wg setconf $IFACE /etc/wireguard/$IFACE.conf
|
||||
post-up iptables -t nat -A POSTROUTING -o eth0 -s 192.168.254.0/24 -j MASQUERADE
|
||||
post-up iptables -A FORWARD -o eth0 -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
|
||||
post-down iptables -D FORWARD -o eth0 -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
|
||||
post-down iptables -t nat -D POSTROUTING -o eth0 -s 192.168.254.0/24 -j MASQUERADE
|
||||
post-down ip link del $IFACE
|
||||
----------------------------------------------
|
||||
|
||||
Поднятие через ifup wgvps, опускание через ifdown wgvps.
|
||||
При поднятии интерфейса заодно настраивается nat. eth0 здесь означает интерфейс vpn сервера с инетовским ip адресом.
|
||||
Если у вас какая-то система управления фаерволом, то надо настройку nat прикручивать туда.
|
||||
Пример написан для простейшего случая, когда никаких ограничений нет, таблицы iptables пустые.
|
||||
Чтобы посмотреть текущие настройки wireguard, запустите 'wg' без параметров.
|
||||
|
||||
|
||||
--- Поднятие клиента ---
|
||||
|
||||
# opkg update
|
||||
# opkg install wireguard
|
||||
|
||||
Добавляем записи в конфиги.
|
||||
|
||||
--/etc/config/network--------------------------
|
||||
config interface 'wgvps'
|
||||
option proto 'wireguard'
|
||||
option auto '1'
|
||||
option private_key 'oAUkmhoREtFQ5D5yZmeHEgYaSWCcLYlKe2jBP7EAGV0='
|
||||
option listen_port '12345'
|
||||
option metric '9'
|
||||
option mtu '1420'
|
||||
|
||||
config wireguard_wgvps
|
||||
option public_key 'EELdA2XzjcKxtriOCPBXMOgxlkgpbRdIyjtc3aIpkxg=
|
||||
list allowed_ips '0.0.0.0/0'
|
||||
option endpoint_host '91.15.68.202'
|
||||
option endpoint_port '12345'
|
||||
option route_allowed_ips '0'
|
||||
option persistent_keepalive '20'
|
||||
|
||||
config interface 'wgvps_ip'
|
||||
option proto 'static'
|
||||
option ifname '@wgvps'
|
||||
list ipaddr '192.168.254.3/24'
|
||||
|
||||
config route
|
||||
option interface 'wgvps'
|
||||
option target '0.0.0.0/0'
|
||||
option table '100'
|
||||
|
||||
config rule
|
||||
option mark '0x800/0x800'
|
||||
option priority '100'
|
||||
option lookup '100'
|
||||
------------------------------------------------
|
||||
|
||||
--/etc/config/firewall--------------------------
|
||||
config zone
|
||||
option name 'tunvps'
|
||||
option output 'ACCEPT'
|
||||
option input 'REJECT'
|
||||
option masq '1'
|
||||
option mtu_fix '1'
|
||||
option forward 'REJECT'
|
||||
option network 'wgvps wgvps_ip'
|
||||
|
||||
config forwarding
|
||||
option dest 'tunvps'
|
||||
option src 'lan'
|
||||
|
||||
config rule
|
||||
option name 'Allow-ICMP-tunvps'
|
||||
option src 'tunvps'
|
||||
option proto 'icmp'
|
||||
option target 'ACCEPT'
|
||||
|
||||
config rule
|
||||
option target 'ACCEPT'
|
||||
option src 'wan'
|
||||
option proto 'udp'
|
||||
option family 'ipv4'
|
||||
option src_port '12345'
|
||||
option src_ip '91.15.68.202'
|
||||
option name 'WG-VPS'
|
||||
------------------------------------------------
|
||||
|
||||
Что тут было сделано :
|
||||
*) Настроен интерфейс wireguard. Указан собственный приватный ключ.
|
||||
*) Настроен пир-партнер с указанием его публичнго ключа и endpoint (ip:port нашего сервера)
|
||||
такая настройка заставит периодически долбиться на сервер по указанному ip
|
||||
route_allowed_ip '0' запрещает автоматическое создание маршрута
|
||||
allowed_ips '0.0.0.0/0' разрешает пакеты с любым адресом источника.
|
||||
ведь мы собираемся подключаться к любым ip в инете
|
||||
persistent_keepalive '20' помогает исключить дропание mapping на nat-е, если мы сидим за ним,
|
||||
да и вообще полезная вещь, чтобы не было подвисших пиров
|
||||
*) Статическая конфигурация ip интерфейса wgvps.
|
||||
*) Маршрут default route на wgvps в отдельной таблице маршрутизации с номером 100. Аналог команды ip route add .. table 100
|
||||
*) Правило использовать таблицу 100 при выставлении в mark бита 0x800. Аналог команды ip rule.
|
||||
*) Отдельная зона фаервола для VPN - 'tunvps'. В принципе ее можно не создавать, можете приписать интерфейс к зоне wan.
|
||||
Но в случае с отдельной зоной можно настроить особые правила на подключения с vpn сервера в сторону клиента.
|
||||
*) Разрешение форвардинга между локалкой за роутером и wgvps.
|
||||
*) Разрешение принимать icmp от vpn сервера, включая пинги. ICMP жизненно важны для правильного функционирования ip сети !
|
||||
*) И обязательно проткнуть дырку в фаерволе, чтобы принимать пакеты wireguard со стороны инетовского ip vpn сервера.
|
||||
|
||||
# fw3 restart
|
||||
# ifup wgvps
|
||||
# ifconfig wgvps
|
||||
# ping 192.168.254.1
|
||||
|
||||
Если все хорошо, должны ходить пинги.
|
||||
С сервера не помешает :
|
||||
# ping 192.168.254.3
|
||||
|
||||
|
||||
--- Маркировка трафика ---
|
||||
|
||||
Завернем на vpn все из ipset zapret на tcp:443 и все из ipban.
|
||||
OUTPUT относится к исходящим с роутера пакетам, PREROUTING - ко всем остальным.
|
||||
Если с самого роутера ничего заруливать не надо, можно опустить все до команд с PREROUTING.
|
||||
|
||||
--/etc/firewall.user----------------------------
|
||||
. /opt/zapret/init.d/openwrt/functions
|
||||
|
||||
create_ipset no-update
|
||||
|
||||
network_find_wan_all wan_iface
|
||||
for ext_iface in $wan_iface; do
|
||||
network_get_device DEVICE $ext_iface
|
||||
ipt OUTPUT -t mangle -o $DEVICE -p tcp --dport 443 -m set --match-set zapret dst -j MARK --set-mark 0x800/0x800
|
||||
ipt OUTPUT -t mangle -o $DEVICE -m set --match-set ipban dst -j MARK --set-mark 0x800/0x800
|
||||
done
|
||||
|
||||
network_get_device DEVICE lan
|
||||
ipt PREROUTING -t mangle -i $DEVICE -p tcp --dport 443 -m set --match-set zapret dst -j MARK --set-mark 0x800/0x800
|
||||
ipt PREROUTING -t mangle -i $DEVICE -m set --match-set ipban dst -j MARK --set-mark 0x800/0x800
|
||||
------------------------------------------------
|
||||
|
||||
# fw3 restart
|
||||
|
||||
|
||||
--- По поводу двойного NAT ---
|
||||
|
||||
В описанной конфигурации nat выполняется дважды : на роутере-клиенте происходит замена адреса источника из LAN
|
||||
на 192.168.254.3 и на сервере замена 192.168.254.3 на внешний адрес сервера в инете.
|
||||
Зачем так делать ? Исключительно для простоты настройки. Но если вы готовы чуток еще поднапрячься и не хотите двойного nat,
|
||||
то можете вписать в /etc/config/firewall "masq '0'", на сервер дописать маршрут до вашей подсети lan.
|
||||
Чтобы не делать это для каждого клиента, можно отвести под всех клиентов диапазон 192.168.0.0-192.168.127.255
|
||||
и прописать его одним маршрутом.
|
||||
|
||||
--/etc/network/interfaces.d/wgvps-------------
|
||||
post-up ip route add dev $IFACE 192.168.0.0/17
|
||||
post-down ip route del dev $IFACE 192.168.0.0/17
|
||||
----------------------------------------------
|
||||
|
||||
Так же необходимо указать wireguard дополнительные разрешенные ip для peer :
|
||||
|
||||
--/etc/wireguard/wgvps.conf-------------------
|
||||
[Peer]
|
||||
PublicKey = bCdDaPYSTBZVO1HTmKD+Tztuf3PbOWGDWfz7Lb1E6C4=
|
||||
AllowedIPs = 192.168.254.3, 192.168.2.0/24
|
||||
----------------------------------------------
|
||||
|
||||
Всем клиентам придется назначать различные диапазоны адресов в lan и индивидуально прописывать AllowedIPs
|
||||
для каждого peer.
|
||||
|
||||
# ifdown wgvps ; ifup wgvps
|
||||
|
||||
На клиенте разрешим форвард icmp, чтобы работал пинг и корректно определялось mtu.
|
||||
|
||||
--/etc/config/firewall--------------------------
|
||||
config rule
|
||||
option name 'Allow-ICMP-tunvps'
|
||||
option src 'tunvps'
|
||||
option dest 'lan'
|
||||
option proto 'icmp'
|
||||
option target 'ACCEPT'
|
||||
------------------------------------------------
|
||||
|
||||
Существуют еще два неочевидных нюанса.
|
||||
|
||||
Первый из них касается пакетов с самого роутера (цепочка OUTPUT).
|
||||
Адрес источника выбирается по особому алгоритму, если программа явно его не задала, еще до этапа iptables.
|
||||
Он берется с интерфейса, куда бы пошел пакет при нормальном раскладе.
|
||||
Обратная маршрутизация с VPN станет невозможной, да и wireguard такие пакеты порежет, поскольку они не вписываются в AllowedIPs.
|
||||
Никаким мистическим образом автоматом source address не поменяется.
|
||||
В прошлом варианте настройки проблема решалось через маскарад. Сейчас же маскарада нет.
|
||||
Потому все же придется его делать в случае, когда пакет изначально направился бы через wan,
|
||||
а мы его завертываем на VPN. Помечаем такие пакеты марком 0x1000.
|
||||
Если вам не актуальны исходящие с самого роутера, то можно ничего не менять.
|
||||
|
||||
Другой нюанс связан с обработкой проброшенных на vps портов, соединения по которым приходят как входящие с интерфейса wgvps.
|
||||
Представьте себе, что вы пробросили порт 2222. Кто-то подключается с адреса 1.2.3.4. Вам приходит пакет SYN 1.2.3.4:51723=>192.168.2.2:2222.
|
||||
По правилам маршрутизации он пойдет в локалку. 192.168.2.2 его обработает, ответит пакетом ACK 192.168.2.2:2222=>1.2.3.4:51723.
|
||||
Этот пакет придет на роутер. И куда он дальше пойдет ? Если он не занесен в ipban, то согласно правилам машрутизации
|
||||
он пойдет по WAN интерфейсу, а не по исходному wgvps.
|
||||
Чтобы решить эту проблему, необходимо воспользоваться CONNMARK. Существуют 2 отдельных марка : fwmark и connmark.
|
||||
connmark относится к соединению, fwmark - к пакету. Трэкингом соединений занимается conntrack.
|
||||
Посмотреть его таблицу можно командой "conntrack -L". Там же найдете connmark : mark=xxxx.
|
||||
Как только видим приходящий с wgvps пакет с новым соединением, отмечаем его connmark как 0x800/0x800.
|
||||
При этом fwmark не меняется, иначе бы пакет тут же бы завернулся обратно на wgvps согласно ip rule.
|
||||
Если к нам приходит пакет с какого-то другого интерфейса, то восстанавливаем его connmark в fwmark по маске 0x800.
|
||||
И теперь он подпадает под правило ip rule, заворачиваясь на wgvps, что и требовалось.
|
||||
|
||||
--/etc/firewall.user----------------------------
|
||||
. /opt/zapret/init.d/openwrt/functions
|
||||
|
||||
create_ipset no-update
|
||||
|
||||
network_find_wan_all wan_iface
|
||||
for ext_iface in $wan_iface; do
|
||||
network_get_device DEVICE $ext_iface
|
||||
ipt OUTPUT -t mangle -o $DEVICE -p tcp --dport 443 -m set --match-set zapret dst -j MARK --set-mark 0x800/0x800
|
||||
ipt OUTPUT -t mangle -o $DEVICE -m set --match-set ipban dst -j MARK --set-mark 0x800/0x800
|
||||
ipt OUTPUT -t mangle -o $DEVICE -j MARK --set-mark 0x1000/0x1000
|
||||
done
|
||||
|
||||
network_get_device DEVICE lan
|
||||
ipt PREROUTING -t mangle -i $DEVICE -p tcp --dport 443 -m set --match-set zapret dst -j MARK --set-mark 0x800/0x800
|
||||
ipt PREROUTING -t mangle -i $DEVICE -m set --match-set ipban dst -j MARK --set-mark 0x800/0x800
|
||||
|
||||
# do masquerade for OUTPUT to ensure correct outgoing address
|
||||
ipt postrouting_tunvps_rule -t nat -m mark --mark 0x1000/0x1000 -j MASQUERADE
|
||||
|
||||
# incoming from wgvps
|
||||
network_get_device DEVICE wgvps
|
||||
ipt PREROUTING -t mangle ! -i $DEVICE -j CONNMARK --restore-mark --nfmask 0x800 --ctmask 0x800
|
||||
ipt PREROUTING -t mangle -i $DEVICE -m conntrack --ctstate NEW -j CONNMARK --set-xmark 0x800/0x800
|
||||
------------------------------------------------
|
||||
|
||||
|
||||
# fw3 restart
|
||||
|
||||
Сейчас уже можно с vpn сервера пингануть ip адрес внутри локалки клиента. Пинги должны ходить.
|
||||
|
||||
Отсутствие двойного NAT значительно облегчает проброс портов с внешнего IP vpn сервера в локалку какого-либо клиента.
|
||||
Для этого надо выполнить 2 действия : добавить разрешение в фаервол на клиенте и сделать dnat на сервере.
|
||||
Пример форварда портов 5001 и 5201 на 192.168.2.2 :
|
||||
|
||||
--/etc/config/firewall--------------------------
|
||||
config rule
|
||||
option target 'ACCEPT'
|
||||
option src 'tunvps'
|
||||
option dest 'lan'
|
||||
option proto 'tcp udp'
|
||||
option dest_port '5001 5201'
|
||||
option dest_ip '192.168.2.2'
|
||||
option name 'IPERF'
|
||||
------------------------------------------------
|
||||
|
||||
# fw3 restart
|
||||
|
||||
--/etc/network/interfaces.d/wgvps-------------
|
||||
post-up iptables -t nat -A PREROUTING -i eth0 -p tcp -m multiport --dports 5001,5201 -j DNAT --to-destination 192.168.2.2
|
||||
post-up iptables -t nat -A POSTROUTING -o $IFACE -d 192.168.2.2 -p tcp -m multiport --dports 5001,5201 -j MASQUERADE
|
||||
post-up iptables -t nat -A PREROUTING -i eth0 -p udp -m multiport --dports 5001,5201 -j DNAT --to-destination 192.168.2.2
|
||||
post-up iptables -t nat -A POSTROUTING -o $IFACE -d 192.168.2.2 -p udp -m multiport --dports 5001,5201 -j MASQUERADE
|
||||
post-down iptables -t nat -D PREROUTING -i eth0 -p tcp -m multiport --dports 5001,5201 -j DNAT --to-destination 192.168.2.2
|
||||
post-down iptables -t nat -D POSTROUTING -o $IFACE -d 192.168.2.2 -p tcp -m multiport --dports 5001,5201 -j MASQUERADE
|
||||
post-down iptables -t nat -D PREROUTING -i eth0 -p udp -m multiport --dports 5001,5201 -j DNAT --to-destination 192.168.2.2
|
||||
post-down iptables -t nat -D POSTROUTING -o $IFACE -d 192.168.2.2 -p udp -m multiport --dports 5001,5201 -j MASQUERADE
|
||||
----------------------------------------------
|
||||
|
||||
# ifdown wgvps ; ifup wgvps
|
||||
|
||||
Пример приведен для iperf и iperf3, чтобы показать как пробрасывать несколько портов tcp+udp с минимальным количеством команд.
|
||||
Проброс tcp и udp порта так же необходим для полноценной работы bittorrent клиента, чтобы работали входящие.
|
||||
|
||||
--- Как мне отправлять на vpn весь трафик с bittorrent ? ---
|
||||
|
||||
Можно поступить так : посмотрите порт в настройках torrent клиента, убедитесь, что не поставлено "случайный порт",
|
||||
добавьте на роутер правило маркировки по порту источника.
|
||||
Но мне предпочтительно иное решение. На windows есть замечательная возможность
|
||||
прописать правило установки поля качества обслуживания в заголовках ip пакетов в зависимости от процесса-источника.
|
||||
Для windows 7/2008R2 необходимо будет установить ключик реестра и перезагрузить комп :
|
||||
# reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\QoS /v "Do not use NLA" /t REG_SZ /d "1"
|
||||
Редактировать политику можно в : gpedit.msc -> Computer Configuration -> Windows Settings -> Policy-based QoS
|
||||
На win 10 ключик реестра больше не работает, правила qos в gpedit применяются только для профиля домена.
|
||||
Необходимо пользоваться командой powershell New-NetQosPolicy. Гуглите хелп по ней. Пример :
|
||||
# powershell New-NetQosPolicy -Name "torrent" -AppPathNameMatchCondition "qbittorrent.exe" -DSCPAction 1
|
||||
Однозначно требуется проверка в wireshark или netmon успешности установки поля dscp. Если там по-прежнему 0x00,
|
||||
значит что-то не сработало. 0x04 означает DSCP=1 (dscp находится в старших 6 битах).
|
||||
|
||||
На роутере в фаер прописываем правило :
|
||||
|
||||
--/etc/config/firewall--------------------------
|
||||
config rule
|
||||
option target 'MARK'
|
||||
option src 'lan'
|
||||
option proto 'all'
|
||||
option extra '-m dscp --dscp 1'
|
||||
option name 'route-dscp-1'
|
||||
option set_mark '0x0800/0x0800'
|
||||
------------------------------------------------
|
||||
|
||||
# fw3 restart
|
||||
|
||||
Теперь все с полем dscp "1" идет на vpn. Клиент сам решает какой трафик ему нужно забрасывать
|
||||
на vpn, перенастраивать роутер не нужно.
|
||||
На linux клиенте проще всего будет выставлять dscp в iptables по номеру порта источника :
|
||||
|
||||
--/etc/rc.local---------------------------------
|
||||
iptables -A OUTPUT -t mangle -p tcp --sport 23444 -j DSCP --set-dscp 1
|
||||
iptables -A OUTPUT -t mangle -p udp --sport 23444 -j DSCP --set-dscp 1
|
||||
------------------------------------------------
|
||||
|
||||
можно привязываться к pid процесса, но тогда нужно перенастраивать iptables при каждом перезапуске
|
||||
торент клиента, это требует рута, и все становится очень неудобно.
|
||||
|
||||
|
||||
--- Автоматизация проброса портов через miniupnd ---
|
||||
|
||||
Да, его тоже можно использовать на vps. Только как всегда есть нюансы.
|
||||
|
||||
miniupnpd поддерживает 3 протокола IGD : upnp,nat-pmp и pcp.
|
||||
upnp и pcp работают через мультикаст, который не пройдет через wgvps.
|
||||
nat-pmp работает через посылку специальных сообщений на udp:5351 на default gateway.
|
||||
Обычно их обслуживает miniupnpd на роутере. При создании lease miniupnpd добавляет
|
||||
правила для проброса портов в цепочку iptables MINIUPNPD, при потери lease - убирает.
|
||||
|
||||
udp:5351 можно перенаправить на vpn сервер через DNAT, чтобы их обрабатывал miniupnpd там.
|
||||
Но вы должны иметь однозначный критерий перенаправления.
|
||||
Если вы решили завернуть на vpn все, то проблем нет. Пробрасываем udp:5351 безусловно.
|
||||
Если у вас идет перенаправление только с торрент, то необходимо к условию перенаправления
|
||||
добавить условия, выделяющие torrent трафик из прочего. Или по dscp, или по sport.
|
||||
Чтобы запросы от остальных программ обрабатывались miniupnpd на роутере.
|
||||
Если какая-то программа создаст lease не там, где нужно, то входящий трафик до нее не дойдет.
|
||||
|
||||
На роутере стоит запретить протокол upnp, чтобы торрент клиент не удовлетворился запросом,
|
||||
обслуженным по upnp на роутере, и пытался использовать nat-pmp.
|
||||
|
||||
--/etc/config/upnp--------------------------
|
||||
config upnpd 'config'
|
||||
.....
|
||||
option enable_upnp '0'
|
||||
------------------------------------------------
|
||||
|
||||
/etc/init.d/miniupnpd restart
|
||||
|
||||
Делаем проброс порта на роутере.
|
||||
Для простоты изложения будем считать, что на vpn у нас завернут весь трафик.
|
||||
Если это не так, то следует добавить фильтр в "config redirect".
|
||||
Заодно выделяем диапазон портов для торрент клиентов.
|
||||
Порт в торент клиенте следует прописать какой-то из этого диапазона.
|
||||
|
||||
------------------------------------------------
|
||||
config redirect
|
||||
option enabled '1'
|
||||
option target 'DNAT'
|
||||
option src 'lan'
|
||||
option dest 'tunvps'
|
||||
option proto 'udp'
|
||||
option src_dport '5351'
|
||||
option dest_ip '192.168.254.1'
|
||||
option dest_port '5351'
|
||||
option name 'NAT-PMP'
|
||||
option reflection '0'
|
||||
config rule
|
||||
option enabled '1'
|
||||
option target 'ACCEPT'
|
||||
option src 'tunvps'
|
||||
option dest 'lan'
|
||||
option name 'tunvps-torrent'
|
||||
option dest_port '28000-28009'
|
||||
------------------------------------------------
|
||||
|
||||
fw3 reload
|
||||
|
||||
|
||||
На сервере :
|
||||
|
||||
apt install miniupnpd
|
||||
|
||||
--- /etc/miniupnpd/miniupnpd.conf --------
|
||||
enable_natpmp=yes
|
||||
enable_upnp=no
|
||||
lease_file=/var/log/upnp.leases
|
||||
system_uptime=yes
|
||||
clean_ruleset_threshold=10
|
||||
clean_ruleset_interval=600
|
||||
force_igd_desc_v1=no
|
||||
listening_ip=192.168.254.1/16
|
||||
ext_ifname=eth0
|
||||
------------------------------------------
|
||||
|
||||
systemctl restart miniupnpd
|
||||
|
||||
listening_ip прописан именно таким образом, чтобы обозначить диапазон разрешенных IP.
|
||||
С других IP он не будет обрабатывать запросы на редирект.
|
||||
В ext_ifname впишите название inet интерфейса на сервере.
|
||||
|
||||
Запускаем торрент клиент. Попутно смотрим в tcpdump весь путь udp:5351 до сервера и обратно.
|
||||
Смотрим syslog сервера на ругань от miniupnpd.
|
||||
Если все ок, то можем проверить редиректы : iptables -t nat -nL MINIUPNPD
|
||||
С какого-нибудь другого хоста (не vpn сервер, не ваше подключение) можно попробовать telnet-нуться на проброшенный порт.
|
||||
Должно установиться соединение. Или качайте торент и смотрите в пирах флаг "I" (incoming).
|
||||
Если "I" есть и по ним идет закачка, значит все в порядке.
|
||||
|
||||
ОСОБЕННОСТЬ НОВЫХ DEBIAN : по умолчанию используются iptables-nft. miniupnpd работает с iptables-legacy.
|
||||
ЛЕЧЕНИЕ : update-alternatives --set iptables /usr/sbin/iptables-legacy
|
||||
|
||||
|
||||
--- А если не заработало ? ---
|
||||
|
||||
Мануал пишется не как копипастная инструкция, а как помощь уже соображающему.
|
||||
В руки вам ifconfig, ip, iptables, tcpdump, ping. В умелых руках творят чудеса.
|
Reference in New Issue
Block a user