碳基体

奋斗在产品安全第一线的安全妹子

Debian 上搭建Tomcat + JSP

自打有了VPS,就开始搭建各种类型的网站,来实验web漏洞。以研究apache/nginx+php居多,其实tomcat+JSP也是主流的,各大电子商务网站,政府网站都是JSP的常客。悲催的Struts 2漏洞就寄居于该框架。 所以很有必要熟悉熟悉。平时爱将搭建过程记录在evernote上,今天抽时间整理一下发到博客上,非常基础的东西。


第一步:安装jdk与tomcat7

apt-get install openjdk-7-jdk

apt-get install tomcat7


安装后,有如下重要文件(默认路径)

(1)tomcat7配置文件

/etc/tomcat7/server.xml

/etc/tomcat7/web.xml

/etc/tomcat7/tomcat-user.xml


(2)tomcat7日志文件

/var/log/tomcat7


(3)tomcat7 web默认根目录文件

/var/lib/tomcat7/webapps/ROOT/



第二步:修改配置文件


1.修改网站根目录docBase


vim /etc/tomcat7/server.xml

在<host> </host>之间添加

<Context path="" docBase="/var/www/xxx/(网站路径)" debug="0" reloadable="true" />


2.修改端口port

 <Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               URIEncoding="UTF-8"
               redirectPort="8443" />


2015.11.12补充:

如果绑定端口小于1023(比如说80),你还需要

apt-get install authbind


vim /etc/default/tomcat7 

修改

AUTHBIND=yes


sudo touch /etc/authbind/byport/80
sudo chmod 500 /etc/authbind/byport/80
sudo chown tomcat7 /etc/authbind/byport/80

参考:https://stackoverflow.com/questions/23272666/tomcat7-bind-to-port-80-fails-in-ubuntu-14-04lts



3.修改日志格式pattern


我喜欢统一各种server的日志格式,以便可以使用统一的脚本分析日志。


具体的日志格式参考https://tomcat.apache.org/tomcat-5.5-doc/config/valve.html


  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %v %u %t &quot;%r&quot; %s %b &quot;%{Referer}i&quot; &quot;%{User-Ag
ent}i&quot;" resolveHosts="true"
/>


以下是这种格式的日志样例

211.138.23.98 www.tanjiti.com - [22/Oct/2013:05:46:34 -0700] "GET /index.jsp HTTP/1.1" 200 129 "https://www.baidu.com" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"



注意pattern部分,具体意思如下

%h Remote host name (or IP address if resolveHosts is false) --示例中的211.138.23.98

%v Local server name --示例中的 www.tanjiti.com

%u Remote user that was authenticated (if any), else '-'  --示例中的-

%t Date and time, in Common Log Format  --示例中的[22/Oct/2013:05:46:34 -0700]

%r First line of the request (method and request URI) --示例中的GET /index.jsp HTTP/1.1

%s HTTP status code of the response --示例中的200

%b Bytes sent, excluding HTTP headers, or '-' if zero --示例中的129

%{Referer}i  HTTP Referer --示例中的https://www.baidu.com

%{User-Agent}i HTTP UserAgent --示例中的Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0



第三步:启动tomcat

tomcat7启动/重启

/etc/init.d/tomcat7 start/restart/stop



第四步:测试tomcat

vim index.jsp


<html>
<head>
<title> My First JSP Page </title>
</head>
<body>
<H3>Today is:
<%= new java.util.Date() %>
</H3>
</body>
</html>


curl -i https://www.tanjiti.com/index.jsp
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=3674979740E75B9C326CFCBD0C896E7C; Path=/; HttpOnly
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 129
Date: Mon, 27 Jan 2014 08:26:42 GMT

<html>
<head>
<title> My First JSP Page </title>
</head>
<body>
<H3>Today is:
Mon Jan 27 00:26:42 PST 2014
</H3>
</body>
</html>



来源:碳基体

评论