Showing posts with label ssh. Show all posts
Showing posts with label ssh. Show all posts

9.7.10

Creating Certificate Authorities and self-signed SSL certificates

 

Following is a step-by-step guide to creating your own CA (Certificate Authority) -- and also self-signed SSL server certificates -- with openssl on Linux. Self-signing is the simpler route to take, but making one's own CA allows the signing of multiple server certificates using the same CA and involves only a few extra steps.
After using openssl to generate the necessary files, you'll need to integrate them into Apache. This process differs between Linux distros and versions of Apache. Additional references exist at the end of this document. My instructions for Setting up SSL: Ubuntu and Apache 2 are kept most current, and will carry you through to completion.
Making a homemade CA or self-signed certificate will cause the client web browser to prompt with a message whether to trust the certificate signing authority (yourself) permanently (store it in the browser), temporarily for that session, or to reject it. The message "web site certified by an unknown authority... accept?" may be a business liability for general public usage, although it's simple enough for the client to accept the certificate permanently.
Whichever route you take, you'll save the periodic expense of paying a recognized signing authority. This is purely for name recognition -- they've paid the major browser producers to have their CA pre-loaded into them. So if you're on a budget, have a special need or small audience, this may be useful.

Before you start
You need Apache and openssl. Compiling them from source, handling dependencies, etc. is beyond the scope of this document. You can consult their documentation, or go with a mainstream Linux distro that will do the preliminary work for you.
Now you need to decide whether you'll make a CA (Certificate Authority) and sign a server certificate with it -- or just self-sign a server certificate. Both procedures are detailed below.


(1A) Create a self-signed certificate.

Complete this section if you do NOT want to make a CA (Certificate Authority). If you want to make a CA, skip 1A entirely and go to 1B instead.
Some steps in this document require priviledged access, and you'll want to limit access to the cert files to all but the root user. So you should su to root and create a working directory that only root has read/write access to (for example: mkdir certwork, chmod 600 certwork). Go to that directory.
Generate a server key:

openssl genrsa -des3 -out server.key 4096

