Curiosidades, dicas e notícias de Linux, Windows e muito mais!



Criando um Servidor SFTP com CHROOT

Publicado em 29/7/2009 | Autor Diogo Salgueiro


Surgiu a necessidade de colocar o SFTP com CHROOT para troca de arquivos segura. O CHROOT é uma técnica que libera conexões SFTP sem ter que abrir toda a estrutura de diretórios para o usuário que se conecta só para transferir arquivos. Pesquisei na internet encontrei o scponly e irei mostrar a vocês como utilizá-lo.

Para este procedimento estou usando Centos 5.3. Algumas distribuições já há pacotes rpm/deb.

Vamos a instalação:

Depois de efetuar o download entre na pasta com que contém o arquivo tgz.

Descompatando o arquivo:
# tar -zxvf scponly-4.6.tgz

# cd scponly-4.6

#./configure –prefix=/  –enable-chrooted-binary
checking build system type… x86_64-unknown-linux-gnu
checking host system type… x86_64-unknown-linux-gnu
checking for gcc… gcc
checking for C compiler default output file name… a.out
checking whether the C compiler works… yes
checking whether we are cross compiling… no
checking for suffix of executables…
….
configure: creating ./config.status
config.status: creating Makefile
config.status: creating setup_chroot.sh
config.status: creating config.h

# make
gcc -g -O2 -I. -I. -DHAVE_CONFIG_H -DDEBUGFILE=’”//etc/scponly/debuglevel”‘ -o scponly.o -c scponly.c
gcc -g -O2 -I. -I. -DHAVE_CONFIG_H -DDEBUGFILE=’”//etc/scponly/debuglevel”‘ -o helper.o -c helper.c
gcc -g -O2 -I. -I. -DHAVE_CONFIG_H -DDEBUGFILE=’”//etc/scponly/debuglevel”‘ -o scponly scponly.o helper.o
gcc -g -O2 -I. -I. -DHAVE_CONFIG_H -DDEBUGFILE=’”//etc/scponly/debuglevel”‘ -o groups groups.c


#make install
echo “0″ > debuglevel
/usr/bin/install -c -d //bin
/usr/bin/install -c -d //man/man8
/usr/bin/install -c -d //etc/scponly
/usr/bin/install -c -o 0 -g 0 scponly //bin/scponly
/usr/bin/install -c -o 0 -g 0 -m 0644 scponly.8 //man/man8/scponly.8
/usr/bin/install -c -o 0 -g 0 -m 0644 debuglevel //etc/scponly/debuglevel
if test “xscponlyc” != “x”; then            \
/usr/bin/install -c -d //sbin;                \
rm -f //sbin/scponlyc;            \
cp scponly scponlyc;                \
/usr/bin/install -c -o 0 -g 0 -m 4755 scponlyc //sbin/scponlyc;    \
fi

Por padrão em algumas distros já vem configurada o arquivo /etc/ssh/sshd_config para aceitar conexões sftp então verifique este o arquivo /etc/ssh/sshd_config e procure uma entrada +- assim:

Subsystem    sftp    /usr/libexec/openssh/sftp-server

Se ainda não tiver adicione uma.

Agora vamos criar a estrutura que servirá como base de diretório para os nossos novos usuários SFTP:
Entre na pasta com o código fonte do scponly

# cd scponly-4.6

# chmod +x setup_chroot.sh

# ./setup_chroot.sh

Next we need to set the home directory for this scponly user.
please note that the user’s home directory MUST NOT be writeable
by the scponly user. this is important so that the scponly user
cannot subvert the .ssh configuration parameters.

for this reason, a writeable subdirectory will be created that
the scponly user can write into.

Username to install [scponly] default
home directory you wish to set for this user [/home/default]
name of the writeable subdirectory [incoming]entrada
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.

creating  /home/default/entrada directory for uploading files

Your platform (Linux) does not have a platform specific setup script.
This install script will attempt a best guess.
If you perform customizations, please consider sending me your changes.
Look to the templates in build_extras/arch.
- joe at sublimation dot org

please set the password for default:
Changing password for user default.

New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
if you experience a warning with winscp regarding groups, please install
the provided hacked out fake groups program into your chroot, like so:
cp groups /home/default/bin/groups

Agora configuraremos a estrutura de chroot para os nossos usuários novos, para facilitar esta tarefa e manter a criação correta da estrutura eu não utilizaremos mais  o script que acompanha o código fonte, criaremos um novo script.

Crie um novo arquivo chamado /usr/bin/cria_usuario_sftp.sh com o seguinte conteúdo.

#!/bin/bash
if [ $1 ];then
HM=/home/$1
adduser -d $HM -k /home/default -m -s /sbin/scponlyc $1
echo “Configure a senha para o usuário $1″
passwd $1
echo “Criando o /dev/null”
mkdir -p $HM/dev
cd $HM/dev
rm -f null
mknod null c 1 3
chmod 666 null

if [ /lib64/ld-linux-x86-64.so.2 ];then
cp /lib64/ld-linux-x86-64.so.2 $HM/lib64
fi
if [ /lib/ld-linux-x86-64.so.2 ];then
cp /lib/ld-linux.so.2 $HM/lib
fi

chown root. $HM
chmod 755 $HM
cd $HM
chown root. bin  dev  etc  lib  lib64  usr

else
echo “Favor informar o nome do usuario $0 nome_usuario”
fi

Agora iremos dar a permissão de execução ao arquivo.

# chmod +x /usr/bin/cria_usuario_sftp.sh

Já podemos adicionar os novos usuários que teram permissão de utilizar o SFTP.

# cria_usuario_sftp.sh user1
Configure a senha para o usuário user1
Changing password for user user1.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
Criando o /dev/null

Agora vamos ao teste de conexão:

# sftp user1@192.168.1.133
Connecting to 192.168.1.133…
user1@testesmb’s password:
sftp> ls -l
drwxr-xr-x    2 0        0            4096 Jul 28 18:02 bin
drwxr-xr-x    2 0        0            4096 Jul 28 18:02 dev
drwxr-xr-x    2 1342     1342         4096 Jul 28 18:02 entrada
drwxr-xr-x    2 0        0            4096 Jul 28 18:02 etc
drwxr-xr-x    2 0        0            4096 Jul 28 18:02 lib
drwxr-xr-x    2 0        0            4096 Jul 28 18:02 lib64
drwxr-xr-x    5 0        0            4096 Jul 28 18:02 usr

Pronto agora temos um servidor com SFTP com CHROOT.

Algumas considerações importantes:

- A porta de conexão do SFTP é 22 mesma do SSH e não a porta 21 do FTP.
- O usuário criado para o SFTP não consegue acessar a máquina utilizando o SSH.

Comments

One Response to “Criando um Servidor SFTP com CHROOT”

  1. Vicente via Rec6
    July 29th, 2009 @ 12:55
    Criando um Servidor SFTP com CHROOT – Blog do Vicente…

    leia mais……

Deixe um comentário!





CommentLuv Enabled
  • Assine nossa lista: