Repository navigation
Expand file tree
/
Copy pathDatabase.java
More file actions
188 lines (169 loc) · 7.23 KB
/
Copy pathDatabase.java
File metadata and controls
188 lines (169 loc) · 7.23 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package dbProcs;
import java.sql.Connection;
import java.sql.DriverManager;
import org.apache.log4j.Logger;
/**
* Used to create database connections using the FileInputProperties.readfile method to gather property information
* Initiated by Getter.java, Setter.java
* <br/><br/>
* This file is part of the Security Shepherd Project.
*
* The Security Shepherd project is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.<br/>
*
* The Security Shepherd project is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.<br/>
*
* You should have received a copy of the GNU General Public License
* along with the Security Shepherd project. If not, see <http://www.gnu.org/licenses/>.
* @author Mark Denihan
*/
public class Database
{
private static org.apache.log4j.Logger log = Logger.getLogger(Database.class);
/**
* This method is used by the application to close an open connection to a database server
* @param conn The connection to close
*/
public static void closeConnection(Connection conn)
{
try
{
//log.debug("Closing database connection");
conn.close();
}
catch(Exception e)
{
log.error("Error closing connection:" + e.toString());
}
}
/**
* This method is used by the application to get a connection to the secure database sever based on the input path to a specific properties file.
* @param ApplicationRoot The running context of the application.
* @param path The path to the properties file to use for this connection. this is filtered for path traversal attacks
* @return A connection to the secure database server
*/
public static Connection getChallengeConnection(String ApplicationRoot, String path)
{
//Some over paranoid input validation never hurts.
path = path.replaceAll("\\.", "").replaceAll("/", "");
log.debug("Path = " + path);
Connection conn = null;
try
{
//Pull Driver and DB URL out of database.properties
String props = ApplicationRoot+"/WEB-INF/database.properties";
String DriverType = FileInputProperties.readfile(props, "DriverType");
Class.forName(DriverType).newInstance();
String connectionURL=FileInputProperties.readfile(props, "databaseConnectionURL");
//Pull DB Schema, Schema User name and Schema Password from level specific properties File
props = ApplicationRoot+"/WEB-INF/challenges/" + path + ".properties";
log.debug("Level Properties File = " + path + ".properties");
//Add DB Schema to the end of the connectionURL
connectionURL= connectionURL + FileInputProperties.readfile(props, "databaseConnectionURL");
//log.debug("Connection URL: " + connectionURL);
String username=FileInputProperties.readfile(props, "databaseUsername");
String password=FileInputProperties.readfile(props, "databasePassword");
conn = DriverManager.getConnection(connectionURL,username,password);
}
catch(Exception e)
{
log.fatal("Unable to create database connection: " + e);
e.printStackTrace();
}
return conn;
}
/**
* Returns connection to core schema in database
* @param ApplicationRoot
* @return Connection to core schema with admin privileges
*/
public static Connection getCoreConnection(String ApplicationRoot)
{
Connection conn = null;
try
{
//log.debug("Getting Prop File");
//Pull Driver and DB URL out of database.properties
String props = ApplicationRoot+"/WEB-INF/database.properties";
//log.debug("Getting Driver");
String DriverType = FileInputProperties.readfile(props, "DriverType");
//log.debug("Getting Driver Instance");
Class.forName(DriverType).newInstance();
String connectionURL=FileInputProperties.readfile(props, "databaseConnectionURL");
//Pull Schema, User name and Password from SqlInjLesson.properties
props = ApplicationRoot+"/WEB-INF/coreDatabase.properties";
//log.debug("Reading Prop File");
connectionURL= connectionURL + FileInputProperties.readfile(props, "databaseConnectionURL");
String username=FileInputProperties.readfile(props, "databaseUsername");
String password=FileInputProperties.readfile(props, "databasePassword");
//log.debug("Creating Connection to DB");
conn = DriverManager.getConnection(connectionURL,username,password);
}
catch(Exception e)
{
log.fatal("Unable to create database connection: " + e);
e.printStackTrace();
}
return conn;
}
/**
* This method is used by the application to get a connection to the secure database sever
* @param ApplicationRoot The running context of the application.
* @return A connection to the secure database server
*/
public static Connection getDatabaseConnection(String ApplicationRoot)
{
Connection conn = null;
try
{
String props = ApplicationRoot+"/WEB-INF/database.properties";
String DriverType = FileInputProperties.readfile(props, "DriverType");
Class.forName(DriverType).newInstance();
String connectionURL=FileInputProperties.readfile(props, "databaseConnectionURL");
String username=FileInputProperties.readfile(props, "databaseUsername");
String password=FileInputProperties.readfile(props, "databasePassword");
conn = DriverManager.getConnection(connectionURL,username,password);
}
catch(Exception e)
{
log.fatal("Unable to create database connection: " + e);
e.printStackTrace();
}
return conn;
}
/**
* This method is used by the application to get a connection to the secure database sever's
* SQL injection Lesson schema
* @param ApplicationRoot The running context of the application.
* @return A connection to the secure database server
*/
public static Connection getSqlInjLessonConnection(String ApplicationRoot)
{
Connection conn = null;
try
{
//Pull Driver and DB URL out of database.properties
String props = ApplicationRoot+"/WEB-INF/database.properties";
String DriverType = FileInputProperties.readfile(props, "DriverType");
Class.forName(DriverType).newInstance();
String connectionURL=FileInputProperties.readfile(props, "databaseConnectionURL");
//Pull Schema, User name and Password from SqlInjLesson.properties
props = ApplicationRoot+"/WEB-INF/lessons/SqlInjLesson.properties";
connectionURL= connectionURL + FileInputProperties.readfile(props, "databaseConnectionURL");
String username=FileInputProperties.readfile(props, "databaseUsername");
String password=FileInputProperties.readfile(props, "databasePassword");
conn = DriverManager.getConnection(connectionURL,username,password);
}
catch(Exception e)
{
log.fatal("Unable to create database connection: " + e);
e.printStackTrace();
}
return conn;
}
}