Then create a certificate signing request with it. This command will prompt for a series of things (country, state or province, etc.). Make sure that "Common Name (eg, YOUR name)" matches the registered fully qualified domain name of your box (or your IP address if you don't have one). I also suggest not making a challenge password at this point, since it'll just mean more typing for you.
The default values for the questions ([AU], Internet Widgits Pty Ltd, etc.) are stored here: /etc/ssl/openssl.cnf. So if you've got a large number of certificate signing requests to process you probably want to carefully edit that file where appropriate. Otherwise, just execute the command below and type what needs to be typed:

openssl req -new -key server.key -out server.csr

Now sign the certificate signing request. This example lasts 365 days:

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Make a version of the server.key which doesn't need a password:

openssl rsa -in server.key -out server.key.insecure
mv server.key server.key.secure
mv server.key.insecure server.key

These files are quite sensitive and should be guarded for permissions very carefully. Chown them to root, if you're not already sudo'd to root. I've found that you can chmod 000 them. That is, root will always retain effective 600 (read) rights on everything.
Now that you've just completed Step 1A, skip ahead to Step 2.

(1B) Generate your own CA (Certificate Authority).

Complete this section if you want to make a CA (Certificate Authority) and sign a server certificate with it. The steps for making a server certificate are also included here. If you'd rather one-time self-sign a server certificate, skip this step entirely and go to 1A instead.
Some steps in this document require priviledged access, and you'll want to limit access to the cert files to all but the root user. So you should su to root and create a working directory that only root has read/write access to (for example: mkdir certwork, chmod 600 certwork). Go to that directory.
In this step you'll take the place of VeriSign, Thawte, etc. You'll first build the CA key, then build the certificate itself.
The Common Name (CN) of the CA and the Server certificates must NOT match or else a naming collision will occur and you'll get errors later on. In this step, you'll provide the CA entries. In a step below, you'll provide the Server entries. In this example, I just added "CA" to the CA's CN field, to distinguish it from the Server's CN field. Use whatever schema you want, just make sure the CA and Server entries are not identical.
CA:
Common Name (CN): www.somesite.edu CA
Organization (O): Somesite
Organizational Unit (OU): Development
Server:
Common Name (CN): www.somesite.edu
Organization (O): Somesite
Organizational Unit (OU): Development
If you don't have a fully qualified domain name, you should use the IP that you'll be using to access your SSL site for Common Name (CN). But, again, make sure that something differentiates the entry of the CA's CN from the Server's CN.

openssl genrsa -des3 -out ca.key 4096
openssl req -new -x509 -days 365 -key ca.key -out ca.crt

Generate a server key and request for signing (csr).

This step creates a server key, and a request that you want it signed (the .csr file) by a Certificate Authority (the one you just created in Step #1B above.)
Think carefully when inputting a Common Name (CN) as you generate the .csr file below. This should match the DNS name, or the IP address you specify in your Apache configuration. If they don't match, client browsers will get a "domain mismatch" message when going to your https web server. If you're doing this for home use, and you don't have a static IP or DNS name, you might not even want worry about the message (but you sure will need to worry if this is a production/public server). For example, you could match it to an internal and static IP you use behind your router, so that you'll never get the "domain mismatch" message if you're accessing the computer on your home LAN, but will always get that message when accessing it elsewhere. Your call -- is your IP stable, do you want to repeat these steps every time your IP changes, do you have a DNS name, do you mainly use it inside your home or LAN, or outside?

openssl genrsa -des3 -out server.key 4096
openssl req -new -key server.key -out server.csr

Sign the certificate signing request (csr) with the self-created Certificate Authority (CA) that you made earlier.

Note that 365 days is used here. After a year you'll need to do this again.
Note also that I set the serial number of the signed server certificate to "01". Each time you do this, especially if you do this before a previously-signed certificate expires, you'll need to change the serial key to something else -- otherwise everyone who's visited your site with a cached version of your certificate will get a browser warning message to the effect that your certificate signing authority has screwed up -- they've signed a new key/request, but kept the old serial number. There are a couple ways to rectify that. crl's (certificate revocation list) is one method, but beyond the scope of the document. Another method is for all clients which have stored the CA certificate to go into their settings and delete the old one manually. But for the purposes of this document, we'll just avoid the problem. (If you're a sysadmin of a production system and your server.key is compromised, you'll certainly need to worry.)
The command below does a number of things. It takes your signing request (csr) and makes a one-year valid signed server certificate (crt) out of it. In doing so, we need to tell it which Certificate Authority (CA) to use, which CA key to use, and which Server key to sign. We set the serial number to 01, and output the signed key in the file named server.crt. If you do this again after people have visited your site and trusted your CA (storing it in their browser), you might want to use 02 for the next serial number, and so on. You might create some scheme to make the serial number more "official" in appearance or makeup but keep in mind that it is fully exposed to the public in their web browsers, so it offers no additional security in itself.

openssl x509 -req -days 365 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt

To examine the components if you're curious:

openssl rsa -noout -text -in server.key
openssl req -noout -text -in server.csr
openssl rsa -noout -text -in ca.key
openssl x509 -noout -text -in ca.crt

Make a server.key which doesn't cause Apache to prompt for a password.

Here we create an insecure version of the server.key. The insecure one will be used for when Apache starts, and will not require a password with every restart of the web server. But keep in mind that while this means you don't have to type in a password when restarting Apache (or worse -- coding it somewhere in plaintext), it does mean that anyone obtaining this insecure key will be able to decrypt your transmissions. Guard it for permissions VERY carefully.

openssl rsa -in server.key -out server.key.insecure
mv server.key server.key.secure
mv server.key.insecure server.key

These files are quite sensitive and should be guarded for permissions very carefully. Chown them to root, if you're not already sudo'd to root. I've found that you can chmod 000 them. That is, root will always retain effective 600 (read) rights on everything.

(2) Copy files into position and tweak Apache.

Some professors like to pause for a moment after a long lecture, and do a little recap. It's a good pedagogical tool, so let's do so here. If you took route 1A above, you should have four files in a working directory:
server.crt: The self-signed server certificate.
server.csr: Server certificate signing request.
server.key: The private server key, does not require a password when starting Apache.
server.key.secure: The private server key, it does require a password when starting Apache.
If you took route 1B and created a CA, you'll have two additional files:
ca.crt: The Certificate Authority's own certificate.
ca.key: The key which the CA uses to sign server signing requests.
The CA files are important to keep if you want to sign additional server certificates and preserve the same CA. You can reuse these so long as they remain secure, and haven't expired.
At a bare minimum, the following considerations must now be addressed:

  • You'll need a virtual host and document root set up for the SSL instance.
  • You'll need to turn on the SSL engine and enable/load the SSL module.
  • Apache must reference server.crt and server.key somewhere in its configuration.
  • Apache must be listening to a port for which SSL is enabled (443 is default).
The particulars differ between Linux distros and versions of Apache. I'm only able to keep the Setting up SSL: Ubuntu and Apache 2 documentation current due to time constraints. Those steps should apply broadly to Debian-based distros with little or no modification. Red Hat and openSUSE commentary is kept online here for historical purposes.
Setting up SSL: Ubuntu and Apache 2
Setting up SSL: Red Hat and Apache 1.3.x
Setting up SSL: openSUSE

http://www.tc.umn.edu/~brams006/selfsign.html

HOWTO: Hosting a Subversion Repository

Author: Nilesh Bansal, Created: December 2005, Updated: $Date: 2007-06-03 02:13:49 -0400 (Sun, 03 Jun 2007) $. Leave your feedback.


This document explains in details the procedure to setup a subversion repository (with trac) in Linux based environment. Depending on the needs, one of the following three schemes can be selected:

 

1. Subversion Over ssh

If only one user is going to use the repository, it is easiest to use the subversion server over ssh. In order to do so, you will first need to choose a directory to store all the files of the repository and then initialize it using the following command

mkdir /home/user/myrepo/
svnadmin create /home/user/myrepo/

You may now edit the configuration file to change the access rules. For example the /home/user/myrepo/conf/svnserve.conf can be:

[general]
anon-access = read
auth-access = write
realm = My First Repository
password-db = passwd

In this case, anyone can read the contents of the repository, but only the user who has write permissions to the filesystem where repository is hosted can write to it.

Now the repository can be accessed using the following url

svn+ssh://user@remote.host/home/user/myrepo/

This means that the subversion client will first ssh to remote.host, where the user can login using the system password, and then will start the subversion server in tunnel mode as svnserve -t -r /home/user/repo/.

Further details can be found at http://svnbook.red-bean.com/en/1.1/ch06s03.html

 

2. Subversion Using svnserve

If fine-grained access control and high security is not an requirement, use of the custom server svnserve is the best choice. The user the user will first need to choose a directory to store all the files of the repository and then initialize it using the following command

mkdir /home/user/myrepo/
svnadmin create /home/user/myrepo/

The user may now edit the configuration file to change the access rules. An example /home/user/myrepo/conf/svnserve.conf will be:

[general]
anon-access = read
auth-access = write
realm = My First Repository
password-db = passwd

In this case, anonymous user can read the contents of the repository, but only an authenticated user can write to it. passwd is the file that will contain the usernames and passwords for all the users. The format of this file is as follows (username followed by the password, separated by colon)

harry:passwd1
sally:passwd2

The password file contains nothing else except a list of user:passwd on each line. Is using svnserve, password needs to be stored in cleartext. Note that an alternate format for this file is possible as suggested elsewhere, but I have not tried using that format (format described above is tested against svn version 1.2.3). To create the encrypted passwords, following small perl script can be used (replace mypasswd with your password)

perl -e '$pass="mypasswd"; print crypt($pass, $pass);'

The subversion server can now be started using the command svnserve -D -r -R /home/user/myrepo/ (this runs on port 631). Since this command needs to be executed every time the system reboots, the user may use the system init scripts. Many linux distributions (like SuSE), which ship with svnserve provide the init script in /etc/init.d/. In this case the server can be started using (after modifying them to adjust the repository path):

chkconfig svnserve on    #always start at boot
/etc/init.d/svnserve start    #start now

If the init script is not available, following script can be copied to /etc/init.d/svnserve (do not forget to change the permissions to executable)

#!/bin/sh
#File: /etc/init.d/svnserve
REPO_ROOT=/home/user/myrepo/
SVN_UID=user
SVN_GID=user
. /etc/rc.status
rc_reset
case "$1" in
   start)
     echo -n "Starting svnserve "
     startproc -u $SVN_UID -g $SVN_GID -e svnserve -d -R -r $REPO_ROOT
     rc_status -v
     ;;
   stop)
     echo -n "Shutting down svnserve "
     killproc -TERM svnserve
     rc_status -v
     ;;
   restart)
     $0 stop
     $0 start
     rc_status
     ;;
   *)
     echo "Usage: $0 {start|stop|restart}"
     exit 1
     ;;
