Como configurar o iptables para funcionar com o tcpcrypt?

6

Plano de fundo

OS: Ubuntu 16.04 x64 em execução no VirtualBox

Sou um desenvolvedor com conhecimento mínimo em Ubuntu / Linux e fui designado para um projeto em que o objetivo é utilizar tcpcrypt ao se comunicar com determinados endpoints.

O tcpcrypt vem com um shell script que configura as entradas necessárias no iptables para rotear os pacotes para tcpcrypt para encrpt / decrypt. Após a execução deste script o iptables se parece com:

filtro

Chain INPUT (policy ACCEPT 4 packets, 552 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     tcp  --  lo     any     anywhere             anywhere             tcp dpt:65530 tos match0x22/0xff
    0     0 NFQUEUE    tcp  --  any    any    !localhost            anywhere             tcp dpt:65530 flags:FIN,SYN,RST,PSH,ACK,URG/SYN NFQUEUE num 666
    0     0 NFQUEUE    tcp  --  any    any     anywhere             anywhere             multiport sports  !ssh,261,https,nntps,614,ldaps,684,695,ftps-data,ftps,telnets:pop3s tcp flags:FIN,SYN,RST,PSH,ACK,URG/SYN,ACK NFQUEUE num 666

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 4 packets, 536 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 NFQUEUE    tcp  --  any    any     anywhere             anywhere             multiport dports  !ssh,261,https,nntps,614,ldaps,684,695,ftps-data,ftps,telnets:pop3s tos match0x04/0xff owner UID match tcpcryptd NFQUEUE num 666
    0     0 NFQUEUE    tcp  --  any    any     anywhere             anywhere             tcp spt:65530 flags:FIN,SYN,RST,PSH,ACK,URG/SYN,ACK NFQUEUE num 666

nat

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
REDIRECT   tcp  --                                            multiport dports  !ssh,261,https,nntps,614,ldaps,684,695,ftps-data,ftps,telnets:pop3s redir ports 65530

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             multiport dports  !ssh,261,https,nntps,614,ldaps,684,695,ftps-data,ftps,telnets:pop3s owner UID match tcpcryptd
REDIRECT   tcp  --  anywhere             anywhere             multiport dports  !ssh,261,https,nntps,614,ldaps,684,695,ftps-data,ftps,telnets:pop3s redir ports 65530

mangle

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
TOS        all  --  anywhere             anywhere             tos match0x04/0xff TOS and 0x00

Com essas entradas, o pacote every é colocado em uma fila onde tcpcrypt escolhe enc / dec.

UPDATE

Este é o script do iptables:

#!/bin/sh
#DAEMON_USER DIVERT_PORT ONLY_PORTS OMIT_PORTS

# determine which operation is requested (Append or Delete)
if [ "" = "start" -o -z "" ]; then
    # during startup, bail early if any of these commands fails
    set -e
    OP="-A"
elif [ "" = "stop" -o "" = "-f" ] ; then
    OP="-D"
else
    echo "Expected \"start\" or \"stop\" as first argument" >&2
    exit 1
fi

# determine which ports should be tcpcrypt-enabled
if [ -z "$ONLY_PORTS" -a -z "$OMIT_PORTS" ] ; then
    echo "Expected either OMIT_PORTS or ONLY_PORTS environment variables to be set" >&2
    exit 1
fi
if [ -n "$ONLY_PORTS" -a -n "$OMIT_PORTS" ] ; then
    echo "Expected only one of OMIT_PORTS or ONLY_PORTS environment variables to be set" >&2
    exit 1
fi
if [ -n "$OMIT_PORTS" ] ; then
    PORT_TEST=!
    PORTS="$OMIT_PORTS"
fi
if [ -n "$ONLY_PORTS" ] ; then
    PORT_TEST=
    PORTS="$ONLY_PORTS"
fi

# more necessary configuration
if [ -z "$DAEMON_USER" ] ; then
    echo "Expected DAEMON_USER environment variable to be set" >&2
    exit 1
fi
if [ -z "$DIVERT_PORT" ] ; then
    echo "Expected DIVERT_PORT environment variable to be set" >&2
    exit 1
fi

# some shorthand to make rules more concise
from_enabled_port="-m multiport $PORT_TEST --source-ports $PORTS"
to_enabled_port="-m multiport $PORT_TEST --destination-ports $PORTS"
NFQUEUE="NFQUEUE --queue-num $DIVERT_PORT"
CRYPT_PORT="65530"
REDIRECT="REDIRECT --to-port $CRYPT_PORT"
INJECT_TOS="0x22"
HANDSHAKE_TOS="0x04"

filter="$ECHO iptables -t filter $OP"

# Injection from daemon: Accept
$filter INPUT -i lo -p tcp --dport $CRYPT_PORT \
          -m tos --tos $INJECT_TOS \
  -j ACCEPT

# SYN redirected to daemon:
#   Queue for daemon to initiate proxy connection with original destination
$filter INPUT -p tcp --dport $CRYPT_PORT --tcp-flags ALL SYN \
  -j $NFQUEUE

# SYN+ACK on proxy connection:
#   Queue for daemon to complete original handshake
$filter INPUT -p tcp $from_enabled_port --tcp-flags ALL SYN,ACK \
  -j $NFQUEUE

# Handshake packet of proxy connection from daemon:
#   Queue for daemon to set tcp options via DIVERT_MODIFY
$filter OUTPUT -p tcp $to_enabled_port \
           -m tos --tos $HANDSHAKE_TOS \
           -m owner --uid-owner $DAEMON_USER \
  -j $NFQUEUE

# SYN+ACK on redirected connection:
#   Queue for daemon to delay handshake until proxy connection succeeds
$filter OUTPUT -p tcp --sport $CRYPT_PORT --tcp-flags ALL SYN,ACK \
  -j $NFQUEUE


nat="$ECHO iptables -t nat $OP"

# Inbound connection for enabled ports:
#   Redirect to daemon (at localhost:$CRYPT_PORT) for encryption
#
# (The nat module will now translate addresses in both directions,
#  for the lifetime of this connection.)
$nat PREROUTING -p tcp $to_enabled_port \
  -j $REDIRECT


# Proxy connection from daemon to enabled port: Accept
$nat OUTPUT -p tcp $to_enabled_port \
        -m owner --uid-owner $DAEMON_USER \
  -j ACCEPT

# Outbound connections to enabled ports on remote hosts:
#   Redirect to daemon (at localhost port $CRYPT_PORT) for encryption
#
# (The nat module will now translate addresses in both directions,
#  for the lifetime of this connection.)
$nat OUTPUT \! -o lo -p tcp $to_enabled_port \
  -j $REDIRECT


mangle="$ECHO iptables -t mangle $OP"

# Packets leaving the machine with bookkeeping mark: Remove mark
$mangle POSTROUTING -m tos --tos $HANDSHAKE_TOS \
  -j TOS --set-tos 0x00

Pergunta

Como devo modificar iptables com as entradas atuais (veja acima) para obter as seguintes restrições:

  1. Apenas pacotes com um determinado destino devem ser enfileirados para tcpcrypt.
  2. Todos os outros pacotes devem não ser enfileirados para o tcpcrypt e devem viajar livremente.

O que tentei

A) Eu tentei adicionar o endereço IP desejado ao destino tcp em OUTPUT chain, que se parece com:

