Home > Todos > Knocking on servers doors

Knocking on servers doors

September 28th, 2006

Artigo retirado do site Dicas-L da unicamp

Colaboração: Fabio Maximo

Essa dica é muito legal para quem tem servidores remotos e preocupa-se com segurança no SSH. Voce pode fechar todo fluxo na porta 22 e so voce abre quando precisa.

O knock é um daemon que atenta para o que ocorre em certas portas no servidor. Quando alguem “bate” nas portas certas e na ordem certa, um determinado comando é executado.

Importante notar que as portas não estão abertas. O Deamon checa isso a nivel de kernel, portanto um port scan não detecta diferença nenhuma entre uma porta que sera knockada e uma outra qualquer.

Uma vez que o knock tenha sido realizado corretamente, o comando padrao é liberar acesso ssh para o IP que knockou o servidor.

Poder ser adicionados outros comandos e combinacoes de porta.
Obter o programa

$ wget “http://www.zeroflux.org/knock/files/knock-0.5.tar.gz”

Descompactar

$ tar -xvzf knock-0.5.tar.gz

instalar

$ cd knock-0.5
$ ./configure
$ make
$ make install

Configurar

$ vi /etc/knockd.conf

Ainda configuracao

$ cd /etc/init.d/

O Seguinte script para inicialização foi escrito por Marcio Andreeta - utilizar mas manter a nota de autoria.

$ vi knockd
============================INICIO DO SCRIPT==========================
#!/bin/sh
# Script written by Marcio Luis Barsi Andreeta 04/07/2005
#
# This script starts/stops knockd
# normal UNIX system operation
#
# This file is installed as:
# /etc/rc.d/init.d/knockd
EXECUTABLE=/usr/local/sbin/knockd
# error “description”
error () {
/bin/echo $0: $* 2>&1
exit 1
}
# find the named process(es)
findproc() {
pid=`/bin/ps auxww |
/bin/grep root |
/bin/grep -e “$1\$” |
/bin/grep -v grep |
/bin/awk ‘{print $2}’`
/bin/echo $pid
}
# for testing purposes only
testproc() {
pid=`/bin/ps auxww |
/bin/grep root |
/bin/grep -e “$1\$” |
/bin/grep -v grep |
/bin/awk ‘{print $2}’`
/bin/echo $pid
}
# kill the named process(es)
killproc() {
pid=`findproc “$1″`
[ "$pid" != "" ] && kill -TERM $pid
}
# kill the named process(es)
forcedkillproc() {
pid=`findproc “$1″`
[ "$pid" != "" ] && kill -9 $pid
}
# Start/stop knockd
case “$1″ in
’start’)
/bin/echo -n “Starting knockd…”
# Check if the server is already running.
if [ -n "`findproc "$EXECUTABLE"`" ]; then
/bin/echo “knockd deamon is running.”
exit 0
fi
$EXECUTABLE &
/bin/echo “done”
;;
’stop’)
/bin/echo -n “Stopping knockd…”
echo `findproc $EXECUTABLE`
if [ -z "`findproc "$EXECUTABLE"`" ]; then
/bin/echo “not running.”
exit 0
fi
killproc “$EXECUTABLE”
/bin/echo “done”
;;
*)
/bin/echo “Usage: $0 { start | stop }”
# for testing testing functions
echo “`testproc “$EXECUTABLE”`”
;;
esac
============================FIM DO SCRIPT==========================

Startar o serviço no RedHat / Fedora

“` $ service knockd start

Em outras distros

“` $ /etc/init.d/knockd start

Para que o mesmo starte no boot da maquina vamos adicionar um link no Run
Level 3 - Isso pode mudar de acordo com a distro.

$ cd /etc/rc3.d/ $ ln -s /etc/init.d/knockd S99knockd

Abaixo esta o conteudo do /etc/knockd.conf - Repare que o mesmo é muito
simples, dando opcao de configuracao do arquivo de log e os comandos abaixo
com as sequiencias de portas na opcao sequence.

==Conteudo do /etc/knockd.conf==

[options] logfile = /var/log/knockd.log

[openSSH] sequence = 7000,8000,9000 seq_timeout = 5 command = /usr/sbin/iptables -A INPUT -s %IP% -p tcp –dport 22 -j ACCEPT tcpflags = syn

[closeSSH] sequence = 9000,8000,7000 seq_timeout = 5 command = /usr/sbin/iptables -D INPUT -s %IP% -p tcp –dport 22 -j ACCEPT tcpflags = syn

Interessante que se voce quiser montar uma sequencia para outro comando voce
pode. Mas se voce tem shell, pra que mais?

Em todo caso poderiamos acrescentar:

[wall] sequence = 7001,8001,9001 seq_timeout = 5 command = /usr/bin/wall “TESTE OK” tcpflags = syn

Bem, mas como vamos bater nessa porta??

Simples:

De sua estação use o comando knock seguindo a sintaxe / exemplo abaixo:

knock [ip] [portas] $ knock 192.168.0.90 7001 8001 9001

Para os que usam estação Windows, ha um “batedor” para windows,
e a sintaxe é a mesma. Da para baixar do seguinte link:
http://www.zeroflux.org/cgi-bin/cvstrac.cgi/knock/wiki

Todos

  1. No comments yet.
  1. December 4th, 2008 at 11:41 | #1
  2. December 7th, 2008 at 00:35 | #2
  3. December 7th, 2008 at 01:29 | #3
  4. December 7th, 2008 at 09:19 | #4
  5. December 7th, 2008 at 17:20 | #5
  6. December 8th, 2008 at 01:42 | #6
  7. December 8th, 2008 at 09:31 | #7
  8. December 8th, 2008 at 17:34 | #8
  9. December 9th, 2008 at 03:25 | #9
  10. December 9th, 2008 at 11:26 | #10
  11. December 10th, 2008 at 06:33 | #11
  12. December 10th, 2008 at 17:14 | #12
  13. December 11th, 2008 at 01:28 | #13
  14. December 11th, 2008 at 10:41 | #14
  15. December 11th, 2008 at 17:44 | #15
  16. December 12th, 2008 at 05:02 | #16
  17. December 12th, 2008 at 23:05 | #17
  18. December 14th, 2008 at 09:47 | #18
  19. December 15th, 2008 at 02:21 | #19
  20. December 15th, 2008 at 21:59 | #20
  21. December 15th, 2008 at 23:10 | #21
  22. December 16th, 2008 at 06:43 | #22
  23. December 17th, 2008 at 07:42 | #23
  24. December 19th, 2008 at 19:44 | #24
  25. December 20th, 2008 at 05:33 | #25
  26. December 27th, 2008 at 08:20 | #26
  27. December 27th, 2008 at 20:32 | #27
  28. December 31st, 2008 at 06:01 | #28
  29. January 2nd, 2009 at 13:45 | #29
  30. January 4th, 2009 at 01:52 | #30
  31. January 5th, 2009 at 20:24 | #31
  32. January 6th, 2009 at 12:19 | #32
  33. January 6th, 2009 at 21:56 | #33
  34. January 7th, 2009 at 07:01 | #34
  35. January 7th, 2009 at 11:52 | #35
  36. January 7th, 2009 at 23:14 | #36
  37. January 8th, 2009 at 08:02 | #37
  38. January 9th, 2009 at 09:47 | #38
  39. January 9th, 2009 at 19:24 | #39