esac
rc_exit

The repository can now be accessed using the following url:

svn://username@remote.host/

 

Installing trac

Trac is an web-based software development management system built around subversion. First download Trac from http://projects.edgewall.com/trac/wiki/TracDownload. Checkhttp://projects.edgewall.com/trac/wiki/TracInstall for additional packages that may be required. Subversion and python are usually available in all the distributions. To install clearsilver:

wget http://www.clearsilver.net/downloads/clearsilver-0.10.2.tar.gz
tar zxvf clearsilver-0.10.2.tar.gz
cd clearsilver-0.10.2/
./configure --with-python=/usr/bin/python
make
su -c "make install"

If trac complains about missing neo package, you may have to copy neo_cgi.so manually to /usr/lib/python2.3/site-packages/. To install the PySQLite project first install SQLite3

wget http://www.sqlite.org/sqlite-3.2.8.tar.gz
tar zxvf sqlite-3.2.8.tar.gz ; cd sqlite-3.2.8
./configure --prefix=/usr/ --disable-tcl
make && make install

and now install PySQLite

wget http://initd.org/pub/software/pysqlite/releases/2.0/2.0.5/pysqlite-2.0.5
tar zxvf pysqlite-2.0.5.tar.gz ; cd pysqlite-2.0.5
python ./setup.py install

