Need help with iptables and redsocks

Hi.
I have TP Link 6400 router with LTE module on OpenWRT (21.03). And trying to make all WiFi users connected to this router have access to internet via proxy (socks5). Redsocks installed.
WiFi devices -> OpenWRT router -> LTE (internet)
Have a trouble with working iptables configuration to redirect all traffic thru socks5.

Network interfaces:
uci show network

network.loopback=interface
network.loopback.device='lo'
network.loopback.proto='static'
network.loopback.ipaddr='127.0.0.1'
network.loopback.netmask='255.0.0.0'
network.globals=globals
network.globals.ula_prefix='fd66:4f36:7636::/48'
network.@device[0]=device
network.@device[0].name='br-lan'
network.@device[0].type='bridge'
network.@device[0].ports='eth0.1'
network.lan=interface
network.lan.device='br-lan'
network.lan.proto='static'
network.lan.netmask='255.255.255.0'
network.lan.ip6assign='60'
network.lan.ipaddr='192.168.0.1'
network.wan=interface
network.wan.device='eth0.2'
network.wan.proto='dhcp'
network.wan6=interface
network.wan6.device='eth0.2'
network.wan6.proto='dhcpv6'
network.@switch[0]=switch
network.@switch[0].name='switch0'
network.@switch[0].reset='1'
network.@switch[0].enable_vlan='1'
network.@switch_vlan[0]=switch_vlan
network.@switch_vlan[0].device='switch0'
network.@switch_vlan[0].vlan='1'
network.@switch_vlan[0].ports='0 1 2 6t'
network.@switch_vlan[1]=switch_vlan
network.@switch_vlan[1].device='switch0'
network.@switch_vlan[1].vlan='2'
network.@switch_vlan[1].ports='3 6t'
network.LTE=interface
network.LTE.proto='qmi'
network.LTE.device='/dev/cdc-wdm0'
network.LTE.auth='none'
network.LTE.pdptype='ipv4'
network.LTE.dhcp='0'
network.LTE.apn='umts'

Redsocks conf:

base {
        log_debug = on;
        log_info = on;
         log = stderr;
        daemon = on;
        redirector = iptables;
}
redsocks {
        local_ip = 127.0.0.1;
        local_port = 12345;
ip = XXXXXX.de;
        port = 14104;
        type = socks5;
        login = "XXXXXXX";
        password = "XXXXXX";
}
redudp {
        local_ip = 127.0.0.1;
        local_port = 10053;
        ip = 127.0.0.1;
        port = 1080;
        dest_ip = 8.8.8.8;
        dest_port = 53;
        udp_timeout = 30;
        udp_timeout_stream = 180;
}
dnstc {
        local_ip = 127.0.0.1;
        local_port = 5300;
}

My current iptables rules:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             /* !fw3 */
input_rule  all  --  anywhere             anywhere             /* !fw3: Custom input rule chain */
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED /* !fw3 */
syn_flood  tcp  --  anywhere             anywhere             tcp flags:FIN,SYN,RST,ACK/SYN /* !fw3 */
zone_lan_input  all  --  anywhere             anywhere             /* !fw3 */
zone_wan_input  all  --  anywhere             anywhere             /* !fw3 */
zone_wan_input  all  --  anywhere             anywhere             /* !fw3 */

Chain FORWARD (policy DROP)
target     prot opt source               destination         
forwarding_rule  all  --  anywhere             anywhere             /* !fw3: Custom forwarding rule chain */
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED /* !fw3 */
zone_lan_forward  all  --  anywhere             anywhere             /* !fw3 */
zone_wan_forward  all  --  anywhere             anywhere             /* !fw3 */
zone_wan_forward  all  --  anywhere             anywhere             /* !fw3 */
reject     all  --  anywhere             anywhere             /* !fw3 */

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             /* !fw3 */
output_rule  all  --  anywhere             anywhere             /* !fw3: Custom output rule chain */
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED /* !fw3 */
zone_lan_output  all  --  anywhere             anywhere             /* !fw3 */
zone_wan_output  all  --  anywhere             anywhere             /* !fw3 */
zone_wan_output  all  --  anywhere             anywhere             /* !fw3 */

