当前位置: > Linux服务器 > Tomcat >

加固你的TOMCAT,让TOMCAT在chroot的环境下运行

时间:2014-12-25 17:02来源:linux.it.net.cn 作者:IT

众所周知,chroot下linux系统的一个安全机制,chroot是linux内核的一个系统调用,通过它,可以设定应用软件的运行环境,让应用软件运行在一个特定目录下,这样,即使应用软件有安全漏洞,被入侵,入侵者也被限制在一个特定的目录,从面限制了入侵者的破坏范围。加固了系统的安全性。本文以tomcat为例,详细记录了tomcat以chroot的方式运行的配置过程,也记录配置过程中的出错及排错方法。 

 

环境及工具:系统64位的CentOS6.4、dk为jdk-7u45-linux-x64.tar.gz、apache-tomcat-6.0.41.tar.gz

 

 

一、配置java chroot环境 

1、先配置jdk,这次配置使用的是 jdk-7u45-linux-x64.tar.gz 

tar zxvf jdk-7u45-linux-x64.tar.gz 

mkdir /usr/java 

cp -a jdk1.7.0_45 /usr/java/ 

[root@2core local]# /usr/java/jdk1.7.0_45/bin/java -version 

java version "1.7.0_45" 

Java(TM) SE Runtime Environment (build 1.7.0_45-b18) 

Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode) 

 

jdk可以正常启动 

 

2、配置jdk chroot 

我选算了 /chroot为 tomcat的根目录 

#D=/chroot 

#mkdir -p $D 

#cd $D 

mkdir -p lib lib64 etc tmp dev usr 

chmod 755 etc dev usr 

chmod 1777 tmp 

cp -a /etc/hosts etc/hosts 

 

为了chroot的环境更接近实际的系统根目录,还需要一个特殊的目录,如果无这些目录,将来可能会报错 

mkdir -p /chroot/dev/pts 

cd /dev 

./MAKEDEV -d /chroot/dev null radom urandom zero loop* log console 

cp MAKEDEV /chroot/dev 

cp -a /dev/shm /chroot/dev 

 

[root@2core local]# ldd /usr/java/jdk1.7.0_45/bin/java 

linux-vdso.so.1 => (0x00007fffeafd0000) 

libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f3a58efd000) 

libjli.so => /usr/java/jdk1.7.0_45/bin/../lib/amd64/jli/libjli.so (0x00007f3a58ce5000) 

libdl.so.2 => /lib64/libdl.so.2 (0x00007f3a58ae1000) 

libc.so.6 => /lib64/libc.so.6 (0x00007f3a5874e000) 

/lib64/ld-linux-x86-64.so.2 (0x00007f3a59123000) 

把上面几个库文件复制到/chroot/lib64/目录下 

[root@2core local]# ls /chroot/lib64/ 

ld-linux-x86-64.so.2 libc.so.6 libdl.so.2 libpthread.so.0 

 

 

[root@2core local]# rm -rf /chroot/usr/java/ 

[root@2core local]# mkdir /chroot/usr/java 

[root@2core local]# cp -a /usr/java/jdk1.7.0_45 /chroot/usr/java/ 

[root@2core local]# ls /chroot/usr/java/ 

jdk1.7.0_45 

 

以chroot方式运行java, 

[root@2core local]# chroot /chroot /usr/java/jdk1.7.0_45/bin/java 

/usr/java/jdk1.7.0_45/bin/java: error while loading shared libraries: libjli.so: cannot open shared object file: No such file or directory 

有报错,根据错误可以是缺少相应的库文件,只要把这些文件复制过来就OK了 

[root@2core local]# find / -name libjli.so 

/usr/java/jdk1.7.0_45/jre/lib/amd64/jli/libjli.so 

/usr/java/jdk1.7.0_45/lib/amd64/jli/libjli.so 

/usr/local/jdk1.7.0_45/jre/lib/amd64/jli/libjli.so 

/usr/local/jdk1.7.0_45/lib/amd64/jli/libjli.so 

/chroot/usr/java/jdk1.7.0_45/jre/lib/amd64/jli/libjli.so 

/chroot/usr/java/jdk1.7.0_45/lib/amd64/jli/libjli.so 

[root@2core local]# cp /chroot/usr/java/jdk1.7.0_45/lib/amd64/jli/libjli.so /chroot/lib64/ 

 

[root@2core local]# chroot /chroot /usr/java/jdk1.7.0_45/bin/java -version 

Error: dl failure on line 863 

Error: failed /usr/java/jdk1.7.0_45/jre/lib/amd64/server/libjvm.so, because libm.so.6: cannot open shared object file: No such file or directory 

[root@2core local]# 

[root@2core local]# 

[root@2core local]# find / -name libm.so.6 

/lib64/libm.so.6 

