Ver Mensaje Individual
  #9 (permalink)  
Antiguo 23/04/2012, 10:46
Avatar de mcun
mcun
 
Fecha de Ingreso: octubre-2010
Ubicación: tras la pantalla
Mensajes: 466
Antigüedad: 13 años, 6 meses
Puntos: 55
Respuesta: Problema creando servidor SSH-FTP (SFTP) enjaulado, no consigo conectar

Mira yo estoy programando una aplicación que usa una jaula chroot, creo que muy segura ..

te la paso es un script en python se ejecuta como root. y usa un script en bash

este lo guardas con el nombre makejail.py y lo ejecutas como root python makejail.py

funciona python 2.7 o cualquier 2.x



Código Python:
Ver original
  1. #!/usr/bin/python2
  2. #-*-cuserjaulag: utf-8 -*
  3. try:    
  4.     import os
  5.     import subprocess
  6.     import re
  7. except ImportError:
  8.     platform_specific_module = None
  9.  
  10. """Classes and functions related to the creation of chroot jail. """  
  11.    
  12. class MakeJailX:  
  13.     """ MakeJail creates jail
  14.        ======================
  15.        
  16.    - Create the directory tree
  17.    - Set permissions
  18.    - Copying the necessary binaries
  19.    - Use chroot
  20.    - Dependencies ** sys ssh rssh **
  21.        
  22.    Sets the type of OS and takes the route of the binary
  23.    using script Written by nixCraft <http://www.cyberciti.biz/tips/>
  24.    (c) 2006 nixCraft under GNU GPL v2.0+.tanks you nixCraft :).
  25.    
  26.    """
  27.    
  28.     def setJail(self):
  29.         """ Set of directory  
  30.            
  31.        """
  32.         os.system('groupadd userjaula')
  33.         #tomo el parametro de user_available.conf
  34.         os.system('useradd -g userjaula -s /bin/false -d /dev/null userjaula')
  35.  
  36.         userjaula = 'aqui el usuario o los usuarios separados por coma'
  37.  
  38.  
  39.         adduseagroup = 'gpasswd -M ' + userjaula + ' userjaula'
  40.         os.system(adduseagroup)
  41.         userjaula2=userjaula.replace(","," ")
  42.         os.system('mkdir -p /var/www/userjaula/')
  43.         os.system('chown root.userjaula /var/www/userjaula/')
  44.         os.system('chmod 710 /var/www/userjaula/')
  45.         paths=os.system('pwd')
  46.         paths = subprocess.Popen(['pwd'],
  47.             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  48.         out, error = paths.communicate()
  49.         output_split_line = out.split('\n')
  50.         os.chdir('/var/www/userjaula/')
  51.         os.system('mkdir -p dev etc lib usr bin home var')
  52.         os.chdir('/var/www/userjaula/home/')
  53.         os.system('mkdir -p '+usersuserjaula2)
  54.         os.chdir('/var/www/userjaula/var/')
  55.         os.system('mkdir -p log')
  56.         os.chdir('/var/www/userjaula/usr/')
  57.         os.system('mkdir -p bin')
  58.        
  59.         os.system('mknod -m 666 /var/www/userjaula/dev/null c 1 3')
  60.         os.system('cp -R /etc/ld* /var/www/userjaula/etc/')
  61.         os.system('cp /etc/nsswitch.conf /var/userjaula/etc/')
  62.         os.system('cp /etc/hosts /var/www/userjaula/etc')
  63.         ## !! no integrar lko que no se decee
  64.         os.system('cp /usr/bin/rssh /var/www/userjaula/usr/bin/')
  65.         os.system('cp /bin/bash /var/www/userjaula/bin/')
  66.         os.system('chown -R userjaula /var/www/userjaula/*')
  67.         os.system('chgrp -R userjaula /var/www/userjaula/*')
  68.         os.system('chmod -R  710 /var/www/userjaula/*')
  69.         os.system('chown '+usersuserjaula2+ ' /var/www/userjaula/home/'+userjaula2)
  70.         os.system('chmod -R  710 /var/www/userjaula/home/'+userjaula2)
  71.         os.chdir(output_split_line[0])
  72.         os.system('pwd')
  73.         # !!! cambiar la ruta al sshd_config del sisema
  74.         f = open ("/etc/ssh/sshd_config", "a")
  75.         f.write("""\n# Set Jail
  76. Match group userjaula
  77.    ChrootDirectory /var/www/userjaula/
  78.    X11Forwarding no
  79.    AllowTcpForwarding no \n""")
  80.         f.close()      
  81.         os.system("sh l2chroot.sh /bin/bash")
  82.    
  83.     #!! REINICIAR EL DEMONIO SSH LUEGO DE INSTALR O NO FUNCIONA LA JAULA
  84.  
  85. # instantiate
  86. if __name__ == "__main__":
  87.     print "Is not shown if import"
  88.     jail = MakeJailX()
  89.     jail.setJail()
  90.     print " enjoy "

Guardas este script en el mismo lugar que el anterior con este nombre --> l2chroot.sh
Código bash:
Ver original
  1. #!/bin/bash
  2. # Use this script to copy shared (libs) files to Apache/Lighttpd chrooted
  3. # jail server.
  4. # ----------------------------------------------------------------------------
  5. # Written by nixCraft <http://www.cyberciti.biz/tips/>
  6. # (c) 2006 nixCraft under GNU GPL v2.0+
  7. # + Added ld-linux support
  8. # + Added error checking support
  9. # ------------------------------------------------------------------------------
  10. # See url for usage:
  11. # http://www.cyberciti.biz/tips/howto-setup-lighttpd-php-mysql-chrooted-jail.html
  12. # -------------------------------------------------------------------------------
  13. # Set CHROOT directory name
  14. BASE="/var/www/userjaula"
  15.  
  16. if [ $# -eq 0 ]; then
  17.   echo "Syntax : $0 /path/to/executable"
  18.   echo "Example: $0 /usr/bin/php5-cgi"
  19.   exit 1
  20. fi
  21.  
  22. [ ! -d $BASE ] && mkdir -p $BASE || :
  23.  
  24. # iggy ld-linux* file as it is not shared one
  25. FILES="$(ldd $1 | awk '{ print $3 }' |egrep -v ^'\(')"
  26.  
  27. echo "Copying shared files/libs to $BASE..."
  28. for i in $FILES
  29. do
  30.   d="$(dirname $i)"
  31.   [ ! -d $BASE$d ] && mkdir -p $BASE$d || :
  32.   /bin/cp $i $BASE$d
  33. done
  34.  
  35. # copy /lib/ld-linux* or /lib64/ld-linux* to $BASE/$sldlsubdir
  36. # get ld-linux full file location
  37. sldl="$(ldd $1 | grep 'ld-linux' | awk '{ print $1}')"
  38. # now get sub-dir
  39. sldlsubdir="$(dirname $sldl)"
  40.  
  41. if [ ! -f $BASE$sldl ];
  42. then
  43.   echo "Copying $sldl $BASE$sldlsubdir..."
  44.   /bin/cp $sldl $BASE$sldlsubdir
  45. else
  46.   :
  47. fi

tal vez tenga cosas que no te sirvan, por lo que te toca modificarlo, si tienes algún problema me avisas.

Con ese script el usuario solo tiene acceso a sftp

Última edición por mcun; 23/04/2012 a las 10:55 Razón: corregir