Chain forwarding_lan_rule (1 references)
target     prot opt source               destination         

Chain forwarding_rule (1 references)
target     prot opt source               destination         

Chain forwarding_wan_rule (1 references)
target     prot opt source               destination         

Chain input_lan_rule (1 references)
target     prot opt source               destination         

Chain input_rule (1 references)
target     prot opt source               destination         

Chain input_wan_rule (1 references)
target     prot opt source               destination         

Chain output_lan_rule (1 references)
target     prot opt source               destination         

Chain output_rule (1 references)
target     prot opt source               destination         

Chain output_wan_rule (1 references)
target     prot opt source               destination         

Chain reject (3 references)
target     prot opt source               destination         
REJECT     tcp  --  anywhere             anywhere             /* !fw3 */ reject-with tcp-reset
REJECT     all  --  anywhere             anywhere             /* !fw3 */ reject-with icmp-port-unreachable

Chain syn_flood (1 references)
target     prot opt source               destination         
RETURN     tcp  --  anywhere             anywhere             tcp flags:FIN,SYN,RST,ACK/SYN limit: avg 25/sec burst 50 /* !fw3 */
DROP       all  --  anywhere             anywhere             /* !fw3 */

Chain zone_lan_dest_ACCEPT (4 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             /* !fw3 */

Chain zone_lan_forward (1 references)
target     prot opt source               destination         
forwarding_lan_rule  all  --  anywhere             anywhere             /* !fw3: Custom lan forwarding rule chain */
zone_wan_dest_ACCEPT  all  --  anywhere             anywhere             /* !fw3: Zone lan to wan forwarding policy */
ACCEPT     all  --  anywhere             anywhere             ctstate DNAT /* !fw3: Accept port forwards */
zone_lan_dest_ACCEPT  all  --  anywhere             anywhere             /* !fw3 */

Chain zone_lan_input (1 references)
target     prot opt source               destination         
input_lan_rule  all  --  anywhere             anywhere             /* !fw3: Custom lan input rule chain */
ACCEPT     all  --  anywhere             anywhere             ctstate DNAT /* !fw3: Accept port redirections */
zone_lan_src_ACCEPT  all  --  anywhere             anywhere             /* !fw3 */

Chain zone_lan_output (1 references)
target     prot opt source               destination         
output_lan_rule  all  --  anywhere             anywhere             /* !fw3: Custom lan output rule chain */
zone_lan_dest_ACCEPT  all  --  anywhere             anywhere             /* !fw3 */

Chain zone_lan_src_ACCEPT (1 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             ctstate NEW,UNTRACKED /* !fw3 */

Chain zone_wan_dest_ACCEPT (2 references)
target     prot opt source               destination         
DROP       all  --  anywhere             anywhere             ctstate INVALID /* !fw3: Prevent NAT leakage */
ACCEPT     all  --  anywhere             anywhere             /* !fw3 */
DROP       all  --  anywhere             anywhere             ctstate INVALID /* !fw3: Prevent NAT leakage */
ACCEPT     all  --  anywhere             anywhere             /* !fw3 */

Chain zone_wan_dest_REJECT (1 references)
target     prot opt source               destination         
reject     all  --  anywhere             anywhere             /* !fw3 */
reject     all  --  anywhere             anywhere             /* !fw3 */

Chain zone_wan_forward (2 references)
target     prot opt source               destination         
forwarding_wan_rule  all  --  anywhere             anywhere             /* !fw3: Custom wan forwarding rule chain */
zone_lan_dest_ACCEPT  esp  --  anywhere             anywhere             /* !fw3: Allow-IPSec-ESP */
zone_lan_dest_ACCEPT  udp  --  anywhere             anywhere             udp dpt:isakmp /* !fw3: Allow-ISAKMP */
ACCEPT     all  --  anywhere             anywhere             ctstate DNAT /* !fw3: Accept port forwards */
zone_wan_dest_REJECT  all  --  anywhere             anywhere             /* !fw3 */

Chain zone_wan_input (2 references)
target     prot opt source               destination         
input_wan_rule  all  --  anywhere             anywhere             /* !fw3: Custom wan input rule chain */
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootpc /* !fw3: Allow-DHCP-Renew */
ACCEPT     icmp --  anywhere             anywhere             icmp echo-request /* !fw3: Allow-Ping */
ACCEPT     igmp --  anywhere             anywhere             /* !fw3: Allow-IGMP */
ACCEPT     all  --  anywhere             anywhere             ctstate DNAT /* !fw3: Accept port redirections */
zone_wan_src_ACCEPT  all  --  anywhere             anywhere             /* !fw3 */

Chain zone_wan_output (2 references)
target     prot opt source               destination         
output_wan_rule  all  --  anywhere             anywhere             /* !fw3: Custom wan output rule chain */
zone_wan_dest_ACCEPT  all  --  anywhere             anywhere             /* !fw3 */

Chain zone_wan_src_ACCEPT (1 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             ctstate NEW,UNTRACKED /* !fw3 */
ACCEPT     all  --  anywhere             anywhere             ctstate NEW,UNTRACKED /* !fw3 */

iptables config from official redsocks github didn't work on me:

# Create new chain                                                                                                                                           
iptables -t nat -X REDSOCKS                                                                                                                                  
iptables -t nat -N REDSOCKS                                                                                                                                  
                                                                                                                                                             
# Ignore LANs and some other reserved addresses.                                                                                                             
iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN                                                                                                           
iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN                                                                                                          
iptables -t nat -A REDSOCKS -d 10.10.1.0/22 -j RETURN                                                                                                        
iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN                                                                                                         
iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN                                                                                                      
iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN                                                                                                       
iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN                                                                                                      
iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN                                                                                                         
iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN                                                                                                         
#iptables -t nat -A REDSOCKS -d 122.248.x.x/31 -j RETURN                                                                                                  
                                                                                                                                                             
iptables -t nat -A REDSOCKS -j LOG -p tcp --syn --log-level info --log-prefix "rs "                                                                          
                                                                                                                                                             
# Anything else should be redirected to port 12345                                                                                                           
#iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 12345                                                                                             
iptables -t nat -A REDSOCKS -p tcp -j DNAT --to-destination 127.0.0.1:12345                                                                                  
                                                                                                                                                             
iptables -t nat -A REDSOCKS -j LOG -p tcp --syn --log-level info --log-prefix "err "                                                                         
                                                                                                                                                             
# Any tcp connection made by `darkk' should be redirected.                                                                                                   
iptables -t nat -I OUTPUT 1 -p tcp -j REDSOCKS                                                                                                               
iptables -t nat -I PREROUTING 1 -p tcp -s 192.168.0.0/22 -j REDSOCKS

Searched and try a lot of iptables examples from official github and OpenWRT forum but unsuccessful.
I will be very appreciate for any help for a newbie on Linux.

This probably won't work as expected.

Configure redsocks to listen on every interface ( local_ip = 0.0.0.0; ) and try the following iptables rules:

iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 12345                                                                                             
# or
iptables -t nat -A REDSOCKS -p tcp -j DNAT --to-destination 192.168.0.1:12345
1 Like

Respect for Pavelgl.

Task successfully resolved. Who also has this problem in future - main problem was in port of DNS. And all iptables rules consist of only 2 rules:
iptables -t nat -A PREROUTING -i br-lan -p udp --dport 53 -j REDIRECT --to-ports 5300
iptables -t nat -A PREROUTING -i br-lan -p tcp --syn -j REDIRECT --to-ports 12345

(br-lan -> change to your interface)
port of DNS in my variant was 5300 (bot 10053)

This article was useful: https://gitlab.torproject.org/legacy/trac/-/wikis/doc/TransparentProxy

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.