[root@2core local]# cp /lib64/libm.so.6 /chroot/lib64/ 

[root@2core local]# chroot /chroot /usr/java/jdk1.7.0_45/bin/java -version 

Java HotSpot(TM) 64-Bit Server VM warning: Can't detect initial thread stack location - find_vma failed 

java version "1.7.0_45" 

Java(TM) SE Runtime Environment (build 1.7.0_45-b18) 

Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode) 

 

这样,可以正常启动java了,但还是有一个警告信息,这是因为jave检测不到相关进程引起的,而linux系统的进程信息是存放在/proc这个目录的,因些,我们还要在/chroot下挂载这个特殊的目录,方法如下 

 

[root@2core local]# mkdir /chroot/proc 

[root@2core local]# mount -t proc proc /chroot/proc 

[root@2core local]# chroot /chroot /usr/java/jdk1.7.0_45/bin/java -version 

java version "1.7.0_45" 

Java(TM) SE Runtime Environment (build 1.7.0_45-b18) 

Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode) 

 

至此,jdk才算完成配置,这时,让我们查看一下启动JDK需要用到下面几个库,需要注意的是,库文件的位置及名字有可能在不同的版本系统中有差别,但一般都可以根据相关的报错信息,找到相应库文件,并复制过来就OK了 

[root@2core local]# ls /chroot/lib64/ 

ld-linux-x86-64.so.2 libc.so.6 libdl.so.2 libjli.so libm.so.6 libpthread.so.0 

 

 

 

二、以下开始配置tomcat了,把tomcat将在/chroot/usr/local这个目录下运行 

[root@2core local]# mkdir /chroot/usr/local 

[root@2core local]# mv apache-tomcat-6.0.41-src /chroot/usr/local/tomcat 

 

[root@2core local]# chroot /chroot /usr/local/tomcat/bin/catalina.sh start 

chroot: failed to run command `/usr/local/tomcat/bin/catalina.sh': Permission denied 

 

[root@2core local]# ls /chroot/usr/local/tomcat/bin/catalina.sh -al 

-rw-r--r--. 1 root root 17717 5月 19 18:51 /chroot/usr/local/tomcat/bin/catalina.sh 

 

[root@2core local]# chmod 755 /chroot/usr/local 

[root@2core local]# chmod 755 /chroot/usr/local/tomcat/bin/*.sh 

[root@2core local]# chroot /chroot /usr/local/tomcat/bin/catalina.sh start 

chroot: failed to run command `/usr/local/tomcat/bin/catalina.sh': No such file or directory 

再次[root@2core local]# strace chroot /chroot /usr/local/tomcat/bin/catalina.sh start 

留意末部信息 

execve("/usr/local/tomcat/bin/catalina.sh", ["/usr/local/tomcat/bin/catalina.s"..., "start"], [/* 25 vars */]) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/locale.alias", O_RDONLY) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/zh_CN.UTF-8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/zh_CN.utf8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/zh_CN/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/zh.UTF-8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/zh.utf8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/zh/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

write(2, "chroot: ", 8chroot: ) = 8 