Chain OUTPUT (policy ACCEPT 4 packets, 536 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 NFQUEUE    tcp  --  any    any     anywhere             XXX.XXX.XXX.XXX      multiport dports  !ssh,261,https,nntps,614,ldaps,684,695,ftps-data,ftps,telnets:pop3s tos match0x04/0xff owner UID match tcpcryptd NFQUEUE num 666
    0     0 NFQUEUE    tcp  --  any    any     anywhere             XXX.XXX.XXX.XXX      tcp spt:65530 flags:FIN,SYN,RST,PSH,ACK,URG/SYN,ACK NFQUEUE num 666

B) Tentei adicionar parâmetros de origem e destino às regras NAT:

target     prot opt source               destination         
REDIRECT   tcp  --  XXX.XXX.XXX.XXX       XXX.XXX.XXX.XXX       multiport dports  !ssh,261,https,nntps,614,ldaps,684,695,ftps-data,ftps,telnets:pop3s redir ports 65530

Mas ainda assim todos os pacotes, independentemente do endereço de destino, são enviados para tcpcrypt.

    
por raidensan 18.02.2017 / 09:51

1 resposta

0

Encontrou uma solução, uma que funciona com base no método B) :

Para pacotes de entrada na cadeia nat da PREROUTING table, eu estava tentando filtrar como abaixo

$nat PREROUTING -p tcp -s XXX.XXX.XXX.XXX $to_enabled_port \
    -j $REDIRECT

onde a opção -s e o valor do IP são depois de -p tcp . Então mudei para

$nat PREROUTING -s XXX.XXX.XXX.XXX -p tcp $to_enabled_port \
    -j $REDIRECT

Com essa alteração e modificação da OUTPUT chain de nat , os resultados desejados foram alcançados.

Curiosamente, a saída de iptables -t nat --line-numbers -L -nv parece exatamente a mesma para qualquer um dos comandos acima. Ainda assim, apenas o segundo produz resultados de acordo com minhas restrições.

Aqui é a versão modificada de iptables.sh , que permite especificar um ou mais IPs para restringir o tcpcrypt.

Veja FILTER_IP abaixo.