To install Trac

wget http://ftp.edgewall.com/pub/trac/trac-0.9.2.tar.gz
tar zxvf trac-0.8.4.tar.gz
cd trac-0.8.4
python ./setup.py install

Run ldconfig if Trac complains about missing libraries

Trac environment can now be initialized as

trac-admin /home/user/trac-env/ initenv

Trac standalone server can now be started as

tracd --port 8080 /home/user/trac-env/

Then, fire up a browser and visit http://remote.host:8080/. A simple listing of all environments that tracd knows about should be displayed. Note that, both svnserve and tracd should run with uid, otherwise their may be permission conflicts.

Since the Trac server needs to started after every boot, user may copy the following to /etc/init.d/tracd and run chkconfig tracd on.

#!/bin/sh
#File: /etc/init.d/tracd
REPO_ROOT=/home/user/myrepo/
SVN_UID=user
SVN_GID=user
. /etc/rc.status
rc_reset
case "$1" in
start)
echo -n "Starting tracd"
startproc -u $SVN_UID -g $SVN_GID -e tracd -d -R -r $REPO_ROOT
rc_status -v
;;
stop)
echo -n "Shutting down tracd"
killproc -TERM tracd
rc_status -v
;;
restart)
$0 stop
$0 start
rc_status
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
rc_exit

 

3. Subversion With Apache and SSL

If you need to host a repository that is going to be used by many projects and users and require fine grained access control, you may choose to use apache httpd with ssl to host it. In this case, since the rpms for some required packages may not be available, you may need to compile everything from source.

First install apache httpd

wget http://apache.mirrored.ca/httpd/httpd-2.0.55.tar.gz
tar zxvf httpd-2.0.55.tar.gz; cd httpd-2.0.55
./configure --prefix=/usr/local/subversion/ --enable-dav --enable-so --enable-ssl
make && make install

Now install subversion

wget http://subversion.tigris.org/downloads/subversion-1.2.3.tar.gz
tar zxvf subversion-1.2.3.tar.gz ; cd subversion-1.2.3
./configure --prefix=/usr/local/subversion/ --with-apxs=/usr/local/subversion/bin/apxs --with-ssl
make && make install
make swig-py && make install-swig-py