write(2, "failed to run command `/usr/loca"..., 57failed to run command `/usr/local/tomcat/bin/catalina.sh') = 57 

open("/usr/share/locale/zh_CN.UTF-8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/zh_CN.utf8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/zh_CN/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/zh.UTF-8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/zh.utf8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

open("/usr/share/locale/zh/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

write(2, ": No such file or directory", 27: No such file or directory) = 27 

write(2, "\n", 1 

) = 1 

close(1) = 0 

close(2) = 0 

exit_group(127) = ? 

 

因为启动tomcat的是一个shell的脚本,因此,在chroot里还需要一个运行bash shell的环境 

 

[root@2core local]# cd /chroot/ 

[root@2core chroot]# mkdir -p bin 

[root@2core chroot]# cp /bin/bash bin/ 

[root@2core chroot]# ln -s /bin/bash bin/sh 

[root@2core chroot]# cd lib64 

[root@2core lib64]# pwd 

/chroot/lib64 

[root@2core lib64]# ldd /bin/bash 

linux-vdso.so.1 => (0x00007fff2536e000) 

libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f0c47bb5000) 

libdl.so.2 => /lib64/libdl.so.2 (0x00007f0c479b1000) 

libc.so.6 => /lib64/libc.so.6 (0x00007f0c4761d000) 

/lib64/ld-linux-x86-64.so.2 (0x00007f0c47ddf000) 

[root@2core lib64]# cp /lib64/libtinfo.so.5 /chroot/lib64/ 

[root@2core lib64]# cp /lib64/libdl.so.2 /chroot/lib64/ 

cp:是否覆盖"/chroot/lib64/libdl.so.2"? n 

[root@2core lib64]# cp /lib64/libc.so.6 /chroot/lib64/ 

cp:是否覆盖"/chroot/lib64/libc.so.6"? n 

[root@2core lib64]# cp /lib64/ld-linux-x86-64.so.2 /chroot/lib64/ 

cp:是否覆盖"/chroot/lib64/ld-linux-x86-64.so.2"? n 

测试 bash是否可以在chroot下正常运行,由于这前已经复制部分的库,所以会提示是否覆盖 

[root@2core lib64]# chroot /chroot /bin/bash 

bash-4.1# pwd 

注意,这时的bash shell提示符已经改变了,这说明已经可以在chroot下正常启动bash了 

bash-4.1# ls 

bash: ls: command not found 

bash-4.1# exit 

exit 

[root@2core lib64]# 

再次启动tomcat,报错的内容已经不一样了,报错的内容很详细,就是有几个命令找不到,那么,我们把这些命令及相应的库复制到chroot的相应目录即可 

[root@2core lib64]# chroot /chroot /usr/local/tomcat/bin/catalina.sh start 

/usr/local/tomcat/bin/catalina.sh: line 89: uname: command not found 

/usr/local/tomcat/bin/catalina.sh: line 109: dirname: command not found 

Cannot find //bin/setclasspath.sh 

This file is needed to run this program 

[root@2core lib64]# 

 

[root@2core lib64]# cp /bin/uname /chroot/bin/ 

[root@2core lib64]# mkdir - /chroot/usr/bin 

[root@2core lib64]# cp /usr/bin/dirname /chroot/usr/bin/ 

[root@2core lib64]# ldd /bin/uname 

linux-vdso.so.1 => (0x00007fff4b5ff000) 

libc.so.6 => /lib64/libc.so.6 (0x00007fcfde5b8000) 

/lib64/ld-linux-x86-64.so.2 (0x00007fcfde954000) 

[root@2core lib64]# ldd /usr/bin/dirname 

linux-vdso.so.1 => (0x00007fffb93ea000) 

libc.so.6 => /lib64/libc.so.6 (0x00007f8ad0266000) 

/lib64/ld-linux-x86-64.so.2 (0x00007f8ad0602000) 

[root@2core lib64]# ls /chroot/lib64 

- ld-linux-x86-64.so.2 libc.so.6 libdl.so.2 libjli.so libm.so.6 libpthread.so.0 libtinfo.so.5 

相关的库之前已经复制了,所以,这里就不需要再复制,再次运行comcat 

[root@2core lib64]# chroot /chroot /usr/local/tomcat/bin/catalina.sh start 

Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 

At least one of these environment variable is needed to run this program 

[root@2core lib64]# 

这次报错的是环境变量的问题,把变量加进去限可,我们先看看catalina.sh这个文件,里面调用了setclasspath.sh 而报错的内容便在setclasspath.sh脚本里。因此,我在setclasspath.sh设置JAVA_HOME变量 

[root@2core bin]# vi /chroot/usr/local/tomcat/bin/setclasspath.sh 

 

# Make sure prerequisite environment variables are set 

export JAVA_HOME=/usr/java/jdk1.7.0_45 

export JRE_HOME=/usr/java/jdk1.7.0_45/jre 

if [ -z "$JAVA_HOME" -a -z "$JRE_HOME" ]; then 

  if $darwin; then 

    # Bugzilla 54390 

    if [ -x '/usr/libexec/java_home' ] ; then 

      export JAVA_HOME=`/usr/libexec/java_home` 

    # Bugzilla 37284 (reviewed). 

    elif [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home" ]; then 

      export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home" 

    fi 

  else 

    JAVA_PATH=`which java 2>/dev/null` 

    if [ "x$JAVA_PATH" != "x" ]; then 

      JAVA_PATH=`dirname $JAVA_PATH 2>/dev/null` 

      JRE_HOME=`dirname $JAVA_PATH 2>/dev/null` 

    fi 

    if [ "x$JRE_HOME" = "x" ]; then 

      # XXX: Should we try other locations? 

      if [ -x /usr/bin/java ]; then 

        JRE_HOME=/usr 

      fi 

    fi 

  fi 

  if [ -z "$JAVA_HOME" -a -z "$JRE_HOME" ]; then 

    echo "Neither the JAVA_HOME nor the JRE_HOME environment variable is defined" 

    echo "At least one of these environment variable is needed to run this program" 

    exit 1 

  fi 

fi 

"setclasspath.sh" 119L, 4252C written 

 

 

再次运行,还是出错,但已经接近成功了, 

[root@2core bin]# chroot /chroot /usr/local/tomcat/bin/catalina.sh start 

/usr/local/tomcat/bin/catalina.sh: line 193: tty: command not found 

Using CATALINA_BASE: /usr/local/tomcat 

Using CATALINA_HOME: /usr/local/tomcat 

Using CATALINA_TMPDIR: /usr/local/tomcat/temp 

Using JRE_HOME: /usr/java/jdk1.7.0_45/jre 

Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar 

/usr/local/tomcat/bin/catalina.sh: line 354: touch: command not found 

/usr/local/tomcat/bin/catalina.sh: line 371: /usr/local/tomcat/logs/catalina.out: No such file or directory 

报错还是由相关系统命令调用及文件权限的引用的,打它复制过来 

[root@2core bin]# cp /bin/touch /chroot/bin/ 

[root@2core bin]# ldd /bin/touch 

linux-vdso.so.1 => (0x00007fff9343f000) 

librt.so.1 => /lib64/librt.so.1 (0x00007fbd55ccc000) 

libc.so.6 => /lib64/libc.so.6 (0x00007fbd55939000) 

libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fbd5571b000) 

/lib64/ld-linux-x86-64.so.2 (0x00007fbd55edd000) 

[root@2core bin]# cp /lib64/librt.so.1 /chroot/lib64/ 

 

[root@2core bin]# mkdir /chroot/usr/local/tomcat/logs 

[root@2core bin]# chmod 666 /chroot/usr/local/tomcat/logs 

[root@2core bin]# 

 

[root@2core bin]# chroot /chroot /usr/local/tomcat/bin/catalina.sh start 

/usr/local/tomcat/bin/catalina.sh: line 193: tty: command not found 

Using CATALINA_BASE: /usr/local/tomcat 

Using CATALINA_HOME: /usr/local/tomcat 

Using CATALINA_TMPDIR: /usr/local/tomcat/temp 

Using JRE_HOME: /usr/java/jdk1.7.0_45/jre 

Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar 

 

把tty这个程序复制过来 

[root@2core bin]# cp /usr/bin/tty /chroot/usr/bin/ 

[root@2core bin]# ldd /usr/bin/tty 

linux-vdso.so.1 => (0x00007fff1f5ff000) 

libc.so.6 => /lib64/libc.so.6 (0x00007f82f2cd9000) 

/lib64/ld-linux-x86-64.so.2 (0x00007f82f3075000) 

 

[root@2core bin]# chroot /chroot /usr/local/tomcat/bin/catalina.sh start 

Using CATALINA_BASE: /usr/local/tomcat 

Using CATALINA_HOME: /usr/local/tomcat 

Using CATALINA_TMPDIR: /usr/local/tomcat/temp 

Using JRE_HOME: /usr/java/jdk1.7.0_45/jre 

Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar 

 

终于无报错了,大功造成了?怎么回事,java 进程还是起不来 

[root@2core bin]# ps auxf|grep java 

root 1449 0.0 0.0 103240 852 pts/1 S+ 11:46 0:00 \_ grep java 

查看下了上tomcat的日志 

[root@2core local]# more /chroot/usr/local/tomcat/logs/catalina.out 

Error: Could not find or load main class org.apache.catalina.startup.Bootstrap 

[root@2core local]# ls /chroot/usr/local/tomcat/bin/bootstrap.jar 

ls: 无法访问/chroot/usr/local/tomcat/bin/bootstrap.jar: 没有那个文件或目录 

原来是这个包缺少一个文件 

重新从官方网站下载了一个完整的包,解压 

 #wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-6/v6.0.41/bin/apache-tomcat-6.0.41.tar.gz

[root@2core local]# cd /chroot/usr/local/ 

[root@2core local]# ls 

tomcat 

[root@2core local]# mv tomcat tomcat.bak 

[root@2core local]# mv /usr/local/apache-tomcat-6.0.41 ./tomcat 

并在/chroot/usr/local/tomcat/bin/setclasspath.sh加入环境变量 

[root@2core bin]# vi /chroot/usr/local/tomcat/bin/setclasspath.sh 

 

# Make sure prerequisite environment variables are set 

export JAVA_HOME=/usr/java/jdk1.7.0_45 

export JRE_HOME=/usr/java/jdk1.7.0_45/jre 

再次运行 

[root@2core local]# chroot /chroot /usr/local/tomcat/bin/catalina.sh start 

Using CATALINA_BASE: /usr/local/tomcat 

Using CATALINA_HOME: /usr/local/tomcat 

Using CATALINA_TMPDIR: /usr/local/tomcat/temp 

Using JRE_HOME: /usr/java/jdk1.7.0_45/jre 

Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar 

[root@2core local]# ps auxf|grep java 

root 8220 0.0 0.0 103240 852 pts/0 S+ 10:19 0:00 \_ grep java 

root 8201 179 6.7 1443396 68980 pts/0 Sl 10:19 0:05 /usr/java/jdk1.7.0_45/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start 

 

终于成功了!把iptables的TCP8080端口打开,便可以通过http://ip:8080访问了




(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容