{"id":30,"date":"2010-05-12T23:58:40","date_gmt":"2010-05-12T22:58:40","guid":{"rendered":"http:\/\/paguilar.org\/?p=30"},"modified":"2010-05-12T23:58:40","modified_gmt":"2010-05-12T22:58:40","slug":"compiling-and-running-dropbear-a-thin-secure-shell-server","status":"publish","type":"post","link":"https:\/\/paguilar.org\/?p=30","title":{"rendered":"Compiling and running Dropbear: a thin Secure Shell server"},"content":{"rendered":"<p>Dropbear is a thin Secure Shell 2 (SSH 2) server and client. It is a good alternative to OpenSSH when you have storage and memory restrictions, specially in embedded systems.<\/p>\n<p>Cross-compiling and running Dropbear in an embedded system is not a difficult task, but the documentation is not very clear for everybody. You can start by reading the README and INSTALL files contained in the package and then, if you still have questions, you can keep reading this article in which I&#8217;ll try to give a short and clear explanation:<\/p>\n<p>As you may expect from a package of this kind that was written with embedded systems in mind, it supports uClibc. The configuration, compilation and installation are the same as for libc, but it can have some tweaks that are described in the INSTALL file.<\/p>\n<p><strong>Download<\/strong><br \/>\n<a href=\"http:\/\/matt.ucc.asn.au\/dropbear\/dropbear-0.52.tar.bz2\">http:\/\/matt.ucc.asn.au\/dropbear\/dropbear-0.52.tar.bz2<\/a><\/p>\n<p><strong>Environment configuration<\/strong><br \/>\nAs normal, set the environment variables that make our life easier. Change them according to your needs.<br \/>\nI normally have an SH4 based processor in my desk at home.<\/p>\n<pre lang=\"bash\">\n$ export PREFIX=\/opt\/STM\/STLinux-2.3\/devkit\/sh4\/target\/usr\/local\n$ export HOST=sh4-linux\n$ export BUILD=i386-linux\n$ export COMPILER=sh4-linux-gcc\n<\/pre>\n<p><strong>Configuration<\/strong><\/p>\n<p>Dropbear doesn&#8217;t have a configure script since it&#8217;s not based on autotools. It contains an options.h file in which you can select many parameters that vary from the keys installation paths to the supported encryption algorithms and X11 forwarding among others.<br \/>\nI strongly suggest you to have a look at this file. This will give you a good idea of what Dropbear is capable of.<br \/>\nIn any case, I list here what I consider some of the most relevant options for embedded systems:<\/p>\n<ul>\n<li>Small footprint &#8211; A speed performance price must be paid when using some ciphers<\/li>\n<li>Six supported ciphers &#8211; 3DES, AES128, AES256, Blowfish, Twofish128 and Twofish256<\/li>\n<li>Adjustable size of receive window size and maximum size of transmission and reception packets<\/li>\n<\/ul>\n<p>Some other <em>default<\/em> functionality in OpenSSH are supported in Dropbear too, but are not very relevant for embedded systems such as X11 forwarding.<\/p>\n<p>Once you have decided the options that you need, the configuration is straight-forward as many other packages:<\/p>\n<pre lang=\"bash\">\n$ CC=$COMPILER .\/configure --build=$BUILD --host=$HOST --prefix=$PREFIX\n<\/pre>\n<p><strong>Compilation<\/strong><\/p>\n<p>The compilation is the step that allows you to choose the services that you want in Dropbear: the generated binary is a multi-purpose executable that contains the functionality that you need. Here you can select if you want an executable that works as a server (<em>dropbear<\/em>), that generates keys (<em>dropbearkey<\/em>), that allows Secure Copy (<em>scp<\/em>), Secure FTP (<em>sftp<\/em>), and keys conversion from OpenSSH to Dropbear format (<em>dropbearconvert<\/em> and <em>dbclient<\/em>).<br \/>\nIf you want at least two of this services you need to give to  <em>make<\/em> the argument <em>PROGRAMS=&#8221;the programs that you want&#8221;<\/em> and the <em>MULTI=1<\/em> argument.<\/p>\n<p>In this same step you can choose if you want a dynamically or statically &#8211; linked executable. For embedded systems is common to use a statically linked executable by adding the <em>STATIC=1<\/em> argument to <em>make<\/em>.<\/p>\n<p>A final <em>make<\/em> command that contains the Dropbear server, it&#8217;s key generation and Secure Copy, looks like follows:<\/p>\n<pre lang=\"bash\">\n$ make PROGRAMS=\"dropbear dropbearkey scp\" STATIC=1 MULTI=1<\/pre>\n<p><strong>Installation<\/strong><br \/>\nYou can use the standard method for installing Dropbear plus the same arguments used during compilation:<\/p>\n<pre lang=\"bash\">\n$ make PROGRAMS=\"dropbear dropbearkey scp\" STATIC=1 MULTI=1 install<\/pre>\n<p>But this will use the default path that some times in embedded systems is not sufficient because different paths can be used. In these cases you&#8217;ll have to copy manually the executable called <em>dropbearmulti<\/em> to the desired location and create symbolic links that have the name of the program that you want to use that point to this executable. If you have used Busybox, the concept of symbolic links pointing to the same executable will be familiar:<\/p>\n<pre lang=\"bash\">\n# $PREFIX\/..\/.. is target's root filesytem: \/opt\/STM\/STLinux-2.3\/devkit\/sh4\/target\/\n# Copy the real executable dropbearmulti to the target's root filesystem \/usr\/local\/bin\n$ cp dropbearmulti $PREFIX\/bin\n\n# Create the symbolic links dropbearkey and dbclient\n$ ln -s dropbearmulti dropbearkey\n$ ln -s dropbearmulti dbclient\n\n# Create the symbolic link scp in the target's filesytem \/usr\/bin\n$ cd $PREFIX\/..\/bin\n$ ln -s ..\/local\/bin\/dropbearmulti scp\n<\/pre>\n<p>Stripping is quite a good thing when you need to save storage:<\/p>\n<pre lang=\"bash\">\n$ cd $PREFIX\/bin\n$ sh4-linux-strip dropbearmulti\n<\/pre>\n<p>Dropbear 0.52 ships with an alternative standalone <em>scp<\/em> program that is taken from OpenSSH. If you want to use this instead of the default built-in, you need to compile it separately:<\/p>\n<pre lang=\"bash\">$ make scp<\/pre>\n<p>For further compilation hints you can take a look at the SMALL and MULTI files provided in the package.<\/p>\n<p><strong>Keys creation<\/strong><\/p>\n<p>SSH uses public-key cryptography to authenticate a remote client. Thus, once you have Dropbear properly installed, but before using it, you need to create the cryptographic keys.<br \/>\nYou can read the following links for an in-depth explanation of how SSH and public-key cryptography work:<br \/>\nSSH: <a href=\"http:\/\/en.wikipedia.org\/wiki\/Secure_Shell\">http:\/\/en.wikipedia.org\/wiki\/Secure_Shell<\/a><br \/>\nPublic-key cryptography: <a href=\"http:\/\/en.wikipedia.org\/wiki\/Public-key_cryptography\">http:\/\/en.wikipedia.org\/wiki\/Public-key_cryptography<\/a><\/p>\n<p>You can create your keys using two different methods: RSA and DSS.<br \/>\nBoth kind of keys seem to be equally secure, but RSA seems to be faster for signature verification that is the most common operation when using the keys.<\/p>\n<p>Let&#8217;s create the keys using both methods.<br \/>\nConnect to your target via serial or telnet (at the end of this post you will, hopefully, use ssh instead of telnet):<\/p>\n<pre lang=\"bash\">\n$ mkdir -pv \/etc\/dropbear\n$ cd \/etc\/dropbear\n\n# Generate the RSA private and public keys.\n$ dropbearkey -t rsa -f dropbear_rsa_host_key\nWill output 1024 bit rsa secret key to 'dropbear_rsa_host_key'\nGenerating key, this may take a while...\nPublic key portion is: \nssh-rsa\nAAAAB3NzaC1yc2EAAAADAQABAAAAgncEwjavI+PfbQ+WNDYev\/g2f5PGTD7ZzAnauZP4dWOi0MTVFGOZXTf9\/cWXwx12zZ2KUOS5UfWk9SdH\/t67MPVDDptFaq0kJ5ReA6JzqhwaKguMIdNGTdM2HfwJabvSnDL4SZgpxXtOJYZhWaqMMO8OP560gE21h6O0mH75\/IBJUak=\nroot@my_target\nFingerprint: md5 0a:bb:43:82:41:7b:8a:8b:4d:41:43:e7:71:54:a7:ce\n\n# Generate the DSS private and public keys.\n$ dropbearkey -t dss -f dropbear_dss_host_key\nWill output 1024 bit dss secret key to 'dropbear_dss_host_key'\nGenerating key, this may take a while...\nPublic key portion is: \nssh-dss\nAAAAB3NzaC1kc3MAAACBALIEmhf5c65czi\/2rq1Xu2I0Vv+DqeEUQDDmeFke4nyBqVOuQTq8JrHzuRz8SbeohqhXWx\/\/546HLK\/RwIG6Gkj5+RQ7uvcc9bcprwJtQVU+U1qmgepPsl6e0Cofldbz+LBLWE5R1sg1ExX\/NzGNtKzIbukXUABK12\/sveGODeWpAAAAFQCNmGPNFcQyso8x+Zc52FLuRrtTfQAAAIEAiN4pNvhdyNX0lg1nop0ei+qp7H8kmurXWhZMZmaKWVYOc8YqvELL\/U1s9fNNPsxEBTi6ysYLM1MCHNizQ4x\/npsC\/1e6URaIub\/Dtf55N5Bn1LaItdK2EO1wLemf1\/+3yuvJvhvI2yaPEEAzIgmQatTdE38HwY3+AxXSgccqsBcAAACBAJv9gJtcu5rHGal4VCaVRcM41z+omA7agAtcpmcXOBXr719T2qSZF+GoAeLlfThndaCTchpDQXNDxwTaW6G9RxGOhrDgyKR4MgShljnBfq0wsVx06l23Ex92d8xSmXJz4E\/BwtLhjUouAsrW\/IJbxQlwp3stmvtik6IYP\/BiM2kf\nroot@my_target\nFingerprint: md5 a7:ad:8e:f8:d2:78:73:ec:a7:1b:aa:ba:9b:ad:aa:2a\n<\/pre>\n<p>Be sure that the private keys generated in <em>\/etc\/dropbear<\/em> remain secret!<br \/>\nThe generated public keys shown in this post are just an example.<\/p>\n<p><strong>Connect to the server using Dropbear<\/strong><\/p>\n<p>At this point you are able to connect to the target using your login\/passwd. Normally in an embedded system the only user is root.<\/p>\n<p>Start dropbear server in the target:<\/p>\n<pre lang=\"bash\">$ dropbear <\/pre>\n<p>Check the different execution options using the <em>&#8211;help<\/em> flag.<br \/>\nOptional, but recommended, is to add in <em>\/etc\/hosts<\/em> in the target&#8217;s filesystem the hostname of the client (host).<\/p>\n<p>Finally, you can use ssh and scp:<\/p>\n<pre lang=\"bash\">\n# Connect from host (client) to target (server) using SSH.\n# It will prompt for root's passwd.\n$ ssh root@192.168.0.135\n\n# Copy the file 'hola' from the host (client) to the directory \/etc in the target (server).\n# It will prompt for root's passwd.\n$ scp hola root@192.168.0.135:\/etc\/hola\n<\/pre>\n<p>If you need to connect very often to the target, typing the passwd each time can be very annoying and could pose a security risk.<br \/>\nIt would be better not to send the passwd at all.<\/p>\n<p>For this purpose you can store the public key of your host (client) in the target (server).<br \/>\nAssuming that your host (client) is a Unix machine that has OpenSSH installed, you can easily create your keys:<\/p>\n<pre lang=\"bash\">\n# Key creation in the host (client) using OpenSSH:\n$ ssh-keygen\nGenerating public\/private rsa key pair.\nEnter file in which to save the key (\/home\/user\/.ssh\/id_rsa): y\nEnter passphrase (empty for no passphrase): \nEnter same passphrase again: \nYour identification has been saved in y.\nYour public key has been saved in y.pub.\nThe key fingerprint is:\n74:84:22:e2:f9:6d:fc:b5:b1:72:40:58:e2:23:ff:ea user@my_client\nThe key's randomart image is:\n+--[ RSA 2048]----+\n|                 |\n|         .       |\n|    . . o ..     |\n|   . . +o.  .    |\n|    . . S..o.    |\n|     . o.+..o.   |\n|        . ....   |\n|          o. ..  |\n|         ..+B=+. |\n+-----------------+\n<\/pre>\n<p>You will end-up with the private key <em>id_rsa<\/em> and public key <em>id_rsa.pub<\/em> in <em>\/home\/user\/.ssh\/<\/em>.<br \/>\nCopy the host&#8217;s (client) public key to  ~\/.ssh\/authorized_keys in the target&#8217;s filesystem. Notice that ~ is normally \/root, but if you&#8217;re logging in with another username, change it accordingly.<br \/>\n<em>Warning<\/em>: If you copy-paste the public key, check that your text editor copies-pastes it correctly, otherwise you will end-up with a broken key. You&#8217;re safe if you use vi.<\/p>\n<p>Once you have your host&#8217;s (client) public key in your target&#8217;s (server) filesystem, you can login to your target without using a passwd speeding up your login procedure!<\/p>\n<p><strong>Running from inetd<\/strong><\/p>\n<p>If you want to use Dropbear from <em>inetd<\/em> you have to enable it in the <em>options.h<\/em> file before compiling:<\/p>\n<pre lang=\"c\">#undef NON_INETD_MODE\n#define INETD_MODE<\/pre>\n<p>In the <em>inetd.conf<\/em> configuration file you have to add the following line:<\/p>\n<pre lang=\"bash\">22 stream tcp nowait root \/usr\/local\/bin\/dropbear dropbear -i<\/pre>\n<p>Notice that if you compile with <em>inetd<\/em> support, Dropbear will refuse to start from the command line. Obviously, this applies for the other way around too.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nHaving a working Dropbear server is not a difficult task. You only have to pay attention at compilation time of the services you want from it, and when running it, of the location where you have to store the keys, specially if you want to use them for logging in without a passwd.<br \/>\nReading the INSTALL, README and other instruction files is always a good idea, no matter how extensive they are.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dropbear is a thin Secure Shell 2 (SSH 2) server and client. It is a good alternative to OpenSSH when you have storage and memory restrictions, specially in embedded systems. Cross-compiling and running Dropbear in an embedded system is not a difficult task, but the documentation is not very clear for everybody. You can start\u2026 <span class=\"read-more\"><a href=\"https:\/\/paguilar.org\/?p=30\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,15],"tags":[22,24,58,59,36],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-compiling","category-security","tag-cross-compiling","tag-dropbear","tag-networking","tag-security","tag-ssh"],"_links":{"self":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=30"}],"version-history":[{"count":0,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions"}],"wp:attachment":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}