Create a new user and group to run the server for repository

groupadd svn
useradd -m -d /srv/svn/ -g svn svn

Create the repositories. Lets assume that we need two repositories named mars and venus.

su - svn
mkdir /srv/svn/repositories/
mkdir /srv/svn/repositories/mars/
mkdir /srv/svn/repositories/venus/
svnadmin create /srv/svn/repositories/mars/
svnadmin create /srv/svn/repositories/venus/


Now edit /usr/local/subversion/conf/httpd.conf to include following lines

ServerRoot "/usr/local/subversion/"
User svn
Group svn
LoadModule dav_svn_module modules/mod_dav_svn.so
LoadModule authz_svn_module modules/mod_authz_svn.so


Turn SSL on by including the following lines

Listen 443
SSLEngine On
SSLCertificateFile /usr/local/subversion/cert/server.crt
SSLCertificateKeyFile /usr/local/subversion/cert/server.key

Where server.crt contains the SSL certificate. To create a self signed certificate refer to http://www.akadia.com/services/ssh_test_certificate.html.

Now include the following lines in httpd.conf to allow access to svn repository

<Location /svn/users/>
Order allow,deny
Allow from all
DAV svn
SVNParentPath /srv/svn/repositories/
# our access control policy
AuthzSVNAccessFile /srv/svn/conf/users-access-file
#try anonymous access first, resort to real
#authentication if necessary.
Satisfy Any
Require valid-user
# how to authenticate a user
AuthType Basic
AuthName "My Subversion repository"
AuthUserFile /srv/svn/conf/passwd
</Location>

passwd (as discussed earlier in this document) file contains username and password pairs and user-access-file will contain access rules. Unlike svnserve, password can be encrypted using one of many encryption algorithms supported (including the one used by UNIX for /etc/passwd). An example user-access-file will be as follows:

[/]
* =
[mars:/]
harry = r
sally = rw
[venus:/]
sally = rw
[venus:/bugs/]
sally =
harry = rw

The first line means that anonymous user ("*") has no access to the repository. harry can read while sally can both read and write in mars repository. sally can also read and write in venus repository (except for directory bugs which only harry can access and modify).

Now create the init script /etc/init.d/apache

#!/bin/sh
#File: /etc/init.d/apache
. /etc/rc.status
HTTPD = /usr/local/subversion/bin/httpd
CONFFILE=/usr/local/subversion/conf/httpd.conf
rc_reset
case "$1" in
start)
echo -n "Starting httpd"
startproc -e $HTTPD
rc_status -v
;;
stop)
echo -n "Shutting down httpd"
killproc -TERM httpd
rc_status -v
;;
restart)
$0 stop
$0 start
rc_status
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
rc_exit

and start the server by

chkconfig apache on
/etc/init.d/apache start

Now the repositories should be accessible on

https://remote.host/svn/mars/
https://remote.host/svn/venus/

Installing trac


First follow the instructions discussed earlier in this document to install trac. Once trac in installed, create trac environments for each of the repositories.

su - svn
mkdir /srv/svn/tracenv
mkdir /srv/svn/tracenv/mars
mkdir /srv/svn/tracenv/venus
trac-admin /srv/svn/tracenv/mars initenv
trac-admin /srv/svn/tracenv/venus initenv

Add the following lines to httpd.conf to make these repositories available

ScriptAlias /trac /usr/local/trac/share/trac/cgi-bin/trac.cgi
<Location "/trac">
SetEnv TRAC_ENV_PARENT_DIR "/srv/svn/tracenv/"
</Location>
<LocationMatch "/trac/[^/]+/login">
AuthType Basic
AuthName "My Trac"
AuthUserFile /srv/svn/conf/passwd
Require valid-user
</LocationMatch>

This will allow everyone to acces the repositories at https://remote.host/trac/. If you do not want anonymous access, instead include something like:

