Mandriva2006 et Fedora :
Dépendances requises : zlib et openssl Lisez le fichier README pour plus de détails.
C. Ecriture du script de démarrage pour sshd
Avec votre éditeur de texte préféré, créez le fichier /etc/init.d/sshd
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
#!/bin/bash case $1 in 'start' ) /usr/local/sbin/sshd ;; 'stop' ) pkill sshd ;; * ) echo "usage : /etc/init.d/sshd {start|stop}" ;; esac |
|
# chmod -v 0755 /etc/init.d/sshd |
|
# useradd -c 'Privilege-separated SSH' -d /var/empty/sshd -s /sbin/nologin sshd |
|
# /etc/init.d/sshd start |
|
# update-rc.d sshd start 99 2 3 4 5 . |
|
# perl /chemin/vers/script/prisonssh.pl |
|
#! /usr/bin/perl # use strict; use warnings; use File::Basename; my (@lib,@ldd,%listreplib); # création de répertoire print "Entrez le nom de repertoire : "; chomp(my $chroot_rep=<STDIN>); mkdir '/home/'.$chroot_rep or die "Impossible de créer le répertoire : $!"; # Les applications d'environement chroot # # Assurez vous que le chemin des applications mentionnées correspond # # Par exemple la commande id # sous Debian et Fedora5 on la trouve dans /usr/bin # # sous Mandriva 2006 dans /bin # # à vous d'ajouter ou enlever des applications # et bien sûr de modifier le script # # les applications (/bin) my @apps0=qw ( /bin/bash /bin/ls /bin/mkdir /bin/mv /bin/pwd /bin/rm /bin/sh /bin/echo /bin/cp /bin/cat /bin/ln /bin/chown /bin/chmod /bin/grep /bin/more /bin/tar /bin/gzip /bin/true /bin/false /bin/ping /bin/egrep /bin/hostname ); # les applications (/usr/bin, etc...) my @apps1=qw ( /usr/local/bin/scp /usr/bin/env /usr/bin/clear /usr/bin/wc /usr/bin/perl /usr/bin/id /usr/local/bin/ssh /usr/bin/du /usr/bin/less /usr/bin/bzip2 /usr/bin/tset ); # le subsystem sftp my @sftp_server=qw ( /usr/local/libexec/sftp-server ); # récupérer les bibliothéques foreach (@apps0,@apps1,@sftp_server){ my $ldd=`ldd $_`; my @temp=split /\n/, $ldd; push @ldd,@temp; } # récupérer le chemin de chaque bibliothèque foreach(@ldd){ if ( $_=~/(?:\/?.*=>\s*)?(\/.*)\b\s*\(/ ){ print "$1\n"; push @lib,$1 if $1 !~ /^\s*$/; } } # les répertoires à créer (/lib,/usr/lib,etc...) foreach(@lib){ my $replib=dirname($_); $listreplib{$replib} +=1; } # création des répertoires dans l'environement chroot foreach (keys %listreplib){ system "mkdir","-p","/home/$chroot_rep/$_"; } # création du répertoire bin dans l'environement chroot mkdir "/home/$chroot_rep/bin"; my @rep=qw { home usr/bin usr/lib/locale usr/share/locale tmp dev/pts etc/pam.d usr/local/libexec }; foreach (@rep) { system "mkdir", "-p", "/home/$chroot_rep/$_"; } # copier les applications dans le répertoire /bin d'environnement chroot foreach(@apps0){ system "cp","-p","$_","/home/$chroot_rep/bin"; } # copier les applications dans répertoire /usr/bin d'environnement chroot foreach(@apps1){ next if /.*sftp-server.*/i; system "cp","-p","$_","/home/$chroot_rep/usr/bin"; } # copier sftp-server system "cp","-p","/usr/local/libexec/sftp-server","/home/$chroot_rep/usr/local/libexec"; # copier les bibliothèques foreach (@lib){ my $rep_lib=dirname($_); system "cp","-p","$_","/home/$chroot_rep/$rep_lib"; } opendir (LIBNSS,"/lib") or die "Impossible d'ouvrir le répertoire : $!"; while (defined (my $libnss = readdir(LIBNSS))) { next unless $libnss=~/libnss.*/; system "cp","-p","/lib/$libnss","/home/$chroot_rep/lib"; } # system "cp","-RPp","/usr/share/terminfo","/home/$chroot_rep/usr/share"; # system "mknod","-m","0666","/home/$chroot_rep/dev/null","c","1","3"; system "mknod","-m","0666","/home/$chroot_rep/dev/zero","c","1","5"; system "mknod","-m","0444","/home/$chroot_rep/dev/urandom","c","1","9"; system "mknod","-m","0666","/home/$chroot_rep/dev/tty","c","5","0"; system "mknod","-m","0666","/home/$chroot_rep/dev/ptmx","c","5","2"; # my @etc_conf=qw( nsswitch.conf host.conf resolv.conf bashrc termcap hosts localtime login.defs profile tsocks.conf ); foreach (@etc_conf){ system "cp","/etc/$_","/home/$chroot_rep/etc"; } # system "cp","-R","/usr/lib/perl5","/home/$chroot_rep/usr/lib"; # my @pamd=qw( other ); foreach (@pamd){ system "cp","/etc/pam.d/$_","/home/$chroot_rep/etc/pam.d"; } # Création des fichiers par défaut { open ETCPASSWD,"> /home/$chroot_rep/etc/passwd" or die "Impossible de créer le fichier : $!\n"; print ETCPASSWD "root:x:0:0::/:/bin/bash\n"; open ETCGROUP,"> /home/$chroot_rep/etc/group" or die "Impossible de créer le fichier : $!\n"; print ETCGROUP "root:x:0:\n" } |
|
# perl /chemin/vers/script/useradd_prisonssh.pl nom_utilisateur répertoire |
|
#! /usr/bin/perl # use strict;use warnings; die "Usage: \n\t$0 nom_utilisateur chroot-dir\n" unless @ARGV == 2; my $user = $ARGV[0]; my $chroot = "/home/$ARGV[1]"; system "groupadd", "$user"; system "useradd", "-d", "$chroot/./home/$user", "-g", "$user", "-m", "-s", "/bin/bash", "$user"; open PASSWD,">>$chroot/etc/passwd" or die "E/S : $!\n"; # my ($u) = grep { /$user/ } `cat /etc/passwd`; $u =~ s/[^:]*\.//; print PASSWD $u; # open GROUP,">>$chroot/etc/group" or die "E/S : $!\n"; print GROUP grep { /$user/ } `cat /etc/group`; # chmod 0700, "$chroot/home/$user"; system "chown", "-R", "$user:$user", "$chroot/home/$user"; # print "Entre le mot de passe pour $user\n"; system "passwd", "$user"; print "L'utilisateur < $user > à été créé avec succés.\n"; |
| Original patch by Ricardo Cerqueira <rmcc@clix.pt> Updated by James Dennis <jdennis@gmail.com> for OpenSSH-4.2p1 A patch to cause sshd to chroot when it encounters the magic token '/./' in a users home directory. The directory portion before the token is the directory to chroot() to, the portion after the token is the user's home directory relative to the new root. Patch source using: patch -p0 < /path/to/patch Systems with a bad diff (doesn't understand -u or -N) should use gnu diff. Solaris may store this as gdiff under /opt/sfw/bin. I can't say much about other systems (unless you email me your experiences!). ============================================== diff -ruN openssh-4.2p1/session.c openssh-4.2p1-chroot/session.c --- openssh-4.2p1/session.c 2005-08-31 09:59:49.000000000 -0700 +++ openssh-4.2p1-chroot/session.c 2005-09-01 10:45:07.000000000 -0700 @@ -59,6 +59,8 @@ #include "kex.h" #include "monitor_wrap.h" +#define CHROOT + #if defined(KRB5) && defined(USE_AFS) #include <kafs.h> #endif @@ -1260,6 +1262,11 @@ void do_setusercontext(struct passwd *pw) { +#ifdef CHROOT + char *user_dir; + char *new_root; +#endif /* CHROOT */ + #ifndef HAVE_CYGWIN if (getuid() == 0 || geteuid() == 0) #endif /* HAVE_CYGWIN */ @@ -1317,6 +1324,27 @@ restore_uid(); } #endif + +#ifdef CHROOT + user_dir = xstrdup(pw->pw_dir); + new_root = user_dir + 1; + + while((new_root = strchr(new_root, '.')) != NULL) { + new_root--; + if(strncmp(new_root, "/./", 3) == 0) { + *new_root = '\0'; + new_root += 2; + + if(chroot(user_dir) != 0) + fatal("Couldn't chroot to user's directory %s", user_dir); + pw->pw_dir = new_root; + break; + } + + new_root += 2; + } +#endif /* CHROOT */ + # ifdef USE_PAM /* * PAM credentials may take the form of supplementary groups. diff -ruN openssh-4.2p1/version.h openssh-4.2p1-chroot/version.h --- openssh-4.2p1/version.h 2005-08-31 02:47:07.000000000 -0700 +++ openssh-4.2p1-chroot/version.h 2005-09-01 10:51:49.000000000 -0700 @@ -1,6 +1,6 @@ /* $OpenBSD: version.h,v 1.45 2005/08/31 09:28:42 markus Exp $ */ -#define SSH_VERSION "OpenSSH_4.2" +#define SSH_VERSION "OpenSSH_4.2-chrootssh" #define SSH_PORTABLE "p1" #define SSH_RELEASE SSH_VERSION SSH_PORTABLE |
|
--- openssh-4.3p1/session.c 2005-12-24 04:59:12.000000000 +0100 +++ openssh-4.3p1-chroot/session.c 2006-02-02 13:39:03.000000000 +0100 @@ -59,6 +59,8 @@ #include "kex.h" #include "monitor_wrap.h" +#define CHROOT + #if defined(KRB5) && defined(USE_AFS) #include <kafs.h> #endif @@ -1251,6 +1253,11 @@ void do_setusercontext(struct passwd *pw) { +#ifdef CHROOT + char *user_dir; + char *new_root; +#endif /* CHROOT */ + #ifndef HAVE_CYGWIN if (getuid() == 0 || geteuid() == 0) #endif /* HAVE_CYGWIN */ @@ -1308,6 +1315,27 @@ restore_uid(); } #endif + +#ifdef CHROOT + user_dir = xstrdup(pw->pw_dir); + new_root = user_dir + 1; + + while((new_root = strchr(new_root, '.')) != NULL) { + new_root--; + if(strncmp(new_root, "/./", 3) == 0) { + *new_root = '\0'; + new_root += 2; + + if(chroot(user_dir) != 0) + fatal("Couldn't chroot to user's directory %s", user_dir); + pw->pw_dir = new_root; + break; + } + + new_root += 2; + } +#endif /* CHROOT */ + # ifdef USE_PAM /* * PAM credentials may take the form of supplementary groups. |
| Original patch by Ricardo Cerqueira <rmcc@clix.pt> Updated by Pierre Schiesser <pierre.schiesser@gmail.com> for OpenSSH-4.5p1 A patch to cause sshd to chroot when it encounters the magic token '/./' in a users home directory. The directory portion before the token is the directory to chroot() to, the portion after the token is the user's home directory relative to the new root. Patch source using: patch -p0 < /path/to/patch Systems with a bad diff (doesn't understand -u or -N) should use gnu diff. Solaris may store this as gdiff under /opt/sfw/bin. I can't say much about other systems (unless you email me your experiences!). ============================================= --- openssh-4.5p1/session.c 2006-10-23 19:01:56.000000000 +0200 +++ openssh-4.5p1-chroot/session.c 2006-11-07 21:33:12.000000000 +0100 @@ -88,6 +88,8 @@ #include "kex.h" #include "monitor_wrap.h" +#define CHROOT + #if defined(KRB5) && defined(USE_AFS) #include <kafs.h> #endif @@ -1287,6 +1289,11 @@ void do_setusercontext(struct passwd *pw) { +#ifdef CHROOT + char *user_dir; + char *new_root; +#endif /* CHROOT */ + #ifndef HAVE_CYGWIN if (getuid() == 0 || geteuid() == 0) #endif /* HAVE_CYGWIN */ @@ -1344,6 +1351,27 @@ restore_uid(); } #endif + +#ifdef CHROOT + user_dir = xstrdup(pw->pw_dir); + new_root = user_dir + 1; + + while((new_root = strchr(new_root, '.')) != NULL) { + new_root--; + if(strncmp(new_root, "/./", 3) == 0) { + *new_root = '\0'; + new_root += 2; + + if(chroot(user_dir) != 0) + fatal("Couldn't chroot to user's directory %s", user_dir); + pw->pw_dir = new_root; + break; + } + + new_root += 2; + } +#endif /* CHROOT */ + # ifdef USE_PAM /* * PAM credentials may take the form of supplementary groups. |