#!/bin/sh
#DAEMON_USER DIVERT_PORT ONLY_PORTS OMIT_PORTS

# determine which operation is requested (Append or Delete)
if [ "" = "start" -o -z "" ]; then
    # during startup, bail early if any of these commands fails
    set -e
    OP="-A"
elif [ "" = "stop" -o "" = "-f" ] ; then
    OP="-D"
else
    echo "Expected \"start\" or \"stop\" as first argument" >&2
    exit 1
fi

# determine which ports should be tcpcrypt-enabled
if [ -z "$ONLY_PORTS" -a -z "$OMIT_PORTS" ] ; then
    echo "Expected either OMIT_PORTS or ONLY_PORTS environment variables to be set" >&2
    exit 1
fi
if [ -n "$ONLY_PORTS" -a -n "$OMIT_PORTS" ] ; then
    echo "Expected only one of OMIT_PORTS or ONLY_PORTS environment variables to be set" >&2
    exit 1
fi
if [ -n "$OMIT_PORTS" ] ; then
    PORT_TEST=!
    PORTS="$OMIT_PORTS"
fi
if [ -n "$ONLY_PORTS" ] ; then
    PORT_TEST=
    PORTS="$ONLY_PORTS"
fi

# more necessary configuration
if [ -z "$DAEMON_USER" ] ; then
    echo "Expected DAEMON_USER environment variable to be set" >&2
    exit 1
fi
if [ -z "$DIVERT_PORT" ] ; then
    echo "Expected DIVERT_PORT environment variable to be set" >&2
    exit 1
fi

# some shorthand to make rules more concise
from_enabled_port="-m multiport $PORT_TEST --source-ports $PORTS"
to_enabled_port="-m multiport $PORT_TEST --destination-ports $PORTS"
NFQUEUE="NFQUEUE --queue-num $DIVERT_PORT"
CRYPT_PORT="65530"
REDIRECT="REDIRECT --to-port $CRYPT_PORT"
INJECT_TOS="0x22"
HANDSHAKE_TOS="0x04"

# You can specify multiple IPs, or a IP range accrding to required format
# For example, restricting tcpcrypt to 192.192.192.192 and 127.127.127.127
# FILTER_IP="192.192.192.192,127.127.127.127"
# See iptables manpage for more info
FILTER_IP="XXX.XXX.XXX.XXX"

filter="$ECHO iptables -t filter $OP"

# Injection from daemon: Accept
$filter INPUT -i lo -p tcp --dport $CRYPT_PORT \
          -m tos --tos $INJECT_TOS \
  -j ACCEPT

# SYN redirected to daemon:
#   Queue for daemon to initiate proxy connection with original destination
$filter INPUT -p tcp --dport $CRYPT_PORT --tcp-flags ALL SYN \
  -j $NFQUEUE

# SYN+ACK on proxy connection:
#   Queue for daemon to complete original handshake
$filter INPUT -p tcp $from_enabled_port --tcp-flags ALL SYN,ACK \
  -j $NFQUEUE

# Handshake packet of proxy connection from daemon:
#   Queue for daemon to set tcp options via DIVERT_MODIFY
$filter OUTPUT -p tcp $to_enabled_port \
           -m tos --tos $HANDSHAKE_TOS \
           -m owner --uid-owner $DAEMON_USER \
  -j $NFQUEUE

# SYN+ACK on redirected connection:
#   Queue for daemon to delay handshake until proxy connection succeeds
$filter OUTPUT -p tcp --sport $CRYPT_PORT --tcp-flags ALL SYN,ACK \
  -j $NFQUEUE


nat="$ECHO iptables -t nat $OP"

# Inbound connection for enabled ports:
#   Redirect to daemon (at localhost:$CRYPT_PORT) for encryption
#
# (The nat module will now translate addresses in both directions,
#  for the lifetime of this connection.)
$nat PREROUTING -s $FILTER_IP -p tcp $to_enabled_port \
  -j $REDIRECT


# Proxy connection from daemon to enabled port: Accept
$nat OUTPUT -p tcp $to_enabled_port \
        -m owner --uid-owner $DAEMON_USER \
  -j ACCEPT

# Outbound connections to enabled ports on remote hosts:
#   Redirect to daemon (at localhost port $CRYPT_PORT) for encryption
#
# (The nat module will now translate addresses in both directions,
#  for the lifetime of this connection.)
$nat OUTPUT -d $FILTER_IP \! -o lo -p tcp $to_enabled_port \
  -j $REDIRECT


mangle="$ECHO iptables -t mangle $OP"

# Packets leaving the machine with bookkeeping mark: Remove mark
$mangle POSTROUTING -m tos --tos $HANDSHAKE_TOS \
  -j TOS --set-tos 0x00
    
por raidensan 08.03.2017 / 15:16