ScriptAlias /trac/mars /usr/local/trac/share/trac/cgi-bin/trac.cgi
<Location "/trac/mars/">
SetEnv TRAC_ENV "/srv/svn/tracenv/mars"
Require user harry
AuthType Basic
AuthName "My Trac"
AuthUserFile /srv/svn/conf/passwd
</Location>
ScriptAlias /trac/venus /usr/local/trac/share/trac/cgi-bin/trac.cgi
<Location "/trac/venus/">
SetEnv TRAC_ENV "/srv/svn/tracenv/venus"
Require user sally
AuthType Basic
AuthName "My Trac"
AuthUserFile /srv/svn/conf/passwd
</Location>

In this case, harry can access trac for mars and sally can access trac for venus. Require clause in above rules can also be preceded by valid-user instead of list of users to allow access by everyone in the passwd file.

trac-admin can be used to grant privileges to users on trac

trac-admin /srv/svn/tracenv/venus permission list
trac-admin /srv/svn/tracenv/venus permission add sally TRAC_ADMIN

Notes



  • All init scripts in this document are SuSE style. They may require editing before they can be used with other distributions.



  • If you find an error, please email me, or leave a comment at my blog, and I will update the document.


References

Establish a Socks 5 Proxy Using OpenSSH

 

It is quiet interesting to learn something about the –D argument of OpenSSH. When you have been stuck on an “untrusted” Internet connection and need to log in (insecurely) to a certain site, you need this. For example, you connect to the Internet using a public hotspot and willing to visit a website with no way of logging in via HTTPS.

For the past few years, I’ve had this plan to get VPN. I know a way to establish a VPN tunnel using SSH. However, that involves a lot of configuration. I was already aware of OpenSSH’s –L option which simply forwards a local port through an SSH tunnel to a port on the remote machine. Very handy when you want to connect surely to a port on the remote machine. Very handy when you want to connect surely to a site hosted on that server and happen to have a shell account on it. But to do much more than that ranges from the complex to impossible. This is where –D comes in. The –D argument tells OpenSSH to be a SOCKS proxy. So you simply log in to the endpoint via SSH with the –D argument like:

ssh -D 1234 user@host.example.com

And then tell your web browser to use a SOCKS v5 proxy on localhost at the specified port and bingo, you have a secure connection to your endpoint. In fact, any application with SOCKS support can have its traffic routed through the SSH tunnel via SOCKS.

Some nice tips from this following article:
OpenSSH: The Poor Man's SOCKS Proxy

8.7.10

OpenSSH Server Configuration

OpenSSH Server Configuration

SSH is the only way one should be connecting a remote shell to their Linux machines. SSH can provide remote shells, secure FTP (SFTP) and functions nicely as a tunnel for rsync. SSH comes with every distribution, 99% anyways, is easy to setup and provides excellent security. This article speaks about OpenSSH 4.2, other implementations exist.

SSH Server Daemon

The sshd daemon provides the server portion of SSH; it's configuration is usually located at/etc/ssh/sshd_config. Distributions vary on the defaults in here, some support protocol one and two, some only two. Regardless of your distribution below are relevant portions of a fairly tightend server with inline comments. Noteably root is denied, passwords are disabled and groups are restricted. Read man sshd_config.

# limit protocol and listen address
ListenAddress 1.2.3.4
Protocol 2
# Restrict Logins, keys only, 20s timeout
LoginGraceTime 20
MaxAuthTries 2
PermitRootLogin without-password
PasswordAuthentication no
# disable this stuffs
UsePAM no
PrintMotd no
UseDNS no

SSH Authorized Keys Automatic Login


SSH can automatically authenticate connections when the client presents an authorized key. A client gives it's public key to a server and then when it connects the server knows it's allowed in and automatically allows the connection. The Keys are specific to users, so a key for user_a will not letuser_b in.

Few Simple Steps


  1. Create Keys

    If the keys don't exists already you must create them. Look in your ~/.ssh for files called id_rsa and id_rsa.pub. If those files don't exist say ssh-keygen -b 2048 -t rsa to create them.


  2. Place Keys on Server

    Copy id_rsa.pub to the server then append that to ~/.ssh/authorized_keys


In the Creo section we have a script to automate this process.

Optionally you can use a DSA key, simply replace `rsa` with `dsa` above.