forked from AKSarav/SecureTomcatJDBC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecureTomcatJDBC.sh
executable file
·144 lines (115 loc) · 4.55 KB
/
SecureTomcatJDBC.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
# Author: SaravAK ([email protected])
#
#
BASE_DIR=`dirname $0`
LOGFILE=$BASE_DIR/SecureTomDB-Exec.log
INFOFILE=$BASE_DIR/TomcatInfo.properties
JAVA_ENC_FILE=EncDecJDBCPass.java
BAK_JAVA_ENC_FILE=EncryptJDBCPassword.java-Original
CLASS_ENC_FILE=EncDecJDBCPass.class
JAVA_DS_FILE=SecureTomcatDataSourceImpl.java
CLASS_DS_FILE=SecureTomcatDataSourceImpl.class
rm -f $INFOFILE
rm -f $BASEDIR/*.class
echo "Enter the Tomcat Instance CATALINA_HOME ( A Parent Directory of conf/ bin/ webapps/ )"
read InstanceDir
if [ -e $InstanceDir ]
then
if [ -e $InstanceDir/bin/version.sh -a -x $InstanceDir/bin/version.sh ]
then
$InstanceDir/bin/version.sh > $INFOFILE
else
echo "ERROR: Unable to find the Version.sh under $InstanceDir/bin [OR] Execute Permission is Not Set"
exit 9
fi
fi
cd $BASE_DIR
echo -e "\n"
if [ -e $INFOFILE -a ! -z $INFOFILE ]
then
grep -i "Server Version" $INFOFILE
grep -i "JVM Version" $INFOFILE
egrep -i "JAVA|JRE" $INFOFILE
grep -i "CATALINA_HOME" $INFOFILE
grep -i "CLASSPATH" $INFOFILE
fi
JAVA_HOME=`egrep -i "JAVA|JRE" $INFOFILE|awk '{print $3}'`
if [ -e $JAVA_HOME/bin/javac -a -e $JAVA_HOME/bin/java -a -e $JAVA_HOME/bin/jar ]
then
echo "INFO: Java Home Validation Successful. Good to Go"
else
echo "ERROR: Java Home Does not seem to be having either JAVAC or JAVA or JAR command."
echo -e "\n Trying to Obtain JAVA_HOME during runtime"
echo "Enter the JAVA_HOME:"
read JAVA_HOME_IN
if [ -e $JAVA_HOME_IN/bin/javac -a -e $JAVA_HOME_IN/bin/java -a -e $JAVA_HOME_IN/bin/jar ]
then
echo "INFO: Java Home Validation Successful - RUNTIME. Good to Go"
else
echo "I am Sorry the Given JAVA_HOME does not seem to having JAVAC or JAVA or JAR command either"
echo "If you feel there is a BUG. Please write email to my author [email protected]"
fi
fi
JULI_JAR_LOC=$InstanceDir/bin/tomcat-juli.jar
JDBC_JAR_LOC=$InstanceDir/lib/tomcat-jdbc.jar
echo -e "\n"
echo "INFO: Vaidating the Tomcat Juli and Tomcat JDBC Jar files availability"
if [ -e $InstanceDir/bin/tomcat-juli.jar -a -e $InstanceDir/lib/tomcat-jdbc.jar ]
then
echo "INFO: Jar files are present. Good to Go"
else
echo "ERROR: Unable to find the Jar files $InstanceDir/bin/tomcat-juli.jar and $InstanceDir/bin/tomcat-jdbc.jar"
exit 10
fi
echo "Enter the Password to Encrypt"
read -s passwordtoencrypt
echo "Enter the Secret PassPhrase"
read -s secretphrase
cp $JAVA_ENC_FILE $BAK_JAVA_ENC_FILE
if [ $? -ne 0 ]
then
echo "ERROR: failed to take backup of $JAVA_ENC_FILE"
fi
if [ $secretphrase != "" -o $passwordtoencrypt != "" ]
then
sed -e "s/PHRASETOREPLACE/$secretphrase/" EncDecJDBCPass.java > EncDecJDBCPass_temp.java && mv EncDecJDBCPass_temp.java EncDecJDBCPass.java
else
echo "ERROR: Either PassPhrase or the Password is Empty"
fi
echo "Creating the JAR module and Compiling the code"
$JAVA_HOME/bin/javac -cp $InstanceDir/lib/tomcat-jdbc.jar:$InstanceDir/bin/tomcat-juli.jar:. $JAVA_ENC_FILE && $JAVA_HOME/bin/javac -cp $InstanceDir/lib/tomcat-jdbc.jar:$InstanceDir/bin/tomcat-juli.jar:. $JAVA_DS_FILE
if [ $? -eq 0 ]
then
if [ ! -e $CLASS_ENC_FILE -o ! -e $CLASS_DS_FILE ]
then
echo "ERROR: Classfiles are not Created. Please check manually"
else
echo "Class files are created. Good to Go"
fi
else
echo "Class Compilation Errors Found. Please check manually"
exit 11
fi
echo "INFO: Creating a Jar file SecureTomcatJDBC.jar"
$JAVA_HOME/bin/jar -cvfM SecureTomcatJDBC.jar *.class META-INF
if [ $? -ne 0 -o ! -e SecureTomcatJDBC.jar ]
then
echo "ERROR: Jar Creation Failed"
else
echo "INFO: Jar file Creation Successful. Good to Go"
fi
echo -e "\nPassword Encryption Begins"
if [ `$JAVA_HOME/bin/java -jar SecureTomcatJDBC.jar|grep -i "^USAGE"|wc -l` -eq 1 ]
then
$JAVA_HOME/bin/java -jar SecureTomcatJDBC.jar $passwordtoencrypt
else
echo -e "ERROR: Unable to Encrypt the Password. Sorry. Please report this problem to my Creator at [email protected]"
fi
echo -e "Password Encryption Completed. Your Encrypted Password is displayed above"
cp $BAK_JAVA_ENC_FILE $JAVA_ENC_FILE
rm -f $BAK_JAVA_ENC_FILE
rm -rf $BASE_DIR/*.class
echo -e "\nNext Steps:\n 1) Copy the Generated SecureTomcatJDBC.jar into the $InstanceDir/lib directory\n 2) Replace the Factory element in Context.xml with factory=\"SecureTomcatDataSourceImpl\"\n 3) Replace the Encrypted Password in place of Clear Text Password password=\"ENCRYPTED PASSWORD\""
echo -e "For Any Questions about this tool read the product page https://www.middlewareinventory.com/blog/secure-tomcat-jdbc/. Leave a Comment there for any help"
echo -e "\nGood Bye. Thanks for using SecureTomcatJDBC Application"