个人工具

“UbuntuHelp:PortKnocking”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
(新页面: {{From|https://help.ubuntu.com/community/PortKnocking}} {{Languages|UbuntuHelp:PortKnocking}} == What is Port Knocking ? == Port knocking is a simple and great method to grant remote acc...)
 
 
(未显示2个用户的7个中间版本)
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/PortKnocking}}
 
{{From|https://help.ubuntu.com/community/PortKnocking}}
 
{{Languages|UbuntuHelp:PortKnocking}}
 
{{Languages|UbuntuHelp:PortKnocking}}
 +
#title Port Knocking
 
== What is Port Knocking ? ==
 
== What is Port Knocking ? ==
 
+
Port knocking is a simple method to grant remote access without leaving a port constantly open. This preserves your server from port scanning and script kiddie attacks.
Port knocking is a simple and great method to grant remote access without leaving a port constantly open. This preserves your server from port scanning and script kiddie attacks.
+
To utilize port knocking, the server must have a firewall and run the knock-daemon. As the name conveys, the daemon is listening for a specific sequence of TCP or UDP "knocks". If the sequence is given correctly, then a command is executed; typically the source IP address is given access through the firewall to the port of an application (such as SSH). This method is reasonably secure, as port knocking is located at a very low level in the TCP/IP stack and does not require any opened ports. The knock-daemon is also invisible to potential attackers.
 
+
On the client side, the only thing needed is to play the sequence with the client of your choice (such as '''knock''').
To utilize port knocking, the server must have a firewall and run the knock-daemon. As the name lets us imaginate, the daemon is listening for a specific sequence of TCP or UDP "knocks". If the sequence is played correctly then a command is executed, typically, the port of the application is opened for the source IP address through the firewall. This method is perfectly secure, as port knocking is located at a very low level in the TCP/IP stack and does not require any opened ports. The knock-daemon is also invisible to attackers.
+
 
+
On the client side, the only thing you have to do is to play the sequence. You can do that whith the client that you prefer, a client program also exists called '''knock'''.
+
 
+
 
== Server Setup ==
 
== Server Setup ==
 
+
The setup of the server is straightforward. First, ensure that your server has a running firewall. Then, install the <code><nowiki>knockd</nowiki></code> package (see [[UbuntuHelp:InstallingSoftware|InstallingSoftware]]).
The setup of the server is quite easy. First, you have to ensure that your server has a running firewall. Then, install the following packages: <code><nowiki>knockd</nowiki></code> (see InstallingSoftware).
+
After the package is installed, edit its configuration file. Three approaches are presented below: the first is intended for connections with no keep-alive (such as HTTP), while the other two are  intended for permanent connections (such as SSH and IRC).
 
+
Then, edit the configuration file. We will present two different approches. One that is more adapted to connections with no keep-alive (http for example !), another mode adapted to permenant connections (SSH, IRC...).
+
 
+
As you will notice, the syntax of the configuration file is quite easy to understand.
+
 
+
 
== Example 1 ==
 
== Example 1 ==
 
Here is the default configuration file for the knock daemon (<code><nowiki>/etc/knockd.conf</nowiki></code>) :
 
 
 
<pre><nowiki>
 
<pre><nowiki>
 
[options]
 
[options]
logfile = /var/log/knockd.log
+
        logfile = /var/log/knockd.log
  
[openSSH]
+
[openHTTP]
sequence    = 7000,8000,9000
+
        sequence    = 7000,8000,9000
seq_timeout = 5
+
        seq_timeout = 5
command    = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 80 -j ACCEPT
+
        command    = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 80 -j ACCEPT
tcpflags    = syn
+
        tcpflags    = syn
  
[closeSSH]
+
[closeHTTP]
sequence    = 9000,8000,7000
+
        sequence    = 9000,8000,7000
seq_timeout = 5
+
        seq_timeout = 5
command    = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 80 -j ACCEPT
+
        command    = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 80 -j ACCEPT
tcpflags    = syn
+
        tcpflags    = syn
 
</nowiki></pre>
 
</nowiki></pre>
 
 
 
Here we have defined two sequences :
 
Here we have defined two sequences :
 
+
* '''openHTTP''' opens the HTTP port if the 7000, 8000 and 9000 port sequence is "knocked"
** '''openSSH''' that opens the http port if the 7000,8000 and 9000 ports are knocked
+
* '''closeHTTP''' closes the HTTP port if the 9000, 8000 and 7000 port sequence is "knocked"
** '''closeSSH''' that close the http port if the 9000,8000 and 7000 ports are knocked
+
 
+
 
== Example 2 ==
 
== Example 2 ==
 
+
The second example is a bit different from the original:
The second example file is a bit different from the orginal :
+
 
+
 
<pre><nowiki>
 
<pre><nowiki>
options]
+
[options]
logfile = /var/log/knockd.log
+
      logfile = /var/log/knockd.log
  
 
[SSH]
 
[SSH]
sequence    = 7000,8000,9000
+
      sequence    = 7000,8000,9000
seq_timeout = 5
+
      seq_timeout = 5
command    = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
+
      command    = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
tcpflags    = syn
+
      tcpflags    = syn
cmd_timeout  = 10
+
      cmd_timeout  = 10
stop_command  = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
+
      stop_command  = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
 
</nowiki></pre>
 
</nowiki></pre>
 
+
== Example 3 ==
 
+
This example uses UFW commands instead of iptables:
In the default configuration file, you have two sequences :
+
** one for opening the port
+
** a second one for closing the port
+
 
+
We advocate for opening the port fort a short time range (in the example 10s). For this example to be functionnal, you have to have a statefull firewall running on your server (which means you have to accept connections with <code><nowiki>-m state --state RELATED,ESTABLISHED</nowiki></code>).
+
 
+
Let's explain this configuration file. If a user "knocks" ports 7000, 8000 and 9000 (in '''that''' order), the command will be played (opening port 22). Ten secondes later, the <code><nowiki>stop_command</nowiki></code> will be executed, closing the port.
+
 
+
Do not forget to change the sequence (this is the example provided by the default installation), and... to provide the sequence to your users.
+
 
+
Change the default configuration <code><nowiki>/etc/default/knockd</nowiki></code> in order that the knock-daemon is lanched :
+
 
+
 
<pre><nowiki>
 
<pre><nowiki>
#
+
[options]
# knockd's default file, for generic sys config
+
      logfile = /var/log/knockd.log
#
+
  
# control if we start knockd at init or not
+
[SSH]
# 1 = start
+
      sequence    = 7000,8000,9000
# anything else = don't start
+
      seq_timeout = 5
START_KNOCKD=1
+
      start_command = ufw allow from %IP% to any port 22
 
+
      tcpflags    = syn
# command line options
+
      cmd_timeout  = 10
#KNOCKD_OPTS="-i eth0"
+
      stop_command  = ufw delete allow from %IP% to any port 22
 
</nowiki></pre>
 
</nowiki></pre>
 +
It's recommended to open the port for a short time (ten seconds, in the examples above). For this to be functional, you must have a state-full firewall running on your server (if using iptables, there must be a rule to accept connections with <code><nowiki>-m conntrack --ctstate RELATED,ESTABLISHED</nowiki></code>).
 +
Let's explain this configuration file. If a user "knocks" on ports 7000, 8000 and 9000 (in order), the command will be played (opening port 22). Ten seconds later, the <code><nowiki>stop_command</nowiki></code> will be executed, closing the port to new connections.
 +
Do not forget to change the sequence (this is the example provided by the default installation) and, of course provide the sequence to your users.
 +
You must also change the default configuration file <code><nowiki>/etc/default/knockd</nowiki></code> for the knockd daemon to start. Uncomment the START_KNOCKD=1 line to enable the daemon.
 +
If you have multiple network adapters or experience issues with knockd not starting automatically during system startup, you can manually specify the network interface to listen on by uncommenting and amending the second line KNOCKD_OPTS as well:
 +
<pre><nowiki>
 +
#
 +
# knockd's default file, for generic sys config
 +
#
  
Now, just launch the daemon :
+
# control if we start knockd at init or not
 +
# 1 = start
 +
# anything else = don't start
 +
START_KNOCKD=1
  
<pre><nowiki>
+
# command line options
sudo /etc/init.d/knockd start
+
KNOCKD_OPTS="-i eth0"
 
</nowiki></pre>
 
</nowiki></pre>
 
+
That's it!
That's it !
+
 
+
 
== Client Side ==
 
== Client Side ==
 
+
On the client side, you can "knock" with the client of your choice: <code><nowiki>telnet</nowiki></code>, <code><nowiki>nc</nowiki></code> or even the software used to connect to the server (for example <code><nowiki>ssh</nowiki></code>). If you do not use a client designed for portknocking, you must do the knock sequence manually.
On the client side, you can "knock" with whatever client you want : <code><nowiki>telnet</nowiki></code>, <code><nowiki>nc</nowiki></code> or even the software used to connect to the server (for example <code><nowiki>ssh</nowiki></code>).
+
An easier method is to use the knock client. Install the <code><nowiki>knockd</nowiki></code> package (see [[UbuntuHelp:InstallingSoftware|InstallingSoftware]]).
 
+
For knocking, launch the command:
But, for more simplicity, you also have the knock client. Install the following packages: <code><nowiki>knockd</nowiki></code> (see InstallingSoftware).
+
 
+
For knocking, just lanch the command !
+
 
+
 
<pre><nowiki>
 
<pre><nowiki>
 
knock ''hostname'' ''port1'' ''port2'' ''port3''
 
knock ''hostname'' ''port1'' ''port2'' ''port3''
 
</nowiki></pre>
 
</nowiki></pre>
 +
Adding the -v option specifies that the request is verbose giving some feedback to the command, so if your target server IP was 192.168.1.250, the command and feedback would be:
 +
<pre><nowiki>
 +
knock -v 192.168.1.250 7000,8000,9000
  
Then connect to your application.
+
hitting tcp 192.168.1.250:7000
 
+
hitting tcp 192.168.1.250:8000
== Conclusion ==
+
hitting tcp 192.168.1.250:9000
 
+
</nowiki></pre>
You have done it ! Easy to setup, but very efficient, isn't it ?
+
If everything is working correctly, this should prompt the target server to open the hole in the firewall to allow access so you can then connect to your application.
 
+
 
== Notice ==
 
== Notice ==
Simple portknocking daemons as knockd are vulnerable because a sniffer may recover which ports where knocked.  
+
Simple portknocking daemons (such as knockd) are vulnerable because a sniffer may recover the port sequence that was used.
 
A better solution is Cryptknock (http://cryptknock.sourceforge.net/)
 
A better solution is Cryptknock (http://cryptknock.sourceforge.net/)
 
Cryptknock's description says:
 
Cryptknock's description says:
"Cryptknock is an encrypted port knocking tool. Unlike other port knockers which use TCP ports or other protocol information to signal the knock, an encrypted string is used as the knock. This makes it extremely difficult for an evesdropper to recover your knock (unlike other port knockers where tcpdump can be used to discover a port knock)."
+
"Cryptknock is an encrypted port knocking tool. Unlike other port knockers which use TCP ports or other protocol information to signal the knock, an encrypted string is used as the knock. This makes it extremely difficult for an eavesdropper to recover your knock (unlike other port knockers where tcpdump can be used to discover a port knock)."
 
+
 
== Links ==
 
== Links ==
 
 
[http://portknocking.org The orginal project] Detailed explanations on how it works and a reference implementation.
 
[http://portknocking.org The orginal project] Detailed explanations on how it works and a reference implementation.
 
 
[http://www.zeroflux.org/knock The port knocking daemon] The Ubuntu package is build from this release. A Win32 package is also available. You will also find other examples and some documentation.
 
[http://www.zeroflux.org/knock The port knocking daemon] The Ubuntu package is build from this release. A Win32 package is also available. You will also find other examples and some documentation.
 +
== See Also ==
 +
* [[UbuntuHelp:SinglePacketAuthorization|SinglePacketAuthorization]]
 
----
 
----
[[category:CategoryNetworking]] [[category:CategoryDocumentation]]
+
[[category:CategorySecurity]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2009年11月17日 (二) 20:22的最新版本

  1. title Port Knocking

What is Port Knocking ?

Port knocking is a simple method to grant remote access without leaving a port constantly open. This preserves your server from port scanning and script kiddie attacks. To utilize port knocking, the server must have a firewall and run the knock-daemon. As the name conveys, the daemon is listening for a specific sequence of TCP or UDP "knocks". If the sequence is given correctly, then a command is executed; typically the source IP address is given access through the firewall to the port of an application (such as SSH). This method is reasonably secure, as port knocking is located at a very low level in the TCP/IP stack and does not require any opened ports. The knock-daemon is also invisible to potential attackers. On the client side, the only thing needed is to play the sequence with the client of your choice (such as knock).

Server Setup

The setup of the server is straightforward. First, ensure that your server has a running firewall. Then, install the knockd package (see InstallingSoftware). After the package is installed, edit its configuration file. Three approaches are presented below: the first is intended for connections with no keep-alive (such as HTTP), while the other two are intended for permanent connections (such as SSH and IRC).

Example 1

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

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

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

Here we have defined two sequences :

  • openHTTP opens the HTTP port if the 7000, 8000 and 9000 port sequence is "knocked"
  • closeHTTP closes the HTTP port if the 9000, 8000 and 7000 port sequence is "knocked"

Example 2

The second example is a bit different from the original:

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

[SSH]
      sequence    = 7000,8000,9000
      seq_timeout = 5
      command     = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
      tcpflags    = syn
      cmd_timeout   = 10
      stop_command  = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT

Example 3

This example uses UFW commands instead of iptables:

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

[SSH]
      sequence    = 7000,8000,9000
      seq_timeout = 5
      start_command = ufw allow from %IP% to any port 22
      tcpflags    = syn
      cmd_timeout   = 10
      stop_command  = ufw delete allow from %IP% to any port 22

It's recommended to open the port for a short time (ten seconds, in the examples above). For this to be functional, you must have a state-full firewall running on your server (if using iptables, there must be a rule to accept connections with -m conntrack --ctstate RELATED,ESTABLISHED). Let's explain this configuration file. If a user "knocks" on ports 7000, 8000 and 9000 (in order), the command will be played (opening port 22). Ten seconds later, the stop_command will be executed, closing the port to new connections. Do not forget to change the sequence (this is the example provided by the default installation) and, of course provide the sequence to your users. You must also change the default configuration file /etc/default/knockd for the knockd daemon to start. Uncomment the START_KNOCKD=1 line to enable the daemon. If you have multiple network adapters or experience issues with knockd not starting automatically during system startup, you can manually specify the network interface to listen on by uncommenting and amending the second line KNOCKD_OPTS as well:

# 
# knockd's default file, for generic sys config 
# 

# control if we start knockd at init or not 
# 1 = start 
# anything else = don't start 
START_KNOCKD=1 

# command line options 
KNOCKD_OPTS="-i eth0" 

That's it!

Client Side

On the client side, you can "knock" with the client of your choice: telnet, nc or even the software used to connect to the server (for example ssh). If you do not use a client designed for portknocking, you must do the knock sequence manually. An easier method is to use the knock client. Install the knockd package (see InstallingSoftware). For knocking, launch the command:

knock ''hostname'' ''port1'' ''port2'' ''port3''

Adding the -v option specifies that the request is verbose giving some feedback to the command, so if your target server IP was 192.168.1.250, the command and feedback would be:

knock -v 192.168.1.250 7000,8000,9000

hitting tcp 192.168.1.250:7000
hitting tcp 192.168.1.250:8000
hitting tcp 192.168.1.250:9000

If everything is working correctly, this should prompt the target server to open the hole in the firewall to allow access so you can then connect to your application.

Notice

Simple portknocking daemons (such as knockd) are vulnerable because a sniffer may recover the port sequence that was used. A better solution is Cryptknock (http://cryptknock.sourceforge.net/) Cryptknock's description says: "Cryptknock is an encrypted port knocking tool. Unlike other port knockers which use TCP ports or other protocol information to signal the knock, an encrypted string is used as the knock. This makes it extremely difficult for an eavesdropper to recover your knock (unlike other port knockers where tcpdump can be used to discover a port knock)."

Links

The orginal project Detailed explanations on how it works and a reference implementation. The port knocking daemon The Ubuntu package is build from this release. A Win32 package is also available. You will also find other examples and some documentation.

See Also