Apr 172011
 

knockdOggi vi mostrerò come utilizzare knockd per migliorare la sicurezza del vostro server Linux, l’uso più comune che ho visto finora è: “Mi piacerebbe connettermi sulla porta 22 (ssh), ma non voglio lasciare la porta aperta per tutti .. e ho un IP dinamico “. In questi casi è possibile chiudere le porte ed usare knockd per bussare alle porte della nostra Linux box e farci entrare.

knockd è un server per eseguire dei comandi al bussare sulle porte. Ascolta tutto il traffico su una interfaccia ethernet (o PPP), alla ricerca di speciali sequenze di “bussate”. Un client esegue queste bussate su una porta inviando un pacchetto TCP (o UDP) a una porta sul server. Questa porta non deve essere aperto – il knockd ascolta a livello link-layer, vede quindi tutto il traffico anche se è destinato a una porta chiusa. Quando il server rileva una sequenza specifica di Porte contattate, viene eseguito un comando definito nel suo file di configurazione. Questo può essere usato per aprire dei buchi in un firewall per l’accesso rapido.

Installazione

Knockd è disponibile nei repository delle principali distribuzioni, lo lo sto usando su Ubuntu/Debian in cui il pacchetto è disponibile.

Configurazione

knockd legge tutti i battiti/eventi da un file di configurazione. Ogni battito/evento inizia con un marcatore titolo, nella forma [nome] , dove ogni nome è il nome dell’evento che apparirà nel registro. Un marcatore speciale, [options], viene utilizzato per definire le opzioni globali.

Esempio:

[options]
        logfile = /var/log/knockd.log
 
[openSSH]
        sequence    = 700,800,900
        seq_timeout = 5
        command     = /sbin/iptables -I INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
        tcpflags    = syn
 
[closeSSH]
        sequence    = 900,800,700
        seq_timeout = 5
        command     = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
        tcpflags    = syn

Questo esempio utilizza due colpi. Il primo consentirà a colui che bussa di accedere alla porta 22 (SSH), e il secondo chiude la porta quando chi bussa ha fatto tutto. Come potete vedere, questo potrebbe essere utile se si ha un firewall molto restrittivo (DENY policy) e si desidera accedere con discrezione.

Esempio numero 2:

 [options]
        logfile = /var/log/knockd.log
 
  [opencloseSSH]
        sequence      = 2222:udp,3333:tcp,4444:udp
        seq_timeout   = 15
        tcpflags      = syn,ack
        start_command = /usr/sbin/iptables -A INPUT -s %IP% -p tcp --syn --dport 22 -j ACCEPT
        cmd_timeout   = 10
        stop_command  = /usr/sbin/iptables -D INPUT -s %IP% -p tcp --syn --dport 22 -j ACCEPT

Questo esempio utilizza una sola sequenza di colpi per controllare l’accesso alla porta 22 (SSH). Dopo aver ricevuto una sequenza corretta, il demone eseguirà lo start_command, attenderà il tempo specificato in cmd_timeout, quindi eseguirà lo stop_command. Questo è utile per chiudere le porte automaticamente.

Per poter utilizzare questo esempio, è importante che abbiate una regola sul firewall relativa a ESTABLISHED,RELATED, come ad esempio:

iptables -A INPUT -m –state ESTABLISHED,RELATED -j ACCEPT

Altrimenti potreste perdere la vostra connessione ssh dopo quei 10 secondi (anche se già collegati).
E naturalmente la policy di DROP:

iptables -P INPUT DROP

Come bussare

Ok, ora avete impostato il server, ma come bussare alle sue porte?
Il modo più semplice è di fare uso del comando knock , che è disponibile nel pacchetto knockd come client.
L’uso base di knock è:

knock yourserver port:protocol port:protocol  port:protocol

Se non mettete :protocollo il default è di usare tutte le porte TCP, questo può essere modificato usando l’opzine -u (tutto UDP)

Esempi:

Bussa su 3 porte TCP:

knock 127.0.0.1 7000 8000 9000

Oppure usare questa sintassi per avere un mix di porte TCP ed UDP

knock 127.0.0.1 123:tcp 456:udp 789:tcp

Client per Windows e Mac sono disponibili all’indirizzo: http://www.zeroflux.org/projects/knock

Riferimenti:

http://www.zeroflux.org/projects/knock

http://www.portknocking.org/

[amazon_enhanced asin=”0596005695″ price=”All” background_color=”FFFFFF” link_color=”FD8D00″ text_color=”FD5100″ /]

Popular Posts:

Flattr this!

  14 Responses to “Knockd, per garantire la sicurezza delle porte su linux.”

  1. Once you configure knockd, can you briefly discuss how to get in to your system. In other words, how do you knock your server with the appropriate sequence? Do you need an external file or shell script to send the knock sequence?

  2. Interesting method! So, we define the knock sequence as if it’s a password?
    Thanks for sharing.

  3. Port knocking is bad idea; a very bad idea.

    Port knocking is, in the end, a password. A sniffable one that is subject to man-in-the-middle attacks so you can’t even use one-time-passwords and be secure.

    Public/private key pairs and/or one-time-passwords (opie, skey and the like) are the real solutions, along with dynamic monitoring to prevent DOS CPU resource exhaustion attacks. (OpenBSD’s PF incorporates a nice solution, as does the iptables with fail2ban/denyhosts/etc. Even swatch can work wonders.)

  4. Sigh… Reading comprehension FAIL!
    Sorry.
    And Thanks.

  5. Hi,
    Interested method indeed, I have forgotten about it!

    But I have wrote about it before. Thanks pal.

    knockd

  6. You add an extra layer of complexity and an extra layer that can fail for less than 3 bits of entropy?
    Can I ask what threat model you have such that port knocking, with its limited usefulness, can help you?

  7. Please checkout my android knock client on the google play store, its completely free https://play.google.com/store/apps/details?id=com.droidknocker

Leave a Reply to Mark Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*